From 9495c2a69d5455570d23bed6001ed8056e5dc272 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 2 Jul 2021 00:55:11 +0700 Subject: [PATCH] throw in incorrect merge --- headers/meta.hpp/meta_class_info.hpp | 3 ++ headers/meta.hpp/meta_field_info.hpp | 5 ++- headers/meta.hpp/meta_function_info.hpp | 4 +- headers/meta.hpp/meta_fwd.hpp | 8 ++++ headers/meta.hpp/meta_method_info.hpp | 5 ++- headers/meta.hpp/meta_variable_info.hpp | 5 ++- untests/meta_class_tests.cpp | 49 ++++++++++++++++++++++++- 7 files changed, 70 insertions(+), 9 deletions(-) diff --git a/headers/meta.hpp/meta_class_info.hpp b/headers/meta.hpp/meta_class_info.hpp index 92752b5..bc74084 100644 --- a/headers/meta.hpp/meta_class_info.hpp +++ b/headers/meta.hpp/meta_class_info.hpp @@ -111,6 +111,9 @@ namespace meta_hpp friend class namespace_; private: void merge_with_(const class_info& other) { + if ( fid() != other.fid() ) { + throw std::logic_error("class_info::merge failed"); + } detail::merge_with(classes_, other.classes_, &class_info::merge_with_); detail::merge_with(datas_, other.datas_, &data_info::merge_with_); detail::merge_with(fields_, other.fields_, &field_info::merge_with_); diff --git a/headers/meta.hpp/meta_field_info.hpp b/headers/meta.hpp/meta_field_info.hpp index 08056ff..71e8c24 100644 --- a/headers/meta.hpp/meta_field_info.hpp +++ b/headers/meta.hpp/meta_field_info.hpp @@ -105,8 +105,9 @@ namespace meta_hpp template < auto Field > friend class field_; private: void merge_with_(const field_info& other) { - getter_ = other.getter_; - setter_ = other.setter_; + if ( fid() != other.fid() ) { + throw std::logic_error("field_info::merge failed"); + } detail::merge_with(datas_, other.datas_, &data_info::merge_with_); } private: diff --git a/headers/meta.hpp/meta_function_info.hpp b/headers/meta.hpp/meta_function_info.hpp index 2de241b..7b9dd23 100644 --- a/headers/meta.hpp/meta_function_info.hpp +++ b/headers/meta.hpp/meta_function_info.hpp @@ -113,7 +113,9 @@ namespace meta_hpp template < auto Function > friend class function_; private: void merge_with_(const function_info& other) { - invoke_ = other.invoke_; + if ( fid() != other.fid() ) { + throw std::logic_error("function_info::merge failed"); + } detail::merge_with(datas_, other.datas_, &data_info::merge_with_); } private: diff --git a/headers/meta.hpp/meta_fwd.hpp b/headers/meta.hpp/meta_fwd.hpp index 7e3a219..f736c5a 100644 --- a/headers/meta.hpp/meta_fwd.hpp +++ b/headers/meta.hpp/meta_fwd.hpp @@ -55,6 +55,14 @@ namespace meta_hpp friend bool operator<(family_id l, family_id r) noexcept { return l.id < r.id; } + + friend bool operator==(family_id l, family_id r) noexcept { + return l.id == r.id; + } + + friend bool operator!=(family_id l, family_id r) noexcept { + return l.id != r.id; + } }; namespace family_id_detail diff --git a/headers/meta.hpp/meta_method_info.hpp b/headers/meta.hpp/meta_method_info.hpp index 14ad0e9..f59324b 100644 --- a/headers/meta.hpp/meta_method_info.hpp +++ b/headers/meta.hpp/meta_method_info.hpp @@ -171,8 +171,9 @@ namespace meta_hpp template < auto Method > friend class method_; private: void merge_with_(const method_info& other) { - invoke_ = other.invoke_; - cinvoke_ = other.cinvoke_; + if ( fid() != other.fid() ) { + throw std::logic_error("method_info::merge failed"); + } detail::merge_with(datas_, other.datas_, &data_info::merge_with_); } private: diff --git a/headers/meta.hpp/meta_variable_info.hpp b/headers/meta.hpp/meta_variable_info.hpp index 5162f28..0cc2574 100644 --- a/headers/meta.hpp/meta_variable_info.hpp +++ b/headers/meta.hpp/meta_variable_info.hpp @@ -103,8 +103,9 @@ namespace meta_hpp template < auto Variable > friend class variable_; private: void merge_with_(const variable_info& other) { - getter_ = other.getter_; - setter_ = other.setter_; + if ( fid() != other.fid() ) { + throw std::logic_error("variable_info::merge failed"); + } detail::merge_with(datas_, other.datas_, &data_info::merge_with_); } private: diff --git a/untests/meta_class_tests.cpp b/untests/meta_class_tests.cpp index 4596024..cd2105e 100644 --- a/untests/meta_class_tests.cpp +++ b/untests/meta_class_tests.cpp @@ -17,6 +17,15 @@ namespace class clazz { public: class clazz2 { + public: + int field{21}; + const int cfield{22}; + }; + + class clazz3 { + public: + int field{31}; + const int cfield{32}; }; int field{1}; @@ -39,8 +48,7 @@ TEST_CASE("meta/class") { namespace meta = meta_hpp; meta::class_ class_{"clazz"}; - const meta::class_& cclass_ = class_; - const meta::class_info& clazz_info = cclass_; + const meta::class_info& clazz_info = class_; CHECK_FALSE(clazz_info.get_class("clazz2")); CHECK_FALSE(clazz_info.get_field("field")); @@ -110,3 +118,40 @@ TEST_CASE("meta/class") { CHECK(cvariable_info.id() == "cvariable"); } } + +TEST_CASE("meta/class/merge") { + namespace meta = meta_hpp; + + meta::class_ clazz_{"clazz"}; + const meta::class_info& clazz_info = clazz_; + + SUBCASE("merge") { + CHECK_NOTHROW(clazz_( + meta::class_("child")( + meta::field_<&clazz::clazz2::field>("field") + ) + )); + + CHECK_NOTHROW(clazz_( + meta::class_("child")( + meta::field_<&clazz::clazz2::cfield>("cfield") + ) + )); + + CHECK_THROWS_AS(clazz_( + meta::class_("child")( + meta::field_<&clazz::clazz3::field>("field") + ) + ), std::logic_error); + + CHECK(clazz_info.get_class("child")); + CHECK(clazz_info.get_class("child")->get_field("field")); + CHECK(clazz_info.get_class("child")->get_field("cfield")); + + { + clazz::clazz2 instance{}; + CHECK(clazz_info.get_class("child")->get_field("field")->get(&instance).to_int() == 21); + CHECK(clazz_info.get_class("child")->get_field("cfield")->get(&instance).to_int() == 22); + } + } +}