Skip to content

Commit

Permalink
Refactored f_as/f_is details.
Browse files Browse the repository at this point in the history
  • Loading branch information
shin1m committed Jun 8, 2023
1 parent 8b2fdd8 commit 7ae6e24
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 125 deletions.
12 changes: 4 additions & 8 deletions include/xemmai/boolean.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ template<>
struct t_type_of<bool> : t_uninstantiatable<t_bears<bool>>
{
template<typename T>
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<typename T>
struct t_is
{
static bool f_call(auto&& a_object)
static bool f_is(auto&& a_object)
{
return reinterpret_cast<uintptr_t>(f_object(std::forward<decltype(a_object)>(a_object))) == e_tag__BOOLEAN;
return reinterpret_cast<uintptr_t>(static_cast<t_object*>(a_object)) == e_tag__BOOLEAN;
}
};

Expand Down
22 changes: 9 additions & 13 deletions include/xemmai/float.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,30 @@ template<>
struct t_type_of<double> : t_derivable<t_bears<double>, t_derived_primitive<double>>
{
template<typename T>
struct t_as
struct t_cast
{
using t_type = typename t_fundamental<T>::t_type;

static t_type f_call(auto&& a_object)
static t_type f_as(auto&& a_object)
{
auto p = f_object(std::forward<decltype(a_object)>(a_object));
auto p = static_cast<t_object*>(a_object);
switch (reinterpret_cast<uintptr_t>(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<double>();
return p->f_as<double>();
}
}
static t_type f_call(t_object* a_object)
static t_type f_as(t_object* a_object)
{
return a_object->f_as<double>();
}
};
template<typename T>
struct t_is
{
static bool f_call(auto&& a_object)
static bool f_is(auto&& a_object)
{
auto p = f_object(std::forward<decltype(a_object)>(a_object));
if (!std::is_same_v<typename t_fundamental<T>::t_type, double>) return reinterpret_cast<uintptr_t>(p) >= e_tag__OBJECT && p->f_type()->template f_derives<typename t_fundamental<T>::t_type>();
auto p = static_cast<t_object*>(a_object);
if (!std::is_same_v<typename t_fundamental<T>::t_type, double>) return reinterpret_cast<uintptr_t>(p) >= e_tag__OBJECT && p->f_type()->f_derives<typename t_fundamental<T>::t_type>();
switch (reinterpret_cast<uintptr_t>(p)) {
case e_tag__NULL:
case e_tag__BOOLEAN:
Expand All @@ -53,7 +49,7 @@ struct t_type_of<double> : t_derivable<t_bears<double>, t_derived_primitive<doub
case e_tag__FLOAT:
return true;
default:
return p->f_type()->template f_derives<double>();
return p->f_type()->f_derives<double>();
}
}
};
Expand Down
18 changes: 7 additions & 11 deletions include/xemmai/integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,22 @@ template<>
struct t_type_of<intptr_t> : t_derivable<t_bears<intptr_t>, t_derived_primitive<intptr_t>>
{
template<typename T>
struct t_as
struct t_cast
{
using t_type = typename t_fundamental<T>::t_type;

static t_type f_call(auto&& a_object)
static t_type f_as(auto&& a_object)
{
return static_cast<t_type>(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<t_type>(a_object->f_as<intptr_t>());
}
};
template<typename T>
struct t_is
{
static bool f_call(auto&& a_object)
static bool f_is(auto&& a_object)
{
auto p = f_object(std::forward<decltype(a_object)>(a_object));
if (!std::is_same_v<typename t_fundamental<T>::t_type, intptr_t>) return reinterpret_cast<uintptr_t>(p) >= e_tag__OBJECT && p->f_type()->template f_derives<typename t_fundamental<T>::t_type>();
auto p = static_cast<t_object*>(a_object);
if (!std::is_same_v<typename t_fundamental<T>::t_type, intptr_t>) return reinterpret_cast<uintptr_t>(p) >= e_tag__OBJECT && p->f_type()->f_derives<typename t_fundamental<T>::t_type>();
switch (reinterpret_cast<uintptr_t>(p)) {
case e_tag__INTEGER:
return true;
Expand All @@ -47,7 +43,7 @@ struct t_type_of<intptr_t> : t_derivable<t_bears<intptr_t>, t_derived_primitive<
case e_tag__FLOAT:
return false;
default:
return p->f_type()->template f_derives<intptr_t>();
return p->f_type()->f_derives<intptr_t>();
}
}
};
Expand Down
43 changes: 42 additions & 1 deletion include/xemmai/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,51 @@ inline bool t_value<T_tag>::f_has(t_object* a_key, size_t& a_index) const
template<typename T>
inline void t_slot_of<T>::f_construct(t_object* a_value)
{
v_p = &a_value->template f_as<T>();
v_p = &a_value->f_as<T>();
v_slot = a_value;
}

template<typename T>
struct t_type::t_cast
{
static T f_as(auto&& a_object)
{
return static_cast<t_object*>(a_object)->f_as<typename t_fundamental<T>::t_type>();
}
static bool f_is(auto&& a_object)
{
if constexpr (std::is_same_v<typename t_fundamental<T>::t_type, t_object>) return true;
auto p = static_cast<t_object*>(a_object);
return reinterpret_cast<uintptr_t>(p) >= e_tag__OBJECT && p->f_type()->f_derives<typename t_fundamental<T>::t_type>();
}
};

template<typename T>
struct t_type::t_cast<T*>
{
static_assert(!std::is_same_v<T, t_object>);

static T* f_as(auto&& a_object)
{
auto p = static_cast<t_object*>(a_object);
return p ? &p->f_as<T>() : nullptr;
}
static bool f_is(auto&& a_object)
{
auto p = static_cast<t_object*>(a_object);
switch (reinterpret_cast<uintptr_t>(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<typename t_fundamental<T>::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)
{
}
Expand Down
26 changes: 1 addition & 25 deletions include/xemmai/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,6 @@ struct t_fundamental<std::wstring_view>
template<>
struct t_type_of<t_string> : t_holds<t_string>
{
template<typename T>
struct t_as
{
static T f_call(auto&& a_object)
{
return f_object(std::forward<decltype(a_object)>(a_object))->template f_as<t_string>();
}
};
template<typename T>
struct t_as<T*>
{
static_assert(std::is_same_v<std::decay_t<T>, t_string>);

static T* f_call(auto&& a_object)
{
auto p = f_object(std::forward<decltype(a_object)>(a_object));
return p ? &p->template f_as<t_string>() : 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)
{
Expand Down Expand Up @@ -153,12 +133,8 @@ struct t_type_of<t_string> : t_holds<t_string>
};

template<>
struct t_type_of<t_string>::t_as<std::wstring_view&&>
struct t_type::t_cast<std::wstring_view&&> : t_type::t_cast<std::wstring_view>
{
static std::wstring_view f_call(auto&& a_object)
{
return f_object(std::forward<decltype(a_object)>(a_object))->template f_as<t_string>();
}
};

}
Expand Down
65 changes: 8 additions & 57 deletions include/xemmai/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T>
const void* v__type_id;
template<typename> const void* v__type_id;
template<typename T>
constexpr t_type_id f_type_id()
{
Expand Down Expand Up @@ -93,65 +92,17 @@ struct t_fields
template<>
struct t_type_of<t_object>
{
static t_object* f_object(auto&& a_value)
{
return a_value;
}
template<typename T>
struct t_as
{
static T f_call(auto&& a_object)
{
return f_object(std::forward<decltype(a_object)>(a_object))->template f_as<typename t_fundamental<T>::t_type>();
}
};
template<typename T>
struct t_as<T*>
{
static_assert(!std::is_same_v<T, t_object>);

static T* f_call(auto&& a_object)
{
auto p = f_object(std::forward<decltype(a_object)>(a_object));
return p ? &p->template f_as<T>() : nullptr;
}
};
template<typename> struct t_cast;
template<typename T_tag>
struct t_as<const t_value<T_tag>&>
struct t_cast<const t_value<T_tag>&>
{
static const t_value<T_tag>& f_call(auto&& a_object)
static const t_value<T_tag>& f_as(auto&& a_object)
{
return a_object;
}
};
template<typename T>
struct t_is
{
static bool f_call(auto&& a_object)
{
if (std::is_same_v<typename t_fundamental<T>::t_type, t_object>) return true;
auto p = f_object(std::forward<decltype(a_object)>(a_object));
return reinterpret_cast<uintptr_t>(p) >= e_tag__OBJECT && p->f_type()->template f_derives<typename t_fundamental<T>::t_type>();
}
};
template<typename T>
struct t_is<T*>
{
static_assert(!std::is_same_v<T, t_object>);

static bool f_call(auto&& a_object)
static bool f_is(auto&& a_object)
{
auto p = f_object(std::forward<decltype(a_object)>(a_object));
switch (reinterpret_cast<uintptr_t>(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<typename t_fundamental<T>::t_type>();
}
return true;
}
};
using t_library = t_global;
Expand Down Expand Up @@ -356,13 +307,13 @@ inline bool t_type_of<t_object>::f_derives<t_object>() const
template<typename T>
inline decltype(auto) f_as(auto&& a_object)
{
return t_type_of<typename t_fundamental<T>::t_type>::template t_as<T>::f_call(std::forward<decltype(a_object)>(a_object));
return t_type_of<typename t_fundamental<T>::t_type>::template t_cast<T>::f_as(std::forward<decltype(a_object)>(a_object));
}

template<typename T>
inline bool f_is(auto&& a_object)
{
return t_type_of<typename t_fundamental<T>::t_type>::template t_is<T>::f_call(std::forward<decltype(a_object)>(a_object));
return t_type_of<typename t_fundamental<T>::t_type>::template t_cast<T>::f_is(std::forward<decltype(a_object)>(a_object));
}

XEMMAI__PORTABLE__EXPORT void f_throw_type_error [[noreturn]] (const std::type_info& a_type, const wchar_t* a_name);
Expand Down
4 changes: 2 additions & 2 deletions src/code_operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<a_type>()) {\
if (a1->f_type()->f_derives<a_type>()) {\
XEMMAI__CODE__PRIMITIVE_CALL(a_cast(a0 a_field) a_operator a1->f_as<a_type>())\
} else {\
goto label__THROW_NOT_SUPPORTED;\
Expand All @@ -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<a_type>() && a0 a_field == a1->f_as<a_type>()))\
XEMMAI__CODE__PRIMITIVE_CALL(a_operator(a1->f_type()->f_derives<a_type>() && a0 a_field == a1->f_as<a_type>()))\
} else {\
XEMMAI__CODE__PRIMITIVE_CALL(a_operator(false))\
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class t_debugger : public xemmai::t_debugger
template<typename T>
bool f_is(t_object* a_object)
{
return a_object->f_type()->template f_derives<T>();
return a_object->f_type()->f_derives<T>();
}
template<typename T>
void f_print_sequence(t_object* a_value, size_t a_depth)
Expand Down
14 changes: 7 additions & 7 deletions test/callback/library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ struct t_type_of<t_client> : t_derivable<t_bears<t_client>>
using t_library = t_callback_library;

template<typename T>
struct t_as
struct t_cast : t_type::t_cast<T>
{
static T f_call(auto&& a_object)
static T f_as(auto&& a_object)
{
return *f_object(std::forward<decltype(a_object)>(a_object))->template f_as<t_client*>();
return *static_cast<t_object*>(a_object)->f_as<t_client*>();
}
};
template<typename T>
struct t_as<T*>
struct t_cast<T*> : t_type::t_cast<T*>
{
static T* f_call(auto&& a_object)
static T* f_as(auto&& a_object)
{
auto p = f_object(std::forward<decltype(a_object)>(a_object));
return p ? p->template f_as<t_client*>() : nullptr;
auto p = static_cast<t_object*>(a_object);
return p ? p->f_as<t_client*>() : nullptr;
}
};

Expand Down

0 comments on commit 7ae6e24

Please sign in to comment.