Skip to content

Commit

Permalink
#1177 Throw compiler error when trying to set a pointer component
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Apr 21, 2024
1 parent 6c4faca commit 05ef289
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 124 deletions.
188 changes: 92 additions & 96 deletions flecs.c

Large diffs are not rendered by default.

25 changes: 22 additions & 3 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19451,7 +19451,7 @@ using actual_type_t = typename actual_type<T>::type;
// Get type without const, *, &
template<typename T>
struct base_type {
using type = decay_t< remove_pointer_t< actual_type_t<T> > >;
using type = decay_t< actual_type_t<T> >;
};

template <typename T>
Expand Down Expand Up @@ -24927,11 +24927,28 @@ struct entity : entity_builder<entity>
* @tparam T component for which to get a reference.
* @return The reference.
*/
template <typename T>
template <typename T, if_t< is_actual<T>::value > = 0>
ref<T> get_ref() const {
return ref<T>(m_world, m_id, _::cpp_type<T>::id(m_world));
}

/** Get reference to component.
* Overload for when T is not the same as the actual type, which happens
* when using pair types.
* A reference allows for quick and safe access to a component value, and is
* a faster alternative to repeatedly calling 'get' for the same component.
*
* @tparam T component for which to get a reference.
* @return The reference.
*/
template <typename T, typename A = actual_type_t<T>, if_t< flecs::is_pair<T>::value > = 0>
ref<A> get_ref() const {
return ref<A>(m_world, m_id,
ecs_pair(_::cpp_type<typename T::first>::id(m_world),
_::cpp_type<typename T::second>::id(m_world)));
}


template <typename First, typename Second, typename P = flecs::pair<First, Second>,
typename A = actual_type_t<P>>
ref<A> get_ref() const {
Expand Down Expand Up @@ -26429,6 +26446,8 @@ void register_lifecycle_actions(
// will register it as a component, and verify whether the input is consistent.
template <typename T>
struct cpp_type_impl {
static_assert(is_pointer<T>::value == false,
"pointer types are not allowed for components");
// Initialize component identifier
static void init(
entity_t entity,
Expand Down Expand Up @@ -27964,7 +27983,7 @@ namespace _ {
struct sig {
sig(flecs::world_t *world)
: m_world(world)
, ids({ (_::cpp_type<Components>::id(world))... })
, ids({ (_::cpp_type<remove_pointer_t<Components>>::id(world))... })
, inout ({ (type_to_inout<Components>())... })
, oper ({ (type_to_oper<Components>())... })
{ }
Expand Down
2 changes: 2 additions & 0 deletions include/flecs/addons/cpp/component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ void register_lifecycle_actions(
// will register it as a component, and verify whether the input is consistent.
template <typename T>
struct cpp_type_impl {
static_assert(is_pointer<T>::value == false,
"pointer types are not allowed for components");
// Initialize component identifier
static void init(
entity_t entity,
Expand Down
2 changes: 1 addition & 1 deletion include/flecs/addons/cpp/pair.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ using actual_type_t = typename actual_type<T>::type;
// Get type without const, *, &
template<typename T>
struct base_type {
using type = decay_t< remove_pointer_t< actual_type_t<T> > >;
using type = decay_t< actual_type_t<T> >;
};

template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion include/flecs/addons/cpp/utils/signature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace _ {
struct sig {
sig(flecs::world_t *world)
: m_world(world)
, ids({ (_::cpp_type<Components>::id(world))... })
, ids({ (_::cpp_type<remove_pointer_t<Components>>::id(world))... })
, inout ({ (type_to_inout<Components>())... })
, oper ({ (type_to_oper<Components>())... })
{ }
Expand Down
1 change: 0 additions & 1 deletion test/cpp_api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,6 @@
"implicit_base",
"implicit_const",
"implicit_ref",
"implicit_ptr",
"implicit_const_ref",
"vector_elem_type"
]
Expand Down
2 changes: 1 addition & 1 deletion test/cpp_api/src/ComponentLifecycle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ void ComponentLifecycle_dtor_relation_target(void) {

auto e = ecs.entity();
auto e2 = ecs.entity().emplace<CountNoDefaultCtor>(5).add<Tag>(e);
auto e3 = ecs.entity().emplace<CountNoDefaultCtor>(5);
ecs.entity().emplace<CountNoDefaultCtor>(5);

test_int(CountNoDefaultCtor::ctor_invoked, 2);
test_int(CountNoDefaultCtor::dtor_invoked, 1);
Expand Down
15 changes: 0 additions & 15 deletions test/cpp_api/src/ImplicitComponents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,6 @@ void ImplicitComponents_implicit_base(void) {

test_int(v.id(), flecs::type_id<Position>());
test_int(v.id(), flecs::type_id<const Position>());
test_int(v.id(), flecs::type_id<Position*>());
test_int(v.id(), flecs::type_id<Position&>());
}

Expand All @@ -434,7 +433,6 @@ void ImplicitComponents_implicit_const(void) {

test_int(v.id(), flecs::type_id<Position>());
test_int(v.id(), flecs::type_id<const Position>());
test_int(v.id(), flecs::type_id<Position*>());
test_int(v.id(), flecs::type_id<Position&>());
}

Expand All @@ -445,18 +443,6 @@ void ImplicitComponents_implicit_ref(void) {

test_int(v.id(), flecs::type_id<Position>());
test_int(v.id(), flecs::type_id<const Position>());
test_int(v.id(), flecs::type_id<Position*>());
test_int(v.id(), flecs::type_id<Position&>());
}

void ImplicitComponents_implicit_ptr(void) {
flecs::world world;

auto v = world.use<Position*>();

test_int(v.id(), flecs::type_id<Position>());
test_int(v.id(), flecs::type_id<const Position>());
test_int(v.id(), flecs::type_id<Position*>());
test_int(v.id(), flecs::type_id<Position&>());
}

Expand All @@ -467,7 +453,6 @@ void ImplicitComponents_implicit_const_ref(void) {

test_int(v.id(), flecs::type_id<Position>());
test_int(v.id(), flecs::type_id<const Position>());
test_int(v.id(), flecs::type_id<Position*>());
test_int(v.id(), flecs::type_id<Position&>());
}

Expand Down
7 changes: 1 addition & 6 deletions test/cpp_api/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,6 @@ void ImplicitComponents_use_const_w_threads(void);
void ImplicitComponents_implicit_base(void);
void ImplicitComponents_implicit_const(void);
void ImplicitComponents_implicit_ref(void);
void ImplicitComponents_implicit_ptr(void);
void ImplicitComponents_implicit_const_ref(void);
void ImplicitComponents_vector_elem_type(void);

Expand Down Expand Up @@ -5462,10 +5461,6 @@ bake_test_case ImplicitComponents_testcases[] = {
"implicit_ref",
ImplicitComponents_implicit_ref
},
{
"implicit_ptr",
ImplicitComponents_implicit_ptr
},
{
"implicit_const_ref",
ImplicitComponents_implicit_const_ref
Expand Down Expand Up @@ -6783,7 +6778,7 @@ static bake_test_suite suites[] = {
"ImplicitComponents",
NULL,
NULL,
28,
27,
ImplicitComponents_testcases
},
{
Expand Down

0 comments on commit 05ef289

Please sign in to comment.