From 8a7f6c26a3a946f1e1a3b89f7af0f726bb407391 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Wed, 26 Dec 2018 18:12:19 +0700 Subject: [PATCH] std ext: as_const --- ecs.hpp | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/ecs.hpp b/ecs.hpp index 45cfecc..4ea4f11 100644 --- a/ecs.hpp +++ b/ecs.hpp @@ -45,6 +45,23 @@ namespace ecs_hpp "ecs_hpp::entity_id must be an unsigned integer"); } +// ----------------------------------------------------------------------------- +// +// utilities +// +// ----------------------------------------------------------------------------- + +namespace ecs_hpp +{ + namespace detail + { + template < typename T > + constexpr std::add_const_t& as_const(T& t) noexcept { + return t; + } + } +} + // ----------------------------------------------------------------------------- // // detail::type_family @@ -384,7 +401,7 @@ namespace ecs_hpp } inline bool entity::is_alive() const noexcept { - return const_cast(owner_).is_entity_alive(*this); + return detail::as_const(owner_).is_entity_alive(*this); } template < typename T, typename... Args > @@ -401,7 +418,7 @@ namespace ecs_hpp template < typename T > bool entity::exists_component() const noexcept { - return const_cast(owner_).exists_component(*this); + return detail::as_const(owner_).exists_component(*this); } inline std::size_t entity::remove_all_components() noexcept { @@ -415,7 +432,7 @@ namespace ecs_hpp template < typename T > const T& entity::get_component() const { - return const_cast(owner_).get_component(*this); + return detail::as_const(owner_).get_component(*this); } template < typename T > @@ -425,7 +442,7 @@ namespace ecs_hpp template < typename T > const T* entity::find_component() const noexcept { - return const_cast(owner_).find_component(*this); + return detail::as_const(owner_).find_component(*this); } template < typename... Ts > @@ -435,7 +452,7 @@ namespace ecs_hpp template < typename... Ts > std::tuple entity::get_components() const { - return const_cast(owner_).get_components(*this); + return detail::as_const(owner_).get_components(*this); } template < typename... Ts > @@ -445,7 +462,7 @@ namespace ecs_hpp template < typename... Ts > std::tuple entity::find_components() const noexcept { - return const_cast(owner_).find_components(*this); + return detail::as_const(owner_).find_components(*this); } inline bool operator==(const entity& l, const entity& r) noexcept {