chenjunfu2-nbt-cpp v2.1.3
一个基于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;
22template <typename List>
23class NBT_List;
24template <typename Compound>
25class NBT_Compound;
26
28class NBT_Type
29{
31 NBT_Type(void) = delete;
33 ~NBT_Type(void) = delete;
34
35public:
36
39 using Float_Raw = uint32_t;
40 using Double_Raw = uint64_t;
42
45
46 //内建类型
47 //默认为无状态,与std::variant兼容
48 using End = std::monostate;
49 using Byte = int8_t;
50 using Short = int16_t;
51 using Int = int32_t;
52 using Long = int64_t;
53
54 //浮点类型
55 //通过编译期确认类型大小来选择正确的类型,优先浮点类型,如果失败则替换为对应的可用类型
56 using Float = std::conditional_t<(sizeof(float) == sizeof(Float_Raw)), float, Float_Raw>;
57 using Double = std::conditional_t<(sizeof(double) == sizeof(Double_Raw)), double, Double_Raw>;
58
59 //数组类型,不存在SortArray,由于NBT标准不提供,所以此处也不提供
63
64 //字符串类型
66
67 //列表类型
68 //存储一系列同类型标签的有效负载(无标签 ID 或名称),原先为list,因为mc内list也通过下标访问,所以改为vector模拟
70
71 //集合类型
72 //挂在序列下的内容都通过map绑定名称
74
76
79 template<typename... Ts>
80 struct _TypeList{};
81
84 <
85 End,
86 Byte,
87 Short,
88 Int,
89 Long,
90 Float,
91 Double,
93 String,
94 List,
98 >;
99
100private:
101 //通过类型Tag映射到字符串指针数组
102 constexpr static inline const char *const cstrTypeName[] =
103 {
104 "End",
105 "Byte",
106 "Short",
107 "Int",
108 "Long",
109 "Float",
110 "Double",
111 "ByteArray",
112 "String",
113 "List",
114 "Compound",
115 "IntArray",
116 "LongArray",
117 };
118
119public:
124 constexpr static inline const char *GetTypeName(NBT_TAG tag) noexcept//运行时类型判断,允许静态
125 {
126 if (tag >= NBT_TAG::ENUM_END)
127 {
128 return "Unknown";
129 }
130
132 return cstrTypeName[tagRaw];
133 }
134
137 using ArrayLength = int32_t;
138 using StringLength = uint16_t;
139 using ListLength = int32_t;
141
144 constexpr static inline ArrayLength ArrayLength_Max = INT32_MAX;
145 constexpr static inline ArrayLength ArrayLength_Min = INT32_MIN;
146
147 constexpr static inline StringLength StringLength_Max = UINT16_MAX;
148 constexpr static inline StringLength StringLength_Min = 0;
149
150 constexpr static inline ListLength ListLength_Max = INT32_MAX;
151 constexpr static inline ListLength ListLength_Min = INT32_MIN;
153
154
157 constexpr static inline Byte Byte_Max = INT8_MAX;
158 constexpr static inline Byte Byte_Min = INT8_MIN;
159
160 constexpr static inline Short Short_Max = INT16_MAX;
161 constexpr static inline Short Short_Min = INT16_MIN;
162
163 constexpr static inline Int Int_Max = INT32_MAX;
164 constexpr static inline Int Int_Min = INT32_MIN;
165
166 constexpr static inline Long Long_Max = INT64_MAX;
167 constexpr static inline Long Long_Min = INT64_MIN;
169
171 template <typename T, typename List>
172 struct IsValidType;
173
174 template <typename T, typename... Ts>
175 struct IsValidType<T, _TypeList<Ts...>> : std::bool_constant<(std::is_same_v<T, Ts> || ...)>
176 {};
178
182 template <typename T>
183 static constexpr bool IsValidType_V = IsValidType<T, TypeList>::value;
184
186 template <typename T, typename... Ts>
187 static consteval NBT_TAG_RAW_TYPE TypeTagHelper()//consteval必须编译期求值
188 {
189 NBT_TAG_RAW_TYPE tagIndex = 0;
190 bool bFound = ((std::is_same_v<T, Ts> ? true : (++tagIndex, false)) || ...);
191 return bFound ? tagIndex : (NBT_TAG_RAW_TYPE)-1;
192 }
193
194 template <typename T, typename List>
195 struct TypeTagImpl;
196
197 template <typename T, typename... Ts>
198 struct TypeTagImpl<T, _TypeList<Ts...>>
199 {
200 static constexpr NBT_TAG_RAW_TYPE value = TypeTagHelper<T, Ts...>();
201 };
203
208 template <typename T>
209 static constexpr NBT_TAG TypeTag_V = (NBT_TAG)TypeTagImpl<T, TypeList>::value;
210
212 template <typename List>
213 struct TypeListSize;
214
215 template <typename... Ts>
216 struct TypeListSize<_TypeList<Ts...>>
217 {
218 static constexpr size_t value = sizeof...(Ts);
219 };
221
224 static constexpr size_t TypeListSize_V = TypeListSize<TypeList>::value;
225
226 //静态断言:确保枚举值与类型数量匹配
227 static_assert(TypeListSize_V == NBT_TAG::ENUM_END, "Enumeration does not match the number of types in the mutator");
228
230 template <NBT_TAG_RAW_TYPE I, typename List> struct TypeAt;
231
232 template <NBT_TAG_RAW_TYPE I, typename... Ts>
233 struct TypeAt<I, _TypeList<Ts...>>
234 {
235 using type = std::tuple_element_t<I, std::tuple<Ts...>>;
236 };
237
238 template <NBT_TAG Tag>
239 requires((NBT_TAG_RAW_TYPE)Tag < TypeListSize_V)
240 struct TagToType
241 {
242 using type = typename TypeAt<(NBT_TAG_RAW_TYPE)Tag, TypeList>::type;
243 };
245
250 template <NBT_TAG Tag>
251 using TagToType_T = typename TagToType<Tag>::type;
252
253
256
260 template <typename T>
261 static constexpr bool IsNumericType_V =
262 std::is_same_v<std::remove_cvref_t<T>, Byte> ||
263 std::is_same_v<std::remove_cvref_t<T>, Short> ||
264 std::is_same_v<std::remove_cvref_t<T>, Int> ||
265 std::is_same_v<std::remove_cvref_t<T>, Long> ||
266 std::is_same_v<std::remove_cvref_t<T>, Float> ||
267 std::is_same_v<std::remove_cvref_t<T>, Double>;
268
272 template <typename T>
273 static constexpr bool IsIntegerType_V =
274 std::is_same_v<std::remove_cvref_t<T>, Byte> ||
275 std::is_same_v<std::remove_cvref_t<T>, Short> ||
276 std::is_same_v<std::remove_cvref_t<T>, Int> ||
277 std::is_same_v<std::remove_cvref_t<T>, Long>;
278
282 template <typename T>
283 static constexpr bool IsFloatingType_V =
284 std::is_same_v<std::remove_cvref_t<T>, Float> ||
285 std::is_same_v<std::remove_cvref_t<T>, Double>;
286
290 template <typename T>
291 static constexpr bool IsArrayType_V =
292 std::is_same_v<std::remove_cvref_t<T>, ByteArray> ||
293 std::is_same_v<std::remove_cvref_t<T>, IntArray> ||
294 std::is_same_v<std::remove_cvref_t<T>, LongArray>;
295
299 template <typename T>
300 static constexpr bool IsContainerType_V =
301 std::is_same_v<std::remove_cvref_t<T>, ByteArray> ||
302 std::is_same_v<std::remove_cvref_t<T>, IntArray> ||
303 std::is_same_v<std::remove_cvref_t<T>, LongArray> ||
304 std::is_same_v<std::remove_cvref_t<T>, List> ||
305 std::is_same_v<std::remove_cvref_t<T>, Compound>;
306
310 template <typename T>
311 static constexpr bool IsStringType_V =
312 std::is_same_v<std::remove_cvref_t<T>, String>;
313
317 template <typename T>
318 static constexpr bool IsListType_V =
319 std::is_same_v<std::remove_cvref_t<T>, List>;
320
324 template <typename T>
325 static constexpr bool IsCompoundType_V =
326 std::is_same_v<std::remove_cvref_t<T>, Compound>;
327
329
331 template<typename T>
333 struct BuiltinRawType
334 {
335 using Type = T;
336 };
338
344 template<typename T>
345 using BuiltinRawType_T = typename BuiltinRawType<T>::Type;
346
349
353 constexpr static inline bool IsNumericTag(NBT_TAG tag) noexcept
354 {
355 switch (tag)
356 {
357 case NBT_TAG::Byte:
358 case NBT_TAG::Short:
359 case NBT_TAG::Int:
360 case NBT_TAG::Long:
361 case NBT_TAG::Float:
362 case NBT_TAG::Double:
363 return true;
364 default:
365 return false;
366 }
367 }
368
372 constexpr static inline bool IsIntegerTag(NBT_TAG tag) noexcept
373 {
374 switch (tag)
375 {
376 case NBT_TAG::Byte:
377 case NBT_TAG::Short:
378 case NBT_TAG::Int:
379 case NBT_TAG::Long:
380 return true;
381 default:
382 return false;
383 }
384 }
385
389 constexpr static inline bool IsFloatingTag(NBT_TAG tag) noexcept
390 {
391 switch (tag)
392 {
393 case NBT_TAG::Float:
394 case NBT_TAG::Double:
395 return true;
396 default:
397 return false;
398 }
399 }
400
404 constexpr static inline bool IsArrayTag(NBT_TAG tag) noexcept
405 {
406 switch (tag)
407 {
411 return true;
412 default:
413 return false;
414 }
415 }
416
420 constexpr static inline bool IsContainerTag(NBT_TAG tag) noexcept
421 {
422 switch (tag)
423 {
427 case NBT_TAG::List:
429 return true;
430 default:
431 return false;
432 }
433 }
434
438 constexpr static inline bool IsStringTag(NBT_TAG tag) noexcept
439 {
440 return tag == NBT_TAG::String;
441 }
442
446 constexpr static inline bool IsListTag(NBT_TAG tag) noexcept
447 {
448 return tag == NBT_TAG::List;
449 }
450
454 constexpr static inline bool IsCompoundTag(NBT_TAG tag) noexcept
455 {
456 return tag == NBT_TAG::Compound;
457 }
458
460};
461
462//显示特化
463
465template<>
466struct NBT_Type::BuiltinRawType<NBT_Type::Float>//浮点数映射
467{
468 using Type = Float_Raw;
469 static_assert(sizeof(Type) == sizeof(Float), "Type size does not match!");
470};
471
472template<>
473struct NBT_Type::BuiltinRawType<NBT_Type::Double>//浮点数映射
474{
475 using Type = Double_Raw;
476 static_assert(sizeof(Type) == sizeof(Double), "Type size does not match!");
477};
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:111
提供NBT类型定义,包括NBT格式中的所有数据类型,以及部分辅助功能,比如静态类型与Tag映射,类型存在查询,类型列表大小,类型最大小值等
定义 NBT_Type.hpp:29
static constexpr StringLength StringLength_Min
字符串长度类型最小值
定义 NBT_Type.hpp:148
int8_t Byte
8位有符号整数
定义 NBT_Type.hpp:49
static constexpr bool IsStringType_V
判断类型是否为NBT String类型
定义 NBT_Type.hpp:311
static constexpr Int Int_Min
整数类型最小值
定义 NBT_Type.hpp:164
typename TagToType< Tag >::type TagToType_T
从NBT_TAG获取对应的类型:编译期从NBT_TAG的enum值获取类型
定义 NBT_Type.hpp:251
static constexpr bool IsNumericType_V
判断类型是否为NBT数值类型(包含所有整数和浮点数类型)
定义 NBT_Type.hpp:261
std::monostate End
结束标记类型,无数据
定义 NBT_Type.hpp:48
static constexpr Byte Byte_Min
字节类型最小值
定义 NBT_Type.hpp:158
static constexpr Short Short_Min
短整数类型形最小值
定义 NBT_Type.hpp:161
int32_t ArrayLength
数组长度类型
定义 NBT_Type.hpp:137
int16_t Short
16位有符号整数
定义 NBT_Type.hpp:50
static constexpr bool IsContainerTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应容器类型(包含List、Compound和所有数组类型)
定义 NBT_Type.hpp:420
static constexpr size_t TypeListSize_V
类型列表大小:获取NBT类型的个数
定义 NBT_Type.hpp:224
static constexpr StringLength StringLength_Max
字符串长度类型最大值
定义 NBT_Type.hpp:147
static constexpr Byte Byte_Max
字节类型最大值
定义 NBT_Type.hpp:157
static constexpr bool IsIntegerType_V
判断类型是否为NBT整数类型(包含所有整数类型)
定义 NBT_Type.hpp:273
static constexpr bool IsCompoundType_V
判断类型是否为NBT Compound类型
定义 NBT_Type.hpp:325
static constexpr bool IsContainerType_V
判断类型是否为NBT容器类型(包含List、Compound和所有数组类型)
定义 NBT_Type.hpp:300
uint64_t Double_Raw
Double 类型的原始表示,用于在平台不支持或需要二进制读写时使用
定义 NBT_Type.hpp:40
NBT_String< MUTF8_String, MUTF8_String_View > String
字符串类型,存储Java M-UTF-8字符串
定义 NBT_Type.hpp:65
static constexpr ArrayLength ArrayLength_Min
数组长度类型最小值
定义 NBT_Type.hpp:145
static constexpr bool IsFloatingTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应浮点数类型(包含所有浮点数类型)
定义 NBT_Type.hpp:389
static constexpr bool IsValidType_V
类型存在检查:用于编译期检查一个给定类型是否为NBT中的类型
定义 NBT_Type.hpp:183
uint32_t Float_Raw
Float 类型的原始表示,用于在平台不支持或需要二进制读写时使用
定义 NBT_Type.hpp:39
int64_t Long
64位有符号整数
定义 NBT_Type.hpp:52
NBT_List< std::vector< NBT_Node > > List
列表类型,可顺序存储任意相同的NBT类型
定义 NBT_Type.hpp:69
static constexpr bool IsStringTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应String类型
定义 NBT_Type.hpp:438
std::conditional_t<(sizeof(float)==sizeof(Float_Raw)), float, Float_Raw > Float
单精度浮点类型
定义 NBT_Type.hpp:56
static constexpr Long Long_Max
长整数类型最大值
定义 NBT_Type.hpp:166
NBT_Array< std::vector< Byte > > ByteArray
存储 8位有符号整数的数组类型
定义 NBT_Type.hpp:60
_TypeList< End, Byte, Short, Int, Long, Float, Double, ByteArray, String, List, Compound, IntArray, LongArray > TypeList
完整的NBT类型列表定义
定义 NBT_Type.hpp:83
NBT_Array< std::vector< Int > > IntArray
存储32位有符号整数的数组类型
定义 NBT_Type.hpp:61
NBT_Compound< std::unordered_map< String, NBT_Node > > Compound
集合类型,可存储任意不同的NBT类型,通过名称映射值
定义 NBT_Type.hpp:73
NBT_Array< std::vector< Long > > LongArray
存储64位有符号整数的数组类型
定义 NBT_Type.hpp:62
typename BuiltinRawType< T >::Type BuiltinRawType_T
映射内建类型到方便读写的raw类型:编译期获得内建类型到可以进行二进制读写的原始类型
定义 NBT_Type.hpp:345
static constexpr bool IsCompoundTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应Compound类型
定义 NBT_Type.hpp:454
int32_t ListLength
列表长度类型
定义 NBT_Type.hpp:139
uint16_t StringLength
字符串长度类型
定义 NBT_Type.hpp:138
std::conditional_t<(sizeof(double)==sizeof(Double_Raw)), double, Double_Raw > Double
双精度浮点类型
定义 NBT_Type.hpp:57
static constexpr bool IsNumericTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应数值类型(包含所有整数和浮点数类型)
定义 NBT_Type.hpp:353
static constexpr bool IsArrayType_V
判断类型是否为NBT数组类型(包含所有数组类型)
定义 NBT_Type.hpp:291
static constexpr Int Int_Max
整数类型最大值
定义 NBT_Type.hpp:163
static constexpr ListLength ListLength_Max
列表长度类型最大值
定义 NBT_Type.hpp:150
static constexpr bool IsListType_V
判断类型是否为NBT List类型
定义 NBT_Type.hpp:318
static constexpr bool IsFloatingType_V
判断类型是否为NBT浮点数类型(包含所有浮点数类型)
定义 NBT_Type.hpp:283
static constexpr ArrayLength ArrayLength_Max
数组长度类型最大值
定义 NBT_Type.hpp:144
static constexpr NBT_TAG TypeTag_V
类型枚举值查询:用于在编译期,通过类型查询它在NBT_TAG中对应的enum值
定义 NBT_Type.hpp:209
static constexpr Short Short_Max
短整数类型形最大值
定义 NBT_Type.hpp:160
static constexpr bool IsArrayTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应数组类型(包含所有数组类型)
定义 NBT_Type.hpp:404
static constexpr bool IsListTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应List类型
定义 NBT_Type.hpp:446
static constexpr bool IsIntegerTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应整数类型(包含所有整数类型)
定义 NBT_Type.hpp:372
static constexpr const char * GetTypeName(NBT_TAG tag) noexcept
通过类型标签获取类型名称
定义 NBT_Type.hpp:124
static constexpr Long Long_Min
长整数类型最小值
定义 NBT_Type.hpp:167
static constexpr ListLength ListLength_Min
列表长度类型最小值
定义 NBT_Type.hpp:151
int32_t Int
32位有符号整数
定义 NBT_Type.hpp:51
类型列表模板
定义 NBT_Type.hpp:80