Skip to content

Commit

Permalink
Add C++ optional pair tests for filters/queries
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Oct 29, 2023
1 parent 11b0dd7 commit bba0a5f
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 11 deletions.
21 changes: 14 additions & 7 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16010,6 +16010,12 @@ using transcribe_volatile_t = conditional_t<is_volatile<Src>::value, Dst volatil
template<class Src, class Dst>
using transcribe_cv_t = transcribe_const_t< Src, transcribe_volatile_t< Src, Dst> >;

template<class Src, class Dst>
using transcribe_pointer_t = conditional_t<is_pointer<Src>::value, Dst*, Dst>;

template<class Src, class Dst>
using transcribe_cvp_t = transcribe_cv_t< Src, transcribe_pointer_t< Src, Dst> >;


// More convenience templates. The if_*_t templates use int as default type
// instead of void. This enables writing code that's a bit less cluttered when
Expand Down Expand Up @@ -18761,25 +18767,26 @@ struct pair : _::pair_base {
template <typename First, typename Second, if_t<is_empty<First>::value> = 0>
using pair_object = pair<First, Second>;

template <typename T>
using raw_type_t = remove_pointer_t<remove_reference_t<T>>;

/** Test if type is a pair. */
template <typename T>
struct is_pair {
static constexpr bool value = is_base_of<_::pair_base, remove_reference_t<T> >::value;
static constexpr bool value = is_base_of<_::pair_base, raw_type_t<T> >::value;
};


/** Get pair::first from pair while preserving cv qualifiers. */
template <typename P>
using pair_first_t = transcribe_cv_t<remove_reference_t<P>, typename remove_reference_t<P>::first>;
using pair_first_t = transcribe_cv_t<remove_reference_t<P>, typename raw_type_t<P>::first>;

/** Get pair::second from pair while preserving cv qualifiers. */
template <typename P>
using pair_second_t = transcribe_cv_t<remove_reference_t<P>, typename remove_reference_t<P>::second>;
using pair_second_t = transcribe_cv_t<remove_reference_t<P>, typename raw_type_t<P>::second>;

/** Get pair::type type from pair while preserving cv qualifiers. */
/** Get pair::type type from pair while preserving cv qualifiers and pointer type. */
template <typename P>
using pair_type_t = transcribe_cv_t<remove_reference_t<P>, typename remove_reference_t<P>::type>;
using pair_type_t = transcribe_cvp_t<remove_reference_t<P>, typename raw_type_t<P>::type>;

/** Get actual type from a regular type or pair. */
template <typename T, typename U = int>
Expand Down Expand Up @@ -24065,7 +24072,7 @@ struct each_column<T, if_t< is_pointer<T>::value &&
each_column(const _::term_ptr& term, size_t row)
: each_column_base(term, row) { }

T get_row() {
actual_type_t<T> get_row() {
if (this->m_term.ptr) {
return &static_cast<actual_type_t<T>>(this->m_term.ptr)[this->m_row];
} else {
Expand Down
6 changes: 4 additions & 2 deletions test/cpp_api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,8 @@
"iter_get_pair_w_id",
"find",
"find_not_found",
"find_w_entity"
"find_w_entity",
"optional_pair_term"
]
}, {
"id": "QueryBuilder",
Expand Down Expand Up @@ -916,7 +917,8 @@
"inspect_terms_w_expr",
"find",
"find_not_found",
"find_w_entity"
"find_w_entity",
"optional_pair_term"
]
}, {
"id": "ComponentLifecycle",
Expand Down
31 changes: 31 additions & 0 deletions test/cpp_api/src/Filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,34 @@ void Filter_find_w_entity(void) {

test_assert(r == e2);
}

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

ecs.entity()
.add<TagA>()
.emplace<Position, Tag>(1.0f, 2.0f);
ecs.entity()
.add<TagA>();

int32_t with_pair = 0, without_pair = 0;

auto f = ecs.filter_builder<flecs::pair<Position, Tag>*>()
.with<TagA>()
.build();

f.each([&](flecs::entity e, Position* p) {
if (p) {
with_pair++;
test_flt(1.0f, p->x);
test_flt(2.0f, p->y);
} else {
without_pair++;
}
});

ecs.progress(1.0);

test_int(1, with_pair);
test_int(1, without_pair);
}
31 changes: 31 additions & 0 deletions test/cpp_api/src/Query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2332,3 +2332,34 @@ void Query_find_w_entity(void) {

test_assert(r == e2);
}

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

ecs.entity()
.add<TagA>()
.emplace<Position, Tag>(1.0f, 2.0f);
ecs.entity()
.add<TagA>();

int32_t with_pair = 0, without_pair = 0;

auto f = ecs.query_builder<flecs::pair<Position, Tag>*>()
.with<TagA>()
.build();

f.each([&](flecs::entity e, Position* p) {
if (p) {
with_pair++;
test_flt(1.0f, p->x);
test_flt(2.0f, p->y);
} else {
without_pair++;
}
});

ecs.progress(1.0);

test_int(1, with_pair);
test_int(1, without_pair);
}
14 changes: 12 additions & 2 deletions test/cpp_api/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ void Query_iter_get_pair_w_id(void);
void Query_find(void);
void Query_find_not_found(void);
void Query_find_w_entity(void);
void Query_optional_pair_term(void);

// Testsuite 'QueryBuilder'
void QueryBuilder_builder_assign_same_type(void);
Expand Down Expand Up @@ -881,6 +882,7 @@ void Filter_inspect_terms_w_expr(void);
void Filter_find(void);
void Filter_find_not_found(void);
void Filter_find_w_entity(void);
void Filter_optional_pair_term(void);

// Testsuite 'ComponentLifecycle'
void ComponentLifecycle_ctor_on_add(void);
Expand Down Expand Up @@ -3534,6 +3536,10 @@ bake_test_case Query_testcases[] = {
{
"find_w_entity",
Query_find_w_entity
},
{
"optional_pair_term",
Query_optional_pair_term
}
};

Expand Down Expand Up @@ -4728,6 +4734,10 @@ bake_test_case Filter_testcases[] = {
{
"find_w_entity",
Filter_find_w_entity
},
{
"optional_pair_term",
Filter_optional_pair_term
}
};

Expand Down Expand Up @@ -6467,7 +6477,7 @@ static bake_test_suite suites[] = {
"Query",
NULL,
NULL,
84,
85,
Query_testcases
},
{
Expand Down Expand Up @@ -6509,7 +6519,7 @@ static bake_test_suite suites[] = {
"Filter",
NULL,
NULL,
26,
27,
Filter_testcases
},
{
Expand Down

0 comments on commit bba0a5f

Please sign in to comment.