static and local scopes

This commit is contained in:
BlackMATov
2021-11-26 06:06:32 +07:00
parent 7b512e182c
commit 89b51d3bdc
25 changed files with 76 additions and 76 deletions

View File

@@ -57,7 +57,11 @@ namespace meta_hpp
{ {
class scope_bind final { class scope_bind final {
public: public:
explicit scope_bind(std::string_view name); struct local_tag {};
struct static_tag {};
explicit scope_bind(std::string_view name, local_tag);
explicit scope_bind(std::string_view name, static_tag);
operator scope() const noexcept; operator scope() const noexcept;
template < detail::class_kind Class > template < detail::class_kind Class >
@@ -88,7 +92,11 @@ namespace meta_hpp
return enum_bind<Enum>{}; return enum_bind<Enum>{};
} }
inline scope_bind scope_(std::string name) { inline scope_bind local_scope_(std::string name) {
return scope_bind{std::move(name)}; return scope_bind{std::move(name), scope_bind::local_tag()};
}
inline scope_bind static_scope_(std::string name) {
return scope_bind{std::move(name), scope_bind::static_tag()};
} }
} }

View File

@@ -13,7 +13,7 @@ namespace meta_hpp
{ {
template < detail::class_kind Class > template < detail::class_kind Class >
class_bind<Class>::class_bind() class_bind<Class>::class_bind()
: data_{detail::get_type_data<Class>()} {} : data_{detail::class_type_data::get_static<Class>()} {}
template < detail::class_kind Class > template < detail::class_kind Class >
class_bind<Class>::operator class_type() const noexcept { class_bind<Class>::operator class_type() const noexcept {
@@ -33,8 +33,7 @@ namespace meta_hpp
template < detail::class_kind Base > template < detail::class_kind Base >
class_bind<Class>& class_bind<Class>::base_() { class_bind<Class>& class_bind<Class>::base_() {
static_assert(std::is_base_of_v<Base, Class>); static_assert(std::is_base_of_v<Base, Class>);
auto base_data = detail::class_type_data::get<Base>(); data_->bases.emplace(resolve_type<Base>());
data_->bases.emplace(base_data);
return *this; return *this;
} }

View File

@@ -13,7 +13,7 @@ namespace meta_hpp
{ {
template < detail::enum_kind Enum > template < detail::enum_kind Enum >
enum_bind<Enum>::enum_bind() enum_bind<Enum>::enum_bind()
: data_{detail::get_type_data<Enum>()} {} : data_{detail::enum_type_data::get_static<Enum>()} {}
template < detail::enum_kind Enum > template < detail::enum_kind Enum >
enum_bind<Enum>::operator enum_type() const noexcept { enum_bind<Enum>::operator enum_type() const noexcept {

View File

@@ -11,8 +11,11 @@
namespace meta_hpp namespace meta_hpp
{ {
inline scope_bind::scope_bind(std::string_view name) inline scope_bind::scope_bind(std::string_view name, local_tag)
: state_{detail::get_scope_state(name)} {} : state_{detail::scope_state::make(std::string{name})} {}
inline scope_bind::scope_bind(std::string_view name, static_tag)
: state_{detail::scope_state::get_static(name)} {}
inline scope_bind::operator scope() const noexcept { inline scope_bind::operator scope() const noexcept {
return scope{state_}; return scope{state_};
@@ -20,15 +23,13 @@ namespace meta_hpp
template < detail::class_kind Class > template < detail::class_kind Class >
scope_bind& scope_bind::class_(std::string name) { scope_bind& scope_bind::class_(std::string name) {
auto class_data = detail::class_type_data::get<Class>(); state_->classes.emplace(std::move(name), resolve_type<Class>());
state_->classes.emplace(std::move(name), class_data);
return *this; return *this;
} }
template < detail::enum_kind Enum > template < detail::enum_kind Enum >
scope_bind& scope_bind::enum_(std::string name) { scope_bind& scope_bind::enum_(std::string name) {
auto enum_data = detail::enum_type_data::get<Enum>(); state_->enums.emplace(std::move(name), resolve_type<Enum>());
state_->enums.emplace(std::move(name), enum_data);
return *this; return *this;
} }

View File

@@ -295,7 +295,8 @@ namespace meta_hpp::detail
function_map functions; function_map functions;
variable_map variables; variable_map variables;
static scope_state_ptr get(std::string_view name); static scope_state_ptr make(std::string name);
static scope_state_ptr get_static(std::string_view name);
explicit scope_state(scope_index index); explicit scope_state(scope_index index);
}; };
@@ -319,14 +320,7 @@ namespace meta_hpp::detail
namespace meta_hpp namespace meta_hpp
{ {
namespace detail
{
inline scope_state_ptr get_scope_state(std::string_view name) {
return scope_state::get(name);
}
}
inline scope resolve_scope(std::string_view name) { inline scope resolve_scope(std::string_view name) {
return scope{detail::get_scope_state(name)}; return scope{detail::scope_state::get_static(name)};
} }
} }

View File

@@ -92,7 +92,7 @@ namespace meta_hpp::detail
{ {
template < class_kind Class, typename... Args > template < class_kind Class, typename... Args >
ctor_state_ptr ctor_state::make() { ctor_state_ptr ctor_state::make() {
ctor_index index{ctor_type_data::get<Class, Args...>()}; ctor_index index{ctor_type_data::get_static<Class, Args...>()};
return std::make_shared<ctor_state>(std::move(index), type_list<Class>{}, type_list<Args...>{}); return std::make_shared<ctor_state>(std::move(index), type_list<Class>{}, type_list<Args...>{});
} }

View File

@@ -15,7 +15,7 @@ namespace meta_hpp::detail
{ {
template < enum_kind Enum > template < enum_kind Enum >
evalue_state_ptr evalue_state::make(std::string name, Enum value) { evalue_state_ptr evalue_state::make(std::string name, Enum value) {
evalue_index index{enum_type_data::get<Enum>(), std::move(name)}; evalue_index index{enum_type_data::get_static<Enum>(), std::move(name)};
return std::make_shared<evalue_state>(std::move(index), std::move(value)); return std::make_shared<evalue_state>(std::move(index), std::move(value));
} }

View File

@@ -102,7 +102,7 @@ namespace meta_hpp::detail
{ {
template < function_kind Function > template < function_kind Function >
function_state_ptr function_state::make(std::string name, Function function) { function_state_ptr function_state::make(std::string name, Function function) {
function_index index{function_type_data::get<Function>(), std::move(name)}; function_index index{function_type_data::get_static<Function>(), std::move(name)};
return std::make_shared<function_state>(std::move(index), std::move(function)); return std::make_shared<function_state>(std::move(index), std::move(function));
} }

View File

@@ -76,7 +76,7 @@ namespace meta_hpp::detail
{ {
template < member_kind Member > template < member_kind Member >
member_state_ptr member_state::make(std::string name, Member member) { member_state_ptr member_state::make(std::string name, Member member) {
member_index index{member_type_data::get<Member>(), std::move(name)}; member_index index{member_type_data::get_static<Member>(), std::move(name)};
return std::make_shared<member_state>(std::move(index), std::move(member)); return std::make_shared<member_state>(std::move(index), std::move(member));
} }

View File

@@ -118,7 +118,7 @@ namespace meta_hpp::detail
{ {
template < method_kind Method > template < method_kind Method >
method_state_ptr method_state::make(std::string name, Method method) { method_state_ptr method_state::make(std::string name, Method method) {
method_index index{method_type_data::get<Method>(), std::move(name)}; method_index index{method_type_data::get_static<Method>(), std::move(name)};
return std::make_shared<method_state>(std::move(index), std::move(method)); return std::make_shared<method_state>(std::move(index), std::move(method));
} }

View File

@@ -11,15 +11,19 @@
namespace meta_hpp::detail namespace meta_hpp::detail
{ {
inline scope_state_ptr scope_state::get(std::string_view name) { inline scope_state_ptr scope_state::make(std::string name) {
scope_index index{std::move(name)};
return std::make_shared<scope_state>(std::move(index));
}
inline scope_state_ptr scope_state::get_static(std::string_view name) {
static std::map<std::string, scope_state_ptr, std::less<>> states; static std::map<std::string, scope_state_ptr, std::less<>> states;
if ( auto iter = states.find(name); iter != states.end() ) { if ( auto iter = states.find(name); iter != states.end() ) {
return iter->second; return iter->second;
} }
auto state = std::make_shared<scope_state>(scope_index{std::string{name}}); return states.emplace(std::string{name}, make(std::string{name})).first->second;
return states.emplace(std::string{name}, state).first->second;
} }
inline scope_state::scope_state(scope_index index) inline scope_state::scope_state(scope_index index)

View File

@@ -58,7 +58,7 @@ namespace meta_hpp::detail
{ {
template < pointer_kind Pointer > template < pointer_kind Pointer >
variable_state_ptr variable_state::make(std::string name, Pointer pointer) { variable_state_ptr variable_state::make(std::string name, Pointer pointer) {
variable_index index{pointer_type_data::get<Pointer>(), std::move(name)}; variable_index index{pointer_type_data::get_static<Pointer>(), std::move(name)};
return std::make_shared<variable_state>(index, pointer); return std::make_shared<variable_state>(index, pointer);
} }

View File

@@ -421,7 +421,7 @@ namespace meta_hpp::detail
const any_type data_type; const any_type data_type;
template < array_kind Array > template < array_kind Array >
static array_type_data_ptr get(); static array_type_data_ptr get_static();
template < array_kind Array > template < array_kind Array >
explicit array_type_data(type_list<Array>); explicit array_type_data(type_list<Array>);
@@ -440,7 +440,7 @@ namespace meta_hpp::detail
variable_map variables; variable_map variables;
template < class_kind Class > template < class_kind Class >
static class_type_data_ptr get(); static class_type_data_ptr get_static();
template < class_kind Class > template < class_kind Class >
explicit class_type_data(type_list<Class>); explicit class_type_data(type_list<Class>);
@@ -452,7 +452,7 @@ namespace meta_hpp::detail
const std::vector<any_type> argument_types; const std::vector<any_type> argument_types;
template < class_kind Class, typename... Args > template < class_kind Class, typename... Args >
static ctor_type_data_ptr get(); static ctor_type_data_ptr get_static();
template < class_kind Class, typename... Args > template < class_kind Class, typename... Args >
explicit ctor_type_data(type_list<Class>, type_list<Args...>); explicit ctor_type_data(type_list<Class>, type_list<Args...>);
@@ -465,7 +465,7 @@ namespace meta_hpp::detail
evalue_map evalues; evalue_map evalues;
template < enum_kind Enum > template < enum_kind Enum >
static enum_type_data_ptr get(); static enum_type_data_ptr get_static();
template < enum_kind Enum > template < enum_kind Enum >
explicit enum_type_data(type_list<Enum>); explicit enum_type_data(type_list<Enum>);
@@ -477,7 +477,7 @@ namespace meta_hpp::detail
const std::vector<any_type> argument_types; const std::vector<any_type> argument_types;
template < function_kind Function > template < function_kind Function >
static function_type_data_ptr get(); static function_type_data_ptr get_static();
template < function_kind Function > template < function_kind Function >
explicit function_type_data(type_list<Function>); explicit function_type_data(type_list<Function>);
@@ -489,7 +489,7 @@ namespace meta_hpp::detail
const any_type value_type; const any_type value_type;
template < member_kind Member > template < member_kind Member >
static member_type_data_ptr get(); static member_type_data_ptr get_static();
template < member_kind Member > template < member_kind Member >
explicit member_type_data(type_list<Member>); explicit member_type_data(type_list<Member>);
@@ -502,7 +502,7 @@ namespace meta_hpp::detail
const std::vector<any_type> argument_types; const std::vector<any_type> argument_types;
template < method_kind Method > template < method_kind Method >
static method_type_data_ptr get(); static method_type_data_ptr get_static();
template < method_kind Method > template < method_kind Method >
explicit method_type_data(type_list<Method>); explicit method_type_data(type_list<Method>);
@@ -513,7 +513,7 @@ namespace meta_hpp::detail
const std::size_t size; const std::size_t size;
template < number_kind Number > template < number_kind Number >
static number_type_data_ptr get(); static number_type_data_ptr get_static();
template < number_kind Number > template < number_kind Number >
explicit number_type_data(type_list<Number>); explicit number_type_data(type_list<Number>);
@@ -524,7 +524,7 @@ namespace meta_hpp::detail
const any_type data_type; const any_type data_type;
template < pointer_kind Pointer > template < pointer_kind Pointer >
static pointer_type_data_ptr get(); static pointer_type_data_ptr get_static();
template < pointer_kind Pointer > template < pointer_kind Pointer >
explicit pointer_type_data(type_list<Pointer>); explicit pointer_type_data(type_list<Pointer>);
@@ -535,7 +535,7 @@ namespace meta_hpp::detail
const any_type data_type; const any_type data_type;
template < reference_kind Reference > template < reference_kind Reference >
static reference_type_data_ptr get(); static reference_type_data_ptr get_static();
template < reference_kind Reference > template < reference_kind Reference >
explicit reference_type_data(type_list<Reference>); explicit reference_type_data(type_list<Reference>);
@@ -545,7 +545,7 @@ namespace meta_hpp::detail
const bitflags<void_flags> flags; const bitflags<void_flags> flags;
template < void_kind Void > template < void_kind Void >
static void_type_data_ptr get(); static void_type_data_ptr get_static();
template < void_kind Void > template < void_kind Void >
explicit void_type_data(type_list<Void>); explicit void_type_data(type_list<Void>);
@@ -672,24 +672,18 @@ namespace meta_hpp
namespace meta_hpp namespace meta_hpp
{ {
namespace detail
{
template < typename T >
kind_type_data_ptr<T> get_type_data() {
static_assert(!std::is_const_v<T> && !std::is_volatile_v<T>);
return kind_type_data<T>::template get<T>();
}
}
template < typename T > template < typename T >
auto resolve_type() { auto resolve_type() {
using raw_type = std::remove_cv_t<T>; using raw_type = std::remove_cv_t<T>;
return detail::kind_type<raw_type>{detail::get_type_data<raw_type>()};
using kind_type = detail::kind_type<raw_type>;
using kind_type_data = detail::kind_type_data<raw_type>;
return kind_type{kind_type_data::template get_static<raw_type>()};
} }
template < typename T > template < typename T >
auto resolve_type(T&&) { auto resolve_type(T&&) {
using raw_type = std::remove_cvref_t<T>; return resolve_type<std::remove_reference_t<T>>();
return detail::kind_type<raw_type>{detail::get_type_data<raw_type>()};
}; };
} }

View File

@@ -17,7 +17,7 @@ namespace meta_hpp::detail
struct array_tag {}; struct array_tag {};
template < array_kind Array > template < array_kind Array >
array_type_data_ptr array_type_data::get() { array_type_data_ptr array_type_data::get_static() {
static array_type_data_ptr data = std::make_shared<array_type_data>(type_list<Array>{}); static array_type_data_ptr data = std::make_shared<array_type_data>(type_list<Array>{});
return data; return data;
} }

View File

@@ -17,7 +17,7 @@ namespace meta_hpp::detail
struct class_tag {}; struct class_tag {};
template < class_kind Class > template < class_kind Class >
class_type_data_ptr class_type_data::get() { class_type_data_ptr class_type_data::get_static() {
static class_type_data_ptr data = std::make_shared<class_type_data>(type_list<Class>{}); static class_type_data_ptr data = std::make_shared<class_type_data>(type_list<Class>{});
return data; return data;
} }

View File

@@ -17,7 +17,7 @@ namespace meta_hpp::detail
struct ctor_tag {}; struct ctor_tag {};
template < class_kind Class, typename... Args > template < class_kind Class, typename... Args >
ctor_type_data_ptr ctor_type_data::get() { ctor_type_data_ptr ctor_type_data::get_static() {
static ctor_type_data_ptr data = std::make_shared<ctor_type_data>(type_list<Class>{}, type_list<Args...>{}); static ctor_type_data_ptr data = std::make_shared<ctor_type_data>(type_list<Class>{}, type_list<Args...>{});
return data; return data;
} }

View File

@@ -17,7 +17,7 @@ namespace meta_hpp::detail
struct enum_tag {}; struct enum_tag {};
template < enum_kind Enum > template < enum_kind Enum >
enum_type_data_ptr enum_type_data::get() { enum_type_data_ptr enum_type_data::get_static() {
static enum_type_data_ptr data = std::make_shared<enum_type_data>(type_list<Enum>{}); static enum_type_data_ptr data = std::make_shared<enum_type_data>(type_list<Enum>{});
return data; return data;
} }

View File

@@ -17,7 +17,7 @@ namespace meta_hpp::detail
struct function_tag {}; struct function_tag {};
template < function_kind Function > template < function_kind Function >
function_type_data_ptr function_type_data::get() { function_type_data_ptr function_type_data::get_static() {
static function_type_data_ptr data = std::make_shared<function_type_data>(type_list<Function>{}); static function_type_data_ptr data = std::make_shared<function_type_data>(type_list<Function>{});
return data; return data;
} }

View File

@@ -17,7 +17,7 @@ namespace meta_hpp::detail
struct member_tag {}; struct member_tag {};
template < member_kind Member > template < member_kind Member >
member_type_data_ptr member_type_data::get() { member_type_data_ptr member_type_data::get_static() {
static member_type_data_ptr data = std::make_shared<member_type_data>(type_list<Member>{}); static member_type_data_ptr data = std::make_shared<member_type_data>(type_list<Member>{});
return data; return data;
} }

View File

@@ -17,7 +17,7 @@ namespace meta_hpp::detail
struct method_tag {}; struct method_tag {};
template < method_kind Method > template < method_kind Method >
method_type_data_ptr method_type_data::get() { method_type_data_ptr method_type_data::get_static() {
static method_type_data_ptr data = std::make_shared<method_type_data>(type_list<Method>{}); static method_type_data_ptr data = std::make_shared<method_type_data>(type_list<Method>{});
return data; return data;
} }

View File

@@ -17,7 +17,7 @@ namespace meta_hpp::detail
struct number_tag {}; struct number_tag {};
template < number_kind Number > template < number_kind Number >
number_type_data_ptr number_type_data::get() { number_type_data_ptr number_type_data::get_static() {
static number_type_data_ptr data = std::make_shared<number_type_data>(type_list<Number>{}); static number_type_data_ptr data = std::make_shared<number_type_data>(type_list<Number>{});
return data; return data;
} }

View File

@@ -17,7 +17,7 @@ namespace meta_hpp::detail
struct pointer_tag {}; struct pointer_tag {};
template < pointer_kind Pointer > template < pointer_kind Pointer >
pointer_type_data_ptr pointer_type_data::get() { pointer_type_data_ptr pointer_type_data::get_static() {
static pointer_type_data_ptr data = std::make_shared<pointer_type_data>(type_list<Pointer>{}); static pointer_type_data_ptr data = std::make_shared<pointer_type_data>(type_list<Pointer>{});
return data; return data;
} }

View File

@@ -17,7 +17,7 @@ namespace meta_hpp::detail
struct reference_tag {}; struct reference_tag {};
template < reference_kind Reference > template < reference_kind Reference >
reference_type_data_ptr reference_type_data::get() { reference_type_data_ptr reference_type_data::get_static() {
static reference_type_data_ptr data = std::make_shared<reference_type_data>(type_list<Reference>{}); static reference_type_data_ptr data = std::make_shared<reference_type_data>(type_list<Reference>{});
return data; return data;
} }

View File

@@ -17,7 +17,7 @@ namespace meta_hpp::detail
struct void_tag {}; struct void_tag {};
template < void_kind Void > template < void_kind Void >
void_type_data_ptr void_type_data::get() { void_type_data_ptr void_type_data::get_static() {
static void_type_data_ptr data = std::make_shared<void_type_data>(type_list<Void>{}); static void_type_data_ptr data = std::make_shared<void_type_data>(type_list<Void>{});
return data; return data;
} }

View File

@@ -33,21 +33,21 @@ namespace
return {l.x + r.x, l.y + r.y, l.z + r.z}; return {l.x + r.x, l.y + r.y, l.z + r.z};
} }
ivec2 global_ivec2 = ivec2{1, 0}; ivec2 static_ivec2 = ivec2{1, 0};
const ivec3 global_const_ivec3 = ivec3{1, 0}; const ivec3 static_const_ivec3 = ivec3{1, 0};
} }
TEST_CASE("meta/meta_states/scope") { TEST_CASE("meta/meta_states/scope") {
namespace meta = meta_hpp; namespace meta = meta_hpp;
meta::scope_("meta/meta_states/scope/math") meta::static_scope_("meta/meta_states/scope/math")
.enum_<color>("color") .enum_<color>("color")
.class_<ivec2>("ivec2") .class_<ivec2>("ivec2")
.class_<ivec3>("ivec3") .class_<ivec3>("ivec3")
.function_("iadd2", &iadd2) .function_("iadd2", &iadd2)
.function_("iadd3", &iadd3) .function_("iadd3", &iadd3)
.variable_("global_ivec2", &global_ivec2) .variable_("static_ivec2", &static_ivec2)
.variable_("global_const_ivec3", &global_const_ivec3); .variable_("static_const_ivec3", &static_const_ivec3);
const meta::scope math_scope = meta::resolve_scope("meta/meta_states/scope/math"); const meta::scope math_scope = meta::resolve_scope("meta/meta_states/scope/math");
REQUIRE(math_scope); REQUIRE(math_scope);
@@ -103,12 +103,12 @@ TEST_CASE("meta/meta_states/scope") {
SUBCASE("variables") { SUBCASE("variables") {
CHECK_FALSE(math_scope.get_variable("non-existent-variable")); CHECK_FALSE(math_scope.get_variable("non-existent-variable"));
const meta::variable global_ivec2_var = math_scope.get_variable("global_ivec2"); const meta::variable static_ivec2_var = math_scope.get_variable("static_ivec2");
REQUIRE(global_ivec2_var); REQUIRE(static_ivec2_var);
CHECK(global_ivec2_var.get_type().get_data_type() == meta::resolve_type<ivec2>()); CHECK(static_ivec2_var.get_type().get_data_type() == meta::resolve_type<ivec2>());
const meta::variable global_const_ivec3_var = math_scope.get_variable("global_const_ivec3"); const meta::variable static_const_ivec3_var = math_scope.get_variable("static_const_ivec3");
REQUIRE(global_const_ivec3_var); REQUIRE(static_const_ivec3_var);
CHECK(global_const_ivec3_var.get_type().get_data_type() == meta::resolve_type<ivec3>()); CHECK(static_const_ivec3_var.get_type().get_data_type() == meta::resolve_type<ivec3>());
} }
} }