mirror of
https://github.com/BlackMATov/meta.hpp.git
synced 2025-12-13 19:18:01 +07:00
type_id is just a data pointer now
This commit is contained in:
@@ -1190,7 +1190,7 @@ namespace meta_hpp::detail
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t get_hash() const noexcept {
|
||||
return std::hash<T*>{}(ptr_);
|
||||
return hash_combiner{}(ptr_);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool operator==(const intrusive_ptr& other) const noexcept {
|
||||
@@ -1514,108 +1514,6 @@ namespace meta_hpp::detail
|
||||
}
|
||||
}
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < typename... Types >
|
||||
struct type_list {};
|
||||
|
||||
template < std::size_t I >
|
||||
using size_constant = std::integral_constant<std::size_t, I>;
|
||||
|
||||
template < std::size_t I >
|
||||
using index_constant = std::integral_constant<std::size_t, I>;
|
||||
}
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < std::size_t Index, typename TypeList >
|
||||
struct type_list_at;
|
||||
|
||||
template < std::size_t Index, typename... Types >
|
||||
struct type_list_at<Index, type_list<Types...>> {
|
||||
using type = std::tuple_element_t<Index, std::tuple<Types...>>;
|
||||
};
|
||||
|
||||
template < std::size_t Index, typename TypeList >
|
||||
using type_list_at_t = typename type_list_at<Index, TypeList>::type;
|
||||
}
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < typename TypeList >
|
||||
struct type_list_arity;
|
||||
|
||||
template < typename... Types >
|
||||
struct type_list_arity<type_list<Types...>> : size_constant<sizeof...(Types)> {};
|
||||
|
||||
template < typename TypeList >
|
||||
inline constexpr std::size_t type_list_arity_v = type_list_arity<TypeList>::value;
|
||||
}
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
class type_id final {
|
||||
public:
|
||||
template < typename T >
|
||||
explicit type_id(type_list<T>) noexcept
|
||||
: id_{type_to_id<T>()} {}
|
||||
|
||||
type_id(type_id&&) = default;
|
||||
type_id(const type_id&) = default;
|
||||
|
||||
type_id& operator=(type_id&&) = default;
|
||||
type_id& operator=(const type_id&) = default;
|
||||
|
||||
~type_id() = default;
|
||||
|
||||
void swap(type_id& other) noexcept {
|
||||
std::swap(id_, other.id_);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t get_hash() const noexcept {
|
||||
return std::hash<underlying_type>{}(id_);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool operator==(type_id other) const noexcept {
|
||||
return id_ == other.id_;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::strong_ordering operator<=>(type_id other) const noexcept {
|
||||
return id_ <=> other.id_;
|
||||
}
|
||||
|
||||
private:
|
||||
using underlying_type = std::uint32_t;
|
||||
underlying_type id_{};
|
||||
|
||||
private:
|
||||
[[nodiscard]] static underlying_type next() noexcept {
|
||||
static std::atomic<underlying_type> id{};
|
||||
return ++id;
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
[[nodiscard]] static underlying_type type_to_id() noexcept {
|
||||
static const underlying_type id{next()};
|
||||
return id;
|
||||
}
|
||||
};
|
||||
|
||||
inline void swap(type_id& l, type_id& r) noexcept {
|
||||
l.swap(r);
|
||||
}
|
||||
}
|
||||
|
||||
namespace std
|
||||
{
|
||||
template <>
|
||||
struct hash<meta_hpp::detail::type_id> {
|
||||
size_t operator()(const meta_hpp::detail::type_id& id) const noexcept {
|
||||
return id.get_hash();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < typename T >
|
||||
@@ -1694,6 +1592,44 @@ namespace meta_hpp::detail
|
||||
}
|
||||
}
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < typename... Types >
|
||||
struct type_list {};
|
||||
|
||||
template < std::size_t I >
|
||||
using size_constant = std::integral_constant<std::size_t, I>;
|
||||
|
||||
template < std::size_t I >
|
||||
using index_constant = std::integral_constant<std::size_t, I>;
|
||||
}
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < std::size_t Index, typename TypeList >
|
||||
struct type_list_at;
|
||||
|
||||
template < std::size_t Index, typename... Types >
|
||||
struct type_list_at<Index, type_list<Types...>> {
|
||||
using type = std::tuple_element_t<Index, std::tuple<Types...>>;
|
||||
};
|
||||
|
||||
template < std::size_t Index, typename TypeList >
|
||||
using type_list_at_t = typename type_list_at<Index, TypeList>::type;
|
||||
}
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < typename TypeList >
|
||||
struct type_list_arity;
|
||||
|
||||
template < typename... Types >
|
||||
struct type_list_arity<type_list<Types...>> : size_constant<sizeof...(Types)> {};
|
||||
|
||||
template < typename TypeList >
|
||||
inline constexpr std::size_t type_list_arity_v = type_list_arity<TypeList>::value;
|
||||
}
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
using detail::error_code;
|
||||
@@ -1707,7 +1643,6 @@ namespace meta_hpp
|
||||
using detail::select_non_const;
|
||||
using detail::select_overload;
|
||||
|
||||
using detail::type_id;
|
||||
using detail::type_kind;
|
||||
using detail::type_list;
|
||||
}
|
||||
@@ -2541,6 +2476,30 @@ namespace meta_hpp
|
||||
using detail::reference_flags;
|
||||
}
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
class type_id final {
|
||||
public:
|
||||
type_id() = default;
|
||||
|
||||
explicit type_id(std::uintptr_t id)
|
||||
: id_{id} {}
|
||||
|
||||
void swap(type_id& other) noexcept {
|
||||
std::swap(id_, other.id_);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t get_hash() const noexcept {
|
||||
return detail::hash_combiner{}(id_);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::strong_ordering operator<=>(const type_id& other) const = default;
|
||||
|
||||
private:
|
||||
std::uintptr_t id_{};
|
||||
};
|
||||
}
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
template < detail::type_family Type >
|
||||
@@ -2549,6 +2508,8 @@ namespace meta_hpp
|
||||
friend data_ptr detail::type_access<Type>(const Type&);
|
||||
|
||||
public:
|
||||
using id_type = type_id;
|
||||
|
||||
type_base() = default;
|
||||
|
||||
explicit type_base(data_ptr data)
|
||||
@@ -2568,8 +2529,9 @@ namespace meta_hpp
|
||||
return is_valid();
|
||||
}
|
||||
|
||||
[[nodiscard]] type_id get_id() const noexcept {
|
||||
return data_->id;
|
||||
[[nodiscard]] id_type get_id() const noexcept {
|
||||
// NOLINTNEXTLINE(*-reinterpret-cast)
|
||||
return id_type{reinterpret_cast<std::uintptr_t>(data_)};
|
||||
}
|
||||
|
||||
[[nodiscard]] type_kind get_kind() const noexcept {
|
||||
@@ -2895,12 +2857,12 @@ namespace meta_hpp
|
||||
namespace meta_hpp
|
||||
{
|
||||
template < detail::type_family L >
|
||||
[[nodiscard]] bool operator==(const L& l, type_id r) noexcept {
|
||||
[[nodiscard]] bool operator==(const L& l, const typename L::id_type& r) noexcept {
|
||||
return l.is_valid() && l.get_id() == r;
|
||||
}
|
||||
|
||||
template < detail::type_family L >
|
||||
[[nodiscard]] std::strong_ordering operator<=>(const L& l, type_id r) noexcept {
|
||||
[[nodiscard]] std::strong_ordering operator<=>(const L& l, const typename L::id_type& r) noexcept {
|
||||
return l.is_valid() ? l.get_id() <=> r : std::strong_ordering::less;
|
||||
}
|
||||
}
|
||||
@@ -2908,14 +2870,12 @@ namespace meta_hpp
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
struct type_data_base {
|
||||
const type_id id;
|
||||
const type_kind kind;
|
||||
|
||||
metadata_map metadata{};
|
||||
|
||||
explicit type_data_base(type_id nid, type_kind nkind)
|
||||
: id{nid}
|
||||
, kind{nkind} {}
|
||||
explicit type_data_base(type_kind nkind)
|
||||
: kind{nkind} {}
|
||||
|
||||
type_data_base(type_data_base&&) = delete;
|
||||
type_data_base(const type_data_base&) = delete;
|
||||
@@ -4024,13 +3984,13 @@ namespace meta_hpp
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
template < detail::state_family L, detail::index_family R >
|
||||
[[nodiscard]] bool operator==(const L& l, const R& r) noexcept {
|
||||
template < detail::state_family L >
|
||||
[[nodiscard]] bool operator==(const L& l, const typename L::index_type& r) noexcept {
|
||||
return l.is_valid() && l.get_index() == r;
|
||||
}
|
||||
|
||||
template < detail::state_family L, detail::index_family R >
|
||||
[[nodiscard]] std::strong_ordering operator<=>(const L& l, const R& r) noexcept {
|
||||
template < detail::state_family L >
|
||||
[[nodiscard]] std::strong_ordering operator<=>(const L& l, const typename L::index_type& r) noexcept {
|
||||
return l.is_valid() ? l.get_index() <=> r : std::strong_ordering::less;
|
||||
}
|
||||
}
|
||||
@@ -4213,16 +4173,6 @@ namespace meta_hpp::detail
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] any_type get_type_by_id(type_id id) const noexcept {
|
||||
const locker lock;
|
||||
|
||||
if ( auto iter{types_.find(id)}; iter != types_.end() ) {
|
||||
return *iter;
|
||||
}
|
||||
|
||||
return any_type{};
|
||||
}
|
||||
|
||||
public:
|
||||
template < typename T >
|
||||
[[nodiscard]] auto resolve_type() {
|
||||
@@ -6281,12 +6231,9 @@ namespace meta_hpp::detail
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < function_pointer_kind Function >
|
||||
struct function_tag {};
|
||||
|
||||
template < function_pointer_kind Function >
|
||||
function_type_data::function_type_data(type_list<Function>)
|
||||
: type_data_base{type_id{type_list<function_tag<Function>>{}}, type_kind::function_}
|
||||
: type_data_base{type_kind::function_}
|
||||
, flags{function_traits<Function>::make_flags()}
|
||||
, return_type{resolve_type<typename function_traits<Function>::return_type>()}
|
||||
, argument_types{resolve_types(typename function_traits<Function>::argument_types{})} {}
|
||||
@@ -6722,12 +6669,9 @@ namespace meta_hpp::detail
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < member_pointer_kind Member >
|
||||
struct member_tag {};
|
||||
|
||||
template < member_pointer_kind Member >
|
||||
member_type_data::member_type_data(type_list<Member>)
|
||||
: type_data_base{type_id{type_list<member_tag<Member>>{}}, type_kind::member_}
|
||||
: type_data_base{type_kind::member_}
|
||||
, flags{member_traits<Member>::make_flags()}
|
||||
, owner_type{resolve_type<typename member_traits<Member>::class_type>()}
|
||||
, value_type{resolve_type<typename member_traits<Member>::value_type>()} {}
|
||||
@@ -7049,12 +6993,9 @@ namespace meta_hpp
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < method_pointer_kind Method >
|
||||
struct method_tag {};
|
||||
|
||||
template < method_pointer_kind Method >
|
||||
method_type_data::method_type_data(type_list<Method>)
|
||||
: type_data_base{type_id{type_list<method_tag<Method>>{}}, type_kind::method_}
|
||||
: type_data_base{type_kind::method_}
|
||||
, flags{method_traits<Method>::make_flags()}
|
||||
, owner_type{resolve_type<typename method_traits<Method>::class_type>()}
|
||||
, return_type{resolve_type<typename method_traits<Method>::return_type>()}
|
||||
@@ -7518,12 +7459,9 @@ namespace meta_hpp
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < class_kind Class, typename... Args >
|
||||
struct constructor_tag {};
|
||||
|
||||
template < class_kind Class, typename... Args >
|
||||
constructor_type_data::constructor_type_data(type_list<Class>, type_list<Args...>)
|
||||
: type_data_base{type_id{type_list<constructor_tag<Class, Args...>>{}}, type_kind::constructor_}
|
||||
: type_data_base{type_kind::constructor_}
|
||||
, flags{constructor_traits<Class, Args...>::make_flags()}
|
||||
, owner_type{resolve_type<typename constructor_traits<Class, Args...>::class_type>()}
|
||||
, argument_types{resolve_types(typename constructor_traits<Class, Args...>::argument_types{})} {}
|
||||
@@ -7773,12 +7711,9 @@ namespace meta_hpp
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < class_kind Class >
|
||||
struct destructor_tag {};
|
||||
|
||||
template < class_kind Class >
|
||||
destructor_type_data::destructor_type_data(type_list<Class>)
|
||||
: type_data_base{type_id{type_list<destructor_tag<Class>>{}}, type_kind::destructor_}
|
||||
: type_data_base{type_kind::destructor_}
|
||||
, flags{destructor_traits<Class>::make_flags()}
|
||||
, owner_type{resolve_type<typename destructor_traits<Class>::class_type>()} {}
|
||||
}
|
||||
@@ -7928,12 +7863,9 @@ namespace meta_hpp
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < enum_kind Enum >
|
||||
struct enum_tag {};
|
||||
|
||||
template < enum_kind Enum >
|
||||
enum_type_data::enum_type_data(type_list<Enum>)
|
||||
: type_data_base{type_id{type_list<enum_tag<Enum>>{}}, type_kind::enum_}
|
||||
: type_data_base{type_kind::enum_}
|
||||
, flags{enum_traits<Enum>::make_flags()}
|
||||
, underlying_type{resolve_type<typename enum_traits<Enum>::underlying_type>()} {}
|
||||
}
|
||||
@@ -8022,12 +7954,9 @@ namespace meta_hpp
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < pointer_kind Pointer >
|
||||
struct pointer_tag {};
|
||||
|
||||
template < pointer_kind Pointer >
|
||||
pointer_type_data::pointer_type_data(type_list<Pointer>)
|
||||
: type_data_base{type_id{type_list<pointer_tag<Pointer>>{}}, type_kind::pointer_}
|
||||
: type_data_base{type_kind::pointer_}
|
||||
, flags{pointer_traits<Pointer>::make_flags()}
|
||||
, data_type{resolve_type<typename pointer_traits<Pointer>::data_type>()} {}
|
||||
}
|
||||
@@ -8227,12 +8156,9 @@ namespace meta_hpp
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < class_kind Class >
|
||||
struct class_tag {};
|
||||
|
||||
template < class_kind Class >
|
||||
class_type_data::class_type_data(type_list<Class>)
|
||||
: type_data_base{type_id{type_list<class_tag<Class>>{}}, type_kind::class_}
|
||||
: type_data_base{type_kind::class_}
|
||||
, flags{class_traits<Class>::make_flags()}
|
||||
, size{class_traits<Class>::size}
|
||||
, align{class_traits<Class>::align}
|
||||
@@ -8943,12 +8869,9 @@ namespace meta_hpp
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < array_kind Array >
|
||||
struct array_tag {};
|
||||
|
||||
template < array_kind Array >
|
||||
array_type_data::array_type_data(type_list<Array>)
|
||||
: type_data_base{type_id{type_list<array_tag<Array>>{}}, type_kind::array_}
|
||||
: type_data_base{type_kind::array_}
|
||||
, flags{array_traits<Array>::make_flags()}
|
||||
, extent{array_traits<Array>::extent}
|
||||
, data_type{resolve_type<typename array_traits<Array>::data_type>()} {}
|
||||
@@ -8971,22 +8894,16 @@ namespace meta_hpp
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < nullptr_kind Nullptr >
|
||||
struct nullptr_tag {};
|
||||
|
||||
template < nullptr_kind Nullptr >
|
||||
nullptr_type_data::nullptr_type_data(type_list<Nullptr>)
|
||||
: type_data_base{type_id{type_list<nullptr_tag<Nullptr>>{}}, type_kind::nullptr_} {}
|
||||
: type_data_base{type_kind::nullptr_} {}
|
||||
}
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < number_kind Number >
|
||||
struct number_tag {};
|
||||
|
||||
template < number_kind Number >
|
||||
number_type_data::number_type_data(type_list<Number>)
|
||||
: type_data_base{type_id{type_list<number_tag<Number>>{}}, type_kind::number_}
|
||||
: type_data_base{type_kind::number_}
|
||||
, flags{number_traits<Number>::make_flags()}
|
||||
, size{number_traits<Number>::size}
|
||||
, align{number_traits<Number>::align} {}
|
||||
@@ -9009,12 +8926,9 @@ namespace meta_hpp
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < reference_kind Reference >
|
||||
struct reference_tag {};
|
||||
|
||||
template < reference_kind Reference >
|
||||
reference_type_data::reference_type_data(type_list<Reference>)
|
||||
: type_data_base{type_id{type_list<reference_tag<Reference>>{}}, type_kind::reference_}
|
||||
: type_data_base{type_kind::reference_}
|
||||
, flags{reference_traits<Reference>::make_flags()}
|
||||
, data_type{resolve_type<typename reference_traits<Reference>::data_type>()} {}
|
||||
}
|
||||
@@ -9032,12 +8946,9 @@ namespace meta_hpp
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < void_kind Void >
|
||||
struct void_tag {};
|
||||
|
||||
template < void_kind Void >
|
||||
void_type_data::void_type_data(type_list<Void>)
|
||||
: type_data_base{type_id{type_list<void_tag<Void>>{}}, type_kind::void_} {}
|
||||
: type_data_base{type_kind::void_} {}
|
||||
}
|
||||
|
||||
namespace meta_hpp::detail
|
||||
@@ -9867,7 +9778,7 @@ namespace meta_hpp
|
||||
}
|
||||
|
||||
inline std::size_t uerror::get_hash() const noexcept {
|
||||
return std::hash<error_code>{}(error_);
|
||||
return detail::hash_combiner{}(error_);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* 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-2023, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#include <meta.hpp/meta_all.hpp>
|
||||
#include <doctest/doctest.h>
|
||||
|
||||
TEST_CASE("meta/meta_base/type_id") {
|
||||
namespace meta = meta_hpp;
|
||||
using meta::detail::type_id;
|
||||
using meta::detail::type_list;
|
||||
|
||||
static_assert(std::is_nothrow_constructible_v<type_id, type_list<int>>);
|
||||
static_assert(std::is_nothrow_copy_constructible_v<type_id>);
|
||||
static_assert(std::is_nothrow_move_constructible_v<type_id>);
|
||||
static_assert(std::is_nothrow_copy_assignable_v<type_id>);
|
||||
static_assert(std::is_nothrow_move_assignable_v<type_id>);
|
||||
|
||||
SUBCASE("ctor") {
|
||||
type_id int1_id{type_list<int>{}};
|
||||
type_id int2_id{type_list<int>{}};
|
||||
type_id float_id{type_list<float>{}};
|
||||
|
||||
CHECK(int1_id == int2_id);
|
||||
CHECK_FALSE(int2_id == float_id);
|
||||
|
||||
CHECK((int2_id < float_id || float_id < int2_id));
|
||||
CHECK_FALSE((int1_id < int2_id || int2_id < int1_id));
|
||||
}
|
||||
|
||||
SUBCASE("get_hash") {
|
||||
type_id int1_id{type_list<int>{}};
|
||||
type_id int2_id{type_list<int>{}};
|
||||
type_id float_id{type_list<float>{}};
|
||||
|
||||
CHECK(int1_id.get_hash() == int2_id.get_hash());
|
||||
CHECK_FALSE(int2_id.get_hash() == float_id.get_hash());
|
||||
}
|
||||
|
||||
SUBCASE("swap") {
|
||||
const type_id int_id{type_list<int>{}};
|
||||
const type_id float_id{type_list<float>{}};
|
||||
|
||||
type_id id1{type_list<int>{}};
|
||||
type_id id2{type_list<float>{}};
|
||||
|
||||
id1.swap(id2);
|
||||
CHECK(id1 == float_id);
|
||||
CHECK(id2 == int_id);
|
||||
|
||||
swap(id1, id2);
|
||||
CHECK(id1 == int_id);
|
||||
CHECK(id2 == float_id);
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* 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-2023, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#include <meta.hpp/meta_all.hpp>
|
||||
#include <doctest/doctest.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
struct A {};
|
||||
struct B {};
|
||||
}
|
||||
|
||||
TEST_CASE("meta/meta_issues/random/3") {
|
||||
namespace meta = meta_hpp;
|
||||
|
||||
meta::detail::type_registry& registry{meta::detail::type_registry::instance()};
|
||||
|
||||
meta::class_type a_type = registry.resolve_class_type<A>();
|
||||
meta::class_type b_type = registry.resolve_class_type<B>();
|
||||
|
||||
CHECK(registry.get_type_by_id(a_type.get_id()) == a_type);
|
||||
CHECK(registry.get_type_by_id(b_type.get_id()) == b_type);
|
||||
}
|
||||
@@ -27,7 +27,6 @@
|
||||
#include "meta_base/overloaded.hpp"
|
||||
#include "meta_base/select_overload.hpp"
|
||||
#include "meta_base/to_underlying.hpp"
|
||||
#include "meta_base/type_id.hpp"
|
||||
#include "meta_base/type_kinds.hpp"
|
||||
#include "meta_base/type_list.hpp"
|
||||
|
||||
@@ -44,7 +43,6 @@ namespace meta_hpp
|
||||
using detail::select_non_const;
|
||||
using detail::select_overload;
|
||||
|
||||
using detail::type_id;
|
||||
using detail::type_kind;
|
||||
using detail::type_list;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "base.hpp"
|
||||
#include "hash_combiner.hpp"
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
@@ -147,7 +148,7 @@ namespace meta_hpp::detail
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t get_hash() const noexcept {
|
||||
return std::hash<T*>{}(ptr_);
|
||||
return hash_combiner{}(ptr_);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool operator==(const intrusive_ptr& other) const noexcept {
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* 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-2023, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "base.hpp"
|
||||
#include "type_list.hpp"
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
class type_id final {
|
||||
public:
|
||||
template < typename T >
|
||||
explicit type_id(type_list<T>) noexcept
|
||||
: id_{type_to_id<T>()} {}
|
||||
|
||||
type_id(type_id&&) = default;
|
||||
type_id(const type_id&) = default;
|
||||
|
||||
type_id& operator=(type_id&&) = default;
|
||||
type_id& operator=(const type_id&) = default;
|
||||
|
||||
~type_id() = default;
|
||||
|
||||
void swap(type_id& other) noexcept {
|
||||
std::swap(id_, other.id_);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t get_hash() const noexcept {
|
||||
return std::hash<underlying_type>{}(id_);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool operator==(type_id other) const noexcept {
|
||||
return id_ == other.id_;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::strong_ordering operator<=>(type_id other) const noexcept {
|
||||
return id_ <=> other.id_;
|
||||
}
|
||||
|
||||
private:
|
||||
using underlying_type = std::uint32_t;
|
||||
underlying_type id_{};
|
||||
|
||||
private:
|
||||
[[nodiscard]] static underlying_type next() noexcept {
|
||||
static std::atomic<underlying_type> id{};
|
||||
return ++id;
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
[[nodiscard]] static underlying_type type_to_id() noexcept {
|
||||
static const underlying_type id{next()};
|
||||
return id;
|
||||
}
|
||||
};
|
||||
|
||||
inline void swap(type_id& l, type_id& r) noexcept {
|
||||
l.swap(r);
|
||||
}
|
||||
}
|
||||
|
||||
namespace std
|
||||
{
|
||||
template <>
|
||||
struct hash<meta_hpp::detail::type_id> {
|
||||
size_t operator()(const meta_hpp::detail::type_id& id) const noexcept {
|
||||
return id.get_hash();
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -45,16 +45,6 @@ namespace meta_hpp::detail
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] any_type get_type_by_id(type_id id) const noexcept {
|
||||
const locker lock;
|
||||
|
||||
if ( auto iter{types_.find(id)}; iter != types_.end() ) {
|
||||
return *iter;
|
||||
}
|
||||
|
||||
return any_type{};
|
||||
}
|
||||
|
||||
public:
|
||||
template < typename T >
|
||||
[[nodiscard]] auto resolve_type() {
|
||||
|
||||
@@ -414,13 +414,13 @@ namespace meta_hpp
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
template < detail::state_family L, detail::index_family R >
|
||||
[[nodiscard]] bool operator==(const L& l, const R& r) noexcept {
|
||||
template < detail::state_family L >
|
||||
[[nodiscard]] bool operator==(const L& l, const typename L::index_type& r) noexcept {
|
||||
return l.is_valid() && l.get_index() == r;
|
||||
}
|
||||
|
||||
template < detail::state_family L, detail::index_family R >
|
||||
[[nodiscard]] std::strong_ordering operator<=>(const L& l, const R& r) noexcept {
|
||||
template < detail::state_family L >
|
||||
[[nodiscard]] std::strong_ordering operator<=>(const L& l, const typename L::index_type& r) noexcept {
|
||||
return l.is_valid() ? l.get_index() <=> r : std::strong_ordering::less;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,30 @@ namespace meta_hpp
|
||||
using detail::reference_flags;
|
||||
}
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
class type_id final {
|
||||
public:
|
||||
type_id() = default;
|
||||
|
||||
explicit type_id(std::uintptr_t id)
|
||||
: id_{id} {}
|
||||
|
||||
void swap(type_id& other) noexcept {
|
||||
std::swap(id_, other.id_);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t get_hash() const noexcept {
|
||||
return detail::hash_combiner{}(id_);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::strong_ordering operator<=>(const type_id& other) const = default;
|
||||
|
||||
private:
|
||||
std::uintptr_t id_{};
|
||||
};
|
||||
}
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
template < detail::type_family Type >
|
||||
@@ -54,6 +78,8 @@ namespace meta_hpp
|
||||
friend data_ptr detail::type_access<Type>(const Type&);
|
||||
|
||||
public:
|
||||
using id_type = type_id;
|
||||
|
||||
type_base() = default;
|
||||
|
||||
explicit type_base(data_ptr data)
|
||||
@@ -73,8 +99,9 @@ namespace meta_hpp
|
||||
return is_valid();
|
||||
}
|
||||
|
||||
[[nodiscard]] type_id get_id() const noexcept {
|
||||
return data_->id;
|
||||
[[nodiscard]] id_type get_id() const noexcept {
|
||||
// NOLINTNEXTLINE(*-reinterpret-cast)
|
||||
return id_type{reinterpret_cast<std::uintptr_t>(data_)};
|
||||
}
|
||||
|
||||
[[nodiscard]] type_kind get_kind() const noexcept {
|
||||
@@ -400,12 +427,12 @@ namespace meta_hpp
|
||||
namespace meta_hpp
|
||||
{
|
||||
template < detail::type_family L >
|
||||
[[nodiscard]] bool operator==(const L& l, type_id r) noexcept {
|
||||
[[nodiscard]] bool operator==(const L& l, const typename L::id_type& r) noexcept {
|
||||
return l.is_valid() && l.get_id() == r;
|
||||
}
|
||||
|
||||
template < detail::type_family L >
|
||||
[[nodiscard]] std::strong_ordering operator<=>(const L& l, type_id r) noexcept {
|
||||
[[nodiscard]] std::strong_ordering operator<=>(const L& l, const typename L::id_type& r) noexcept {
|
||||
return l.is_valid() ? l.get_id() <=> r : std::strong_ordering::less;
|
||||
}
|
||||
}
|
||||
@@ -413,14 +440,12 @@ namespace meta_hpp
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
struct type_data_base {
|
||||
const type_id id;
|
||||
const type_kind kind;
|
||||
|
||||
metadata_map metadata{};
|
||||
|
||||
explicit type_data_base(type_id nid, type_kind nkind)
|
||||
: id{nid}
|
||||
, kind{nkind} {}
|
||||
explicit type_data_base(type_kind nkind)
|
||||
: kind{nkind} {}
|
||||
|
||||
type_data_base(type_data_base&&) = delete;
|
||||
type_data_base(const type_data_base&) = delete;
|
||||
|
||||
@@ -14,12 +14,9 @@
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < array_kind Array >
|
||||
struct array_tag {};
|
||||
|
||||
template < array_kind Array >
|
||||
array_type_data::array_type_data(type_list<Array>)
|
||||
: type_data_base{type_id{type_list<array_tag<Array>>{}}, type_kind::array_}
|
||||
: type_data_base{type_kind::array_}
|
||||
, flags{array_traits<Array>::make_flags()}
|
||||
, extent{array_traits<Array>::extent}
|
||||
, data_type{resolve_type<typename array_traits<Array>::data_type>()} {}
|
||||
|
||||
@@ -20,12 +20,9 @@
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < class_kind Class >
|
||||
struct class_tag {};
|
||||
|
||||
template < class_kind Class >
|
||||
class_type_data::class_type_data(type_list<Class>)
|
||||
: type_data_base{type_id{type_list<class_tag<Class>>{}}, type_kind::class_}
|
||||
: type_data_base{type_kind::class_}
|
||||
, flags{class_traits<Class>::make_flags()}
|
||||
, size{class_traits<Class>::size}
|
||||
, align{class_traits<Class>::align}
|
||||
|
||||
@@ -14,12 +14,9 @@
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < class_kind Class, typename... Args >
|
||||
struct constructor_tag {};
|
||||
|
||||
template < class_kind Class, typename... Args >
|
||||
constructor_type_data::constructor_type_data(type_list<Class>, type_list<Args...>)
|
||||
: type_data_base{type_id{type_list<constructor_tag<Class, Args...>>{}}, type_kind::constructor_}
|
||||
: type_data_base{type_kind::constructor_}
|
||||
, flags{constructor_traits<Class, Args...>::make_flags()}
|
||||
, owner_type{resolve_type<typename constructor_traits<Class, Args...>::class_type>()}
|
||||
, argument_types{resolve_types(typename constructor_traits<Class, Args...>::argument_types{})} {}
|
||||
|
||||
@@ -14,12 +14,9 @@
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < class_kind Class >
|
||||
struct destructor_tag {};
|
||||
|
||||
template < class_kind Class >
|
||||
destructor_type_data::destructor_type_data(type_list<Class>)
|
||||
: type_data_base{type_id{type_list<destructor_tag<Class>>{}}, type_kind::destructor_}
|
||||
: type_data_base{type_kind::destructor_}
|
||||
, flags{destructor_traits<Class>::make_flags()}
|
||||
, owner_type{resolve_type<typename destructor_traits<Class>::class_type>()} {}
|
||||
}
|
||||
|
||||
@@ -17,12 +17,9 @@
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < enum_kind Enum >
|
||||
struct enum_tag {};
|
||||
|
||||
template < enum_kind Enum >
|
||||
enum_type_data::enum_type_data(type_list<Enum>)
|
||||
: type_data_base{type_id{type_list<enum_tag<Enum>>{}}, type_kind::enum_}
|
||||
: type_data_base{type_kind::enum_}
|
||||
, flags{enum_traits<Enum>::make_flags()}
|
||||
, underlying_type{resolve_type<typename enum_traits<Enum>::underlying_type>()} {}
|
||||
}
|
||||
|
||||
@@ -14,12 +14,9 @@
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < function_pointer_kind Function >
|
||||
struct function_tag {};
|
||||
|
||||
template < function_pointer_kind Function >
|
||||
function_type_data::function_type_data(type_list<Function>)
|
||||
: type_data_base{type_id{type_list<function_tag<Function>>{}}, type_kind::function_}
|
||||
: type_data_base{type_kind::function_}
|
||||
, flags{function_traits<Function>::make_flags()}
|
||||
, return_type{resolve_type<typename function_traits<Function>::return_type>()}
|
||||
, argument_types{resolve_types(typename function_traits<Function>::argument_types{})} {}
|
||||
|
||||
@@ -14,12 +14,9 @@
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < member_pointer_kind Member >
|
||||
struct member_tag {};
|
||||
|
||||
template < member_pointer_kind Member >
|
||||
member_type_data::member_type_data(type_list<Member>)
|
||||
: type_data_base{type_id{type_list<member_tag<Member>>{}}, type_kind::member_}
|
||||
: type_data_base{type_kind::member_}
|
||||
, flags{member_traits<Member>::make_flags()}
|
||||
, owner_type{resolve_type<typename member_traits<Member>::class_type>()}
|
||||
, value_type{resolve_type<typename member_traits<Member>::value_type>()} {}
|
||||
|
||||
@@ -14,12 +14,9 @@
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < method_pointer_kind Method >
|
||||
struct method_tag {};
|
||||
|
||||
template < method_pointer_kind Method >
|
||||
method_type_data::method_type_data(type_list<Method>)
|
||||
: type_data_base{type_id{type_list<method_tag<Method>>{}}, type_kind::method_}
|
||||
: type_data_base{type_kind::method_}
|
||||
, flags{method_traits<Method>::make_flags()}
|
||||
, owner_type{resolve_type<typename method_traits<Method>::class_type>()}
|
||||
, return_type{resolve_type<typename method_traits<Method>::return_type>()}
|
||||
|
||||
@@ -12,10 +12,7 @@
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < nullptr_kind Nullptr >
|
||||
struct nullptr_tag {};
|
||||
|
||||
template < nullptr_kind Nullptr >
|
||||
nullptr_type_data::nullptr_type_data(type_list<Nullptr>)
|
||||
: type_data_base{type_id{type_list<nullptr_tag<Nullptr>>{}}, type_kind::nullptr_} {}
|
||||
: type_data_base{type_kind::nullptr_} {}
|
||||
}
|
||||
|
||||
@@ -14,12 +14,9 @@
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < number_kind Number >
|
||||
struct number_tag {};
|
||||
|
||||
template < number_kind Number >
|
||||
number_type_data::number_type_data(type_list<Number>)
|
||||
: type_data_base{type_id{type_list<number_tag<Number>>{}}, type_kind::number_}
|
||||
: type_data_base{type_kind::number_}
|
||||
, flags{number_traits<Number>::make_flags()}
|
||||
, size{number_traits<Number>::size}
|
||||
, align{number_traits<Number>::align} {}
|
||||
|
||||
@@ -14,12 +14,9 @@
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < pointer_kind Pointer >
|
||||
struct pointer_tag {};
|
||||
|
||||
template < pointer_kind Pointer >
|
||||
pointer_type_data::pointer_type_data(type_list<Pointer>)
|
||||
: type_data_base{type_id{type_list<pointer_tag<Pointer>>{}}, type_kind::pointer_}
|
||||
: type_data_base{type_kind::pointer_}
|
||||
, flags{pointer_traits<Pointer>::make_flags()}
|
||||
, data_type{resolve_type<typename pointer_traits<Pointer>::data_type>()} {}
|
||||
}
|
||||
|
||||
@@ -14,12 +14,9 @@
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < reference_kind Reference >
|
||||
struct reference_tag {};
|
||||
|
||||
template < reference_kind Reference >
|
||||
reference_type_data::reference_type_data(type_list<Reference>)
|
||||
: type_data_base{type_id{type_list<reference_tag<Reference>>{}}, type_kind::reference_}
|
||||
: type_data_base{type_kind::reference_}
|
||||
, flags{reference_traits<Reference>::make_flags()}
|
||||
, data_type{resolve_type<typename reference_traits<Reference>::data_type>()} {}
|
||||
}
|
||||
|
||||
@@ -12,10 +12,7 @@
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < void_kind Void >
|
||||
struct void_tag {};
|
||||
|
||||
template < void_kind Void >
|
||||
void_type_data::void_type_data(type_list<Void>)
|
||||
: type_data_base{type_id{type_list<void_tag<Void>>{}}, type_kind::void_} {}
|
||||
: type_data_base{type_kind::void_} {}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace meta_hpp
|
||||
}
|
||||
|
||||
inline std::size_t uerror::get_hash() const noexcept {
|
||||
return std::hash<error_code>{}(error_);
|
||||
return detail::hash_combiner{}(error_);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user