Skip to content

Commit 98e722f

Browse files
committed
#124 allow enum constants to be used with other component types
1 parent 24c5f5b commit 98e722f

File tree

6 files changed

+377
-128
lines changed

6 files changed

+377
-128
lines changed

flecs.h

Lines changed: 133 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -16495,14 +16495,27 @@ struct entity_view : public id {
1649516495
* @tparam R the relation type.
1649616496
* @param object the object.
1649716497
*/
16498-
template<typename R>
16499-
const R* get(flecs::id_t object) const {
16498+
template<typename R, typename O, if_not_t< is_enum<O>::value> = 0>
16499+
const R* get(O object) const {
1650016500
auto comp_id = _::cpp_type<R>::id(m_world);
1650116501
ecs_assert(_::cpp_type<R>::size() != 0, ECS_INVALID_PARAMETER, NULL);
1650216502
return static_cast<const R*>(
1650316503
ecs_get_id(m_world, m_id, ecs_pair(comp_id, object)));
1650416504
}
1650516505

16506+
/** Get a pair.
16507+
* This operation gets the value for a pair from the entity.
16508+
*
16509+
* @tparam R the relation type.
16510+
* @param constant the enum constant.
16511+
*/
16512+
template<typename R, typename O, if_t< is_enum<O>::value> = 0>
16513+
const R* get(O constant) const {
16514+
const auto& et = enum_type<O>(this->m_world);
16515+
flecs::entity_t object = et.entity(constant);
16516+
return get<R>(object);
16517+
}
16518+
1650616519
/** Get component value (untyped).
1650716520
*
1650816521
* @param comp The component to get.
@@ -16683,16 +16696,29 @@ struct entity_view : public id {
1668316696

1668416697
/** Check if entity has the provided pair.
1668516698
*
16686-
* @tparam Relation The relation type.
16699+
* @tparam R The relation type.
1668716700
* @param object The object.
1668816701
* @return True if the entity has the provided component, false otherwise.
1668916702
*/
16690-
template <typename Relation>
16691-
bool has(flecs::id_t object) const {
16692-
auto comp_id = _::cpp_type<Relation>::id(m_world);
16703+
template<typename R, typename O, if_not_t< is_enum<O>::value > = 0>
16704+
bool has(O object) const {
16705+
auto comp_id = _::cpp_type<R>::id(m_world);
1669316706
return ecs_has_id(m_world, m_id, ecs_pair(comp_id, object));
1669416707
}
1669516708

16709+
/** Check if entity has the provided pair.
16710+
*
16711+
* @tparam R The relation type.
16712+
* @param value The enum constant.
16713+
* @return True if the entity has the provided component, false otherwise.
16714+
*/
16715+
template<typename R, typename E, if_t< is_enum<E>::value > = 0>
16716+
bool has(E value) const {
16717+
const auto& et = enum_type<E>(this->m_world);
16718+
flecs::entity_t object = et.entity(value);
16719+
return has<R>(object);
16720+
}
16721+
1669616722
/** Check if entity has the provided pair.
1669716723
*
1669816724
* @param relation The relation.
@@ -16951,8 +16977,8 @@ struct entity_builder : entity_view {
1695116977
/** Add a pair.
1695216978
* This operation adds a pair to the entity.
1695316979
*
16954-
* @param relation The relation id.
16955-
* @param object The object id.
16980+
* @param relation The relation.
16981+
* @param object The object.
1695616982
*/
1695716983
Self& add(entity_t relation, entity_t object) {
1695816984
ecs_add_pair(this->m_world, this->m_id, relation, object);
@@ -16962,8 +16988,8 @@ struct entity_builder : entity_view {
1696216988
/** Add a pair.
1696316989
* This operation adds a pair to the entity.
1696416990
*
16965-
* @tparam R the relation type.
16966-
* @tparam O the object type.
16991+
* @tparam R The relation type
16992+
* @tparam O The object type.
1696716993
*/
1696816994
template<typename R, typename O>
1696916995
Self& add() {
@@ -16973,27 +16999,43 @@ struct entity_builder : entity_view {
1697316999
/** Add a pair.
1697417000
* This operation adds a pair to the entity.
1697517001
*
16976-
* @tparam R the relation type.
16977-
* @param object the object type.
17002+
* @tparam R The relation type
17003+
* @param object The object.
1697817004
*/
16979-
template<typename R>
16980-
Self& add(entity_t object) {
17005+
template<typename R, typename O, if_not_t< is_enum<O>::value > = 0>
17006+
Self& add(O object) {
1698117007
flecs_static_assert(is_flecs_constructible<R>::value,
16982-
"cannot default construct type: add T::T() or use emplace<T>()");
17008+
"cannot default construct type: add T::T() or use emplace<T>()");
1698317009
return this->add(_::cpp_type<R>::id(this->m_world), object);
1698417010
}
1698517011

17012+
/** Add a pair.
17013+
* This operation adds a pair to the entity that consists out of a tag
17014+
* combined with an enum constant.
17015+
*
17016+
* @tparam R The relation type
17017+
* @param constant the enum constant.
17018+
*/
17019+
template<typename R, typename O, if_t< is_enum<O>::value > = 0>
17020+
Self& add(O constant) {
17021+
flecs_static_assert(is_flecs_constructible<R>::value,
17022+
"cannot default construct type: add T::T() or use emplace<T>()");
17023+
const auto& et = enum_type<O>(this->m_world);
17024+
flecs::entity_t object = et.entity(constant);
17025+
return this->add<R>(object);
17026+
}
17027+
1698617028
/** Shortcut for add(IsA, obj).
1698717029
*
16988-
* @param object the object id.
17030+
* @param object The object.
1698917031
*/
1699017032
Self& is_a(entity_t object) {
1699117033
return this->add(flecs::IsA, object);
1699217034
}
1699317035

1699417036
/** Shortcut for add(IsA, obj).
1699517037
*
16996-
* @tparam T the type associated with the object.
17038+
* @tparam T the type associated with the O.
1699717039
*/
1699817040
template <typename T>
1699917041
Self& is_a() {
@@ -17002,27 +17044,27 @@ struct entity_builder : entity_view {
1700217044

1700317045
/** Shortcut for add(ChildOf, obj).
1700417046
*
17005-
* @param object the object id.
17047+
* @param object The object.
1700617048
*/
1700717049
Self& child_of(entity_t object) {
1700817050
return this->add(flecs::ChildOf, object);
1700917051
}
1701017052

1701117053
/** Shortcut for add(ChildOf, obj).
1701217054
*
17013-
* @tparam T the type associated with the object.
17055+
* @tparam T the type associated with the O.
1701417056
*/
1701517057
template <typename T>
1701617058
Self& child_of() {
1701717059
return this->add(flecs::ChildOf, _::cpp_type<T>::id(this->m_world));
1701817060
}
1701917061

17020-
/** Add a pair with object type.
17021-
* This operation adds a pair to the entity. The relation part of the pair
17062+
/** Add a pair with O type.
17063+
* This operation adds a pair to the entity. The R part of the pair
1702217064
* should not be a component.
1702317065
*
17024-
* @param relation the relation type.
17025-
* @tparam O the object type.
17066+
* @param relation The relation.
17067+
* @tparam O The object type.
1702617068
*/
1702717069
template<typename O>
1702817070
Self& add_w_object(entity_t relation) {
@@ -17053,8 +17095,8 @@ struct entity_builder : entity_view {
1705317095
/** Remove a pair.
1705417096
* This operation removes a pair from the entity.
1705517097
*
17056-
* @param relation The relation id.
17057-
* @param object The object id.
17098+
* @param relation The relation.
17099+
* @param object The object.
1705817100
*/
1705917101
Self& remove(entity_t relation, entity_t object) {
1706017102
ecs_remove_pair(this->m_world, this->m_id, relation, object);
@@ -17064,34 +17106,47 @@ struct entity_builder : entity_view {
1706417106
/** Removes a pair.
1706517107
* This operation removes a pair from the entity.
1706617108
*
17067-
* @tparam Relation the relation type.
17068-
* @tparam Object the object type.
17109+
* @tparam R The relation type
17110+
* @tparam O The object type.
1706917111
*/
17070-
template<typename Relation, typename Object>
17112+
template<typename R, typename O>
1707117113
Self& remove() {
17072-
return this->remove<Relation>(_::cpp_type<Object>::id(this->m_world));
17114+
return this->remove<R>(_::cpp_type<O>::id(this->m_world));
1707317115
}
1707417116

1707517117
/** Remove a pair.
1707617118
* This operation adds a pair to the entity.
1707717119
*
17078-
* @tparam Relation the relation type.
17079-
* @param object the object type.
17120+
* @tparam R The relation type
17121+
* @param object The object.
17122+
*/
17123+
template<typename R, typename O, if_not_t< is_enum<O>::value > = 0>
17124+
Self& remove(O object) {
17125+
return this->remove(_::cpp_type<R>::id(this->m_world), object);
17126+
}
17127+
17128+
/** Remove a pair.
17129+
* This operation adds a pair to the entity.
17130+
*
17131+
* @tparam R The relation type
17132+
* @param constant the enum constant.
1708017133
*/
17081-
template<typename Relation>
17082-
Self& remove(entity_t object) {
17083-
return this->remove(_::cpp_type<Relation>::id(this->m_world), object);
17134+
template<typename R, typename O, if_t< is_enum<O>::value > = 0>
17135+
Self& remove(O constant) {
17136+
const auto& et = enum_type<O>(this->m_world);
17137+
flecs::entity_t object = et.entity(constant);
17138+
return this->remove<R>(object);
1708417139
}
1708517140

17086-
/** Removes a pair with object type.
17141+
/** Removes a pair with O type.
1708717142
* This operation removes a pair from the entity.
1708817143
*
17089-
* @param relation the relation type.
17090-
* @tparam Object the object type.
17144+
* @param relation The relation.
17145+
* @tparam O The object type.
1709117146
*/
17092-
template<typename Object>
17147+
template<typename O>
1709317148
Self& remove_w_object(entity_t relation) {
17094-
return this->remove(relation, _::cpp_type<Object>::id(this->m_world));
17149+
return this->remove(relation, _::cpp_type<O>::id(this->m_world));
1709517150
}
1709617151

1709717152
/** Add owned flag for component (forces ownership when instantiating)
@@ -17133,7 +17188,7 @@ struct entity_builder : entity_view {
1713317188
this->override<T>();
1713417189
this->set<T>(val);
1713517190
return to_base();
17136-
}
17191+
}
1713717192

1713817193
/** Add a switch to an entity by id.
1713917194
* The switch entity must be a type, that is it must have the EcsType
@@ -17244,10 +17299,10 @@ struct entity_builder : entity_view {
1724417299
*/
1724517300
template <typename E, if_t< is_enum<E>::value > = 0>
1724617301
Self& add(E value) {
17247-
flecs::entity_t r = _::cpp_type<E>::id(this->m_world);
17302+
flecs::entity_t relation = _::cpp_type<E>::id(this->m_world);
1724817303
const auto& et = enum_type<E>(this->m_world);
17249-
flecs::entity_t o = et.entity(value);
17250-
return this->add(r, o);
17304+
flecs::entity_t object = et.entity(value);
17305+
return this->add(relation, object);
1725117306
}
1725217307

1725317308
/** Remove pair for enum.
@@ -17257,8 +17312,8 @@ struct entity_builder : entity_view {
1725717312
*/
1725817313
template <typename E, if_t< is_enum<E>::value > = 0>
1725917314
Self& remove() {
17260-
flecs::entity_t r = _::cpp_type<E>::id(this->m_world);
17261-
return this->remove(r, flecs::Wildcard);
17315+
flecs::entity_t relation = _::cpp_type<E>::id(this->m_world);
17316+
return this->remove(relation, flecs::Wildcard);
1726217317
}
1726317318

1726417319
/** Enable an entity.
@@ -17367,11 +17422,11 @@ struct entity_builder : entity_view {
1736717422
}
1736817423

1736917424
/** Set a pair for an entity.
17370-
* This operation sets the pair value, and uses the relation as type. If the
17425+
* This operation sets the pair value, and uses the R as type. If the
1737117426
* entity did not yet have the pair, it will be added.
1737217427
*
17373-
* @tparam R The relation part of the pair.
17374-
* @tparam O The object part of the pair.
17428+
* @tparam R The relation type.
17429+
* @tparam O The object type.
1737517430
* @param value The value to set.
1737617431
*/
1737717432
template <typename R, typename O, typename P = pair<R, O>,
@@ -17382,27 +17437,42 @@ struct entity_builder : entity_view {
1738217437
}
1738317438

1738417439
/** Set a pair for an entity.
17385-
* This operation sets the pair value, and uses the relation as type. If the
17440+
* This operation sets the pair value, and uses the R as type. If the
1738617441
* entity did not yet have the pair, it will be added.
1738717442
*
17388-
* @tparam R The relation part of the pair.
17389-
* @param object The object part of the pair.
17443+
* @tparam R The relation type.
17444+
* @param object The object.
1739017445
* @param value The value to set.
1739117446
*/
17392-
template <typename R>
17393-
Self& set(entity_t object, const R& value) {
17447+
template <typename R, typename O, if_not_t< is_enum<O>::value > = 0>
17448+
Self& set(O object, const R& value) {
1739417449
auto relation = _::cpp_type<R>::id(this->m_world);
1739517450
flecs::set(this->m_world, this->m_id, value,
1739617451
ecs_pair(relation, object));
1739717452
return to_base();
1739817453
}
1739917454

1740017455
/** Set a pair for an entity.
17401-
* This operation sets the pair value, and uses the relation as type. If the
17456+
* This operation sets the pair value, and uses the R as type. If the
17457+
* entity did not yet have the pair, it will be added.
17458+
*
17459+
* @tparam R The relation type.
17460+
* @param constant The enum constant.
17461+
* @param value The value to set.
17462+
*/
17463+
template <typename R, typename O, if_t< is_enum<O>::value > = 0>
17464+
Self& set(O constant, const R& value) {
17465+
const auto& et = enum_type<O>(this->m_world);
17466+
flecs::entity_t object = et.entity(constant);
17467+
return set<R>(object, value);
17468+
}
17469+
17470+
/** Set a pair for an entity.
17471+
* This operation sets the pair value, and uses the R as type. If the
1740217472
* entity did not yet have the pair, it will be added.
1740317473
*
17404-
* @tparam O The object part of the pair.
17405-
* @param relation The relation part of the pair.
17474+
* @tparam O The object type.
17475+
* @param relation The relation.
1740617476
* @param value The value to set.
1740717477
*/
1740817478
template <typename O>
@@ -17439,7 +17509,7 @@ struct entity_builder : entity_view {
1743917509

1744017510
/** Emplace component.
1744117511
* Emplace constructs a component in the storage, which prevents calling the
17442-
* destructor on the object passed into the function.
17512+
* destructor on the O passed into the function.
1744317513
*
1744417514
* Emplace attempts the following signatures to construct the component:
1744517515
* T{Args...}
@@ -17474,25 +17544,25 @@ struct entity_builder : entity_view {
1747417544
return to_base();
1747517545
}
1747617546

17477-
/** Entities created in function will have (Relation, this)
17547+
/** Entities created in function will have (relation, this)
1747817548
* This operation is thread safe.
1747917549
*
17480-
* @tparam Relation The relation to use.
17550+
* @tparam R The R to use.
1748117551
* @param func The function to call.
1748217552
*/
17483-
template <typename Relation, typename Func>
17553+
template <typename R, typename Func>
1748417554
Self& with(const Func& func) {
17485-
with(_::cpp_type<Relation>::id(this->m_world), func);
17555+
with(_::cpp_type<R>::id(this->m_world), func);
1748617556
return to_base();
1748717557
}
1748817558

1748917559
/** Entities created in function will have (relation, this)
1749017560
*
17491-
* @param relation The relation to use.
17561+
* @param relation The relation.
1749217562
* @param func The function to call.
1749317563
*/
1749417564
template <typename Func>
17495-
Self& with(id_t relation, const Func& func) {
17565+
Self& with(entity_t relation, const Func& func) {
1749617566
ecs_id_t prev = ecs_set_with(this->m_world,
1749717567
ecs_pair(relation, this->m_id));
1749817568
func();

0 commit comments

Comments
 (0)