From 5a20a1c7b225e60ad1a627d6cbe9555ea9520c85 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Tue, 8 Feb 2022 06:21:29 +0700 Subject: [PATCH] little copy/paste refactoring --- TODO.md | 1 + headers/meta.hpp/meta_states.hpp | 2 + headers/meta.hpp/meta_states/scope.hpp | 31 ++---- headers/meta.hpp/meta_types.hpp | 6 ++ headers/meta.hpp/meta_types/class_type.hpp | 109 ++++++--------------- 5 files changed, 47 insertions(+), 102 deletions(-) diff --git a/TODO.md b/TODO.md index a98a338..75a811d 100644 --- a/TODO.md +++ b/TODO.md @@ -3,6 +3,7 @@ - metadata - add policy tests - rebind types support +- rewrite ctor/dtor system * argument defaults * argument names diff --git a/headers/meta.hpp/meta_states.hpp b/headers/meta.hpp/meta_states.hpp index 589bafe..1fb5005 100644 --- a/headers/meta.hpp/meta_states.hpp +++ b/headers/meta.hpp/meta_states.hpp @@ -306,6 +306,8 @@ namespace meta_hpp template < typename... Args > [[nodiscard]] function get_function_with(std::string_view name) const noexcept; + template < typename Iter > + [[nodiscard]] function get_function_with(std::string_view name, Iter first, Iter last) const noexcept; [[nodiscard]] function get_function_with(std::string_view name, const std::vector& args) const noexcept; [[nodiscard]] function get_function_with(std::string_view name, std::initializer_list args) const noexcept; private: diff --git a/headers/meta.hpp/meta_states/scope.hpp b/headers/meta.hpp/meta_states/scope.hpp index 81b56d9..f4c3334 100644 --- a/headers/meta.hpp/meta_states/scope.hpp +++ b/headers/meta.hpp/meta_states/scope.hpp @@ -101,39 +101,26 @@ namespace meta_hpp return get_function_with(name, {detail::resolve_type()...}); } - inline function scope::get_function_with(std::string_view name, const std::vector& args) const noexcept { + template < typename Iter > + function scope::get_function_with(std::string_view name, Iter first, Iter last) const noexcept { for ( auto&& [index, function] : state_->functions ) { if ( index.get_name() != name ) { continue; } - if ( function.get_type().get_arity() != args.size() ) { - continue; - } - - const std::vector& function_args = function.get_type().get_argument_types(); - if ( std::equal(args.begin(), args.end(), function_args.begin(), function_args.end()) ) { + const std::vector& args = function.get_type().get_argument_types(); + if ( std::equal(first, last, args.begin(), args.end()) ) { return function; } } return function{}; } + inline function scope::get_function_with(std::string_view name, const std::vector& args) const noexcept { + return get_function_with(name, args.begin(), args.end()); + } + inline function scope::get_function_with(std::string_view name, std::initializer_list args) const noexcept { - for ( auto&& [index, function] : state_->functions ) { - if ( index.get_name() != name ) { - continue; - } - - if ( function.get_type().get_arity() != args.size() ) { - continue; - } - - const std::vector& function_args = function.get_type().get_argument_types(); - if ( std::equal(args.begin(), args.end(), function_args.begin(), function_args.end()) ) { - return function; - } - } - return function{}; + return get_function_with(name, args.begin(), args.end()); } } diff --git a/headers/meta.hpp/meta_types.hpp b/headers/meta.hpp/meta_types.hpp index 675bfaf..c9105dd 100644 --- a/headers/meta.hpp/meta_types.hpp +++ b/headers/meta.hpp/meta_types.hpp @@ -183,16 +183,22 @@ namespace meta_hpp template < typename... Args > [[nodiscard]] ctor get_ctor_with() const noexcept; + template < typename Iter > + [[nodiscard]] ctor get_ctor_with(Iter first, Iter last) const noexcept; [[nodiscard]] ctor get_ctor_with(const std::vector& args) const noexcept; [[nodiscard]] ctor get_ctor_with(std::initializer_list args) const noexcept; template < typename... Args > [[nodiscard]] function get_function_with(std::string_view name) const noexcept; + template < typename Iter > + [[nodiscard]] function get_function_with(std::string_view name, Iter first, Iter last) const noexcept; [[nodiscard]] function get_function_with(std::string_view name, const std::vector& args) const noexcept; [[nodiscard]] function get_function_with(std::string_view name, std::initializer_list args) const noexcept; template < typename... Args > [[nodiscard]] method get_method_with(std::string_view name) const noexcept; + template < typename Iter > + [[nodiscard]] method get_method_with(std::string_view name, Iter first, Iter last) const noexcept; [[nodiscard]] method get_method_with(std::string_view name, const std::vector& args) const noexcept; [[nodiscard]] method get_method_with(std::string_view name, std::initializer_list args) const noexcept; private: diff --git a/headers/meta.hpp/meta_types/class_type.hpp b/headers/meta.hpp/meta_types/class_type.hpp index d9c5740..542d71c 100644 --- a/headers/meta.hpp/meta_types/class_type.hpp +++ b/headers/meta.hpp/meta_types/class_type.hpp @@ -242,34 +242,23 @@ namespace meta_hpp return get_ctor_with({detail::resolve_type()...}); } - inline ctor class_type::get_ctor_with(const std::vector& args) const noexcept { + template < typename Iter > + ctor class_type::get_ctor_with(Iter first, Iter last) const noexcept { for ( auto&& [index, ctor] : data_->ctors ) { - if ( ctor.get_type().get_arity() != args.size() ) { - continue; - } - - const std::vector& ctor_args = ctor.get_type().get_argument_types(); - if ( std::equal(args.begin(), args.end(), ctor_args.begin(), ctor_args.end()) ) { + const std::vector& args = ctor.get_type().get_argument_types(); + if ( std::equal(first, last, args.begin(), args.end()) ) { return ctor; } } - return ctor{}; } + inline ctor class_type::get_ctor_with(const std::vector& args) const noexcept { + return get_ctor_with(args.begin(), args.end()); + } + inline ctor class_type::get_ctor_with(std::initializer_list args) const noexcept { - for ( auto&& [index, ctor] : data_->ctors ) { - if ( ctor.get_type().get_arity() != args.size() ) { - continue; - } - - const std::vector& ctor_args = ctor.get_type().get_argument_types(); - if ( std::equal(args.begin(), args.end(), ctor_args.begin(), ctor_args.end()) ) { - return ctor; - } - } - - return ctor{}; + return get_ctor_with(args.begin(), args.end()); } // @@ -281,24 +270,21 @@ namespace meta_hpp return get_function_with(name, {detail::resolve_type()...}); } - inline function class_type::get_function_with(std::string_view name, const std::vector& args) const noexcept { + template < typename Iter > + function class_type::get_function_with(std::string_view name, Iter first, Iter last) const noexcept { for ( auto&& [index, function] : data_->functions ) { if ( index.get_name() != name ) { continue; } - if ( function.get_type().get_arity() != args.size() ) { - continue; - } - - const std::vector& function_args = function.get_type().get_argument_types(); - if ( std::equal(args.begin(), args.end(), function_args.begin(), function_args.end()) ) { + const std::vector& args = function.get_type().get_argument_types(); + if ( std::equal(first, last, args.begin(), args.end()) ) { return function; } } for ( auto&& base : data_->bases ) { - if ( function function = base.get_function_with(name, args); function ) { + if ( function function = base.get_function_with(name, first, last); function ) { return function; } } @@ -306,29 +292,12 @@ namespace meta_hpp return function{}; } + inline function class_type::get_function_with(std::string_view name, const std::vector& args) const noexcept { + return get_function_with(name, args.begin(), args.end()); + } + inline function class_type::get_function_with(std::string_view name, std::initializer_list args) const noexcept { - for ( auto&& [index, function] : data_->functions ) { - if ( index.get_name() != name ) { - continue; - } - - if ( function.get_type().get_arity() != args.size() ) { - continue; - } - - const std::vector& function_args = function.get_type().get_argument_types(); - if ( std::equal(args.begin(), args.end(), function_args.begin(), function_args.end()) ) { - return function; - } - } - - for ( auto&& base : data_->bases ) { - if ( function function = base.get_function_with(name, args); function ) { - return function; - } - } - - return function{}; + return get_function_with(name, args.begin(), args.end()); } // @@ -340,24 +309,21 @@ namespace meta_hpp return get_method_with(name, {detail::resolve_type()...}); } - inline method class_type::get_method_with(std::string_view name, const std::vector& args) const noexcept { + template < typename Iter > + method class_type::get_method_with(std::string_view name, Iter first, Iter last) const noexcept { for ( auto&& [index, method] : data_->methods ) { if ( index.get_name() != name ) { continue; } - if ( method.get_type().get_arity() != args.size() ) { - continue; - } - - const std::vector& method_args = method.get_type().get_argument_types(); - if ( std::equal(args.begin(), args.end(), method_args.begin(), method_args.end()) ) { + const std::vector& args = method.get_type().get_argument_types(); + if ( std::equal(first, last, args.begin(), args.end()) ) { return method; } } for ( auto&& base : data_->bases ) { - if ( method method = base.get_method_with(name, args); method ) { + if ( method method = base.get_method_with(name, first, last); method ) { return method; } } @@ -365,28 +331,11 @@ namespace meta_hpp return method{}; } + inline method class_type::get_method_with(std::string_view name, const std::vector& args) const noexcept { + return get_method_with(name, args.begin(), args.end()); + } + inline method class_type::get_method_with(std::string_view name, std::initializer_list args) const noexcept { - for ( auto&& [index, method] : data_->methods ) { - if ( index.get_name() != name ) { - continue; - } - - if ( method.get_type().get_arity() != args.size() ) { - continue; - } - - const std::vector& method_args = method.get_type().get_argument_types(); - if ( std::equal(args.begin(), args.end(), method_args.begin(), method_args.end()) ) { - return method; - } - } - - for ( auto&& base : data_->bases ) { - if ( method method = base.get_method_with(name, args); method ) { - return method; - } - } - - return method{}; + return get_method_with(name, args.begin(), args.end()); } }