META_HPP_NO_RTTI

This commit is contained in:
BlackMATov
2022-02-07 03:11:40 +07:00
parent 1f06462af4
commit 6c156da20d
2 changed files with 15 additions and 0 deletions

View File

@@ -49,6 +49,10 @@
# define META_HPP_NO_EXCEPTIONS # define META_HPP_NO_EXCEPTIONS
#endif #endif
#if !defined(__cpp_rtti)
# define META_HPP_NO_RTTI
#endif
namespace meta_hpp namespace meta_hpp
{ {
using detail::select_const; using detail::select_const;

View File

@@ -19,6 +19,7 @@ namespace meta_hpp::detail
} }
[[nodiscard]] any_type get_type_by_id(type_id id) const noexcept { [[nodiscard]] any_type get_type_by_id(type_id id) const noexcept {
const std::lock_guard lock{mutex_};
if ( auto iter = type_by_id_.find(id); iter != type_by_id_.end() ) { if ( auto iter = type_by_id_.find(id); iter != type_by_id_.end() ) {
return iter->second; return iter->second;
} }
@@ -26,6 +27,7 @@ namespace meta_hpp::detail
} }
[[nodiscard]] any_type get_type_by_rtti(const std::type_index& index) const noexcept { [[nodiscard]] any_type get_type_by_rtti(const std::type_index& index) const noexcept {
const std::lock_guard lock{mutex_};
if ( auto iter = type_by_rtti_.find(index); iter != type_by_rtti_.end() ) { if ( auto iter = type_by_rtti_.find(index); iter != type_by_rtti_.end() ) {
return iter->second; return iter->second;
} }
@@ -200,12 +202,16 @@ namespace meta_hpp::detail
TypeData ensure_type(const TypeData& type_data) { TypeData ensure_type(const TypeData& type_data) {
static std::once_flag init_flag{}; static std::once_flag init_flag{};
std::call_once(init_flag, [this, &type_data](){ std::call_once(init_flag, [this, &type_data](){
const std::lock_guard lock{mutex_};
type_by_id_[type_data->id] = any_type{type_data}; type_by_id_[type_data->id] = any_type{type_data};
#ifndef META_HPP_NO_RTTI
type_by_rtti_[typeid(Type)] = any_type{type_data}; type_by_rtti_[typeid(Type)] = any_type{type_data};
#endif
}); });
return type_data; return type_data;
} }
private: private:
mutable std::mutex mutex_;
std::map<type_id, any_type, std::less<>> type_by_id_; std::map<type_id, any_type, std::less<>> type_by_id_;
std::map<std::type_index, any_type, std::less<>> type_by_rtti_; std::map<std::type_index, any_type, std::less<>> type_by_rtti_;
}; };
@@ -259,7 +265,12 @@ namespace meta_hpp::detail
{ {
template < typename T > template < typename T >
[[nodiscard]] any_type resolve_polymorphic_type(T&& v) noexcept { [[nodiscard]] any_type resolve_polymorphic_type(T&& v) noexcept {
#ifndef META_HPP_NO_RTTI
type_registry& registry = type_registry::instance(); type_registry& registry = type_registry::instance();
return registry.get_type_by_rtti(typeid(v)); return registry.get_type_by_rtti(typeid(v));
#else
(void)v;
return any_type{};
#endif
} }
} }