From 028ab8eb2b089636e7c2ca4c11eaad501fab9924 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Wed, 7 Jul 2021 06:12:54 +0700 Subject: [PATCH] instance type for field and method infos --- headers/meta.hpp/meta_field_info.hpp | 13 +++++++++++++ headers/meta.hpp/meta_method_info.hpp | 25 +++++++++++++++++++------ untests/meta_field_tests.cpp | 3 +++ untests/meta_method_tests.cpp | 26 ++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 6 deletions(-) diff --git a/headers/meta.hpp/meta_field_info.hpp b/headers/meta.hpp/meta_field_info.hpp index 9535050..634a44f 100644 --- a/headers/meta.hpp/meta_field_info.hpp +++ b/headers/meta.hpp/meta_field_info.hpp @@ -81,6 +81,13 @@ namespace meta_hpp::field_detail using value_type = typename ft::value_type; return get_family_id(); } + + template < typename FieldType > + family_id make_instance_type() noexcept { + using ft = detail::field_traits; + using instance_type = typename ft::instance_type; + return get_family_id(); + } } namespace meta_hpp @@ -103,6 +110,10 @@ namespace meta_hpp return value_type_; } + family_id instance_type() const noexcept { + return instance_type_; + } + value get(cinstance instance) const { return getter_(instance); } @@ -142,11 +153,13 @@ namespace meta_hpp field_info(std::string id, FieldType field_ptr) : id_{std::move(id)} , value_type_{field_detail::make_value_type()} + , instance_type_{field_detail::make_instance_type()} , getter_{field_detail::make_getter(field_ptr)} , setter_{field_detail::make_setter(field_ptr)} {} private: std::string id_; family_id value_type_; + family_id instance_type_; field_detail::field_getter getter_; field_detail::field_setter setter_; std::map> datas_; diff --git a/headers/meta.hpp/meta_method_info.hpp b/headers/meta.hpp/meta_method_info.hpp index ef7c4c4..75bd1d1 100644 --- a/headers/meta.hpp/meta_method_info.hpp +++ b/headers/meta.hpp/meta_method_info.hpp @@ -132,10 +132,17 @@ namespace meta_hpp::method_detail return std::bind(&raw_cinvoke, method, _1, _2, _3); } + template < typename MethodType > + family_id make_instance_type() noexcept { + using mt = detail::method_traits; + using instance_type = typename mt::instance_type; + return get_family_id(); + } + template < typename MethodType > std::optional make_return_type() { - using ft = detail::method_traits; - using return_type = typename ft::return_type; + using mt = detail::method_traits; + using return_type = typename mt::return_type; if constexpr ( !std::is_void_v ) { return get_family_id(); @@ -146,15 +153,15 @@ namespace meta_hpp::method_detail template < typename MethodType, std::size_t... Is > std::vector make_argument_types_impl(std::index_sequence) { - using ft = detail::method_traits; - using argument_types = typename ft::argument_types; + using mt = detail::method_traits; + using argument_types = typename mt::argument_types; return { get_family_id>()... }; } template < typename MethodType > std::vector make_argument_types() { - using ft = detail::method_traits; - return make_argument_types_impl(std::make_index_sequence()); + using mt = detail::method_traits; + return make_argument_types_impl(std::make_index_sequence()); } } @@ -178,6 +185,10 @@ namespace meta_hpp return argument_types_.size(); } + family_id instance_type() const noexcept { + return instance_type_; + } + std::optional return_type() const noexcept { return return_type_; } @@ -238,12 +249,14 @@ namespace meta_hpp template < typename MethodType > method_info(std::string id, MethodType method_ptr) : id_{std::move(id)} + , instance_type_{method_detail::make_instance_type()} , return_type_{method_detail::make_return_type()} , argument_types_{method_detail::make_argument_types()} , invoke_{method_detail::make_invoke(method_ptr)} , cinvoke_{method_detail::make_cinvoke(method_ptr)} {} private: std::string id_; + family_id instance_type_; std::optional return_type_; std::vector argument_types_; method_detail::method_invoke invoke_; diff --git a/untests/meta_field_tests.cpp b/untests/meta_field_tests.cpp index 4f488ad..3efcd57 100644 --- a/untests/meta_field_tests.cpp +++ b/untests/meta_field_tests.cpp @@ -28,6 +28,9 @@ TEST_CASE("meta/field") { { CHECK(field_info.value_type() == meta::get_family_id()); CHECK(cfield_info.value_type() == meta::get_family_id()); + + CHECK(field_info.instance_type() == meta::get_family_id()); + CHECK(cfield_info.instance_type() == meta::get_family_id()); } { diff --git a/untests/meta_method_tests.cpp b/untests/meta_method_tests.cpp index 88b3798..03f57c3 100644 --- a/untests/meta_method_tests.cpp +++ b/untests/meta_method_tests.cpp @@ -60,6 +60,11 @@ TEST_CASE("meta/non_const_method") { CHECK(int_f_int2_info.arity() == 2); } + SUBCASE("instance_type") { + CHECK(void_f_void_info.instance_type() == meta::get_family_id()); + CHECK(int_f_void_info.instance_type() == meta::get_family_id()); + } + SUBCASE("return_type") { CHECK_FALSE(void_f_void_info.return_type()); CHECK(int_f_void_info.return_type() == meta::get_family_id()); @@ -158,6 +163,27 @@ TEST_CASE("meta/const_method") { const meta::method_info& int_f_int_info = int_f_int_method_; const meta::method_info& int_f_int2_info = int_f_int2_method_; + SUBCASE("instance_type") { + CHECK(void_f_void_info.instance_type() == meta::get_family_id()); + CHECK(int_f_void_info.instance_type() == meta::get_family_id()); + } + + SUBCASE("return_type") { + CHECK_FALSE(void_f_void_info.return_type()); + CHECK(int_f_void_info.return_type() == meta::get_family_id()); + } + + SUBCASE("argument_types") { + CHECK_FALSE(void_f_void_info.argument_type(0u)); + + CHECK(int_f_int_info.argument_type(0u) == meta::get_family_id()); + CHECK_FALSE(int_f_int_info.argument_type(1u)); + + CHECK(int_f_int2_info.argument_type(0u) == meta::get_family_id()); + CHECK(int_f_int2_info.argument_type(1u) == meta::get_family_id()); + CHECK_FALSE(int_f_int2_info.argument_type(2u)); + } + SUBCASE("another_instance") { const clazz2 instance; CHECK_THROWS_AS(void_f_void_info.invoke(instance), std::logic_error);