remove "auto Variable"

This commit is contained in:
BlackMATov
2021-07-07 03:11:17 +07:00
parent 4e829bc7b7
commit 014a1c4491
10 changed files with 52 additions and 32 deletions

View File

@@ -37,7 +37,7 @@ namespace meta_hpp::field_detail
void raw_setter(
[[maybe_unused]] FieldType field,
[[maybe_unused]] instance instance,
value value)
[[maybe_unused]] value value)
{
using ft = detail::field_traits<FieldType>;
using value_type = typename ft::value_type;

View File

@@ -68,7 +68,7 @@ namespace meta_hpp::method_detail
std::optional<value> raw_cinvoke_impl(
[[maybe_unused]] MethodType method,
[[maybe_unused]] cinstance instance,
value* args,
[[maybe_unused]] value* args,
std::index_sequence<Is...>)
{
using mt = detail::method_traits<MethodType>;

View File

@@ -14,11 +14,11 @@
namespace meta_hpp
{
template < auto Variable >
template < typename VariableType >
class variable_ {
public:
explicit variable_(std::string id)
: info_{detail::auto_arg<META_HPP_AUTO_T(Variable)>, std::move(id)} {}
explicit variable_(std::string id, VariableType variable)
: info_{std::move(id), variable} {}
operator const variable_info&() const noexcept {
return info_;

View File

@@ -14,17 +14,22 @@
namespace meta_hpp::variable_detail
{
template < typename VariableType, VariableType Variable >
value getter() {
template < typename VariableType >
value raw_getter(
VariableType variable)
{
using vt = detail::variable_traits<std::remove_reference_t<VariableType>>;
using value_type = typename vt::value_type;
value_type typed_value{*Variable};
value_type typed_value{*variable};
return value{std::move(typed_value)};
}
template < typename VariableType, VariableType Variable >
void setter(value value) {
template < typename VariableType >
void raw_setter(
[[maybe_unused]] VariableType variable,
[[maybe_unused]] value value)
{
using vt = detail::variable_traits<std::remove_reference_t<VariableType>>;
using value_type = typename vt::value_type;
@@ -34,11 +39,26 @@ namespace meta_hpp::variable_detail
throw std::logic_error("an attempt to set a variable with incorrect argument type");
}
*Variable = std::move(*typed_value);
*variable = std::move(*typed_value);
} else {
throw std::logic_error("an attempt to set a constant variable");
}
}
using variable_getter = std::function<value()>;
using variable_setter = std::function<void(value)>;
template < typename VariableType >
variable_getter make_getter(VariableType variable) {
using namespace std::placeholders;
return std::bind(&raw_getter<VariableType>, variable);
}
template < typename VariableType >
variable_setter make_setter(VariableType variable) {
using namespace std::placeholders;
return std::bind(&raw_setter<VariableType>, variable, _1);
}
}
namespace meta_hpp
@@ -89,18 +109,18 @@ namespace meta_hpp
detail::merge_with(datas_, other.datas_, &data_info::merge);
}
private:
template < auto Variable >
template < typename VariableType >
friend class variable_;
template < typename VariableType, VariableType Variable >
variable_info(detail::auto_arg_t<VariableType, Variable>, std::string id)
template < typename VariableType >
variable_info(std::string id, VariableType variable)
: id_{std::move(id)}
, getter_{&variable_detail::getter<VariableType, Variable>}
, setter_{&variable_detail::setter<VariableType, Variable>} {}
, getter_{variable_detail::make_getter(variable)}
, setter_{variable_detail::make_setter(variable)} {}
private:
std::string id_;
value(*getter_)();
void(*setter_)(value);
variable_detail::variable_getter getter_;
variable_detail::variable_setter setter_;
std::map<std::string, data_info, std::less<>> datas_;
};
}

View File

@@ -66,8 +66,8 @@ TEST_CASE("meta/class") {
meta::function_("func", &clazz::func),
meta::method_("method", &clazz::method),
meta::method_("cmethod", &clazz::cmethod),
meta::variable_<&clazz::variable>("variable"),
meta::variable_<&clazz::cvariable>("cvariable"));
meta::variable_("variable", &clazz::variable),
meta::variable_("cvariable", &clazz::cvariable));
CHECK(clazz_info.get_class("clazz2"));
CHECK(clazz_info.get_field("field"));

View File

@@ -157,7 +157,7 @@ TEST_CASE("meta/data/variable") {
using namespace std::string_literals;
const meta::class_info clazz_info = meta::class_<clazz>("clazz")(
meta::variable_<&clazz::variable>("variable")(
meta::variable_("variable", &clazz::variable)(
meta::data_("hello"s, "world"s)
)
);

View File

@@ -36,8 +36,8 @@ TEST_CASE("meta/namespace") {
meta::class_<clazz>("clazz"),
meta::function_("func", &func),
meta::namespace_("ns2"),
meta::variable_<&variable>("variable"),
meta::variable_<&cvariable>("cvariable"));
meta::variable_("variable", &variable),
meta::variable_("cvariable", &cvariable));
CHECK(ns_info.get_class("clazz"));
CHECK(ns_info.get_function("func"));
@@ -82,7 +82,7 @@ TEST_CASE("meta/namespace/merge") {
meta::class_<clazz>{"clazz"},
meta::function_("func", &func),
meta::namespace_{"ns3"},
meta::variable_<&variable>("variable")
meta::variable_("variable", &variable)
)
);
@@ -92,7 +92,7 @@ TEST_CASE("meta/namespace/merge") {
meta::function_("func2", &func),
meta::namespace_{"ns3"}(
meta::namespace_{"ns4"},
meta::variable_<&variable>("variable2")
meta::variable_("variable2", &variable)
)
)
);

View File

@@ -54,14 +54,14 @@ TEST_CASE("meta/registry") {
meta::field_("x", &ivec2::x),
meta::field_("y", &ivec2::y),
meta::method_("dot", &ivec2::dot),
meta::variable_<&ivec2::zero>("zero")
meta::variable_("zero", &ivec2::zero)
),
meta::class_<ivec3>("ivec3")(
meta::field_("x", &ivec3::x),
meta::field_("y", &ivec3::y),
meta::field_("z", &ivec3::z),
meta::method_("dot", &ivec3::dot),
meta::variable_<&ivec3::zero>("zero")
meta::variable_("zero", &ivec3::zero)
),
meta::function_("iadd2", &iadd2),
meta::function_("iadd3", &iadd3)

View File

@@ -51,7 +51,7 @@ TEST_CASE("meta/type") {
CHECK(namespace_type.is_namespace());
CHECK(namespace_type.get_namespace()->id() == "ns");
meta::type variable_type = meta::variable_<&clazz::variable>("variable");
meta::type variable_type = meta::variable_("variable", &clazz::variable);
CHECK(variable_type.is_variable());
CHECK(variable_type.get_variable()->id() == "variable");
}

View File

@@ -26,8 +26,8 @@ TEST_CASE("meta/variable") {
namespace meta = meta_hpp;
SUBCASE("in_namespace") {
meta::variable_<&variable> variable_{"variable"};
meta::variable_<&cvariable> cvariable_{"cvariable"};
meta::variable_ variable_{"variable", &variable};
meta::variable_ cvariable_{"cvariable", &cvariable};
const meta::variable_info& variable_info = variable_;
const meta::variable_info& cvariable_info = cvariable_;
@@ -60,8 +60,8 @@ TEST_CASE("meta/variable") {
}
SUBCASE("in_class") {
meta::variable_<&clazz::variable> variable_{"variable"};
meta::variable_<&clazz::cvariable> cvariable_{"cvariable"};
meta::variable_ variable_{"variable", &clazz::variable};
meta::variable_ cvariable_{"cvariable", &clazz::cvariable};
const meta::variable_info& variable_info = variable_;
const meta::variable_info& cvariable_info = cvariable_;