chenjunfu2-nbt-cpp v2.1.5
一个基于CPP20的NBT(Named Binary Tag)库
载入中...
搜索中...
未找到
NBT_Type.hpp
浏览该文件的文档.
1#pragma once
2
3#include <stdint.h>
4#include <stddef.h>//size_t
5#include <variant>
6#include <vector>
7#include <unordered_map>
8#include <string>
9#include <type_traits>
10
11#include "NBT_TAG.hpp"
12#include "MUTF8_Tool.hpp"
13
16
17class NBT_Node;
18template <typename Array>
19class NBT_Array;
20template <typename String, typename StringView>
21class NBT_String;
24template <typename List>
25class NBT_List;
26template <typename Compound>
27class NBT_Compound;
28
30class NBT_Type
31{
33 NBT_Type(void) = delete;
35 ~NBT_Type(void) = delete;
36
37public:
38
41 using Float_Raw = uint32_t;
42 using Double_Raw = uint64_t;
44
47
48 //内建类型
49 //默认为无状态,与std::variant兼容
50 using End = std::monostate;
51 using Byte = int8_t;
52 using Short = int16_t;
53 using Int = int32_t;
54 using Long = int64_t;
55
56 //浮点类型
57 //通过编译期确认类型大小来选择正确的类型,优先浮点类型,如果失败则替换为对应的可用类型
58 using Float = std::conditional_t<(sizeof(float) == sizeof(Float_Raw)), float, Float_Raw>;
59 using Double = std::conditional_t<(sizeof(double) == sizeof(Double_Raw)), double, Double_Raw>;
60
61 //数组类型,不存在SortArray,由于NBT标准不提供,所以此处也不提供
65
66 //字符串类型
68
69 //列表类型
70 //存储一系列同类型标签的有效负载(无标签 ID 或名称),原先为list,因为mc内list也通过下标访问,所以改为vector模拟
72
73 //集合类型
74 //挂在序列下的内容都通过map绑定名称
76
78
81 template<typename... Ts>
82 struct _TypeList{};
83
86 <
87 End,
88 Byte,
89 Short,
90 Int,
91 Long,
92 Float,
93 Double,
95 String,
96 List,
100 >;
101
102private:
103 //通过类型Tag映射到字符串指针数组
104 constexpr static inline const char *const cstrTypeName[] =
105 {
106 "End",
107 "Byte",
108 "Short",
109 "Int",
110 "Long",
111 "Float",
112 "Double",
113 "ByteArray",
114 "String",
115 "List",
116 "Compound",
117 "IntArray",
118 "LongArray",
119 };
120
121public:
126 constexpr static inline const char *GetTypeName(NBT_TAG tag) noexcept//运行时类型判断,允许静态
127 {
128 if (tag >= NBT_TAG::ENUM_END)
129 {
130 return "Unknown";
131 }
132
134 return cstrTypeName[tagRaw];
135 }
136
139 using ArrayLength = int32_t;
140 using StringLength = uint16_t;
141 using ListLength = int32_t;
143
146 constexpr static inline ArrayLength ArrayLength_Max = INT32_MAX;
147 constexpr static inline ArrayLength ArrayLength_Min = INT32_MIN;
148
149 constexpr static inline StringLength StringLength_Max = UINT16_MAX;
150 constexpr static inline StringLength StringLength_Min = 0;
151
152 constexpr static inline ListLength ListLength_Max = INT32_MAX;
153 constexpr static inline ListLength ListLength_Min = INT32_MIN;
155
156
159 constexpr static inline Byte Byte_Max = INT8_MAX;
160 constexpr static inline Byte Byte_Min = INT8_MIN;
161
162 constexpr static inline Short Short_Max = INT16_MAX;
163 constexpr static inline Short Short_Min = INT16_MIN;
164
165 constexpr static inline Int Int_Max = INT32_MAX;
166 constexpr static inline Int Int_Min = INT32_MIN;
167
168 constexpr static inline Long Long_Max = INT64_MAX;
169 constexpr static inline Long Long_Min = INT64_MIN;
171
173 template <typename T, typename List>
174 struct IsValidType;
175
176 template <typename T, typename... Ts>
177 struct IsValidType<T, _TypeList<Ts...>> : std::bool_constant<(std::is_same_v<T, Ts> || ...)>
178 {};
180
184 template <typename T>
185 static constexpr bool IsValidType_V = IsValidType<T, TypeList>::value;
186
188 template <typename T, typename... Ts>
189 static consteval NBT_TAG_RAW_TYPE TypeTagHelper()//consteval必须编译期求值
190 {
191 NBT_TAG_RAW_TYPE tagIndex = 0;
192 bool bFound = ((std::is_same_v<T, Ts> ? true : (++tagIndex, false)) || ...);
193 return bFound ? tagIndex : (NBT_TAG_RAW_TYPE)-1;
194 }
195
196 template <typename T, typename List>
197 struct TypeTagImpl;
198
199 template <typename T, typename... Ts>
200 struct TypeTagImpl<T, _TypeList<Ts...>>
201 {
202 static constexpr NBT_TAG_RAW_TYPE value = TypeTagHelper<T, Ts...>();
203 };
205
210 template <typename T>
211 static constexpr NBT_TAG TypeTag_V = (NBT_TAG)TypeTagImpl<T, TypeList>::value;
212
214 template <typename List>
215 struct TypeListSize;
216
217 template <typename... Ts>
218 struct TypeListSize<_TypeList<Ts...>>
219 {
220 static constexpr size_t value = sizeof...(Ts);
221 };
223
226 static constexpr size_t TypeListSize_V = TypeListSize<TypeList>::value;
227
228 //静态断言:确保枚举值与类型数量匹配
229 static_assert(TypeListSize_V == NBT_TAG::ENUM_END, "Enumeration does not match the number of types in the mutator");
230
232 template <NBT_TAG_RAW_TYPE I, typename List> struct TypeAt;
233
234 template <NBT_TAG_RAW_TYPE I, typename... Ts>
235 struct TypeAt<I, _TypeList<Ts...>>
236 {
237 using type = std::tuple_element_t<I, std::tuple<Ts...>>;
238 };
239
240 template <NBT_TAG Tag>
241 requires((NBT_TAG_RAW_TYPE)Tag < TypeListSize_V)
242 struct TagToType
243 {
244 using type = typename TypeAt<(NBT_TAG_RAW_TYPE)Tag, TypeList>::type;
245 };
247
252 template <NBT_TAG Tag>
253 using TagToType_T = typename TagToType<Tag>::type;
254
255
258
262 template <typename T>
263 static constexpr bool IsNumericType_V =
264 std::is_same_v<std::remove_cvref_t<T>, Byte> ||
265 std::is_same_v<std::remove_cvref_t<T>, Short> ||
266 std::is_same_v<std::remove_cvref_t<T>, Int> ||
267 std::is_same_v<std::remove_cvref_t<T>, Long> ||
268 std::is_same_v<std::remove_cvref_t<T>, Float> ||
269 std::is_same_v<std::remove_cvref_t<T>, Double>;
270
274 template <typename T>
275 static constexpr bool IsIntegerType_V =
276 std::is_same_v<std::remove_cvref_t<T>, Byte> ||
277 std::is_same_v<std::remove_cvref_t<T>, Short> ||
278 std::is_same_v<std::remove_cvref_t<T>, Int> ||
279 std::is_same_v<std::remove_cvref_t<T>, Long>;
280
284 template <typename T>
285 static constexpr bool IsFloatingType_V =
286 std::is_same_v<std::remove_cvref_t<T>, Float> ||
287 std::is_same_v<std::remove_cvref_t<T>, Double>;
288
292 template <typename T>
293 static constexpr bool IsArrayType_V =
294 std::is_same_v<std::remove_cvref_t<T>, ByteArray> ||
295 std::is_same_v<std::remove_cvref_t<T>, IntArray> ||
296 std::is_same_v<std::remove_cvref_t<T>, LongArray>;
297
301 template <typename T>
302 static constexpr bool IsContainerType_V =
303 std::is_same_v<std::remove_cvref_t<T>, ByteArray> ||
304 std::is_same_v<std::remove_cvref_t<T>, IntArray> ||
305 std::is_same_v<std::remove_cvref_t<T>, LongArray> ||
306 std::is_same_v<std::remove_cvref_t<T>, List> ||
307 std::is_same_v<std::remove_cvref_t<T>, Compound>;
308
312 template <typename T>
313 static constexpr bool IsStringType_V =
314 std::is_same_v<std::remove_cvref_t<T>, String>;
315
319 template <typename T>
320 static constexpr bool IsListType_V =
321 std::is_same_v<std::remove_cvref_t<T>, List>;
322
326 template <typename T>
327 static constexpr bool IsCompoundType_V =
328 std::is_same_v<std::remove_cvref_t<T>, Compound>;
329
331
333 template<typename T>
335 struct BuiltinRawType
336 {
337 using Type = T;
338 };
340
346 template<typename T>
347 using BuiltinRawType_T = typename BuiltinRawType<T>::Type;
348
351
355 constexpr static inline bool IsNumericTag(NBT_TAG tag) noexcept
356 {
357 switch (tag)
358 {
359 case NBT_TAG::Byte:
360 case NBT_TAG::Short:
361 case NBT_TAG::Int:
362 case NBT_TAG::Long:
363 case NBT_TAG::Float:
364 case NBT_TAG::Double:
365 return true;
366 default:
367 return false;
368 }
369 }
370
374 constexpr static inline bool IsIntegerTag(NBT_TAG tag) noexcept
375 {
376 switch (tag)
377 {
378 case NBT_TAG::Byte:
379 case NBT_TAG::Short:
380 case NBT_TAG::Int:
381 case NBT_TAG::Long:
382 return true;
383 default:
384 return false;
385 }
386 }
387
391 constexpr static inline bool IsFloatingTag(NBT_TAG tag) noexcept
392 {
393 switch (tag)
394 {
395 case NBT_TAG::Float:
396 case NBT_TAG::Double:
397 return true;
398 default:
399 return false;
400 }
401 }
402
406 constexpr static inline bool IsArrayTag(NBT_TAG tag) noexcept
407 {
408 switch (tag)
409 {
413 return true;
414 default:
415 return false;
416 }
417 }
418
422 constexpr static inline bool IsContainerTag(NBT_TAG tag) noexcept
423 {
424 switch (tag)
425 {
429 case NBT_TAG::List:
431 return true;
432 default:
433 return false;
434 }
435 }
436
440 constexpr static inline bool IsStringTag(NBT_TAG tag) noexcept
441 {
442 return tag == NBT_TAG::String;
443 }
444
448 constexpr static inline bool IsListTag(NBT_TAG tag) noexcept
449 {
450 return tag == NBT_TAG::List;
451 }
452
456 constexpr static inline bool IsCompoundTag(NBT_TAG tag) noexcept
457 {
458 return tag == NBT_TAG::Compound;
459 }
460
462};
463
464//显示特化
465
467template<>
468struct NBT_Type::BuiltinRawType<NBT_Type::Float>//浮点数映射
469{
470 using Type = Float_Raw;
471 static_assert(sizeof(Type) == sizeof(Float), "Type size does not match!");
472};
473
474template<>
475struct NBT_Type::BuiltinRawType<NBT_Type::Double>//浮点数映射
476{
477 using Type = Double_Raw;
478 static_assert(sizeof(Type) == sizeof(Double), "Type size does not match!");
479};
Java Modified-UTF-8工具集
NBT类型枚举与它的运算重载
uint8_t NBT_TAG_RAW_TYPE
NBT_TAG的原始类型,用于二进制读写或判断等
定义 NBT_TAG.hpp:12
NBT_TAG
枚举NBT类型对应的类型标签值
定义 NBT_TAG.hpp:17
@ Int
对应NBT_Type::Int
定义 NBT_TAG.hpp:21
@ Float
对应NBT_Type::Float
定义 NBT_TAG.hpp:23
@ Compound
对应NBT_Type::Compound
定义 NBT_TAG.hpp:28
@ String
对应NBT_Type::String
定义 NBT_TAG.hpp:26
@ ByteArray
对应NBT_Type::ByteArray
定义 NBT_TAG.hpp:25
@ Short
对应NBT_Type::Short
定义 NBT_TAG.hpp:20
@ List
对应NBT_Type::List
定义 NBT_TAG.hpp:27
@ Long
对应NBT_Type::Long
定义 NBT_TAG.hpp:22
@ 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
@ ENUM_END
枚举结束标记,用于计算enum元素个数,范围判断等
定义 NBT_TAG.hpp:31
继承自标准库std::vector的代理类。 无特殊成员,构造与使用方式与标准库std::vector一致。
定义 NBT_Array.hpp:18
继承自标准库std::unordered_map的代理类,用于存储和管理NBT键值对
定义 NBT_Compound.hpp:23
继承自标准库容器的代理类,用于存储和管理NBT列表
定义 NBT_List.hpp:23
NBT节点,用于存储NBT格式的各种数据类型
定义 NBT_Node.hpp:37
继承自标准库std::basic_string的代理类,用于存储、处理与转换Modified-UTF-8字符串
定义 NBT_String.hpp:129
提供NBT类型定义,包括NBT格式中的所有数据类型,以及部分辅助功能,比如静态类型与Tag映射,类型存在查询,类型列表大小,类型最大小值等
定义 NBT_Type.hpp:31
static constexpr StringLength StringLength_Min
字符串长度类型最小值
定义 NBT_Type.hpp:150
int8_t Byte
8位有符号整数
定义 NBT_Type.hpp:51
static constexpr bool IsStringType_V
判断类型是否为NBT String类型
定义 NBT_Type.hpp:313
static constexpr Int Int_Min
整数类型最小值
定义 NBT_Type.hpp:166
typename TagToType< Tag >::type TagToType_T
从NBT_TAG获取对应的类型:编译期从NBT_TAG的enum值获取类型
定义 NBT_Type.hpp:253
static constexpr bool IsNumericType_V
判断类型是否为NBT数值类型(包含所有整数和浮点数类型)
定义 NBT_Type.hpp:263
std::monostate End
结束标记类型,无数据
定义 NBT_Type.hpp:50
static constexpr Byte Byte_Min
字节类型最小值
定义 NBT_Type.hpp:160
static constexpr Short Short_Min
短整数类型形最小值
定义 NBT_Type.hpp:163
int32_t ArrayLength
数组长度类型
定义 NBT_Type.hpp:139
int16_t Short
16位有符号整数
定义 NBT_Type.hpp:52
static constexpr bool IsContainerTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应容器类型(包含List、Compound和所有数组类型)
定义 NBT_Type.hpp:422
static constexpr size_t TypeListSize_V
类型列表大小:获取NBT类型的个数
定义 NBT_Type.hpp:226
static constexpr StringLength StringLength_Max
字符串长度类型最大值
定义 NBT_Type.hpp:149
static constexpr Byte Byte_Max
字节类型最大值
定义 NBT_Type.hpp:159
static constexpr bool IsIntegerType_V
判断类型是否为NBT整数类型(包含所有整数类型)
定义 NBT_Type.hpp:275
static constexpr bool IsCompoundType_V
判断类型是否为NBT Compound类型
定义 NBT_Type.hpp:327
static constexpr bool IsContainerType_V
判断类型是否为NBT容器类型(包含List、Compound和所有数组类型)
定义 NBT_Type.hpp:302
uint64_t Double_Raw
Double 类型的原始表示,用于在平台不支持或需要二进制读写时使用
定义 NBT_Type.hpp:42
NBT_String< MUTF8_String, MUTF8_String_View > String
字符串类型,存储Java M-UTF-8字符串
定义 NBT_Type.hpp:67
static constexpr ArrayLength ArrayLength_Min
数组长度类型最小值
定义 NBT_Type.hpp:147
static constexpr bool IsFloatingTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应浮点数类型(包含所有浮点数类型)
定义 NBT_Type.hpp:391
static constexpr bool IsValidType_V
类型存在检查:用于编译期检查一个给定类型是否为NBT中的类型
定义 NBT_Type.hpp:185
uint32_t Float_Raw
Float 类型的原始表示,用于在平台不支持或需要二进制读写时使用
定义 NBT_Type.hpp:41
int64_t Long
64位有符号整数
定义 NBT_Type.hpp:54
NBT_List< std::vector< NBT_Node > > List
列表类型,可顺序存储任意相同的NBT类型
定义 NBT_Type.hpp:71
static constexpr bool IsStringTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应String类型
定义 NBT_Type.hpp:440
std::conditional_t<(sizeof(float)==sizeof(Float_Raw)), float, Float_Raw > Float
单精度浮点类型
定义 NBT_Type.hpp:58
static constexpr Long Long_Max
长整数类型最大值
定义 NBT_Type.hpp:168
NBT_Array< std::vector< Byte > > ByteArray
存储 8位有符号整数的数组类型
定义 NBT_Type.hpp:62
_TypeList< End, Byte, Short, Int, Long, Float, Double, ByteArray, String, List, Compound, IntArray, LongArray > TypeList
完整的NBT类型列表定义
定义 NBT_Type.hpp:85
NBT_Array< std::vector< Int > > IntArray
存储32位有符号整数的数组类型
定义 NBT_Type.hpp:63
NBT_Array< std::vector< Long > > LongArray
存储64位有符号整数的数组类型
定义 NBT_Type.hpp:64
typename BuiltinRawType< T >::Type BuiltinRawType_T
映射内建类型到方便读写的raw类型:编译期获得内建类型到可以进行二进制读写的原始类型
定义 NBT_Type.hpp:347
static constexpr bool IsCompoundTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应Compound类型
定义 NBT_Type.hpp:456
int32_t ListLength
列表长度类型
定义 NBT_Type.hpp:141
uint16_t StringLength
字符串长度类型
定义 NBT_Type.hpp:140
std::conditional_t<(sizeof(double)==sizeof(Double_Raw)), double, Double_Raw > Double
双精度浮点类型
定义 NBT_Type.hpp:59
static constexpr bool IsNumericTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应数值类型(包含所有整数和浮点数类型)
定义 NBT_Type.hpp:355
static constexpr bool IsArrayType_V
判断类型是否为NBT数组类型(包含所有数组类型)
定义 NBT_Type.hpp:293
static constexpr Int Int_Max
整数类型最大值
定义 NBT_Type.hpp:165
static constexpr ListLength ListLength_Max
列表长度类型最大值
定义 NBT_Type.hpp:152
NBT_Compound< std::unordered_map< String, NBT_Node, NBT_String_Transparent_Hash, NBT_String_Transparent_Equal > > Compound
集合类型,可存储任意不同的NBT类型,通过名称映射值
定义 NBT_Type.hpp:75
static constexpr bool IsListType_V
判断类型是否为NBT List类型
定义 NBT_Type.hpp:320
static constexpr bool IsFloatingType_V
判断类型是否为NBT浮点数类型(包含所有浮点数类型)
定义 NBT_Type.hpp:285
static constexpr ArrayLength ArrayLength_Max
数组长度类型最大值
定义 NBT_Type.hpp:146
static constexpr NBT_TAG TypeTag_V
类型枚举值查询:用于在编译期,通过类型查询它在NBT_TAG中对应的enum值
定义 NBT_Type.hpp:211
static constexpr Short Short_Max
短整数类型形最大值
定义 NBT_Type.hpp:162
static constexpr bool IsArrayTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应数组类型(包含所有数组类型)
定义 NBT_Type.hpp:406
static constexpr bool IsListTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应List类型
定义 NBT_Type.hpp:448
static constexpr bool IsIntegerTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应整数类型(包含所有整数类型)
定义 NBT_Type.hpp:374
static constexpr const char * GetTypeName(NBT_TAG tag) noexcept
通过类型标签获取类型名称
定义 NBT_Type.hpp:126
static constexpr Long Long_Min
长整数类型最小值
定义 NBT_Type.hpp:169
static constexpr ListLength ListLength_Min
列表长度类型最小值
定义 NBT_Type.hpp:153
int32_t Int
32位有符号整数
定义 NBT_Type.hpp:53
透明相等比较函数对象,用于 unordered 容器的异构查找
定义 NBT_String.hpp:569
透明哈希函数对象,用于 unordered 容器的异构查找
定义 NBT_String.hpp:551
类型列表模板
定义 NBT_Type.hpp:82