Skip to content

Commit

Permalink
add table get for enum components + comment fix (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
Indra-db authored Sep 18, 2023
1 parent ca73ed2 commit 55e1611
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 5 deletions.
14 changes: 12 additions & 2 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -21570,7 +21570,7 @@ struct entity_view : public id {
return m_id;
}

/** Check is entity is valid.
/** Check if entity is valid.
*
* @return True if the entity is alive, false otherwise.
*/
Expand All @@ -21582,7 +21582,7 @@ struct entity_view : public id {
return is_valid();
}

/** Check is entity is alive.
/** Check if entity is alive.
*
* @return True if the entity is alive, false otherwise.
*/
Expand Down Expand Up @@ -25874,6 +25874,16 @@ struct table {
return static_cast<T*>(get(_::cpp_type<T>::id(m_world)));
}

/** Get pointer to component array by (enum) component.
*
* @tparam T The (enum) component.
* @return Pointer to the column, NULL if not found.
*/
template <typename T, if_t< is_enum<T>::value > = 0>
T* get() const {
return static_cast<T*>(get(_::cpp_type<T>::id(m_world)));
}

/** Get pointer to component array by component.
*
* @tparam T The component.
Expand Down
4 changes: 2 additions & 2 deletions include/flecs/addons/cpp/entity_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct entity_view : public id {
return m_id;
}

/** Check is entity is valid.
/** Check if entity is valid.
*
* @return True if the entity is alive, false otherwise.
*/
Expand All @@ -63,7 +63,7 @@ struct entity_view : public id {
return is_valid();
}

/** Check is entity is alive.
/** Check if entity is alive.
*
* @return True if the entity is alive, false otherwise.
*/
Expand Down
10 changes: 10 additions & 0 deletions include/flecs/addons/cpp/table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,16 @@ struct table {
return static_cast<T*>(get(_::cpp_type<T>::id(m_world)));
}

/** Get pointer to component array by (enum) component.
*
* @tparam T The (enum) component.
* @return Pointer to the column, NULL if not found.
*/
template <typename T, if_t< is_enum<T>::value > = 0>
T* get() const {
return static_cast<T*>(get(_::cpp_type<T>::id(m_world)));
}

/** Get pointer to component array by component.
*
* @tparam T The component.
Expand Down
6 changes: 6 additions & 0 deletions test/cpp_api/include/cpp_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ struct Self {
flecs::entity_view value;
};

enum Number {
One = 1,
Two = 2,
Three = 3
};

class Pod {
public:
struct Child { };
Expand Down
1 change: 1 addition & 0 deletions test/cpp_api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,7 @@
"has_pair_R_T",
"get_id",
"get_T",
"get_T_enum",
"get_pair_r_t",
"get_pair_R_t",
"get_pair_R_T",
Expand Down
19 changes: 19 additions & 0 deletions test/cpp_api/src/Table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,3 +663,22 @@ void Table_iter_type(void) {
}
test_int(count, 2);
}

void Table_get_T_enum(void) {
flecs::world ecs;

flecs::entity e = ecs.entity()
.set<Number>(Number::One);
ecs.entity()
.set<Number>(Number::Two);
ecs.entity()
.set<Number>(Number::Three);

flecs::table table = e.table();

Number *n = table.get<Number>();
test_assert(n != NULL);
test_int(n[0], Number::One);
test_int(n[1], Number::Two);
test_int(n[2], Number::Three);
}
7 changes: 6 additions & 1 deletion test/cpp_api/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,7 @@ void Table_has_pair_R_t(void);
void Table_has_pair_R_T(void);
void Table_get_id(void);
void Table_get_T(void);
void Table_get_T_enum(void);
void Table_get_pair_r_t(void);
void Table_get_pair_R_t(void);
void Table_get_pair_R_T(void);
Expand Down Expand Up @@ -6155,6 +6156,10 @@ bake_test_case Table_testcases[] = {
"get_T",
Table_get_T
},
{
"get_T_enum",
Table_get_T_enum
},
{
"get_pair_r_t",
Table_get_pair_r_t
Expand Down Expand Up @@ -6411,7 +6416,7 @@ static bake_test_suite suites[] = {
"Table",
NULL,
NULL,
31,
32,
Table_testcases
},
{
Expand Down

0 comments on commit 55e1611

Please sign in to comment.