NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
reflect/any.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_REFLECT_ANY_HPP__
2#define NEFORCE_CORE_REFLECT_ANY_HPP__
3
11
15NEFORCE_BEGIN_NAMESPACE__
16NEFORCE_BEGIN_REFLECT__
17
23
24using type_id = size_t;
25
26
34template <typename T>
35struct type_name {
36 static constexpr string_view value = "unknown";
37};
38
42template <typename T>
43NEFORCE_INLINE17 constexpr string_view type_name_v = type_name<T>::value;
44
46#define __NEFORCE_SPECIALIZE_TYPE_NAME(T) \
47 template <> \
48 struct type_name<T> { \
49 static constexpr string_view value = #T; \
50 };
51
52NEFORCE_MACRO_RANGE_ARITHMETIC(__NEFORCE_SPECIALIZE_TYPE_NAME)
53#undef __NEFORCE_SPECIALIZE_TYPE_NAME
55
62class meta_any {
63private:
64 struct concepts {
65 virtual ~concepts() = default;
66 NEFORCE_NODISCARD virtual unique_ptr<concepts> clone() const = 0;
67 NEFORCE_NODISCARD virtual reflect::type_id type_id() const noexcept = 0;
68 };
69
70 template <typename T>
71 struct model final : concepts {
72 T value_;
73
74 explicit model(T value) :
75 value_(_NEFORCE move(value)) {}
76
77 NEFORCE_NODISCARD unique_ptr<concepts> clone() const override { return _NEFORCE make_unique<model<T>>(value_); }
78
79 NEFORCE_NODISCARD reflect::type_id type_id() const noexcept override { return type_name_v<T>.to_hash(); }
80 };
81
82 unique_ptr<concepts> storage_{nullptr};
83
84public:
88 meta_any() noexcept = default;
89
95 template <typename T, typename = enable_if_t<!is_same_v<decay_t<T>, meta_any>>>
96 explicit meta_any(T&& value) :
97 storage_(_NEFORCE make_unique<model<decay_t<T>>>(_NEFORCE forward<T>(value))) {}
98
99 meta_any(meta_any&&) noexcept = default;
100 meta_any& operator=(meta_any&&) noexcept = default;
101
106 meta_any(const meta_any& other) {
107 if (other.storage_) {
108 storage_ = other.storage_->clone();
109 }
110 }
111
117 meta_any& operator=(const meta_any& other) {
118 if (addressof(other) == this) {
119 return *this;
120 }
121
122 if (other.storage_) {
123 storage_ = other.storage_->clone();
124 } else {
125 storage_.reset();
126 }
127
128 return *this;
129 }
130
135 NEFORCE_NODISCARD reflect::type_id type_id() const noexcept { return storage_ ? storage_->type_id() : 0; }
136
141 NEFORCE_NODISCARD bool has_value() const noexcept { return !!storage_; }
142
147 explicit operator bool() const noexcept { return has_value(); }
148
154 template <typename T>
155 NEFORCE_NODISCARD T* cast() noexcept {
156 if (!storage_) {
157 return nullptr;
158 }
159 if (storage_->type_id() != type_name_v<T>.to_hash()) {
160 return nullptr;
161 }
162 auto* md = dynamic_cast<model<T>*>(storage_.get());
163 return md ? &md->value_ : nullptr;
164 }
165
171 template <typename T>
172 NEFORCE_NODISCARD const T* cast() const noexcept {
173 if (!storage_) {
174 return nullptr;
175 }
176 if (storage_->type_id() != type_name_v<T>.to_hash()) {
177 return nullptr;
178 }
179 auto* md = dynamic_cast<model<T>*>(storage_.get());
180 return md ? &md->value_ : nullptr;
181 }
182
189 template <typename T>
190 NEFORCE_NODISCARD T& get() {
191 if (auto* ptr = cast<T>()) {
192 return *ptr;
193 }
194 NEFORCE_THROW_EXCEPTION(typecast_exception("Not a valid type"));
195 unreachable();
196 }
197
204 template <typename T>
205 NEFORCE_NODISCARD const T& get() const {
206 if (auto* ptr = cast<T>()) {
207 return *ptr;
208 }
209 NEFORCE_THROW_EXCEPTION(typecast_exception("Not a valid type"));
210 unreachable();
211 }
212
218 template <typename T>
219 NEFORCE_NODISCARD bool can_cast() const noexcept {
220 return cast<T>() != nullptr;
221 }
222
231 template <typename T>
232 NEFORCE_NODISCARD T convert() const {
233 if (auto* ptr = cast<T>()) {
234 return *ptr;
235 }
236 NEFORCE_THROW_EXCEPTION(typecast_exception("Not a valid type"));
237 unreachable();
238 }
239};
240 // Reflection
242
243NEFORCE_END_REFLECT__
244NEFORCE_END_NAMESPACE__
245#endif // NEFORCE_CORE_REFLECT_ANY_HPP__
类型擦除容器
T & get()
获取存储值的引用
T convert() const
转换为指定类型的值
bool can_cast() const noexcept
检查是否可以转换为指定类型
reflect::type_id type_id() const noexcept
获取存储值的类型ID
bool has_value() const noexcept
检查是否包含值
T * cast() noexcept
尝试转换为指定类型的指针
const T & get() const
获取存储值的常量引用
meta_any & operator=(const meta_any &other)
拷贝赋值运算符
meta_any() noexcept=default
默认构造函数
const T * cast() const noexcept
尝试转换为指定类型的常量指针
独占智能指针
异常处理框架
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
constexpr T * addressof(T &x) noexcept
获取对象的地址
NEFORCE_NORETURN void unreachable() noexcept
标记不可达代码路径
uint64_t size_t
无符号大小类型
size_t type_id
类型标识符
constexpr string_view type_name_v
type_name的便捷访问变量模板
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
basic_string_view< char > string_view
字符字符串视图
typename decay< T >::type decay_t
decay的便捷别名
#define NEFORCE_MACRO_RANGE_ARITHMETIC(MAC)
所有算术类型列表宏
constexpr bool is_same_v
is_same的便捷变量模板
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
constexpr unique_ptr< T > make_unique(Args &&... args)
创建unique_ptr
字符串视图类型别名和实用函数
类型名称获取器
类型转换异常
独占智能指针