Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added user-defined context variable guard #800

Open
wants to merge 1 commit into
base: v3.8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/entt/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) =
#endif


#ifndef ENTT_REGISTRY_CONTEXT_GUARD
#define ENTT_REGISTRY_CONTEXT_GUARD()
#endif


#ifndef ENTT_STANDARD_CPP
# if defined __clang__ || defined __GNUC__
# define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
Expand Down
7 changes: 7 additions & 0 deletions src/entt/entity/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,7 @@ class basic_registry {
template<typename Type, typename... Args>
Type & set(Args &&... args) {
unset<Type>();
ENTT_REGISTRY_CONTEXT_GUARD()
vars.emplace_back(std::in_place_type<Type>, std::forward<Args>(args)...);
return any_cast<Type &>(vars.back());
}
Expand All @@ -1606,6 +1607,7 @@ class basic_registry {
*/
template<typename Type>
void unset() {
ENTT_REGISTRY_CONTEXT_GUARD()
vars.erase(std::remove_if(vars.begin(), vars.end(), [type = type_id<Type>()](auto &&var) { return var.type() == type; }), vars.end());
}

Expand Down Expand Up @@ -1634,13 +1636,15 @@ class basic_registry {
*/
template<typename Type>
[[nodiscard]] std::add_const_t<Type> * try_ctx() const {
ENTT_REGISTRY_CONTEXT_GUARD()
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<std::add_const_t<Type>>(&*it);
}

/*! @copydoc try_ctx */
template<typename Type>
[[nodiscard]] Type * try_ctx() {
ENTT_REGISTRY_CONTEXT_GUARD()
auto it = std::find_if(vars.begin(), vars.end(), [type = type_id<Type>()](auto &&var) { return var.type() == type; });
return it == vars.end() ? nullptr : any_cast<Type>(&*it);
}
Expand All @@ -1657,6 +1661,7 @@ class basic_registry {
*/
template<typename Type>
[[nodiscard]] std::add_const_t<Type> & ctx() const {
ENTT_REGISTRY_CONTEXT_GUARD()
auto it = std::find_if(vars.cbegin(), vars.cend(), [type = type_id<Type>()](auto &&var) { return var.type() == type; });
ENTT_ASSERT(it != vars.cend(), "Invalid instance");
return any_cast<std::add_const_t<Type> &>(*it);
Expand All @@ -1665,6 +1670,7 @@ class basic_registry {
/*! @copydoc ctx */
template<typename Type>
[[nodiscard]] Type & ctx() {
ENTT_REGISTRY_CONTEXT_GUARD()
auto it = std::find_if(vars.begin(), vars.end(), [type = type_id<Type>()](auto &&var) { return var.type() == type; });
ENTT_ASSERT(it != vars.end(), "Invalid instance");
return any_cast<Type &>(*it);
Expand Down Expand Up @@ -1693,6 +1699,7 @@ class basic_registry {
*/
template<typename Func>
void ctx(Func func) const {
ENTT_REGISTRY_CONTEXT_GUARD()
for(auto pos = vars.size(); pos; --pos) {
func(vars[pos-1].type());
}
Expand Down