typedefs instead class_ and enum_

This commit is contained in:
BlackMATov
2022-02-13 07:10:25 +07:00
parent 84619af864
commit e93f89d4e5
12 changed files with 94 additions and 136 deletions

View File

@@ -205,6 +205,7 @@ namespace meta_hpp
{ {
using argument_list = std::vector<argument>; using argument_list = std::vector<argument>;
using metadata_map = std::map<std::string, uvalue, std::less<>>; using metadata_map = std::map<std::string, uvalue, std::less<>>;
using typedef_map = std::map<std::string, any_type, std::less<>>;
using class_set = std::set<class_type, std::less<>>; using class_set = std::set<class_type, std::less<>>;
using class_map = std::map<std::string, class_type, std::less<>>; using class_map = std::map<std::string, class_type, std::less<>>;

View File

@@ -100,11 +100,6 @@ namespace meta_hpp
explicit class_bind(metadata_map metadata); explicit class_bind(metadata_map metadata);
operator class_type() const noexcept; operator class_type() const noexcept;
// class_
template < detail::class_kind InternalClass >
class_bind& class_(std::string name);
// base_ // base_
template < detail::class_kind Base > template < detail::class_kind Base >
@@ -131,11 +126,6 @@ namespace meta_hpp
class_bind& destructor_(destructor_opts opts) class_bind& destructor_(destructor_opts opts)
requires detail::class_bind_destructor_kind<Class>; requires detail::class_bind_destructor_kind<Class>;
// enum_
template < detail::enum_kind InternalEnum >
class_bind& enum_(std::string name);
// function_ // function_
template < detail::function_kind Function template < detail::function_kind Function
@@ -208,6 +198,11 @@ namespace meta_hpp
Policy = Policy{}) Policy = Policy{})
requires detail::class_bind_method_kind<Class, Method>; requires detail::class_bind_method_kind<Class, Method>;
// typdef_
template < typename Type >
class_bind& typedef_(std::string name);
// variable_ // variable_
template < detail::pointer_kind Pointer template < detail::pointer_kind Pointer
@@ -361,12 +356,6 @@ namespace meta_hpp
explicit scope_bind(std::string_view name, metadata_map metadata, static_tag); explicit scope_bind(std::string_view name, metadata_map metadata, static_tag);
operator scope() const noexcept; operator scope() const noexcept;
template < detail::class_kind Class >
scope_bind& class_(std::string name);
template < detail::enum_kind Enum >
scope_bind& enum_(std::string name);
// function_ // function_
template < detail::function_kind Function template < detail::function_kind Function
@@ -392,6 +381,11 @@ namespace meta_hpp
std::initializer_list<std::string_view> arguments, std::initializer_list<std::string_view> arguments,
Policy = Policy{}); Policy = Policy{});
// typedef_
template < typename Type >
scope_bind& typedef_(std::string name);
// variable_ // variable_
template < detail::pointer_kind Pointer template < detail::pointer_kind Pointer

View File

@@ -25,15 +25,6 @@ namespace meta_hpp
return class_type{data_}; return class_type{data_};
} }
// class_
template < detail::class_kind Class >
template < detail::class_kind InternalClass >
class_bind<Class>& class_bind<Class>::class_(std::string name) {
data_->classes.insert_or_assign(std::move(name), detail::resolve_type<InternalClass>());
return *this;
}
// //
// base_ // base_
// //
@@ -113,17 +104,6 @@ namespace meta_hpp
return *this; return *this;
} }
//
// enum_
//
template < detail::class_kind Class >
template < detail::enum_kind InternalEnum >
class_bind<Class>& class_bind<Class>::enum_(std::string name) {
data_->enums.insert_or_assign(std::move(name), detail::resolve_type<InternalEnum>());
return *this;
}
// //
// function_ // function_
// //
@@ -293,6 +273,17 @@ namespace meta_hpp
return *this; return *this;
} }
//
// typedef_
//
template < detail::class_kind Class >
template < typename Type >
class_bind<Class>& class_bind<Class>::typedef_(std::string name) {
data_->typedefs.insert_or_assign(std::move(name), detail::resolve_type<Type>());
return *this;
}
// //
// variable_ // variable_
// //

View File

@@ -29,26 +29,6 @@ namespace meta_hpp
return scope{state_}; return scope{state_};
} }
//
// class_
//
template < detail::class_kind Class >
scope_bind& scope_bind::class_(std::string name) {
state_->classes.insert_or_assign(std::move(name), detail::resolve_type<Class>());
return *this;
}
//
// enum_
//
template < detail::enum_kind Enum >
scope_bind& scope_bind::enum_(std::string name) {
state_->enums.insert_or_assign(std::move(name), detail::resolve_type<Enum>());
return *this;
}
// //
// function_ // function_
// //
@@ -113,6 +93,16 @@ namespace meta_hpp
return *this; return *this;
} }
//
// typedef_
//
template < typename Type >
scope_bind& scope_bind::typedef_(std::string name) {
state_->typedefs.insert_or_assign(std::move(name), detail::resolve_type<Type>());
return *this;
}
// //
// variable_ // variable_
// //

View File

@@ -329,14 +329,12 @@ namespace meta_hpp
[[nodiscard]] const std::string& get_name() const noexcept; [[nodiscard]] const std::string& get_name() const noexcept;
[[nodiscard]] const class_map& get_classes() const noexcept;
[[nodiscard]] const enum_map& get_enums() const noexcept;
[[nodiscard]] const function_map& get_functions() const noexcept; [[nodiscard]] const function_map& get_functions() const noexcept;
[[nodiscard]] const typedef_map& get_typedefs() const noexcept;
[[nodiscard]] const variable_map& get_variables() const noexcept; [[nodiscard]] const variable_map& get_variables() const noexcept;
[[nodiscard]] class_type get_class(std::string_view name) const noexcept;
[[nodiscard]] enum_type get_enum(std::string_view name) const noexcept;
[[nodiscard]] function get_function(std::string_view name) const noexcept; [[nodiscard]] function get_function(std::string_view name) const noexcept;
[[nodiscard]] any_type get_typedef(std::string_view name) const noexcept;
[[nodiscard]] variable get_variable(std::string_view name) const noexcept; [[nodiscard]] variable get_variable(std::string_view name) const noexcept;
template < typename... Args > template < typename... Args >
@@ -529,9 +527,8 @@ namespace meta_hpp::detail
scope_index index; scope_index index;
metadata_map metadata; metadata_map metadata;
class_map classes{};
enum_map enums{};
function_map functions{}; function_map functions{};
typedef_map typedefs{};
variable_map variables{}; variable_map variables{};
[[nodiscard]] static scope_state_ptr make(std::string name, metadata_map metadata); [[nodiscard]] static scope_state_ptr make(std::string name, metadata_map metadata);

View File

@@ -58,36 +58,18 @@ namespace meta_hpp
return state_->index.get_name(); return state_->index.get_name();
} }
inline const class_map& scope::get_classes() const noexcept {
return state_->classes;
}
inline const enum_map& scope::get_enums() const noexcept {
return state_->enums;
}
inline const function_map& scope::get_functions() const noexcept { inline const function_map& scope::get_functions() const noexcept {
return state_->functions; return state_->functions;
} }
inline const typedef_map& scope::get_typedefs() const noexcept {
return state_->typedefs;
}
inline const variable_map& scope::get_variables() const noexcept { inline const variable_map& scope::get_variables() const noexcept {
return state_->variables; return state_->variables;
} }
inline class_type scope::get_class(std::string_view name) const noexcept {
if ( auto iter = state_->classes.find(name); iter != state_->classes.end() ) {
return iter->second;
}
return class_type{};
}
inline enum_type scope::get_enum(std::string_view name) const noexcept {
if ( auto iter = state_->enums.find(name); iter != state_->enums.end() ) {
return iter->second;
}
return enum_type{};
}
inline function scope::get_function(std::string_view name) const noexcept { inline function scope::get_function(std::string_view name) const noexcept {
for ( auto&& [index, function] : state_->functions ) { for ( auto&& [index, function] : state_->functions ) {
if ( index.get_name() == name ) { if ( index.get_name() == name ) {
@@ -97,6 +79,15 @@ namespace meta_hpp
return function{}; return function{};
} }
inline any_type scope::get_typedef(std::string_view name) const noexcept {
for ( auto&& [index, type] : state_->typedefs ) {
if ( index == name ) {
return type;
}
}
return any_type{};
}
inline variable scope::get_variable(std::string_view name) const noexcept { inline variable scope::get_variable(std::string_view name) const noexcept {
for ( auto&& [index, variable] : state_->variables ) { for ( auto&& [index, variable] : state_->variables ) {
if ( index.get_name() == name ) { if ( index.get_name() == name ) {

View File

@@ -155,14 +155,13 @@ namespace meta_hpp
[[nodiscard]] any_type get_argument_type(std::size_t position) 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 std::vector<any_type>& get_argument_types() const noexcept;
[[nodiscard]] const class_map& get_classes() const noexcept;
[[nodiscard]] const class_set& get_bases() const noexcept; [[nodiscard]] const class_set& get_bases() const noexcept;
[[nodiscard]] const constructor_map& get_ctors() const noexcept; [[nodiscard]] const constructor_map& get_ctors() const noexcept;
[[nodiscard]] const destructor_map& get_dtors() const noexcept; [[nodiscard]] const destructor_map& get_dtors() const noexcept;
[[nodiscard]] const enum_map& get_enums() const noexcept;
[[nodiscard]] const function_map& get_functions() const noexcept; [[nodiscard]] const function_map& get_functions() const noexcept;
[[nodiscard]] const member_map& get_members() const noexcept; [[nodiscard]] const member_map& get_members() const noexcept;
[[nodiscard]] const method_map& get_methods() const noexcept; [[nodiscard]] const method_map& get_methods() const noexcept;
[[nodiscard]] const typedef_map& get_typedefs() const noexcept;
[[nodiscard]] const variable_map& get_variables() const noexcept; [[nodiscard]] const variable_map& get_variables() const noexcept;
template < typename... Args > template < typename... Args >
@@ -182,11 +181,10 @@ namespace meta_hpp
[[nodiscard]] bool is_derived_from() const noexcept; [[nodiscard]] bool is_derived_from() const noexcept;
[[nodiscard]] bool is_derived_from(const class_type& base) const noexcept; [[nodiscard]] bool is_derived_from(const class_type& base) const noexcept;
[[nodiscard]] class_type get_class(std::string_view name) const noexcept;
[[nodiscard]] enum_type get_enum(std::string_view name) const noexcept;
[[nodiscard]] function get_function(std::string_view name) const noexcept; [[nodiscard]] function get_function(std::string_view name) const noexcept;
[[nodiscard]] member get_member(std::string_view name) const noexcept; [[nodiscard]] member get_member(std::string_view name) const noexcept;
[[nodiscard]] method get_method(std::string_view name) const noexcept; [[nodiscard]] method get_method(std::string_view name) const noexcept;
[[nodiscard]] any_type get_typedef(std::string_view name) const noexcept;
[[nodiscard]] variable get_variable(std::string_view name) const noexcept; [[nodiscard]] variable get_variable(std::string_view name) const noexcept;
template < typename... Args > template < typename... Args >
@@ -487,14 +485,13 @@ namespace meta_hpp::detail
const std::size_t size; const std::size_t size;
const std::vector<any_type> argument_types; const std::vector<any_type> argument_types;
class_map classes;
class_set bases; class_set bases;
constructor_map constructors; constructor_map constructors;
destructor_map destructors; destructor_map destructors;
enum_map enums;
function_map functions; function_map functions;
member_map members; member_map members;
method_map methods; method_map methods;
typedef_map typedefs;
variable_map variables; variable_map variables;
struct base_info final { struct base_info final {

View File

@@ -73,10 +73,6 @@ namespace meta_hpp
return data_->argument_types; return data_->argument_types;
} }
inline const class_map& class_type::get_classes() const noexcept {
return data_->classes;
}
inline const class_set& class_type::get_bases() const noexcept { inline const class_set& class_type::get_bases() const noexcept {
return data_->bases; return data_->bases;
} }
@@ -89,10 +85,6 @@ namespace meta_hpp
return data_->destructors; return data_->destructors;
} }
inline const enum_map& class_type::get_enums() const noexcept {
return data_->enums;
}
inline const function_map& class_type::get_functions() const noexcept { inline const function_map& class_type::get_functions() const noexcept {
return data_->functions; return data_->functions;
} }
@@ -105,6 +97,10 @@ namespace meta_hpp
return data_->methods; return data_->methods;
} }
inline const typedef_map& class_type::get_typedefs() const noexcept {
return data_->typedefs;
}
inline const variable_map& class_type::get_variables() const noexcept { inline const variable_map& class_type::get_variables() const noexcept {
return data_->variables; return data_->variables;
} }
@@ -181,20 +177,6 @@ namespace meta_hpp
return false; return false;
} }
inline class_type class_type::get_class(std::string_view name) const noexcept {
if ( auto iter = data_->classes.find(name); iter != data_->classes.end() ) {
return iter->second;
}
return class_type{};
}
inline enum_type class_type::get_enum(std::string_view name) const noexcept {
if ( auto iter = data_->enums.find(name); iter != data_->enums.end() ) {
return iter->second;
}
return enum_type{};
}
inline function class_type::get_function(std::string_view name) const noexcept { inline function class_type::get_function(std::string_view name) const noexcept {
for ( auto&& [index, function] : data_->functions ) { for ( auto&& [index, function] : data_->functions ) {
if ( index.get_name() == name ) { if ( index.get_name() == name ) {
@@ -243,6 +225,22 @@ namespace meta_hpp
return method{}; return method{};
} }
inline any_type class_type::get_typedef(std::string_view name) const noexcept {
for ( auto&& [index, type] : data_->typedefs ) {
if ( index == name ) {
return type;
}
}
for ( auto&& base : data_->bases ) {
if ( any_type type = base.get_typedef(name); type ) {
return type;
}
}
return any_type{};
}
inline variable class_type::get_variable(std::string_view name) const noexcept { inline variable class_type::get_variable(std::string_view name) const noexcept {
for ( auto&& [index, variable] : data_->variables ) { for ( auto&& [index, variable] : data_->variables ) {
if ( index.get_name() == name ) { if ( index.get_name() == name ) {

View File

@@ -10,7 +10,10 @@ namespace
{ {
class shape { class shape {
public: public:
shape() = default;
shape(const shape&) = default;
virtual ~shape() = default; virtual ~shape() = default;
virtual int get_area() const = 0; virtual int get_area() const = 0;
}; };

View File

@@ -11,6 +11,7 @@
#pragma clang diagnostic push #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc++20-compat" #pragma clang diagnostic ignored "-Wc++20-compat"
#pragma clang diagnostic ignored "-Wdocumentation-unknown-command" #pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
#pragma clang diagnostic ignored "-Wreserved-identifier"
#pragma clang diagnostic ignored "-Wsigned-enum-bitfield" #pragma clang diagnostic ignored "-Wsigned-enum-bitfield"
#pragma clang diagnostic ignored "-Wswitch-enum" #pragma clang diagnostic ignored "-Wswitch-enum"
#pragma clang diagnostic ignored "-Wundefined-func-template" #pragma clang diagnostic ignored "-Wundefined-func-template"

View File

@@ -41,9 +41,9 @@ TEST_CASE("meta/meta_states/scope") {
namespace meta = meta_hpp; namespace meta = meta_hpp;
meta::static_scope_("meta/meta_states/scope/math") meta::static_scope_("meta/meta_states/scope/math")
.enum_<color>("color") .typedef_<color>("color")
.class_<ivec2>("ivec2") .typedef_<ivec2>("ivec2")
.class_<ivec3>("ivec3") .typedef_<ivec3>("ivec3")
.function_("iadd2", &iadd2, {"l", "r"}) .function_("iadd2", &iadd2, {"l", "r"})
.function_("iadd3", &iadd3, {"l"}) .function_("iadd3", &iadd3, {"l"})
.variable_("static_ivec2", &static_ivec2) .variable_("static_ivec2", &static_ivec2)
@@ -54,10 +54,8 @@ TEST_CASE("meta/meta_states/scope") {
REQUIRE(math_scope.is_valid()); REQUIRE(math_scope.is_valid());
CHECK(math_scope.get_name() == "meta/meta_states/scope/math"); CHECK(math_scope.get_name() == "meta/meta_states/scope/math");
CHECK(math_scope.get_classes().size() == 2);
CHECK(math_scope.get_enums().size() == 1);
CHECK(math_scope.get_functions().size() == 2);
CHECK(math_scope.get_variables().size() == 2); CHECK(math_scope.get_variables().size() == 2);
CHECK(math_scope.get_typedefs().size() == 3);
SUBCASE("") { SUBCASE("") {
const meta::scope scope; const meta::scope scope;
@@ -74,20 +72,20 @@ TEST_CASE("meta/meta_states/scope") {
} }
SUBCASE("classes") { SUBCASE("classes") {
CHECK_FALSE(math_scope.get_class("non-existent-class")); CHECK_FALSE(math_scope.get_typedef("non-existent-class"));
const meta::class_type ivec2_type = math_scope.get_class("ivec2"); const meta::any_type ivec2_type = math_scope.get_typedef("ivec2");
REQUIRE(ivec2_type); CHECK(ivec2_type == meta::resolve_type<ivec2>());
const meta::class_type ivec3_type = math_scope.get_class("ivec3"); const meta::any_type ivec3_type = math_scope.get_typedef("ivec3");
REQUIRE(ivec3_type); CHECK(ivec3_type == meta::resolve_type<ivec3>());
} }
SUBCASE("enums") { SUBCASE("enums") {
CHECK_FALSE(math_scope.get_enum("non-existent-enum")); CHECK_FALSE(math_scope.get_typedef("non-existent-enum"));
const meta::enum_type color_type = math_scope.get_enum("color"); const meta::any_type color_type = math_scope.get_typedef("color");
REQUIRE(color_type); CHECK(color_type == meta::resolve_type<color>());
} }
SUBCASE("functions") { SUBCASE("functions") {

View File

@@ -18,20 +18,17 @@ TEST_CASE("meta/meta_types/class_type2") {
namespace meta = meta_hpp; namespace meta = meta_hpp;
meta::class_<clazz>() meta::class_<clazz>()
.class_<clazz::internal_clazz>("internal_clazz") .typedef_<clazz::internal_clazz>("internal_clazz")
.enum_<clazz::internal_enum>("internal_enum"); .typedef_<clazz::internal_enum>("internal_enum");
const meta::class_type clazz_type = meta::resolve_type<clazz>(); const meta::class_type clazz_type = meta::resolve_type<clazz>();
REQUIRE(clazz_type); REQUIRE(clazz_type);
CHECK(clazz_type.get_class("internal_clazz") == meta::resolve_type<clazz::internal_clazz>()); CHECK(clazz_type.get_typedef("internal_clazz") == meta::resolve_type<clazz::internal_clazz>());
CHECK(clazz_type.get_enum("internal_enum") == meta::resolve_type<clazz::internal_enum>()); CHECK(clazz_type.get_typedef("internal_enum") == meta::resolve_type<clazz::internal_enum>());
CHECK(clazz_type.get_classes() == meta::class_map{ CHECK(clazz_type.get_typedefs() == meta::typedef_map{
{"internal_clazz", meta::resolve_type<clazz::internal_clazz>()} {"internal_clazz", meta::resolve_type<clazz::internal_clazz>()},
}); {"internal_enum", meta::resolve_type<clazz::internal_enum>()},
CHECK(clazz_type.get_enums() == meta::enum_map{
{"internal_enum", meta::resolve_type<clazz::internal_enum>()}
}); });
} }