From 952bd4b214882f9acc75c7d23c7f26401eb35495 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Tue, 3 Aug 2021 12:01:07 +0700 Subject: [PATCH] get internals from info --- headers/meta.hpp/meta_infos/class_info.hpp | 31 ++++++++ headers/meta.hpp/meta_infos/ctor_info.hpp | 6 ++ headers/meta.hpp/meta_infos/data_info.hpp | 6 ++ headers/meta.hpp/meta_infos/enum_info.hpp | 11 +++ headers/meta.hpp/meta_infos/evalue_info.hpp | 6 ++ headers/meta.hpp/meta_infos/function_info.hpp | 6 ++ headers/meta.hpp/meta_infos/member_info.hpp | 6 ++ headers/meta.hpp/meta_infos/method_info.hpp | 6 ++ .../meta.hpp/meta_infos/namespace_info.hpp | 26 +++++++ headers/meta.hpp/meta_registry.hpp | 48 ++++++------- headers/meta.hpp/meta_utilities/value.hpp | 23 ++++++ untests/features/infos/function_tests.cpp | 12 ++-- untests/features/registry/_registry_fwd.hpp | 9 +++ untests/features/registry/registry_tests.cpp | 70 +++++++++++++++++++ 14 files changed, 236 insertions(+), 30 deletions(-) create mode 100644 untests/features/registry/_registry_fwd.hpp create mode 100644 untests/features/registry/registry_tests.cpp diff --git a/headers/meta.hpp/meta_infos/class_info.hpp b/headers/meta.hpp/meta_infos/class_info.hpp index 2095cf7..6ffc3a2 100644 --- a/headers/meta.hpp/meta_infos/class_info.hpp +++ b/headers/meta.hpp/meta_infos/class_info.hpp @@ -50,6 +50,13 @@ namespace meta_hpp template < typename F > void each_method(F&& f) const; + + class_info get_class_by_name(std::string_view name) const noexcept; + data_info get_data_by_name(std::string_view name) const noexcept; + enum_info get_enum_by_name(std::string_view name) const noexcept; + function_info get_function_by_name(std::string_view name) const noexcept; + member_info get_member_by_name(std::string_view name) const noexcept; + method_info get_method_by_name(std::string_view name) const noexcept; private: template < typename Class > friend class class_; @@ -157,6 +164,30 @@ namespace meta_hpp std::invoke(f, name_info.second); } } + + inline class_info class_info::get_class_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->classes, name); + } + + inline data_info class_info::get_data_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->datas, name); + } + + inline enum_info class_info::get_enum_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->enums, name); + } + + inline function_info class_info::get_function_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->functions, name); + } + + inline member_info class_info::get_member_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->members, name); + } + + inline method_info class_info::get_method_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->methods, name); + } } namespace meta_hpp diff --git a/headers/meta.hpp/meta_infos/ctor_info.hpp b/headers/meta.hpp/meta_infos/ctor_info.hpp index 464d28c..970840c 100644 --- a/headers/meta.hpp/meta_infos/ctor_info.hpp +++ b/headers/meta.hpp/meta_infos/ctor_info.hpp @@ -26,6 +26,8 @@ namespace meta_hpp template < typename F > void each_data(F&& f) const; + + data_info get_data_by_name(std::string_view name) const noexcept; private: template < typename... Args > friend class ctor_; @@ -74,6 +76,10 @@ namespace meta_hpp std::invoke(f, name_info.second); } } + + inline data_info ctor_info::get_data_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->datas, name); + } } namespace meta_hpp diff --git a/headers/meta.hpp/meta_infos/data_info.hpp b/headers/meta.hpp/meta_infos/data_info.hpp index 560d31c..e6554c8 100644 --- a/headers/meta.hpp/meta_infos/data_info.hpp +++ b/headers/meta.hpp/meta_infos/data_info.hpp @@ -25,6 +25,8 @@ namespace meta_hpp template < typename F > void each_data(F&& f) const; + + data_info get_data_by_name(std::string_view name) const noexcept; private: friend class data_; @@ -77,6 +79,10 @@ namespace meta_hpp std::invoke(f, name_info.second); } } + + inline data_info data_info::get_data_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->datas, name); + } } namespace meta_hpp diff --git a/headers/meta.hpp/meta_infos/enum_info.hpp b/headers/meta.hpp/meta_infos/enum_info.hpp index 9b13e68..450f4ee 100644 --- a/headers/meta.hpp/meta_infos/enum_info.hpp +++ b/headers/meta.hpp/meta_infos/enum_info.hpp @@ -31,6 +31,9 @@ namespace meta_hpp template < typename F > void each_evalue(F&& f) const; + + data_info get_data_by_name(std::string_view name) const noexcept; + evalue_info get_evalue_by_name(std::string_view name) const noexcept; private: template < typename Enum > friend class enum_; @@ -93,6 +96,14 @@ namespace meta_hpp std::invoke(f, name_info.second); } } + + inline data_info enum_info::get_data_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->datas, name); + } + + inline evalue_info enum_info::get_evalue_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->evalues, name); + } } namespace meta_hpp diff --git a/headers/meta.hpp/meta_infos/evalue_info.hpp b/headers/meta.hpp/meta_infos/evalue_info.hpp index a1c988b..7bb90a8 100644 --- a/headers/meta.hpp/meta_infos/evalue_info.hpp +++ b/headers/meta.hpp/meta_infos/evalue_info.hpp @@ -27,6 +27,8 @@ namespace meta_hpp template < typename F > void each_data(F&& f) const; + + data_info get_data_by_name(std::string_view name) const noexcept; private: template < typename Enum > friend class evalue_; @@ -80,6 +82,10 @@ namespace meta_hpp std::invoke(f, name_info.second); } } + + inline data_info evalue_info::get_data_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->datas, name); + } } namespace meta_hpp diff --git a/headers/meta.hpp/meta_infos/function_info.hpp b/headers/meta.hpp/meta_infos/function_info.hpp index d4efcb7..ebfb2c5 100644 --- a/headers/meta.hpp/meta_infos/function_info.hpp +++ b/headers/meta.hpp/meta_infos/function_info.hpp @@ -27,6 +27,8 @@ namespace meta_hpp template < typename F > void each_data(F&& f) const; + + data_info get_data_by_name(std::string_view name) const noexcept; private: template < typename Function > friend class function_; @@ -80,6 +82,10 @@ namespace meta_hpp std::invoke(f, name_info.second); } } + + inline data_info function_info::get_data_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->datas, name); + } } namespace meta_hpp diff --git a/headers/meta.hpp/meta_infos/member_info.hpp b/headers/meta.hpp/meta_infos/member_info.hpp index ab5b147..f23841e 100644 --- a/headers/meta.hpp/meta_infos/member_info.hpp +++ b/headers/meta.hpp/meta_infos/member_info.hpp @@ -27,6 +27,8 @@ namespace meta_hpp template < typename F > void each_data(F&& f) const; + + data_info get_data_by_name(std::string_view name) const noexcept; private: template < typename Member > friend class member_; @@ -80,6 +82,10 @@ namespace meta_hpp std::invoke(f, name_info.second); } } + + inline data_info member_info::get_data_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->datas, name); + } } namespace meta_hpp diff --git a/headers/meta.hpp/meta_infos/method_info.hpp b/headers/meta.hpp/meta_infos/method_info.hpp index 0d99b9d..50d14b5 100644 --- a/headers/meta.hpp/meta_infos/method_info.hpp +++ b/headers/meta.hpp/meta_infos/method_info.hpp @@ -27,6 +27,8 @@ namespace meta_hpp template < typename F > void each_data(F&& f) const; + + data_info get_data_by_name(std::string_view name) const noexcept; private: template < typename Method > friend class method_; @@ -80,6 +82,10 @@ namespace meta_hpp std::invoke(f, name_info.second); } } + + inline data_info method_info::get_data_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->datas, name); + } } namespace meta_hpp diff --git a/headers/meta.hpp/meta_infos/namespace_info.hpp b/headers/meta.hpp/meta_infos/namespace_info.hpp index 6f7638f..0cb2ef5 100644 --- a/headers/meta.hpp/meta_infos/namespace_info.hpp +++ b/headers/meta.hpp/meta_infos/namespace_info.hpp @@ -41,6 +41,12 @@ namespace meta_hpp template < typename F > void each_namespace(F&& f) const; + + class_info get_class_by_name(std::string_view name) const noexcept; + data_info get_data_by_name(std::string_view name) const noexcept; + enum_info get_enum_by_name(std::string_view name) const noexcept; + function_info get_function_by_name(std::string_view name) const noexcept; + namespace_info get_namespace_by_name(std::string_view name) const noexcept; private: friend class namespace_; @@ -124,6 +130,26 @@ namespace meta_hpp std::invoke(f, name_info.second); } } + + inline class_info namespace_info::get_class_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->classes, name); + } + + inline data_info namespace_info::get_data_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->datas, name); + } + + inline enum_info namespace_info::get_enum_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->enums, name); + } + + inline function_info namespace_info::get_function_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->functions, name); + } + + inline namespace_info namespace_info::get_namespace_by_name(std::string_view name) const noexcept { + return detail::find_or_default(state_->namespaces, name); + } } namespace meta_hpp diff --git a/headers/meta.hpp/meta_registry.hpp b/headers/meta.hpp/meta_registry.hpp index 4d32ec6..dafe434 100644 --- a/headers/meta.hpp/meta_registry.hpp +++ b/headers/meta.hpp/meta_registry.hpp @@ -24,14 +24,14 @@ namespace meta_hpp public: registry() = default; - class_info get_class_by_name(std::string_view name) const noexcept; - data_info get_data_by_name(std::string_view name) const noexcept; - enum_info get_enum_by_name(std::string_view name) const noexcept; - evalue_info get_evalue_by_name(std::string_view name) const noexcept; - function_info get_function_by_name(std::string_view name) const noexcept; - member_info get_member_by_name(std::string_view name) const noexcept; - method_info get_method_by_name(std::string_view name) const noexcept; - namespace_info get_namespace_by_name(std::string_view name) const noexcept; + class_info get_class_by_path(std::string_view path) const noexcept; + data_info get_data_by_path(std::string_view path) const noexcept; + enum_info get_enum_by_path(std::string_view path) const noexcept; + evalue_info get_evalue_by_path(std::string_view path) const noexcept; + function_info get_function_by_path(std::string_view path) const noexcept; + member_info get_member_by_path(std::string_view path) const noexcept; + method_info get_method_by_path(std::string_view path) const noexcept; + namespace_info get_namespace_by_path(std::string_view path) const noexcept; public: template < typename... Internals > registry& operator()(Internals&&... internals); @@ -75,36 +75,36 @@ namespace meta_hpp namespace meta_hpp { - inline class_info registry::get_class_by_name(std::string_view name) const noexcept { - return detail::find_or_default(classes_, name); + inline class_info registry::get_class_by_path(std::string_view path) const noexcept { + return detail::find_or_default(classes_, path); } - inline data_info registry::get_data_by_name(std::string_view name) const noexcept { - return detail::find_or_default(datas_, name); + inline data_info registry::get_data_by_path(std::string_view path) const noexcept { + return detail::find_or_default(datas_, path); } - inline enum_info registry::get_enum_by_name(std::string_view name) const noexcept { - return detail::find_or_default(enums_, name); + inline enum_info registry::get_enum_by_path(std::string_view path) const noexcept { + return detail::find_or_default(enums_, path); } - inline evalue_info registry::get_evalue_by_name(std::string_view name) const noexcept { - return detail::find_or_default(evalues_, name); + inline evalue_info registry::get_evalue_by_path(std::string_view path) const noexcept { + return detail::find_or_default(evalues_, path); } - inline function_info registry::get_function_by_name(std::string_view name) const noexcept { - return detail::find_or_default(functions_, name); + inline function_info registry::get_function_by_path(std::string_view path) const noexcept { + return detail::find_or_default(functions_, path); } - inline member_info registry::get_member_by_name(std::string_view name) const noexcept { - return detail::find_or_default(members_, name); + inline member_info registry::get_member_by_path(std::string_view path) const noexcept { + return detail::find_or_default(members_, path); } - inline method_info registry::get_method_by_name(std::string_view name) const noexcept { - return detail::find_or_default(methods_, name); + inline method_info registry::get_method_by_path(std::string_view path) const noexcept { + return detail::find_or_default(methods_, path); } - inline namespace_info registry::get_namespace_by_name(std::string_view name) const noexcept { - return detail::find_or_default(namespaces_, name); + inline namespace_info registry::get_namespace_by_path(std::string_view path) const noexcept { + return detail::find_or_default(namespaces_, path); } } diff --git a/headers/meta.hpp/meta_utilities/value.hpp b/headers/meta.hpp/meta_utilities/value.hpp index 461fa3f..1926e55 100644 --- a/headers/meta.hpp/meta_utilities/value.hpp +++ b/headers/meta.hpp/meta_utilities/value.hpp @@ -19,5 +19,28 @@ namespace meta_hpp value(const value&) = default; value& operator=(const value&) = default; + + template < typename T, typename Tp = std::decay_t + , std::enable_if_t, int> = 0 > + value(T&& val) + : raw_{std::forward(val)} {} + + template < typename T, typename Tp = std::decay_t + , std::enable_if_t, int> = 0 > + value& operator=(T&& val) { + value{std::forward(val)}.swap(*this); + return *this; + } + + void swap(value& other) noexcept { + using std::swap; + swap(raw_, other.raw_); + } + private: + std::any raw_{}; }; + + inline void swap(value& l, value& r) noexcept { + l.swap(r); + } } diff --git a/untests/features/infos/function_tests.cpp b/untests/features/infos/function_tests.cpp index c7af985..6b67cb2 100644 --- a/untests/features/infos/function_tests.cpp +++ b/untests/features/infos/function_tests.cpp @@ -37,12 +37,12 @@ TEST_CASE("features/infos/function") { function_{"arg_crref", &arg_crref} ); - const function_info arg_copy_info = db.get_function_by_name("arg_copy"); - const function_info arg_ref_info = db.get_function_by_name("arg_ref"); - const function_info arg_cref_info = db.get_function_by_name("arg_cref"); - const function_info arg_rref_info = db.get_function_by_name("arg_rref"); - const function_info arg_crref_info = db.get_function_by_name("arg_crref"); - const function_info arg_not_exist_info = db.get_function_by_name("arg_not_exist"); + const function_info arg_copy_info = db.get_function_by_path("arg_copy"); + const function_info arg_ref_info = db.get_function_by_path("arg_ref"); + const function_info arg_cref_info = db.get_function_by_path("arg_cref"); + const function_info arg_rref_info = db.get_function_by_path("arg_rref"); + const function_info arg_crref_info = db.get_function_by_path("arg_crref"); + const function_info arg_not_exist_info = db.get_function_by_path("arg_not_exist"); REQUIRE(arg_copy_info); REQUIRE(arg_ref_info); diff --git a/untests/features/registry/_registry_fwd.hpp b/untests/features/registry/_registry_fwd.hpp new file mode 100644 index 0000000..b826cce --- /dev/null +++ b/untests/features/registry/_registry_fwd.hpp @@ -0,0 +1,9 @@ +/******************************************************************************* + * This file is part of the "https://github.com/blackmatov/meta.hpp" + * For conditions of distribution and use, see copyright notice in LICENSE.md + * Copyright (C) 2021, by Matvey Cherevko (blackmatov@gmail.com) + ******************************************************************************/ + +#pragma once + +#include "../../meta_tests.hpp" diff --git a/untests/features/registry/registry_tests.cpp b/untests/features/registry/registry_tests.cpp new file mode 100644 index 0000000..c72b2e1 --- /dev/null +++ b/untests/features/registry/registry_tests.cpp @@ -0,0 +1,70 @@ +/******************************************************************************* + * This file is part of the "https://github.com/blackmatov/meta.hpp" + * For conditions of distribution and use, see copyright notice in LICENSE.md + * Copyright (C) 2021, by Matvey Cherevko (blackmatov@gmail.com) + ******************************************************************************/ + +#include "_registry_fwd.hpp" + +namespace +{ + using namespace meta_hpp; + using namespace std::string_literals; +} + +namespace +{ + struct ivec2 { + int x{}; + int y{}; + + [[maybe_unused]] ivec2() = default; + [[maybe_unused]] explicit ivec2(int v): x{v}, y{v} {} + [[maybe_unused]] ivec2(int x, int y): x{x}, y{y} {} + }; +} + +TEST_CASE("features/registry/registry") { + auto db = registry{}( + namespace_{"vmath"}(data_{"info", "vmath namespace"}, + class_{"ivec2"}(data_{"info", "ivec2 class"}, + ctor_<>{}, + ctor_{}, + ctor_{}, + member_{"x", &ivec2::x}(data_{"info", "x-coord"}), + member_{"y", &ivec2::y}(data_{"info", "y-coord"}) + ) + ) + ); + + { + const namespace_info vmath_info = db.get_namespace_by_path("vmath"); + REQUIRE(vmath_info); + CHECK(vmath_info.name() == "vmath"); + CHECK(vmath_info.get_data_by_name("info")); + + const class_info ivec2_info = vmath_info.get_class_by_name("ivec2"); + REQUIRE(ivec2_info); + CHECK(ivec2_info.name() == "ivec2"); + CHECK(ivec2_info.get_data_by_name("info")); + + const member_info ivec2_x_info = ivec2_info.get_member_by_name("x"); + REQUIRE(ivec2_x_info); + CHECK(ivec2_x_info.name() == "x"); + CHECK(ivec2_x_info.get_data_by_name("info")); + } + + { + const class_info ivec2_info = db.get_class_by_path("vmath::ivec2"); + REQUIRE(ivec2_info); + CHECK(ivec2_info.name() == "ivec2"); + + const member_info ivec2_x_info = db.get_member_by_path("vmath::ivec2::x"); + REQUIRE(ivec2_x_info); + CHECK(ivec2_x_info.name() == "x"); + + const data_info ivec2_x_data_info = db.get_data_by_path("vmath::ivec2::x::info"); + REQUIRE(ivec2_x_data_info); + CHECK(ivec2_x_data_info.name() == "info"); + } +}