chenjunfu2-nbt-cpp v2.1.5
一个基于CPP20的NBT(Named Binary Tag)库
载入中...
搜索中...
未找到
NBT_Node_View.hpp
浏览该文件的文档.
1#pragma once
2
3#include "NBT_Node.hpp"
4
7
8//用法和NBT_Node一致,但是只持有构造时传入对象的指针,且不持有对象
9//对象随时可销毁,如果销毁后使用持有销毁对象的view则行为未定义,用户自行负责
10//构造传入nullptr指针且进行使用则后果自负
11
22template <bool bIsConst>
23class NBT_Node_View
24{
25 template <bool _bIsConst>
26 friend class NBT_Node_View;//需要设置自己为友元,这样不同模板的类实例之间才能相互访问
27
28public:
30 //类型列表展开,声明std::variant
31 template <typename T>
32 struct AddConstIf
33 {
34 using type = std::conditional<bIsConst, const T, T>::type;
35 };
36
37 template <typename T>
38 using PtrType = typename AddConstIf<T>::type *;
39
40 template <typename T>
41 struct TypeListPointerToVariant;
42
43 template <typename... Ts>
44 struct TypeListPointerToVariant<NBT_Type::_TypeList<Ts...>>
45 {
46 using type = std::variant<PtrType<Ts>...>;//展开成指针类型
47 };
49
51 using VariantData = TypeListPointerToVariant<NBT_Type::TypeList>::type;
52
53protected:
56
57public:
59 static inline constexpr bool is_const = bIsConst;
60
66 template <typename T>
67 requires(!std::is_same_v<std::decay_t<T>, NBT_Node> &&NBT_Type::IsValidType_V<std::decay_t<T>> && !bIsConst)
68 NBT_Node_View(T &value) : data(&value)
69 {}
70
76 template <typename T>
77 requires(!std::is_same_v<std::decay_t<T>, NBT_Node> && NBT_Type::IsValidType_V<std::decay_t<T>> && bIsConst)
78 NBT_Node_View(const T &value) : data(&value)
79 {}
80
86 {
87 std::visit([this](auto &arg)
88 {
89 this->data = &arg;
90 }, node.data);
91 }
92
97 template <typename = void>//requires占位模板
98 requires(bIsConst)//此构造只在const的情况下生效,因为非const的情况下不能通过const对象初始化非const指针
100 {
101 std::visit([this](auto &arg)
102 {
103 this->data = &arg;
104 }, node.data);
105 }
106
111 template <typename = void>//requires占位模板
112 requires(bIsConst)//此构造只在const的情况下生效,不能从const视图转换到非const视图
113 NBT_Node_View(const NBT_Node_View<false> &_Other)
114 {
115 std::visit([this](auto &arg)
116 {
117 this->data = arg;//此处arg为非const指针
118 }, _Other.data);
119 }
120
125 template <typename T>
126 requires(!std::is_lvalue_reference_v<T>)
127 NBT_Node_View(T &&_Temp) = delete;
128
135 template <typename T>
136 requires(!std::is_same_v<std::decay_t<T>, NBT_Node> && NBT_Type::IsValidType_V<std::decay_t<T>> && !bIsConst)
137 T &Set(T &value)
138 {
139 data.template emplace<PtrType<T>>(&value);
140 return value;
141 }
142
149 template <typename T>
150 requires(!std::is_same_v<std::decay_t<T>, NBT_Node> && NBT_Type::IsValidType_V<std::decay_t<T>> && bIsConst)
151 const T &Set(const T &value)
152 {
153 data.template emplace<PtrType<T>>(&value);
154 return value;
155 }
156
162 template <typename = void>//requires占位模板
163 requires(!bIsConst)
165 {
166 std::visit([this](auto &arg)
167 {
168 this->data = &arg;
169 }, node.data);
170
171 return node;
172 }
173
179 template <typename = void>//requires占位模板
180 requires(bIsConst)
181 const NBT_Node &Set(const NBT_Node &node)
182 {
183 std::visit([this](auto &arg)
184 {
185 this->data = &arg;
186 }, node.data);
187
188 return node;
189 }
190
195 template <typename T>
196 requires(!std::is_lvalue_reference_v<T>)
197 T &Set(T &&_Temp) = delete;
198
205 template <typename T>
206 requires(!std::is_same_v<std::decay_t<T>, NBT_Node> && NBT_Type::IsValidType_V<std::decay_t<T>> && !bIsConst)
207 NBT_Node_View &operator=(T &value)
208 {
209 data = &value;
210
211 return *this;
212 }
213
220 template <typename T>
221 requires(!std::is_same_v<std::decay_t<T>, NBT_Node> && NBT_Type::IsValidType_V<std::decay_t<T>> && bIsConst)
222 NBT_Node_View &operator=(const T &value)
223 {
224 data = &value;
225
226 return *this;
227 }
228
234 template <typename = void>
235 requires(!bIsConst)
236 NBT_Node_View &operator=(NBT_Node &node)
237 {
238 std::visit([this](auto &arg)
239 {
240 this->data = &arg;
241 }, node.data);
242
243 return *this;
244 }
245
251 template <typename = void>
252 requires(bIsConst)
253 NBT_Node_View &operator=(const NBT_Node &node)
254 {
255 std::visit([this](auto &arg)
256 {
257 this->data = &arg;
258 }, node.data);
259
260 return *this;
261 }
262
267 template <typename T>
268 requires(!std::is_lvalue_reference_v<T>)
269 NBT_Node_View &operator=(T &&_Temp) = delete;
270
271public:
275 {}
276
279 ~NBT_Node_View() = default;
280
284 NBT_Node_View(const NBT_Node_View &_Copy) : data(_Copy.data)
285 {}
286
290 NBT_Node_View(NBT_Node_View &&_Move) noexcept : data(std::move(_Move.data))
291 {}
292
295 const VariantData &GetData(void) const noexcept
296 {
297 return data;
298 }
299
302 VariantData &GetData(void) noexcept
303 {
304 return data;
305 }
306
311 NBT_Node_View &operator=(const NBT_Node_View &_Copy)
312 {
313 data = _Copy.data;
314 return *this;
315 }
316
321 NBT_Node_View &operator=(NBT_Node_View &&_Move) noexcept
322 {
323 data = std::move(_Move.data);
324 return *this;
325 }
326
332 bool operator==(const NBT_Node_View &_Right) const noexcept
333 {
334 if (GetTag() != _Right.GetTag())
335 {
336 return false;
337 }
338
339 return std::visit([this](const auto *argL, const auto *argR)-> bool
340 {
341 using TL = const std::decay_t<decltype(argL)>;
342 return *argL == *(TL)argR;
343 }, this->data, _Right.data);
344 }
345
351 bool operator!=(const NBT_Node_View &_Right) const noexcept
352 {
353 if (GetTag() != _Right.GetTag())
354 {
355 return true;
356 }
357
358 return std::visit([this](const auto *argL, const auto *argR)-> bool
359 {
360 using TL = const std::decay_t<decltype(argL)>;
361 return *argL != *(TL)argR;
362 }, this->data, _Right.data);
363 }
364
370 std::partial_ordering operator<=>(const NBT_Node_View &_Right) const noexcept
371 {
372 if (GetTag() != _Right.GetTag())
373 {
374 return std::partial_ordering::unordered;
375 }
376
377 return std::visit([this](const auto *argL, const auto *argR)-> std::partial_ordering
378 {
379 using TL = const std::decay_t<decltype(argL)>;
380 return *argL <=> *(TL)argR;
381 }, this->data, _Right.data);
382 }
383
386 NBT_TAG GetTag() const noexcept
387 {
388 return (NBT_TAG)data.index();//返回当前存储类型的index(0基索引,与NBT_TAG enum一一对应)
389 }
390
395 template<typename T>
396 std::conditional_t<bIsConst, const T &, T &> Get() const
397 {
398 return *std::get<PtrType<T>>(data);
399 }
400
405 template<typename T>
406 std::conditional_t<bIsConst, const T &, T &> Get()
407 {
408 return *std::get<PtrType<T>>(data);
409 }
410
415 template<typename T>
416 std::conditional_t<bIsConst, const T *, T *>GetIf() const noexcept
417 {
418 auto p = std::get_if<PtrType<T>>(&data);//二维指针
419 return p != nullptr ? *p : nullptr;
420 }
421
426 template<typename T>
427 std::conditional_t<bIsConst, const T *, T *>GetIf() noexcept
428 {
429 auto p = std::get_if<PtrType<T>>(&data);//二维指针
430 return p != nullptr ? *p : nullptr;
431 }
432
436 template<typename T>
437 bool TypeHolds() const
438 {
439 return std::holds_alternative<PtrType<T>>(data);
440 }
441
444 bool IsEmpty() const
445 {
446 return TypeHolds<NBT_Type::End>() &&
447 std::get<PtrType<NBT_Type::End>>(data) == PtrType<NBT_Type::End>{};
448 }
449
451 void SetEmpty()
452 {
453 data.template emplace<PtrType<NBT_Type::End>>(PtrType<NBT_Type::End>{});
454 }
455
456//针对每种类型生成一个方便的函数
457//通过宏定义批量生成
458
462#define TYPE_GET_FUNC(type)\
463\
468std::conditional_t<bIsConst, const NBT_Type::type &, NBT_Type::type &> Get##type() const\
469{\
470 return *std::get<PtrType<NBT_Type::type>>(data);\
471}\
472\
473\
478std::conditional_t<bIsConst, const NBT_Type::type &, NBT_Type::type &> Get##type()\
479{\
480 return *std::get<PtrType<NBT_Type::type>>(data);\
481}\
482\
483\
488std::conditional_t<bIsConst, const NBT_Type::type *, NBT_Type::type *> GetIf##type() const noexcept\
489{\
490 auto p = std::get_if<PtrType<NBT_Type::type>>(&data);\
491 return p != nullptr ? *p : nullptr;\
492}\
493\
494\
499std::conditional_t<bIsConst, const NBT_Type::type *, NBT_Type::type *> GetIf##type() noexcept\
500{\
501 auto p = std::get_if<PtrType<NBT_Type::type>>(&data); \
502 return p != nullptr ? *p : nullptr; \
503}\
504\
505\
509bool Is##type() const\
510{\
511 return std::holds_alternative<PtrType<NBT_Type::type>>(data);\
512}\
513\
519friend std::conditional_t<bIsConst, const NBT_Type::type &, NBT_Type::type &> Get##type(NBT_Node_View & node)\
520{\
521 return node.Get##type();\
522}\
523\
524\
530friend std::conditional_t<bIsConst, const NBT_Type::type &, NBT_Type::type &> Get##type(const NBT_Node_View & node)\
532 return node.Get##type();\
541friend std::conditional_t<bIsConst, const NBT_Type::type *, NBT_Type::type *> GetIf##type(NBT_Node_View & node) noexcept\
543 return node.GetIf##type();\
544}\
545\
546
551 */\
552friend std::conditional_t<bIsConst, const NBT_Type::type *, NBT_Type::type *> GetIf##type(const NBT_Node_View & node) noexcept\
553{\
554 return node.GetIf##type();\
555}\
556\
557\
562friend bool Is##type(const NBT_Node_View &node)\
563{\
564 return node.Is##type();\
565}
566
570
572
585 TYPE_GET_FUNC(Compound);
589#undef TYPE_GET_FUNC
590
594#define TYPE_SET_FUNC(type)\
595\
600template <typename = void>\
601requires(!bIsConst)\
602NBT_Type::type &Set##type(NBT_Type::type &value)\
603{\
604 return Set<NBT_Type::type>(value); \
605}\
606\
607\
612template <typename = void>\
613requires(bIsConst)\
614const NBT_Type::type &Set##type(const NBT_Type::type &value)\
615{\
616 return Set<NBT_Type::type>(value); \
617}\
618\
619\
624NBT_Type::type &Set##type(NBT_Type::type &&_Temp) = delete;
625
630
641 TYPE_SET_FUNC(String);
642 TYPE_SET_FUNC(List);
643 TYPE_SET_FUNC(Compound);
644
646
647#undef TYPE_SET_FUNC
648};
NBT节点类型,支持存储所有NBT类型的变体
#define TYPE_SET_FUNC(type)
不同类型名接口生成宏
定义 NBT_Node_View.hpp:551
#define TYPE_GET_FUNC(type)
不同类型名接口生成宏
定义 NBT_Node_View.hpp:462
NBT_TAG
枚举NBT类型对应的类型标签值
定义 NBT_TAG.hpp:17
@ Int
对应NBT_Type::Int
定义 NBT_TAG.hpp:21
@ Float
对应NBT_Type::Float
定义 NBT_TAG.hpp:23
@ ByteArray
对应NBT_Type::ByteArray
定义 NBT_TAG.hpp:25
@ Short
对应NBT_Type::Short
定义 NBT_TAG.hpp:20
@ Long
对应NBT_Type::Long
定义 NBT_TAG.hpp:22
@ End
对应NBT_Type::End
定义 NBT_TAG.hpp:18
@ LongArray
对应NBT_Type::LongArray
定义 NBT_TAG.hpp:30
@ Byte
对应NBT_Type::Byte
定义 NBT_TAG.hpp:19
@ IntArray
对应NBT_Type::IntArray
定义 NBT_TAG.hpp:29
@ Double
对应NBT_Type::Double
定义 NBT_TAG.hpp:24
TypeListPointerToVariant< NBT_Type::TypeList >::type VariantData
用于存储指向NBT数据的指针的具体变体类型
定义 NBT_Node_View.hpp:51
static constexpr bool is_const
静态常量,表示当前视图指向的数据是否只读
定义 NBT_Node_View.hpp:59
NBT_Node_View(const NBT_Node &node)
从NBT_Node构造视图(仅适用于const)
定义 NBT_Node_View.hpp:99
bool operator==(const NBT_Node_View &_Right) const noexcept
相等比较运算符
定义 NBT_Node_View.hpp:332
std::conditional_t< bIsConst, const T *, T * > GetIf() noexcept
通过指定类型获取当前视图指向的数据对象的指针(根据视图的const属性决定返回常量指针或非常量指针)
定义 NBT_Node_View.hpp:427
~NBT_Node_View()=default
默认析构函数
NBT_Node_View(const NBT_Node_View< false > &_Other)
从非const视图隐式构造const视图
定义 NBT_Node_View.hpp:113
NBT_Node_View & operator=(NBT_Node_View &&_Move) noexcept
移动赋值运算符
定义 NBT_Node_View.hpp:321
std::conditional_t< bIsConst, const T *, T * > GetIf() const noexcept
通过指定类型获取当前视图指向的数据对象的指针(根据视图的const属性决定返回常量指针或非常量指针)
定义 NBT_Node_View.hpp:416
std::conditional_t< bIsConst, const T &, T & > Get() const
通过指定类型获取当前视图指向的数据对象
定义 NBT_Node_View.hpp:396
NBT_Node_View(T &&_Temp)=delete
删除临时对象构造方式,防止从临时对象构造导致悬空指针
std::conditional_t< bIsConst, const T &, T & > Get()
通过指定类型获取当前视图指向的数据对象
定义 NBT_Node_View.hpp:406
const T & Set(const T &value)
通用设置函数(仅适用于const)
定义 NBT_Node_View.hpp:151
NBT_Node_View(NBT_Node_View &&_Move) noexcept
移动构造函数
定义 NBT_Node_View.hpp:290
NBT_Node_View(T &value)
通用构造函数(仅适用于非const)
定义 NBT_Node_View.hpp:68
const NBT_Node & Set(const NBT_Node &node)
从NBT_Node重设视图(仅适用于const)
定义 NBT_Node_View.hpp:181
bool IsEmpty() const
为空判断
定义 NBT_Node_View.hpp:444
NBT_TAG GetTag() const noexcept
获取当前视图指向的NBT类型的枚举值
定义 NBT_Node_View.hpp:386
T & Set(T &&_Temp)=delete
删除临时对象设置方式,防止从临时对象构造导致悬空指针
NBT_Node_View(NBT_Node &node)
从NBT_Node构造视图(仅适用于非const)
定义 NBT_Node_View.hpp:85
VariantData & GetData(void) noexcept
获取底层容器数据的引用
定义 NBT_Node_View.hpp:302
NBT_Node_View()
默认构造函数
定义 NBT_Node_View.hpp:274
void SetEmpty()
设置为空
定义 NBT_Node_View.hpp:451
std::partial_ordering operator<=>(const NBT_Node_View &_Right) const noexcept
三路比较运算符
定义 NBT_Node_View.hpp:370
VariantData data
数据对象(仅持有数据的指针)
定义 NBT_Node_View.hpp:55
NBT_Node_View(const T &value)
通用构造函数(仅适用于const)
定义 NBT_Node_View.hpp:78
bool TypeHolds() const
类型判断
定义 NBT_Node_View.hpp:437
NBT_Node_View & operator=(const NBT_Node_View &_Copy)
拷贝赋值运算符
定义 NBT_Node_View.hpp:311
bool operator!=(const NBT_Node_View &_Right) const noexcept
不等比较运算符
定义 NBT_Node_View.hpp:351
NBT_Node_View(const NBT_Node_View &_Copy)
拷贝构造函数
定义 NBT_Node_View.hpp:284
T & Set(T &value)
通用设置函数(仅适用于非const)
定义 NBT_Node_View.hpp:137
NBT_Node & Set(NBT_Node &node)
从NBT_Node重设视图(仅适用于非const)
定义 NBT_Node_View.hpp:164
const VariantData & GetData(void) const noexcept
获取底层容器数据的常量引用
定义 NBT_Node_View.hpp:295
NBT节点,用于存储NBT格式的各种数据类型
定义 NBT_Node.hpp:37
VariantData data
数据对象(要求必须持有数据)
定义 NBT_Node.hpp:59
提供NBT类型定义,包括NBT格式中的所有数据类型,以及部分辅助功能,比如静态类型与Tag映射,类型存在查询,类型列表大小,类型最大小值等
定义 NBT_Type.hpp:29
static constexpr bool IsValidType_V
类型存在检查:用于编译期检查一个给定类型是否为NBT中的类型
定义 NBT_Type.hpp:183