registry:
* context const correctness * added support for aliased variables to context * code coverage to avoid regressions
This commit is contained in:
@@ -1520,9 +1520,9 @@ public:
|
||||
* registry, a null pointer otherwise.
|
||||
*/
|
||||
template<typename Type>
|
||||
[[nodiscard]] const Type * try_ctx() const {
|
||||
[[nodiscard]] Type * try_ctx() const {
|
||||
auto it = std::find_if(vars.cbegin(), vars.cend(), [type = type_id<Type>()](auto &&var) { return var.type() == type; });
|
||||
return it == vars.cend() ? nullptr : any_cast<const Type>(&*it);
|
||||
return it == vars.cend() ? nullptr : any_cast<Type>(&*it);
|
||||
}
|
||||
|
||||
/*! @copydoc try_ctx */
|
||||
@@ -1543,9 +1543,9 @@ public:
|
||||
* @return A valid reference to the object in the context of the registry.
|
||||
*/
|
||||
template<typename Type>
|
||||
[[nodiscard]] const Type & ctx() const {
|
||||
[[nodiscard]] Type & ctx() const {
|
||||
auto it = std::find_if(vars.cbegin(), vars.cend(), [type = type_id<Type>()](auto &&var) { return var.type() == type; });
|
||||
return (ENTT_ASSERT(it != vars.cend()), any_cast<const Type &>(*it));
|
||||
return (ENTT_ASSERT(it != vars.cend()), any_cast<Type &>(*it));
|
||||
}
|
||||
|
||||
/*! @copydoc ctx */
|
||||
|
||||
@@ -54,7 +54,7 @@ TEST(Registry, Context) {
|
||||
entt::registry registry;
|
||||
|
||||
ASSERT_EQ(registry.try_ctx<char>(), nullptr);
|
||||
ASSERT_EQ(registry.try_ctx<int>(), nullptr);
|
||||
ASSERT_EQ(registry.try_ctx<const int>(), nullptr);
|
||||
ASSERT_EQ(registry.try_ctx<double>(), nullptr);
|
||||
|
||||
registry.set<char>();
|
||||
@@ -63,7 +63,7 @@ TEST(Registry, Context) {
|
||||
static_cast<void>(registry.ctx_or_set<double>());
|
||||
|
||||
ASSERT_NE(registry.try_ctx<char>(), nullptr);
|
||||
ASSERT_NE(registry.try_ctx<int>(), nullptr);
|
||||
ASSERT_NE(registry.try_ctx<const int>(), nullptr);
|
||||
ASSERT_NE(registry.try_ctx<double>(), nullptr);
|
||||
|
||||
registry.unset<int>();
|
||||
@@ -79,7 +79,7 @@ TEST(Registry, Context) {
|
||||
ASSERT_EQ(count, 1);
|
||||
|
||||
ASSERT_NE(registry.try_ctx<char>(), nullptr);
|
||||
ASSERT_EQ(registry.try_ctx<int>(), nullptr);
|
||||
ASSERT_EQ(registry.try_ctx<const int>(), nullptr);
|
||||
ASSERT_EQ(registry.try_ctx<double>(), nullptr);
|
||||
|
||||
registry.set<char>('c');
|
||||
@@ -90,21 +90,59 @@ TEST(Registry, Context) {
|
||||
ASSERT_EQ(registry.ctx_or_set<char>('a'), 'c');
|
||||
ASSERT_NE(registry.try_ctx<char>(), nullptr);
|
||||
ASSERT_EQ(registry.try_ctx<char>(), ®istry.ctx<char>());
|
||||
ASSERT_EQ(registry.ctx<char>(), std::as_const(registry).ctx<char>());
|
||||
ASSERT_EQ(registry.ctx<char>(), std::as_const(registry).ctx<const char>());
|
||||
|
||||
ASSERT_EQ(registry.ctx<int>(), 42);
|
||||
ASSERT_EQ(registry.ctx<const int>(), 42);
|
||||
ASSERT_NE(registry.try_ctx<int>(), nullptr);
|
||||
ASSERT_EQ(registry.try_ctx<int>(), ®istry.ctx<int>());
|
||||
ASSERT_EQ(registry.ctx<int>(), std::as_const(registry).ctx<int>());
|
||||
ASSERT_EQ(registry.try_ctx<const int>(), ®istry.ctx<int>());
|
||||
ASSERT_EQ(registry.ctx<int>(), std::as_const(registry).ctx<const int>());
|
||||
|
||||
ASSERT_EQ(registry.ctx<double>(), 1.);
|
||||
ASSERT_EQ(registry.ctx<const double>(), 1.);
|
||||
ASSERT_NE(registry.try_ctx<double>(), nullptr);
|
||||
ASSERT_EQ(registry.try_ctx<double>(), ®istry.ctx<double>());
|
||||
ASSERT_EQ(registry.ctx<double>(), std::as_const(registry).ctx<double>());
|
||||
ASSERT_EQ(registry.try_ctx<const double>(), ®istry.ctx<double>());
|
||||
ASSERT_EQ(registry.ctx<double>(), std::as_const(registry).ctx<const double>());
|
||||
|
||||
ASSERT_EQ(registry.try_ctx<float>(), nullptr);
|
||||
}
|
||||
|
||||
TEST(Registry, ContextAsRef) {
|
||||
entt::registry registry;
|
||||
int value{3};
|
||||
|
||||
registry.set<int &>(value);
|
||||
|
||||
ASSERT_NE(registry.try_ctx<int>(), nullptr);
|
||||
ASSERT_NE(registry.try_ctx<const int>(), nullptr);
|
||||
ASSERT_NE(std::as_const(registry).try_ctx<const int>(), nullptr);
|
||||
ASSERT_EQ(registry.ctx<const int>(), 3);
|
||||
ASSERT_EQ(registry.ctx<int>(), 3);
|
||||
|
||||
registry.ctx<int>() = 42;
|
||||
|
||||
ASSERT_EQ(registry.ctx<int>(), 42);
|
||||
ASSERT_EQ(value, 42);
|
||||
|
||||
value = 3;
|
||||
|
||||
ASSERT_EQ(std::as_const(registry).ctx<const int>(), 3);
|
||||
}
|
||||
|
||||
TEST(Registry, ContextAsConstRef) {
|
||||
entt::registry registry;
|
||||
int value{3};
|
||||
|
||||
registry.set<const int &>(value);
|
||||
|
||||
ASSERT_EQ(registry.try_ctx<int>(), nullptr);
|
||||
ASSERT_NE(registry.try_ctx<const int>(), nullptr);
|
||||
ASSERT_NE(std::as_const(registry).try_ctx<const int>(), nullptr);
|
||||
ASSERT_EQ(registry.ctx<const int>(), 3);
|
||||
|
||||
value = 42;
|
||||
|
||||
ASSERT_EQ(std::as_const(registry).ctx<const int>(), 42);
|
||||
}
|
||||
|
||||
TEST(Registry, Functionalities) {
|
||||
entt::registry registry;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user