instance type for field and method infos

This commit is contained in:
BlackMATov
2021-07-07 06:12:54 +07:00
parent 065b5751e9
commit 028ab8eb2b
4 changed files with 61 additions and 6 deletions

View File

@@ -81,6 +81,13 @@ namespace meta_hpp::field_detail
using value_type = typename ft::value_type;
return get_family_id<value_type>();
}
template < typename FieldType >
family_id make_instance_type() noexcept {
using ft = detail::field_traits<FieldType>;
using instance_type = typename ft::instance_type;
return get_family_id<instance_type>();
}
}
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<FieldType>()}
, instance_type_{field_detail::make_instance_type<FieldType>()}
, 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<std::string, data_info, std::less<>> datas_;

View File

@@ -132,10 +132,17 @@ namespace meta_hpp::method_detail
return std::bind(&raw_cinvoke<MethodType>, method, _1, _2, _3);
}
template < typename MethodType >
family_id make_instance_type() noexcept {
using mt = detail::method_traits<MethodType>;
using instance_type = typename mt::instance_type;
return get_family_id<instance_type>();
}
template < typename MethodType >
std::optional<family_id> make_return_type() {
using ft = detail::method_traits<MethodType>;
using return_type = typename ft::return_type;
using mt = detail::method_traits<MethodType>;
using return_type = typename mt::return_type;
if constexpr ( !std::is_void_v<return_type> ) {
return get_family_id<return_type>();
@@ -146,15 +153,15 @@ namespace meta_hpp::method_detail
template < typename MethodType, std::size_t... Is >
std::vector<family_id> make_argument_types_impl(std::index_sequence<Is...>) {
using ft = detail::method_traits<MethodType>;
using argument_types = typename ft::argument_types;
using mt = detail::method_traits<MethodType>;
using argument_types = typename mt::argument_types;
return { get_family_id<std::tuple_element_t<Is, argument_types>>()... };
}
template < typename MethodType >
std::vector<family_id> make_argument_types() {
using ft = detail::method_traits<MethodType>;
return make_argument_types_impl<MethodType>(std::make_index_sequence<ft::arity>());
using mt = detail::method_traits<MethodType>;
return make_argument_types_impl<MethodType>(std::make_index_sequence<mt::arity>());
}
}
@@ -178,6 +185,10 @@ namespace meta_hpp
return argument_types_.size();
}
family_id instance_type() const noexcept {
return instance_type_;
}
std::optional<family_id> 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<MethodType>()}
, return_type_{method_detail::make_return_type<MethodType>()}
, argument_types_{method_detail::make_argument_types<MethodType>()}
, invoke_{method_detail::make_invoke(method_ptr)}
, cinvoke_{method_detail::make_cinvoke(method_ptr)} {}
private:
std::string id_;
family_id instance_type_;
std::optional<family_id> return_type_;
std::vector<family_id> argument_types_;
method_detail::method_invoke invoke_;

View File

@@ -28,6 +28,9 @@ TEST_CASE("meta/field") {
{
CHECK(field_info.value_type() == meta::get_family_id<int>());
CHECK(cfield_info.value_type() == meta::get_family_id<int>());
CHECK(field_info.instance_type() == meta::get_family_id<clazz>());
CHECK(cfield_info.instance_type() == meta::get_family_id<clazz>());
}
{

View File

@@ -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<clazz>());
CHECK(int_f_void_info.instance_type() == meta::get_family_id<clazz>());
}
SUBCASE("return_type") {
CHECK_FALSE(void_f_void_info.return_type());
CHECK(int_f_void_info.return_type() == meta::get_family_id<int>());
@@ -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<clazz>());
CHECK(int_f_void_info.instance_type() == meta::get_family_id<clazz>());
}
SUBCASE("return_type") {
CHECK_FALSE(void_f_void_info.return_type());
CHECK(int_f_void_info.return_type() == meta::get_family_id<int>());
}
SUBCASE("argument_types") {
CHECK_FALSE(void_f_void_info.argument_type(0u));
CHECK(int_f_int_info.argument_type(0u) == meta::get_family_id<int>());
CHECK_FALSE(int_f_int_info.argument_type(1u));
CHECK(int_f_int2_info.argument_type(0u) == meta::get_family_id<int>());
CHECK(int_f_int2_info.argument_type(1u) == meta::get_family_id<int>());
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);