Skip to content

Commit

Permalink
sigh mixin: refine auto signals
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Sep 11, 2024
1 parent 42a1905 commit b5fbc68
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 80 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ TODO:
* view and view iterator specializations for multi, single and filtered elements
* organizer support to groups
* meta range: move id to meta objects and return plain types (?), then remove id from meta base and meta ctor too
* doc: automatic signal registration
27 changes: 9 additions & 18 deletions src/entt/entity/mixin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,25 @@ namespace entt {
/*! @cond TURN_OFF_DOXYGEN */
namespace internal {

template<typename T, typename Reg, typename = void>
template<typename, typename, typename = void>
struct has_on_construct final: std::false_type {};

template<typename T, typename Reg>
struct has_on_construct<
T, Reg,
std::void_t<decltype(T::on_construct(std::declval<Reg &>(),
std::declval<Reg &>().create()))>>
template<typename Type, typename Registry>
struct has_on_construct<Type, Registry, std::void_t<decltype(Type::on_construct(std::declval<Registry &>(), std::declval<Registry>().create()))>>
: std::true_type {};

template<typename T, typename Reg, typename = void>
template<typename, typename, typename = void>
struct has_on_update final: std::false_type {};

template<typename T, typename Reg>
struct has_on_update<
T, Reg,
std::void_t<decltype(T::on_update(std::declval<Reg &>(),
std::declval<Reg &>().create()))>>
template<typename Type, typename Registry>
struct has_on_update<Type, Registry, std::void_t<decltype(Type::on_update(std::declval<Registry &>(), std::declval<Registry>().create()))>>
: std::true_type {};

template<typename T, typename Reg, typename = void>
template<typename, typename, typename = void>
struct has_on_destroy final: std::false_type {};

template<typename T, typename Reg>
struct has_on_destroy<
T, Reg,
std::void_t<decltype(T::on_destroy(std::declval<Reg &>(),
std::declval<Reg &>().create()))>>
template<typename Type, typename Registry>
struct has_on_destroy<Type, Registry, std::void_t<decltype(Type::on_destroy(std::declval<Registry &>(), std::declval<Registry>().create()))>>
: std::true_type {};

} // namespace internal
Expand Down
45 changes: 45 additions & 0 deletions test/entt/entity/sigh_mixin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@
#include "../../common/throwing_allocator.hpp"
#include "../../common/throwing_type.hpp"

struct auto_signal final {
inline static void on_construct(const entt::registry &, entt::entity) {
++created;
}

inline static void on_update(entt::registry &, const entt::entity) {
++updated;
}

inline static void on_destroy(entt::registry &, entt::entity) {
++destroyed;
}

inline static std::size_t created{};
inline static std::size_t updated{};
inline static std::size_t destroyed{};
};

template<typename Registry>
void listener(std::size_t &counter, Registry &, typename Registry::entity_type) {
++counter;
Expand Down Expand Up @@ -625,3 +643,30 @@ TEST(SighMixin, ThrowingComponent) {
ASSERT_EQ(on_construct, 2u);
ASSERT_EQ(on_destroy, 3u);
}

TEST(SighMixin, AutoSignal) {
entt::registry registry;
auto const entity = registry.create();

registry.emplace<auto_signal>(entity);
registry.replace<auto_signal>(entity);
registry.erase<auto_signal>(entity);

ASSERT_EQ(auto_signal::created, 1u);
ASSERT_EQ(auto_signal::updated, 1u);
ASSERT_EQ(auto_signal::destroyed, 1u);

ASSERT_TRUE(registry.storage<auto_signal>().empty());
ASSERT_TRUE(registry.valid(entity));

registry.emplace<auto_signal>(entity);
registry.replace<auto_signal>(entity);
registry.destroy(entity);

ASSERT_EQ(auto_signal::created, 2u);
ASSERT_EQ(auto_signal::updated, 2u);
ASSERT_EQ(auto_signal::destroyed, 2u);

ASSERT_TRUE(registry.storage<auto_signal>().empty());
ASSERT_FALSE(registry.valid(entity));
}
62 changes: 0 additions & 62 deletions test/entt/entity/storage_signals.cpp

This file was deleted.

0 comments on commit b5fbc68

Please sign in to comment.