From 7ae6e24b5bffc119e5cbc0791901d88bb9f4de09 Mon Sep 17 00:00:00 2001 From: Shin-ichi MORITA Date: Fri, 9 Jun 2023 06:43:35 +0900 Subject: [PATCH] Refactored f_as/f_is details. --- include/xemmai/boolean.h | 12 +++----- include/xemmai/float.h | 22 ++++++-------- include/xemmai/integer.h | 18 +++++------ include/xemmai/object.h | 43 +++++++++++++++++++++++++- include/xemmai/string.h | 26 +--------------- include/xemmai/type.h | 65 +++++----------------------------------- src/code_operator.h | 4 +-- src/main.cc | 2 +- test/callback/library.cc | 14 ++++----- 9 files changed, 81 insertions(+), 125 deletions(-) diff --git a/include/xemmai/boolean.h b/include/xemmai/boolean.h index c4787a9b..de362948 100644 --- a/include/xemmai/boolean.h +++ b/include/xemmai/boolean.h @@ -10,19 +10,15 @@ template<> struct t_type_of : t_uninstantiatable> { template - struct t_as + struct t_cast { - static bool f_call(auto&& a_object) + static bool f_as(auto&& a_object) { return a_object.f_boolean(); } - }; - template - struct t_is - { - static bool f_call(auto&& a_object) + static bool f_is(auto&& a_object) { - return reinterpret_cast(f_object(std::forward(a_object))) == e_tag__BOOLEAN; + return reinterpret_cast(static_cast(a_object)) == e_tag__BOOLEAN; } }; diff --git a/include/xemmai/float.h b/include/xemmai/float.h index 15f8bdff..24190ab0 100644 --- a/include/xemmai/float.h +++ b/include/xemmai/float.h @@ -17,34 +17,30 @@ template<> struct t_type_of : t_derivable, t_derived_primitive> { template - struct t_as + struct t_cast { using t_type = typename t_fundamental::t_type; - static t_type f_call(auto&& a_object) + static t_type f_as(auto&& a_object) { - auto p = f_object(std::forward(a_object)); + auto p = static_cast(a_object); switch (reinterpret_cast(p)) { case e_tag__INTEGER: return a_object.f_integer(); case e_tag__FLOAT: return a_object.f_float(); default: - return p->template f_as(); + return p->f_as(); } } - static t_type f_call(t_object* a_object) + static t_type f_as(t_object* a_object) { return a_object->f_as(); } - }; - template - struct t_is - { - static bool f_call(auto&& a_object) + static bool f_is(auto&& a_object) { - auto p = f_object(std::forward(a_object)); - if (!std::is_same_v::t_type, double>) return reinterpret_cast(p) >= e_tag__OBJECT && p->f_type()->template f_derives::t_type>(); + auto p = static_cast(a_object); + if (!std::is_same_v::t_type, double>) return reinterpret_cast(p) >= e_tag__OBJECT && p->f_type()->f_derives::t_type>(); switch (reinterpret_cast(p)) { case e_tag__NULL: case e_tag__BOOLEAN: @@ -53,7 +49,7 @@ struct t_type_of : t_derivable, t_derived_primitivef_type()->template f_derives(); + return p->f_type()->f_derives(); } } }; diff --git a/include/xemmai/integer.h b/include/xemmai/integer.h index 055414ee..7b943251 100644 --- a/include/xemmai/integer.h +++ b/include/xemmai/integer.h @@ -19,26 +19,22 @@ template<> struct t_type_of : t_derivable, t_derived_primitive> { template - struct t_as + struct t_cast { using t_type = typename t_fundamental::t_type; - static t_type f_call(auto&& a_object) + static t_type f_as(auto&& a_object) { return static_cast(a_object.f_integer()); } - static t_type f_call(t_object* a_object) + static t_type f_as(t_object* a_object) { return static_cast(a_object->f_as()); } - }; - template - struct t_is - { - static bool f_call(auto&& a_object) + static bool f_is(auto&& a_object) { - auto p = f_object(std::forward(a_object)); - if (!std::is_same_v::t_type, intptr_t>) return reinterpret_cast(p) >= e_tag__OBJECT && p->f_type()->template f_derives::t_type>(); + auto p = static_cast(a_object); + if (!std::is_same_v::t_type, intptr_t>) return reinterpret_cast(p) >= e_tag__OBJECT && p->f_type()->f_derives::t_type>(); switch (reinterpret_cast(p)) { case e_tag__INTEGER: return true; @@ -47,7 +43,7 @@ struct t_type_of : t_derivable, t_derived_primitive< case e_tag__FLOAT: return false; default: - return p->f_type()->template f_derives(); + return p->f_type()->f_derives(); } } }; diff --git a/include/xemmai/object.h b/include/xemmai/object.h index 51ac19aa..a6ba5adc 100644 --- a/include/xemmai/object.h +++ b/include/xemmai/object.h @@ -307,10 +307,51 @@ inline bool t_value::f_has(t_object* a_key, size_t& a_index) const template inline void t_slot_of::f_construct(t_object* a_value) { - v_p = &a_value->template f_as(); + v_p = &a_value->f_as(); v_slot = a_value; } +template +struct t_type::t_cast +{ + static T f_as(auto&& a_object) + { + return static_cast(a_object)->f_as::t_type>(); + } + static bool f_is(auto&& a_object) + { + if constexpr (std::is_same_v::t_type, t_object>) return true; + auto p = static_cast(a_object); + return reinterpret_cast(p) >= e_tag__OBJECT && p->f_type()->f_derives::t_type>(); + } +}; + +template +struct t_type::t_cast +{ + static_assert(!std::is_same_v); + + static T* f_as(auto&& a_object) + { + auto p = static_cast(a_object); + return p ? &p->f_as() : nullptr; + } + static bool f_is(auto&& a_object) + { + auto p = static_cast(a_object); + switch (reinterpret_cast(p)) { + case e_tag__NULL: + return true; + case e_tag__BOOLEAN: + case e_tag__INTEGER: + case e_tag__FLOAT: + return false; + default: + return p->f_type()->f_derives::t_type>(); + } + } +}; + inline t_type::t_type_of() : v_this(t_object::f_of(this)), v_depth(V_ids.size() - 1), v_ids(V_ids.data()), v_fields_offset(t_object::f_fields_offset(0)), v_instance_fields(0), v_fields(0) { } diff --git a/include/xemmai/string.h b/include/xemmai/string.h index c6733ac0..04c1418b 100644 --- a/include/xemmai/string.h +++ b/include/xemmai/string.h @@ -81,26 +81,6 @@ struct t_fundamental template<> struct t_type_of : t_holds { - template - struct t_as - { - static T f_call(auto&& a_object) - { - return f_object(std::forward(a_object))->template f_as(); - } - }; - template - struct t_as - { - static_assert(std::is_same_v, t_string>); - - static T* f_call(auto&& a_object) - { - auto p = f_object(std::forward(a_object)); - return p ? &p->template f_as() : nullptr; - } - }; - static t_object* f__construct(t_type* a_class, const wchar_t* a_p, size_t a_n); static t_pvalue f__construct(t_type* a_class, const t_string& a_value) { @@ -153,12 +133,8 @@ struct t_type_of : t_holds }; template<> -struct t_type_of::t_as +struct t_type::t_cast : t_type::t_cast { - static std::wstring_view f_call(auto&& a_object) - { - return f_object(std::forward(a_object))->template f_as(); - } }; } diff --git a/include/xemmai/type.h b/include/xemmai/type.h index f58d78a8..39182671 100644 --- a/include/xemmai/type.h +++ b/include/xemmai/type.h @@ -50,8 +50,7 @@ inline t_type_id f_type_id() #define XEMMAI__TYPE__IDS_MODIFIER inline const #else using t_type_id = const void*; -template -const void* v__type_id; +template const void* v__type_id; template constexpr t_type_id f_type_id() { @@ -93,65 +92,17 @@ struct t_fields template<> struct t_type_of { - static t_object* f_object(auto&& a_value) - { - return a_value; - } - template - struct t_as - { - static T f_call(auto&& a_object) - { - return f_object(std::forward(a_object))->template f_as::t_type>(); - } - }; - template - struct t_as - { - static_assert(!std::is_same_v); - - static T* f_call(auto&& a_object) - { - auto p = f_object(std::forward(a_object)); - return p ? &p->template f_as() : nullptr; - } - }; + template struct t_cast; template - struct t_as&> + struct t_cast&> { - static const t_value& f_call(auto&& a_object) + static const t_value& f_as(auto&& a_object) { return a_object; } - }; - template - struct t_is - { - static bool f_call(auto&& a_object) - { - if (std::is_same_v::t_type, t_object>) return true; - auto p = f_object(std::forward(a_object)); - return reinterpret_cast(p) >= e_tag__OBJECT && p->f_type()->template f_derives::t_type>(); - } - }; - template - struct t_is - { - static_assert(!std::is_same_v); - - static bool f_call(auto&& a_object) + static bool f_is(auto&& a_object) { - auto p = f_object(std::forward(a_object)); - switch (reinterpret_cast(p)) { - case e_tag__NULL: - return true; - case e_tag__BOOLEAN: - case e_tag__INTEGER: - case e_tag__FLOAT: - return false; - default: - return p->f_type()->template f_derives::t_type>(); - } + return true; } }; using t_library = t_global; @@ -356,13 +307,13 @@ inline bool t_type_of::f_derives() const template inline decltype(auto) f_as(auto&& a_object) { - return t_type_of::t_type>::template t_as::f_call(std::forward(a_object)); + return t_type_of::t_type>::template t_cast::f_as(std::forward(a_object)); } template inline bool f_is(auto&& a_object) { - return t_type_of::t_type>::template t_is::f_call(std::forward(a_object)); + return t_type_of::t_type>::template t_cast::f_is(std::forward(a_object)); } XEMMAI__PORTABLE__EXPORT void f_throw_type_error [[noreturn]] (const std::type_info& a_type, const wchar_t* a_name); diff --git a/src/code_operator.h b/src/code_operator.h index cc3ec406..349c9a5f 100644 --- a/src/code_operator.h +++ b/src/code_operator.h @@ -84,7 +84,7 @@ #endif #define XEMMAI__CODE__BINARY_DERIVED_OR_THROW(a_operator, a_type, a_cast, a_field)\ if (a1.f_tag() >= e_tag__OBJECT) {\ - if (a1->f_type()->template f_derives()) {\ + if (a1->f_type()->f_derives()) {\ XEMMAI__CODE__PRIMITIVE_CALL(a_cast(a0 a_field) a_operator a1->f_as())\ } else {\ goto label__THROW_NOT_SUPPORTED;\ @@ -94,7 +94,7 @@ } #define XEMMAI__CODE__BINARY_DERIVED_OR_FALSE(a_operator, a_type, a_field)\ if (a1.f_tag() >= e_tag__OBJECT) {\ - XEMMAI__CODE__PRIMITIVE_CALL(a_operator(a1->f_type()->template f_derives() && a0 a_field == a1->f_as()))\ + XEMMAI__CODE__PRIMITIVE_CALL(a_operator(a1->f_type()->f_derives() && a0 a_field == a1->f_as()))\ } else {\ XEMMAI__CODE__PRIMITIVE_CALL(a_operator(false))\ } diff --git a/src/main.cc b/src/main.cc index c2fcadd6..9b065a01 100644 --- a/src/main.cc +++ b/src/main.cc @@ -169,7 +169,7 @@ class t_debugger : public xemmai::t_debugger template bool f_is(t_object* a_object) { - return a_object->f_type()->template f_derives(); + return a_object->f_type()->f_derives(); } template void f_print_sequence(t_object* a_value, size_t a_depth) diff --git a/test/callback/library.cc b/test/callback/library.cc index 466ce79c..41e0549c 100644 --- a/test/callback/library.cc +++ b/test/callback/library.cc @@ -14,20 +14,20 @@ struct t_type_of : t_derivable> using t_library = t_callback_library; template - struct t_as + struct t_cast : t_type::t_cast { - static T f_call(auto&& a_object) + static T f_as(auto&& a_object) { - return *f_object(std::forward(a_object))->template f_as(); + return *static_cast(a_object)->f_as(); } }; template - struct t_as + struct t_cast : t_type::t_cast { - static T* f_call(auto&& a_object) + static T* f_as(auto&& a_object) { - auto p = f_object(std::forward(a_object)); - return p ? p->template f_as() : nullptr; + auto p = static_cast(a_object); + return p ? p->f_as() : nullptr; } };