parameter states for function, method and ctor

This commit is contained in:
BlackMATov
2022-02-08 07:30:21 +07:00
parent 9afb3920fc
commit 40b76839d8
16 changed files with 387 additions and 28 deletions

View File

@@ -199,7 +199,10 @@ namespace meta_hpp
class parameter_index;
class scope_index;
class variable_index;
}
namespace meta_hpp
{
using class_set = std::set<class_type, std::less<>>;
using class_map = std::map<std::string, class_type, std::less<>>;
@@ -214,4 +217,6 @@ namespace meta_hpp
using method_map = std::map<method_index, method, std::less<>>;
using scope_map = std::map<scope_index, scope, std::less<>>;
using variable_map = std::map<variable_index, variable, std::less<>>;
using parameter_list = std::vector<parameter>;
}

View File

@@ -50,6 +50,13 @@ namespace meta_hpp
class_bind& ctor_(Policy = Policy{})
requires detail::class_bind_ctor_kind<Class, Args...>;
template < typename... Args
, ctor_policy_kind Policy = ctor_policy::as_object >
class_bind& ctor_(
std::initializer_list<std::string_view> pnames,
Policy = Policy{})
requires detail::class_bind_ctor_kind<Class, Args...>;
class_bind& dtor_()
requires detail::class_bind_dtor_kind<Class>;
@@ -61,6 +68,14 @@ namespace meta_hpp
, function_policy_kind Policy = function_policy::as_copy >
class_bind& function_(std::string name, Function function, Policy = Policy{});
template < detail::function_kind Function
, function_policy_kind Policy = function_policy::as_copy >
class_bind& function_(
std::string name,
Function function,
std::initializer_list<std::string_view> pnames,
Policy = Policy{});
template < detail::member_kind Member
, member_policy_kind Policy = member_policy::as_copy >
class_bind& member_(std::string name, Member member, Policy = Policy{})
@@ -71,6 +86,15 @@ namespace meta_hpp
class_bind& method_(std::string name, Method method, Policy = Policy{})
requires detail::class_bind_method_kind<Class, Method>;
template < detail::method_kind Method
, method_policy_kind Policy = method_policy::as_copy >
class_bind& method_(
std::string name,
Method method,
std::initializer_list<std::string_view> pnames,
Policy = Policy{})
requires detail::class_bind_method_kind<Class, Method>;
template < detail::pointer_kind Pointer
, variable_policy_kind Policy = variable_policy::as_copy >
class_bind& variable_(std::string name, Pointer pointer, Policy = Policy{});
@@ -114,6 +138,14 @@ namespace meta_hpp
, function_policy_kind Policy = function_policy::as_copy >
scope_bind& function_(std::string name, Function function, Policy = Policy{});
template < detail::function_kind Function
, function_policy_kind Policy = function_policy::as_copy >
scope_bind& function_(
std::string name,
Function function,
std::initializer_list<std::string_view> pnames,
Policy = Policy{});
template < detail::pointer_kind Pointer
, variable_policy_kind Policy = variable_policy::as_copy >
scope_bind& variable_(std::string name, Pointer pointer, Policy = Policy{});

View File

@@ -24,11 +24,30 @@ namespace meta_hpp
template < detail::class_kind Class >
template < typename... Args, ctor_policy_kind Policy >
// NOLINTNEXTLINE(readability-named-parameter)
class_bind<Class>& class_bind<Class>::ctor_(Policy)
class_bind<Class>& class_bind<Class>::ctor_(Policy policy)
requires detail::class_bind_ctor_kind<Class, Args...>
{
return ctor_<Args...>({}, policy);
}
template < detail::class_kind Class >
template < typename... Args, ctor_policy_kind Policy >
class_bind<Class>& class_bind<Class>::ctor_(
std::initializer_list<std::string_view> pnames,
[[maybe_unused]] Policy policy)
requires detail::class_bind_ctor_kind<Class, Args...>
{
auto ctor_state = detail::ctor_state::make<Policy, Class, Args...>();
if ( pnames.size() > ctor_state->parameters.size() ) {
detail::throw_exception_with("provided parameter names don't match constructor argument count");
}
for ( std::size_t i = 0; i < pnames.size(); ++i ) {
parameter& param = ctor_state->parameters[i];
detail::state_access(param)->name = std::string{std::data(pnames)[i]};
}
data_->ctors.emplace(ctor_state->index, std::move(ctor_state));
return *this;
}
@@ -58,17 +77,36 @@ namespace meta_hpp
template < detail::class_kind Class >
template < detail::function_kind Function, function_policy_kind Policy >
// NOLINTNEXTLINE(readability-named-parameter)
class_bind<Class>& class_bind<Class>::function_(std::string name, Function function, Policy) {
class_bind<Class>& class_bind<Class>::function_(std::string name, Function function, Policy policy) {
return function_(std::move(name), std::move(function), {}, policy);
}
template < detail::class_kind Class >
template < detail::function_kind Function, function_policy_kind Policy >
class_bind<Class>& class_bind<Class>::function_(
std::string name,
Function function,
std::initializer_list<std::string_view> pnames,
[[maybe_unused]] Policy policy)
{
auto function_state = detail::function_state::make<Policy>(std::move(name), std::move(function));
if ( pnames.size() > function_state->parameters.size() ) {
detail::throw_exception_with("provided parameter names don't match function argument count");
}
for ( std::size_t i = 0; i < pnames.size(); ++i ) {
parameter& param = function_state->parameters[i];
detail::state_access(param)->name = std::string{std::data(pnames)[i]};
}
data_->functions.emplace(function_state->index, std::move(function_state));
return *this;
}
template < detail::class_kind Class >
template < detail::member_kind Member, member_policy_kind Policy >
// NOLINTNEXTLINE(readability-named-parameter)
class_bind<Class>& class_bind<Class>::member_(std::string name, Member member, Policy)
class_bind<Class>& class_bind<Class>::member_(std::string name, Member member, [[maybe_unused]] Policy policy)
requires detail::class_bind_member_kind<Class, Member>
{
auto member_state = detail::member_state::make<Policy>(std::move(name), std::move(member));
@@ -78,19 +116,39 @@ namespace meta_hpp
template < detail::class_kind Class >
template < detail::method_kind Method, method_policy_kind Policy >
// NOLINTNEXTLINE(readability-named-parameter)
class_bind<Class>& class_bind<Class>::method_(std::string name, Method method, Policy)
class_bind<Class>& class_bind<Class>::method_(std::string name, Method method, Policy policy)
requires detail::class_bind_method_kind<Class, Method>
{
return method_(std::move(name), std::move(method), {}, policy);
}
template < detail::class_kind Class >
template < detail::method_kind Method, method_policy_kind Policy >
class_bind<Class>& class_bind<Class>::method_(
std::string name,
Method method,
std::initializer_list<std::string_view> pnames,
[[maybe_unused]] Policy policy)
requires detail::class_bind_method_kind<Class, Method>
{
auto method_state = detail::method_state::make<Policy>(std::move(name), std::move(method));
if ( pnames.size() > method_state->parameters.size() ) {
detail::throw_exception_with("provided parameter names don't match method argument count");
}
for ( std::size_t i = 0; i < pnames.size(); ++i ) {
parameter& param = method_state->parameters[i];
detail::state_access(param)->name = std::string{std::data(pnames)[i]};
}
data_->methods.emplace(method_state->index, std::move(method_state));
return *this;
}
template < detail::class_kind Class >
template < detail::pointer_kind Pointer, variable_policy_kind Policy >
// NOLINTNEXTLINE(readability-named-parameter)
class_bind<Class>& class_bind<Class>::variable_(std::string name, Pointer pointer, Policy) {
class_bind<Class>& class_bind<Class>::variable_( std::string name, Pointer pointer, [[maybe_unused]] Policy policy) {
auto variable_state = detail::variable_state::make<Policy>(std::move(name), std::move(pointer));
data_->variables.emplace(variable_state->index, std::move(variable_state));
return *this;

View File

@@ -39,16 +39,34 @@ namespace meta_hpp
}
template < detail::function_kind Function, function_policy_kind Policy >
// NOLINTNEXTLINE(readability-named-parameter)
scope_bind& scope_bind::function_(std::string name, Function function, Policy) {
scope_bind& scope_bind::function_(std::string name, Function function, Policy policy) {
return function_(std::move(name), std::move(function), {}, policy);
}
template < detail::function_kind Function, function_policy_kind Policy >
scope_bind& scope_bind::function_(
std::string name,
Function function,
std::initializer_list<std::string_view> pnames,
[[maybe_unused]] Policy policy)
{
auto function_state = detail::function_state::make<Policy>(std::move(name), std::move(function));
if ( pnames.size() > function_state->parameters.size() ) {
detail::throw_exception_with("provided parameter names don't match function argument count");
}
for ( std::size_t i = 0; i < pnames.size(); ++i ) {
parameter& param = function_state->parameters[i];
detail::state_access(param)->name = std::string{std::data(pnames)[i]};
}
state_->functions.emplace(function_state->index, std::move(function_state));
return *this;
}
template < detail::pointer_kind Pointer, variable_policy_kind Policy >
// NOLINTNEXTLINE(readability-named-parameter)
scope_bind& scope_bind::variable_(std::string name, Pointer pointer, Policy) {
scope_bind& scope_bind::variable_(std::string name, Pointer pointer, [[maybe_unused]] Policy policy) {
auto variable_state = detail::variable_state::make<Policy>(std::move(name), std::move(pointer));
state_->variables.emplace(variable_state->index, std::move(variable_state));
return *this;

View File

@@ -120,6 +120,9 @@ namespace meta_hpp
template < typename... Args >
[[nodiscard]] bool is_invocable_with(Args&&... args) const noexcept;
[[nodiscard]] parameter get_parameter(std::size_t position) const noexcept;
[[nodiscard]] const parameter_list& get_parameters() const noexcept;
private:
detail::ctor_state_ptr state_;
friend auto detail::state_access<ctor>(const ctor&);
@@ -194,6 +197,9 @@ namespace meta_hpp
template < typename... Args >
[[nodiscard]] bool is_invocable_with(Args&&... args) const noexcept;
[[nodiscard]] parameter get_parameter(std::size_t position) const noexcept;
[[nodiscard]] const parameter_list& get_parameters() const noexcept;
private:
detail::function_state_ptr state_;
friend auto detail::state_access<function>(const function&);
@@ -262,6 +268,9 @@ namespace meta_hpp
template < typename Instance, typename... Args >
[[nodiscard]] bool is_invocable_with(Instance&& instance, Args&&... args) const noexcept;
[[nodiscard]] parameter get_parameter(std::size_t position) const noexcept;
[[nodiscard]] const parameter_list& get_parameters() const noexcept;
private:
detail::method_state_ptr state_;
friend auto detail::state_access<method>(const method&);
@@ -277,6 +286,7 @@ namespace meta_hpp
[[nodiscard]] const parameter_index& get_index() const noexcept;
[[nodiscard]] const any_type& get_type() const noexcept;
[[nodiscard]] std::size_t get_position() const noexcept;
[[nodiscard]] const std::string& get_name() const noexcept;
private:
detail::parameter_state_ptr state_;
@@ -392,6 +402,8 @@ namespace meta_hpp::detail
invoke_impl invoke;
is_invocable_with_impl is_invocable_with;
parameter_list parameters;
template < ctor_policy_kind Policy, class_kind Class, typename... Args >
[[nodiscard]] static ctor_state_ptr make();
};
@@ -425,6 +437,8 @@ namespace meta_hpp::detail
invoke_impl invoke;
is_invocable_with_impl is_invocable_with;
parameter_list parameters;
template < function_policy_kind Policy, function_kind Function >
[[nodiscard]] static function_state_ptr make(std::string name, Function function);
};
@@ -454,6 +468,8 @@ namespace meta_hpp::detail
invoke_impl invoke;
is_invocable_with_impl is_invocable_with;
parameter_list parameters;
template < method_policy_kind Policy, method_kind Method >
[[nodiscard]] static method_state_ptr make(std::string name, Method method);
};

View File

@@ -88,6 +88,24 @@ namespace meta_hpp::detail
ctor_state::is_invocable_with_impl make_ctor_is_invocable_with() {
return &raw_ctor_is_invocable_with<Class, Args...>;
}
template < class_kind Class, typename... Args >
parameter_list make_ctor_parameters() {
using ct = detail::ctor_traits<Class, Args...>;
parameter_list parameters;
parameters.reserve(ct::arity);
// NOLINTNEXTLINE(readability-named-parameter)
[&parameters]<std::size_t... Is>(std::index_sequence<Is...>) mutable {
(parameters.push_back([]<std::size_t I>(){
using P = detail::type_list_at_t<I, typename ct::argument_types>;
return parameter{detail::parameter_state::make<P>(I)};
}.template operator()<Is>()), ...);
}(std::make_index_sequence<ct::arity>());
return parameters;
}
}
namespace meta_hpp::detail
@@ -98,6 +116,7 @@ namespace meta_hpp::detail
.index{ctor_index::make<Class, Args...>()},
.invoke{make_ctor_invoke<Policy, Class, Args...>()},
.is_invocable_with{make_ctor_is_invocable_with<Class, Args...>()},
.parameters{make_ctor_parameters<Class, Args...>()},
});
}
}
@@ -160,4 +179,12 @@ namespace meta_hpp
return state_->is_invocable_with({});
}
}
inline parameter ctor::get_parameter(std::size_t position) const noexcept {
return position < state_->parameters.size() ? state_->parameters[position] : parameter{};
}
inline const parameter_list& ctor::get_parameters() const noexcept {
return state_->parameters;
}
}

View File

@@ -94,6 +94,24 @@ namespace meta_hpp::detail
function_state::is_invocable_with_impl make_function_is_invocable_with() {
return &raw_function_is_invocable_with<Function>;
}
template < function_kind Function >
parameter_list make_function_parameters() {
using ft = detail::function_traits<Function>;
parameter_list parameters;
parameters.reserve(ft::arity);
// NOLINTNEXTLINE(readability-named-parameter)
[&parameters]<std::size_t... Is>(std::index_sequence<Is...>) mutable {
(parameters.push_back([]<std::size_t I>(){
using P = detail::type_list_at_t<I, typename ft::argument_types>;
return parameter{detail::parameter_state::make<P>(I)};
}.template operator()<Is>()), ...);
}(std::make_index_sequence<ft::arity>());
return parameters;
}
}
namespace meta_hpp::detail
@@ -104,6 +122,7 @@ namespace meta_hpp::detail
.index{function_index::make<Function>(std::move(name))},
.invoke{make_function_invoke<Policy>(std::move(function))},
.is_invocable_with{make_function_is_invocable_with<Function>()},
.parameters{make_function_parameters<Function>()},
});
}
}
@@ -170,4 +189,12 @@ namespace meta_hpp
return state_->is_invocable_with({});
}
}
inline parameter function::get_parameter(std::size_t position) const noexcept {
return position < state_->parameters.size() ? state_->parameters[position] : parameter{};
}
inline const parameter_list& function::get_parameters() const noexcept {
return state_->parameters;
}
}

View File

@@ -107,6 +107,24 @@ namespace meta_hpp::detail
method_state::is_invocable_with_impl make_method_is_invocable_with() {
return &raw_method_is_invocable_with<Method>;
}
template < method_kind Method >
parameter_list make_method_parameters() {
using mt = detail::method_traits<Method>;
parameter_list parameters;
parameters.reserve(mt::arity);
// NOLINTNEXTLINE(readability-named-parameter)
[&parameters]<std::size_t... Is>(std::index_sequence<Is...>) mutable {
(parameters.push_back([]<std::size_t I>(){
using P = detail::type_list_at_t<I, typename mt::argument_types>;
return parameter{detail::parameter_state::make<P>(I)};
}.template operator()<Is>()), ...);
}(std::make_index_sequence<mt::arity>());
return parameters;
}
}
namespace meta_hpp::detail
@@ -117,6 +135,7 @@ namespace meta_hpp::detail
.index{method_index::make<Method>(std::move(name))},
.invoke{make_method_invoke<Policy>(std::move(method))},
.is_invocable_with{make_method_is_invocable_with<Method>()},
.parameters{make_method_parameters<Method>()},
});
}
}
@@ -186,4 +205,12 @@ namespace meta_hpp
return state_->is_invocable_with(vinst, {});
}
}
inline parameter method::get_parameter(std::size_t position) const noexcept {
return position < state_->parameters.size() ? state_->parameters[position] : parameter{};
}
inline const parameter_list& method::get_parameters() const noexcept {
return state_->parameters;
}
}

View File

@@ -148,7 +148,7 @@ namespace meta_hpp
[[nodiscard]] std::size_t get_size() const noexcept;
[[nodiscard]] std::size_t get_arity() const noexcept;
[[nodiscard]] any_type get_argument_type(std::size_t index) const noexcept;
[[nodiscard]] any_type get_argument_type(std::size_t position) const noexcept;
[[nodiscard]] const std::vector<any_type>& get_argument_types() const noexcept;
[[nodiscard]] const ctor_map& get_ctors() const noexcept;
@@ -219,7 +219,7 @@ namespace meta_hpp
[[nodiscard]] std::size_t get_arity() const noexcept;
[[nodiscard]] any_type get_class_type() const noexcept;
[[nodiscard]] any_type get_argument_type(std::size_t index) const noexcept;
[[nodiscard]] any_type get_argument_type(std::size_t position) const noexcept;
[[nodiscard]] const std::vector<any_type>& get_argument_types() const noexcept;
private:
detail::ctor_type_data_ptr data_;
@@ -281,7 +281,7 @@ namespace meta_hpp
[[nodiscard]] std::size_t get_arity() const noexcept;
[[nodiscard]] any_type get_return_type() const noexcept;
[[nodiscard]] any_type get_argument_type(std::size_t index) const noexcept;
[[nodiscard]] any_type get_argument_type(std::size_t position) const noexcept;
[[nodiscard]] const std::vector<any_type>& get_argument_types() const noexcept;
private:
detail::function_type_data_ptr data_;
@@ -320,7 +320,7 @@ namespace meta_hpp
[[nodiscard]] std::size_t get_arity() const noexcept;
[[nodiscard]] class_type get_owner_type() const noexcept;
[[nodiscard]] any_type get_return_type() const noexcept;
[[nodiscard]] any_type get_argument_type(std::size_t index) const noexcept;
[[nodiscard]] any_type get_argument_type(std::size_t position) const noexcept;
[[nodiscard]] const std::vector<any_type>& get_argument_types() const noexcept;
private:
detail::method_type_data_ptr data_;

View File

@@ -61,8 +61,8 @@ namespace meta_hpp
return data_->argument_types.size();
}
inline any_type class_type::get_argument_type(std::size_t index) const noexcept {
return index < data_->argument_types.size() ? data_->argument_types[index] : any_type{};
inline any_type class_type::get_argument_type(std::size_t position) const noexcept {
return position < data_->argument_types.size() ? data_->argument_types[position] : any_type{};
}
inline const std::vector<any_type>& class_type::get_argument_types() const noexcept {

View File

@@ -55,8 +55,8 @@ namespace meta_hpp
return data_->class_type;
}
inline any_type ctor_type::get_argument_type(std::size_t index) const noexcept {
return index < data_->argument_types.size() ? data_->argument_types[index] : any_type{};
inline any_type ctor_type::get_argument_type(std::size_t position) const noexcept {
return position < data_->argument_types.size() ? data_->argument_types[position] : any_type{};
}
inline const std::vector<any_type>& ctor_type::get_argument_types() const noexcept {

View File

@@ -55,8 +55,8 @@ namespace meta_hpp
return data_->return_type;
}
inline any_type function_type::get_argument_type(std::size_t index) const noexcept {
return index < data_->argument_types.size() ? data_->argument_types[index] : any_type{};
inline any_type function_type::get_argument_type(std::size_t position) const noexcept {
return position < data_->argument_types.size() ? data_->argument_types[position] : any_type{};
}
inline const std::vector<any_type>& function_type::get_argument_types() const noexcept {

View File

@@ -60,8 +60,8 @@ namespace meta_hpp
return data_->return_type;
}
inline any_type method_type::get_argument_type(std::size_t index) const noexcept {
return index < data_->argument_types.size() ? data_->argument_types[index] : any_type{};
inline any_type method_type::get_argument_type(std::size_t position) const noexcept {
return position < data_->argument_types.size() ? data_->argument_types[position] : any_type{};
}
inline const std::vector<any_type>& method_type::get_argument_types() const noexcept {