From 0e65288a9c0153d81a58c59b26818309013f3989 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 1 Jul 2021 05:20:31 +0700 Subject: [PATCH] family id for infos --- headers/meta.hpp/meta_class.hpp | 2 +- headers/meta.hpp/meta_class_info.hpp | 10 ++++-- headers/meta.hpp/meta_field.hpp | 2 +- headers/meta.hpp/meta_field_info.hpp | 10 ++++-- headers/meta.hpp/meta_function.hpp | 2 +- headers/meta.hpp/meta_function_info.hpp | 10 ++++-- headers/meta.hpp/meta_fwd.hpp | 43 +++++++++++++++++++++++++ headers/meta.hpp/meta_method.hpp | 2 +- headers/meta.hpp/meta_method_info.hpp | 10 ++++-- headers/meta.hpp/meta_variable.hpp | 2 +- headers/meta.hpp/meta_variable_info.hpp | 10 ++++-- 11 files changed, 88 insertions(+), 15 deletions(-) diff --git a/headers/meta.hpp/meta_class.hpp b/headers/meta.hpp/meta_class.hpp index 3d2c155..8a965ad 100644 --- a/headers/meta.hpp/meta_class.hpp +++ b/headers/meta.hpp/meta_class.hpp @@ -22,7 +22,7 @@ namespace meta_hpp class class_ { public: explicit class_(std::string id) - : info_(std::move(id)) {} + : info_{get_family_id(), std::move(id)} {} const class_info& info() const noexcept { return info_; diff --git a/headers/meta.hpp/meta_class_info.hpp b/headers/meta.hpp/meta_class_info.hpp index 6f3ea3b..92752b5 100644 --- a/headers/meta.hpp/meta_class_info.hpp +++ b/headers/meta.hpp/meta_class_info.hpp @@ -27,9 +27,14 @@ namespace meta_hpp class_info& operator=(class_info&&) = default; class_info& operator=(const class_info&) = default; - class_info(std::string id) - : id_(std::move(id)) {} + class_info(family_id fid, std::string id) + : fid_(std::move(fid)) + , id_(std::move(id)) {} public: + const family_id& fid() const noexcept { + return fid_; + } + const std::string& id() const noexcept { return id_; } @@ -114,6 +119,7 @@ namespace meta_hpp detail::merge_with(variables_, other.variables_, &variable_info::merge_with_); } private: + family_id fid_; std::string id_; std::map> classes_; std::map> datas_; diff --git a/headers/meta.hpp/meta_field.hpp b/headers/meta.hpp/meta_field.hpp index 07c9417..e4b3934 100644 --- a/headers/meta.hpp/meta_field.hpp +++ b/headers/meta.hpp/meta_field.hpp @@ -20,7 +20,7 @@ namespace meta_hpp static_assert(std::is_member_object_pointer_v); explicit field_(std::string id) - : info_(std::move(id)) { + : info_{get_family_id(), std::move(id)} { info_.getter_ = &field_detail::getter; info_.setter_ = &field_detail::setter; } diff --git a/headers/meta.hpp/meta_field_info.hpp b/headers/meta.hpp/meta_field_info.hpp index 3099767..08056ff 100644 --- a/headers/meta.hpp/meta_field_info.hpp +++ b/headers/meta.hpp/meta_field_info.hpp @@ -68,9 +68,14 @@ namespace meta_hpp field_info& operator=(field_info&&) = default; field_info& operator=(const field_info&) = default; - field_info(std::string id) - : id_(std::move(id)) {} + field_info(family_id fid, std::string id) + : fid_(std::move(fid)) + , id_(std::move(id)) {} public: + const family_id& fid() const noexcept { + return fid_; + } + const std::string& id() const noexcept { return id_; } @@ -105,6 +110,7 @@ namespace meta_hpp detail::merge_with(datas_, other.datas_, &data_info::merge_with_); } private: + family_id fid_; std::string id_; value(*getter_)(const void*); void(*setter_)(void*, value); diff --git a/headers/meta.hpp/meta_function.hpp b/headers/meta.hpp/meta_function.hpp index 35d3fe1..eb77afc 100644 --- a/headers/meta.hpp/meta_function.hpp +++ b/headers/meta.hpp/meta_function.hpp @@ -20,7 +20,7 @@ namespace meta_hpp static_assert(std::is_function_v>); explicit function_(std::string id) - : info_(std::move(id)) { + : info_{get_family_id(), std::move(id)} { info_.invoke_ = &function_detail::invoke; } diff --git a/headers/meta.hpp/meta_function_info.hpp b/headers/meta.hpp/meta_function_info.hpp index b1b66e8..2de241b 100644 --- a/headers/meta.hpp/meta_function_info.hpp +++ b/headers/meta.hpp/meta_function_info.hpp @@ -75,9 +75,14 @@ namespace meta_hpp function_info& operator=(function_info&&) = default; function_info& operator=(const function_info&) = default; - function_info(std::string id) - : id_(std::move(id)) {} + function_info(family_id fid, std::string id) + : fid_(std::move(fid)) + , id_(std::move(id)) {} public: + const family_id& fid() const noexcept { + return fid_; + } + const std::string& id() const noexcept { return id_; } @@ -112,6 +117,7 @@ namespace meta_hpp detail::merge_with(datas_, other.datas_, &data_info::merge_with_); } private: + family_id fid_; std::string id_; value(*invoke_)(value*, std::size_t); std::map> datas_; diff --git a/headers/meta.hpp/meta_fwd.hpp b/headers/meta.hpp/meta_fwd.hpp index 173d887..09be441 100644 --- a/headers/meta.hpp/meta_fwd.hpp +++ b/headers/meta.hpp/meta_fwd.hpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -44,6 +45,48 @@ namespace meta_hpp template < auto Variable > class variable_; } +namespace meta_hpp +{ + struct family_id { + using underlying_type = std::size_t; + underlying_type id{}; + + friend bool operator<(family_id l, family_id r) noexcept { + return l.id < r.id; + } + }; + + namespace family_id_detail + { + template < typename Void = void > + class type_family_base { + static_assert( + std::is_void_v, + "unexpected internal error"); + protected: + static family_id::underlying_type last_id_; + }; + + template < typename T > + class type_family final : public type_family_base<> { + public: + static family_id id() noexcept { + static family_id self_id{++last_id_}; + assert(self_id.id > 0u && "family_id overflow"); + return self_id; + } + }; + + template < typename Void > + family_id::underlying_type type_family_base::last_id_{}; + } + + template < typename T > + family_id get_family_id() noexcept { + return family_id_detail::type_family::id(); + } +} + namespace meta_hpp { template < typename Signature > diff --git a/headers/meta.hpp/meta_method.hpp b/headers/meta.hpp/meta_method.hpp index 740c7a4..6b1c419 100644 --- a/headers/meta.hpp/meta_method.hpp +++ b/headers/meta.hpp/meta_method.hpp @@ -20,7 +20,7 @@ namespace meta_hpp static_assert(std::is_member_function_pointer_v); explicit method_(std::string id) - : info_(std::move(id)) { + : info_{get_family_id(), std::move(id)} { info_.invoke_ = &method_detail::invoke; info_.cinvoke_ = &method_detail::cinvoke; } diff --git a/headers/meta.hpp/meta_method_info.hpp b/headers/meta.hpp/meta_method_info.hpp index 106ca97..14ad0e9 100644 --- a/headers/meta.hpp/meta_method_info.hpp +++ b/headers/meta.hpp/meta_method_info.hpp @@ -132,9 +132,14 @@ namespace meta_hpp method_info& operator=(method_info&&) = default; method_info& operator=(const method_info&) = default; - method_info(std::string id) - : id_(std::move(id)) {} + method_info(family_id fid, std::string id) + : fid_(std::move(fid)) + , id_(std::move(id)) {} public: + const family_id& fid() const noexcept { + return fid_; + } + const std::string& id() const noexcept { return id_; } @@ -171,6 +176,7 @@ namespace meta_hpp detail::merge_with(datas_, other.datas_, &data_info::merge_with_); } private: + family_id fid_; std::string id_; value(*invoke_)(void*, value*, std::size_t); value(*cinvoke_)(const void*, value*, std::size_t); diff --git a/headers/meta.hpp/meta_variable.hpp b/headers/meta.hpp/meta_variable.hpp index 7ba050a..656219f 100644 --- a/headers/meta.hpp/meta_variable.hpp +++ b/headers/meta.hpp/meta_variable.hpp @@ -18,7 +18,7 @@ namespace meta_hpp class variable_ { public: explicit variable_(std::string id) - : info_(std::move(id)) { + : info_{get_family_id(), std::move(id)} { info_.getter_ = &variable_detail::getter; info_.setter_ = &variable_detail::setter; } diff --git a/headers/meta.hpp/meta_variable_info.hpp b/headers/meta.hpp/meta_variable_info.hpp index c385e89..5162f28 100644 --- a/headers/meta.hpp/meta_variable_info.hpp +++ b/headers/meta.hpp/meta_variable_info.hpp @@ -63,9 +63,14 @@ namespace meta_hpp variable_info& operator=(variable_info&&) = default; variable_info& operator=(const variable_info&) = default; - variable_info(std::string id) - : id_(std::move(id)) {} + variable_info(family_id fid, std::string id) + : fid_(std::move(fid)) + , id_(std::move(id)) {} public: + const family_id& fid() const noexcept { + return fid_; + } + const std::string& id() const noexcept { return id_; } @@ -103,6 +108,7 @@ namespace meta_hpp detail::merge_with(datas_, other.datas_, &data_info::merge_with_); } private: + family_id fid_; std::string id_; value(*getter_)(); void(*setter_)(value);