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
14NEFORCE_BEGIN_NAMESPACE__
15NEFORCE_BEGIN_REFLECT__
16
22
23using type_id = size_t;
24
25
33template <typename T>
34struct type_name {
35 static constexpr string_view value = "unknown";
36};
37
41template <typename T>
42NEFORCE_INLINE17 constexpr string_view type_name_v = type_name<T>::value;
43
45#define __NEFORCE_SPECIALIZE_TYPE_NAME(T) \
46 template <> \
47 struct type_name<T> { \
48 static constexpr string_view value = #T; \
49 };
50
51NEFORCE_MACRO_RANGE_ARITHMETIC(__NEFORCE_SPECIALIZE_TYPE_NAME)
52#undef __NEFORCE_SPECIALIZE_TYPE_NAME
54
61class meta_any {
62private:
63 struct concepts {
64 virtual ~concepts() = default;
65 virtual unique_ptr<concepts> clone() const = 0;
66 virtual reflect::type_id type_id() const noexcept = 0;
67 };
68
69 template <typename T>
70 struct model final : concepts {
71 T value_;
72
73 explicit model(T value) :
74 value_(_NEFORCE move(value)) {}
75
76 unique_ptr<concepts> clone() const override { return _NEFORCE make_unique<model<T>>(value_); }
77
78 reflect::type_id type_id() const noexcept override { return type_name_v<T>.to_hash(); }
79 };
80
81 unique_ptr<concepts> storage_{nullptr};
82
83public:
87 meta_any() noexcept = default;
88
94 template <typename T, typename = enable_if_t<!is_same_v<decay_t<T>, meta_any>>>
95 explicit meta_any(T&& value) :
96 storage_(_NEFORCE make_unique<model<decay_t<T>>>(_NEFORCE forward<T>(value))) {}
97
98 meta_any(meta_any&&) noexcept = default;
99 meta_any& operator=(meta_any&&) noexcept = default;
100
105 meta_any(const meta_any& other) {
106 if (other.storage_) {
107 storage_ = other.storage_->clone();
108 }
109 }
110
116 meta_any& operator=(const meta_any& other) {
117 if (addressof(other) == this) {
118 return *this;
119 }
120
121 if (other.storage_) {
122 storage_ = other.storage_->clone();
123 } else {
124 storage_.reset();
125 }
126
127 return *this;
128 }
129
134 NEFORCE_NODISCARD reflect::type_id type_id() const noexcept { return storage_ ? storage_->type_id() : 0; }
135
140 NEFORCE_NODISCARD bool has_value() const noexcept { return !!storage_; }
141
146 explicit operator bool() const noexcept { return has_value(); }
147
153 template <typename T>
154 NEFORCE_NODISCARD T* cast() noexcept {
155 if (!storage_) {
156 return nullptr;
157 }
158 if (storage_->type_id() != type_name_v<T>.to_hash()) {
159 return nullptr;
160 }
161 auto* md = dynamic_cast<model<T>*>(storage_.get());
162 return md ? &md->value_ : nullptr;
163 }
164
170 template <typename T>
171 NEFORCE_NODISCARD const T* cast() const noexcept {
172 if (!storage_) {
173 return nullptr;
174 }
175 if (storage_->type_id() != type_name_v<T>.to_hash()) {
176 return nullptr;
177 }
178 auto* md = dynamic_cast<model<T>*>(storage_.get());
179 return md ? &md->value_ : nullptr;
180 }
181
188 template <typename T>
189 NEFORCE_NODISCARD T& get() {
190 if (auto* ptr = cast<T>()) {
191 return *ptr;
192 }
193 NEFORCE_THROW_EXCEPTION(typecast_exception("Not a valid type"));
194 unreachable();
195 }
196
203 template <typename T>
204 NEFORCE_NODISCARD const T& get() const {
205 if (auto* ptr = cast<T>()) {
206 return *ptr;
207 }
208 NEFORCE_THROW_EXCEPTION(typecast_exception("Not a valid type"));
209 unreachable();
210 }
211
217 template <typename T>
218 NEFORCE_NODISCARD bool can_cast() const noexcept {
219 return cast<T>() != nullptr;
220 }
221
230 template <typename T>
231 NEFORCE_NODISCARD T convert() const {
232 if (auto* ptr = cast<T>()) {
233 return *ptr;
234 }
235 NEFORCE_THROW_EXCEPTION(typecast_exception("Not a valid type"));
236 unreachable();
237 }
238};
239 // Reflection
241
242NEFORCE_END_REFLECT__
243NEFORCE_END_NAMESPACE__
244#endif // NEFORCE_CORE_REFLECT_ANY_HPP__
类型擦除容器
NEFORCE_NODISCARD bool has_value() const noexcept
检查是否包含值
NEFORCE_NODISCARD T convert() const
转换为指定类型的值
NEFORCE_NODISCARD reflect::type_id type_id() const noexcept
获取存储值的类型ID
NEFORCE_NODISCARD const T * cast() const noexcept
尝试转换为指定类型的常量指针
NEFORCE_NODISCARD T & get()
获取存储值的引用
NEFORCE_NODISCARD bool can_cast() const noexcept
检查是否可以转换为指定类型
NEFORCE_NODISCARD const T & get() const
获取存储值的常量引用
meta_any & operator=(const meta_any &other)
拷贝赋值运算符
meta_any() noexcept=default
默认构造函数
NEFORCE_NODISCARD T * cast() noexcept
尝试转换为指定类型的指针
独占智能指针
NEFORCE_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
NEFORCE_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
NEFORCE_NORETURN NEFORCE_ALWAYS_INLINE_INLINE void unreachable() noexcept
标记不可达代码路径
uint64_t size_t
无符号大小类型
NEFORCE_INLINE17 constexpr string_view type_name_v
type_name的便捷访问变量模板
size_t type_id
类型标识符
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)
所有算术类型列表宏
NEFORCE_INLINE17 constexpr bool is_same_v
is_same的便捷变量模板
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
NEFORCE_CONSTEXPR20 unique_ptr< T > make_unique(Args &&... args)
创建unique_ptr
字符串视图类型别名和实用函数
类型名称获取器
类型转换异常
独占智能指针