registry: visit functionality for context variables (close #390)
This commit is contained in:
@@ -172,7 +172,7 @@ class basic_registry {
|
||||
|
||||
struct basic_variable {
|
||||
virtual ~basic_variable() = default;
|
||||
virtual ENTT_ID_TYPE id() const ENTT_NOEXCEPT = 0;
|
||||
virtual ENTT_ID_TYPE type_id() const ENTT_NOEXCEPT = 0;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
@@ -185,7 +185,7 @@ class basic_registry {
|
||||
: value{std::forward<Args>(args)...}
|
||||
{}
|
||||
|
||||
ENTT_ID_TYPE id() const ENTT_NOEXCEPT override {
|
||||
ENTT_ID_TYPE type_id() const ENTT_NOEXCEPT override {
|
||||
return type_info<Type>::id();
|
||||
}
|
||||
};
|
||||
@@ -1714,7 +1714,7 @@ public:
|
||||
template<typename Type>
|
||||
void unset() {
|
||||
vars.erase(std::remove_if(vars.begin(), vars.end(), [](auto &&handler) {
|
||||
return handler->id() == type_info<Type>::id();
|
||||
return handler->type_id() == type_info<Type>::id();
|
||||
}), vars.end());
|
||||
}
|
||||
|
||||
@@ -1743,7 +1743,7 @@ public:
|
||||
*/
|
||||
template<typename Type>
|
||||
const Type * try_ctx() const {
|
||||
auto it = std::find_if(vars.cbegin(), vars.cend(), [](auto &&handler) { return handler->id() == type_info<Type>::id(); });
|
||||
auto it = std::find_if(vars.cbegin(), vars.cend(), [](auto &&handler) { return handler->type_id() == type_info<Type>::id(); });
|
||||
return it == vars.cend() ? nullptr : &static_cast<const variable_handler<Type> &>(*it->get()).value;
|
||||
}
|
||||
|
||||
@@ -1778,6 +1778,33 @@ public:
|
||||
return const_cast<Type &>(std::as_const(*this).template ctx<Type>());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Visits a registry and returns the types for its context variables.
|
||||
*
|
||||
* The signature of the function should be equivalent to the following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* void(const ENTT_ID_TYPE);
|
||||
* @endcode
|
||||
*
|
||||
* Returned identifiers are those of the context variables currently set.
|
||||
*
|
||||
* @sa type_info
|
||||
*
|
||||
* @warning
|
||||
* It's not specified whether a context variable created during the visit is
|
||||
* returned or not to the caller.
|
||||
*
|
||||
* @tparam Func Type of the function object to invoke.
|
||||
* @param func A valid function object.
|
||||
*/
|
||||
template<typename Func>
|
||||
void ctx(Func func) const {
|
||||
for(auto pos = vars.size(); pos; --pos) {
|
||||
func(vars[pos-1]->type_id());
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<group_data> groups{};
|
||||
mutable std::vector<pool_data> pools{};
|
||||
|
||||
@@ -60,6 +60,15 @@ TEST(Registry, Context) {
|
||||
registry.unset<int>();
|
||||
registry.unset<double>();
|
||||
|
||||
auto count = 0;
|
||||
|
||||
registry.ctx([&count](const auto var) {
|
||||
ASSERT_EQ(var, entt::type_info<char>::id());
|
||||
++count;
|
||||
});
|
||||
|
||||
ASSERT_EQ(count, 1);
|
||||
|
||||
ASSERT_NE(registry.try_ctx<char>(), nullptr);
|
||||
ASSERT_EQ(registry.try_ctx<int>(), nullptr);
|
||||
ASSERT_EQ(registry.try_ctx<double>(), nullptr);
|
||||
|
||||
Reference in New Issue
Block a user