chenjunfu2-nbt-cpp v2.1.0
一个基于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
15
16class NBT_Node;
17template <typename Array>
18class NBT_Array;
19template <typename String, typename StringView>
20class NBT_String;
21template <typename List>
22class NBT_List;
23template <typename Compound>
24class NBT_Compound;
25
27class NBT_Type
28{
30 NBT_Type(void) = delete;
32 ~NBT_Type(void) = delete;
33
34public:
35
38 using Float_Raw = uint32_t;
39 using Double_Raw = uint64_t;
41
44
45 //内建类型
46 //默认为无状态,与std::variant兼容
47 using End = std::monostate;
48 using Byte = int8_t;
49 using Short = int16_t;
50 using Int = int32_t;
51 using Long = int64_t;
52
53 //浮点类型
54 //通过编译期确认类型大小来选择正确的类型,优先浮点类型,如果失败则替换为对应的可用类型
55 using Float = std::conditional_t<(sizeof(float) == sizeof(Float_Raw)), float, Float_Raw>;
56 using Double = std::conditional_t<(sizeof(double) == sizeof(Double_Raw)), double, Double_Raw>;
57
58 //数组类型,不存在SortArray,由于NBT标准不提供,所以此处也不提供
62
63 //字符串类型
64 using String = NBT_String<std::basic_string<uint8_t>, std::basic_string_view<uint8_t>>;
65
66 //列表类型
67 //存储一系列同类型标签的有效负载(无标签 ID 或名称),原先为list,因为mc内list也通过下标访问,所以改为vector模拟
69
70 //集合类型
71 //挂在序列下的内容都通过map绑定名称
73
75
78 template<typename... Ts>
79 struct _TypeList{};
80
83 <
84 End,
85 Byte,
86 Short,
87 Int,
88 Long,
89 Float,
90 Double,
92 String,
93 List,
97 >;
98
99private:
100 //通过类型Tag映射到字符串指针数组
101 constexpr static inline const char *const cstrTypeName[] =
102 {
103 "End",
104 "Byte",
105 "Short",
106 "Int",
107 "Long",
108 "Float",
109 "Double",
110 "ByteArray",
111 "String",
112 "List",
113 "Compound",
114 "IntArray",
115 "LongArray",
116 };
117
118public:
123 constexpr static inline const char *GetTypeName(NBT_TAG tag) noexcept//运行时类型判断,允许静态
124 {
125 if (tag >= NBT_TAG::ENUM_END)
126 {
127 return "Unknown";
128 }
129
131 return cstrTypeName[tagRaw];
132 }
133
136 using ArrayLength = int32_t;
137 using StringLength = uint16_t;
138 using ListLength = int32_t;
140
143 constexpr static inline ArrayLength ArrayLength_Max = INT32_MAX;
144 constexpr static inline ArrayLength ArrayLength_Min = INT32_MIN;
145
146 constexpr static inline StringLength StringLength_Max = UINT16_MAX;
147 constexpr static inline StringLength StringLength_Min = 0;
148
149 constexpr static inline ListLength ListLength_Max = INT32_MAX;
150 constexpr static inline ListLength ListLength_Min = INT32_MIN;
152
153
156 constexpr static inline Byte Byte_Max = INT8_MAX;
157 constexpr static inline Byte Byte_Min = INT8_MIN;
158
159 constexpr static inline Short Short_Max = INT16_MAX;
160 constexpr static inline Short Short_Min = INT16_MIN;
161
162 constexpr static inline Int Int_Max = INT32_MAX;
163 constexpr static inline Int Int_Min = INT32_MIN;
164
165 constexpr static inline Long Long_Max = INT64_MAX;
166 constexpr static inline Long Long_Min = INT64_MIN;
168
170 template <typename T, typename List>
171 struct IsValidType;
172
173 template <typename T, typename... Ts>
174 struct IsValidType<T, _TypeList<Ts...>> : std::bool_constant<(std::is_same_v<T, Ts> || ...)>
175 {};
177
181 template <typename T>
182 static constexpr bool IsValidType_V = IsValidType<T, TypeList>::value;
183
185 template <typename T, typename... Ts>
186 static consteval NBT_TAG_RAW_TYPE TypeTagHelper()//consteval必须编译期求值
187 {
188 NBT_TAG_RAW_TYPE tagIndex = 0;
189 bool bFound = ((std::is_same_v<T, Ts> ? true : (++tagIndex, false)) || ...);
190 return bFound ? tagIndex : (NBT_TAG_RAW_TYPE)-1;
191 }
192
193 template <typename T, typename List>
194 struct TypeTagImpl;
195
196 template <typename T, typename... Ts>
197 struct TypeTagImpl<T, _TypeList<Ts...>>
198 {
199 static constexpr NBT_TAG_RAW_TYPE value = TypeTagHelper<T, Ts...>();
200 };
202
207 template <typename T>
208 static constexpr NBT_TAG TypeTag_V = (NBT_TAG)TypeTagImpl<T, TypeList>::value;
209
211 template <typename List>
212 struct TypeListSize;
213
214 template <typename... Ts>
215 struct TypeListSize<_TypeList<Ts...>>
216 {
217 static constexpr size_t value = sizeof...(Ts);
218 };
220
223 static constexpr size_t TypeListSize_V = TypeListSize<TypeList>::value;
224
225 //静态断言:确保枚举值与类型数量匹配
226 static_assert(TypeListSize_V == NBT_TAG::ENUM_END, "Enumeration does not match the number of types in the mutator");
227
229 template <NBT_TAG_RAW_TYPE I, typename List> struct TypeAt;
230
231 template <NBT_TAG_RAW_TYPE I, typename... Ts>
232 struct TypeAt<I, _TypeList<Ts...>>
233 {
234 using type = std::tuple_element_t<I, std::tuple<Ts...>>;
235 };
236
237 template <NBT_TAG Tag>
238 requires((NBT_TAG_RAW_TYPE)Tag < TypeListSize_V)
239 struct TagToType
240 {
241 using type = typename TypeAt<(NBT_TAG_RAW_TYPE)Tag, TypeList>::type;
242 };
244
249 template <NBT_TAG Tag>
250 using TagToType_T = typename TagToType<Tag>::type;
251
252
255
259 template <typename T>
260 static constexpr bool IsNumericType_V =
261 std::is_same_v<T, Byte> ||
262 std::is_same_v<T, Short> ||
263 std::is_same_v<T, Int> ||
264 std::is_same_v<T, Long> ||
265 std::is_same_v<T, Float> ||
266 std::is_same_v<T, Double>;
267
271 template <typename T>
272 static constexpr bool IsIntegerType_V =
273 std::is_same_v<T, Byte> ||
274 std::is_same_v<T, Short> ||
275 std::is_same_v<T, Int> ||
276 std::is_same_v<T, Long>;
277
281 template <typename T>
282 static constexpr bool IsFloatingType_V =
283 std::is_same_v<T, Float> ||
284 std::is_same_v<T, Double>;
285
289 template <typename T>
290 static constexpr bool IsArrayType_V =
291 std::is_same_v<T, ByteArray> ||
292 std::is_same_v<T, IntArray> ||
293 std::is_same_v<T, LongArray>;
294
298 template <typename T>
299 static constexpr bool IsContainerType_V =
300 std::is_same_v<T, ByteArray> ||
301 std::is_same_v<T, IntArray> ||
302 std::is_same_v<T, LongArray> ||
303 std::is_same_v<T, List> ||
304 std::is_same_v<T, Compound>;
305
309 template <typename T>
310 static constexpr bool IsStringType_V = std::is_same_v<T, String>;
311
315 template <typename T>
316 static constexpr bool IsListType_V = std::is_same_v<T, List>;
317
321 template <typename T>
322 static constexpr bool IsCompoundType_V = std::is_same_v<T, Compound>;
323
325
327 template<typename T>
329 struct BuiltinRawType
330 {
331 using Type = T;
332 };
334
340 template<typename T>
341 using BuiltinRawType_T = typename BuiltinRawType<T>::Type;
342
345
349 constexpr static inline bool IsNumericTag(NBT_TAG tag) noexcept
350 {
351 switch (tag)
352 {
353 case NBT_TAG::Byte:
354 case NBT_TAG::Short:
355 case NBT_TAG::Int:
356 case NBT_TAG::Long:
357 case NBT_TAG::Float:
358 case NBT_TAG::Double:
359 return true;
360 default:
361 return false;
362 }
363 }
364
368 constexpr static inline bool IsIntegerTag(NBT_TAG tag) noexcept
369 {
370 switch (tag)
371 {
372 case NBT_TAG::Byte:
373 case NBT_TAG::Short:
374 case NBT_TAG::Int:
375 case NBT_TAG::Long:
376 return true;
377 default:
378 return false;
379 }
380 }
381
385 constexpr static inline bool IsFloatingTag(NBT_TAG tag) noexcept
386 {
387 switch (tag)
388 {
389 case NBT_TAG::Float:
390 case NBT_TAG::Double:
391 return true;
392 default:
393 return false;
394 }
395 }
396
400 constexpr static inline bool IsArrayTag(NBT_TAG tag) noexcept
401 {
402 switch (tag)
403 {
407 return true;
408 default:
409 return false;
410 }
411 }
412
416 constexpr static inline bool IsContainerTag(NBT_TAG tag) noexcept
417 {
418 switch (tag)
419 {
423 case NBT_TAG::List:
425 return true;
426 default:
427 return false;
428 }
429 }
430
434 constexpr static inline bool IsStringTag(NBT_TAG tag) noexcept
435 {
436 return tag == NBT_TAG::String;
437 }
438
442 constexpr static inline bool IsListTag(NBT_TAG tag) noexcept
443 {
444 return tag == NBT_TAG::List;
445 }
446
450 constexpr static inline bool IsCompoundTag(NBT_TAG tag) noexcept
451 {
452 return tag == NBT_TAG::Compound;
453 }
454
456};
457
458//显示特化
459
461template<>
462struct NBT_Type::BuiltinRawType<NBT_Type::Float>//浮点数映射
463{
464 using Type = Float_Raw;
465 static_assert(sizeof(Type) == sizeof(Float), "Type size does not match!");
466};
467
468template<>
469struct NBT_Type::BuiltinRawType<NBT_Type::Double>//浮点数映射
470{
471 using Type = Double_Raw;
472 static_assert(sizeof(Type) == sizeof(Double), "Type size does not match!");
473};
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:65
继承自标准库容器的代理类,用于存储和管理NBT列表
定义 NBT_List.hpp:23
NBT节点,用于存储NBT格式的各种数据类型
定义 NBT_Node.hpp:37
继承自标准库std::basic_string的代理类,用于存储、处理与转换Modified-UTF-8字符串
定义 NBT_String.hpp:114
提供NBT类型定义,包括NBT格式中的所有数据类型,以及部分辅助功能,比如静态类型与Tag映射,类型存在查询,类型列表大小,类型最大小值等
定义 NBT_Type.hpp:28
static constexpr StringLength StringLength_Min
字符串长度类型最小值
定义 NBT_Type.hpp:147
int8_t Byte
8位有符号整数
定义 NBT_Type.hpp:48
static constexpr bool IsStringType_V
判断类型是否为NBT String类型
定义 NBT_Type.hpp:310
static constexpr Int Int_Min
整数类型最小值
定义 NBT_Type.hpp:163
typename TagToType< Tag >::type TagToType_T
从NBT_TAG获取对应的类型:编译期从NBT_TAG的enum值获取类型
定义 NBT_Type.hpp:250
static constexpr bool IsNumericType_V
判断类型是否为NBT数值类型(包含所有整数和浮点数类型)
定义 NBT_Type.hpp:260
std::monostate End
结束标记类型,无数据
定义 NBT_Type.hpp:47
static constexpr Byte Byte_Min
字节类型最小值
定义 NBT_Type.hpp:157
static constexpr Short Short_Min
短整数类型形最小值
定义 NBT_Type.hpp:160
int32_t ArrayLength
数组长度类型
定义 NBT_Type.hpp:136
int16_t Short
16位有符号整数
定义 NBT_Type.hpp:49
_TypeList< End, Byte, Short, Int, Long, Float, Double, ByteArray, String, List, Compound, IntArray, LongArray > TypeList
完整的NBT类型列表定义
定义 NBT_Type.hpp:82
static constexpr bool IsContainerTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应容器类型(包含List、Compound和所有数组类型)
定义 NBT_Type.hpp:416
static constexpr size_t TypeListSize_V
类型列表大小:获取NBT类型的个数
定义 NBT_Type.hpp:223
static constexpr StringLength StringLength_Max
字符串长度类型最大值
定义 NBT_Type.hpp:146
static constexpr Byte Byte_Max
字节类型最大值
定义 NBT_Type.hpp:156
static constexpr bool IsIntegerType_V
判断类型是否为NBT整数类型(包含所有整数类型)
定义 NBT_Type.hpp:272
static constexpr bool IsCompoundType_V
判断类型是否为NBT Compound类型
定义 NBT_Type.hpp:322
static constexpr bool IsContainerType_V
判断类型是否为NBT容器类型(包含List、Compound和所有数组类型)
定义 NBT_Type.hpp:299
uint64_t Double_Raw
Double 类型的原始表示,用于在平台不支持或需要二进制读写时使用
定义 NBT_Type.hpp:39
static constexpr ArrayLength ArrayLength_Min
数组长度类型最小值
定义 NBT_Type.hpp:144
static constexpr bool IsFloatingTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应浮点数类型(包含所有浮点数类型)
定义 NBT_Type.hpp:385
static constexpr bool IsValidType_V
类型存在检查:用于编译期检查一个给定类型是否为NBT中的类型
定义 NBT_Type.hpp:182
uint32_t Float_Raw
Float 类型的原始表示,用于在平台不支持或需要二进制读写时使用
定义 NBT_Type.hpp:38
int64_t Long
64位有符号整数
定义 NBT_Type.hpp:51
NBT_String< std::basic_string< uint8_t >, std::basic_string_view< uint8_t > > String
字符串类型,存储Java M-UTF-8字符串
定义 NBT_Type.hpp:64
NBT_List< std::vector< NBT_Node > > List
列表类型,可顺序存储任意相同的NBT类型
定义 NBT_Type.hpp:68
static constexpr bool IsStringTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应String类型
定义 NBT_Type.hpp:434
std::conditional_t<(sizeof(float)==sizeof(Float_Raw)), float, Float_Raw > Float
单精度浮点类型
定义 NBT_Type.hpp:55
static constexpr Long Long_Max
长整数类型最大值
定义 NBT_Type.hpp:165
NBT_Array< std::vector< Byte > > ByteArray
存储 8位有符号整数的数组类型
定义 NBT_Type.hpp:59
NBT_Array< std::vector< Int > > IntArray
存储32位有符号整数的数组类型
定义 NBT_Type.hpp:60
NBT_Compound< std::unordered_map< String, NBT_Node > > Compound
集合类型,可存储任意不同的NBT类型,通过名称映射值
定义 NBT_Type.hpp:72
NBT_Array< std::vector< Long > > LongArray
存储64位有符号整数的数组类型
定义 NBT_Type.hpp:61
typename BuiltinRawType< T >::Type BuiltinRawType_T
映射内建类型到方便读写的raw类型:编译期获得内建类型到可以进行二进制读写的原始类型
定义 NBT_Type.hpp:341
static constexpr bool IsCompoundTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应Compound类型
定义 NBT_Type.hpp:450
int32_t ListLength
列表长度类型
定义 NBT_Type.hpp:138
uint16_t StringLength
字符串长度类型
定义 NBT_Type.hpp:137
std::conditional_t<(sizeof(double)==sizeof(Double_Raw)), double, Double_Raw > Double
双精度浮点类型
定义 NBT_Type.hpp:56
static constexpr bool IsNumericTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应数值类型(包含所有整数和浮点数类型)
定义 NBT_Type.hpp:349
static constexpr bool IsArrayType_V
判断类型是否为NBT数组类型(包含所有数组类型)
定义 NBT_Type.hpp:290
static constexpr Int Int_Max
整数类型最大值
定义 NBT_Type.hpp:162
static constexpr ListLength ListLength_Max
列表长度类型最大值
定义 NBT_Type.hpp:149
static constexpr bool IsListType_V
判断类型是否为NBT List类型
定义 NBT_Type.hpp:316
static constexpr bool IsFloatingType_V
判断类型是否为NBT浮点数类型(包含所有浮点数类型)
定义 NBT_Type.hpp:282
static constexpr ArrayLength ArrayLength_Max
数组长度类型最大值
定义 NBT_Type.hpp:143
static constexpr NBT_TAG TypeTag_V
类型枚举值查询:用于在编译期,通过类型查询它在NBT_TAG中对应的enum值
定义 NBT_Type.hpp:208
static constexpr Short Short_Max
短整数类型形最大值
定义 NBT_Type.hpp:159
static constexpr bool IsArrayTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应数组类型(包含所有数组类型)
定义 NBT_Type.hpp:400
static constexpr bool IsListTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应List类型
定义 NBT_Type.hpp:442
static constexpr bool IsIntegerTag(NBT_TAG tag) noexcept
判断给定的NBT_TAG是否对应整数类型(包含所有整数类型)
定义 NBT_Type.hpp:368
static constexpr const char * GetTypeName(NBT_TAG tag) noexcept
通过类型标签获取类型名称
定义 NBT_Type.hpp:123
static constexpr Long Long_Min
长整数类型最小值
定义 NBT_Type.hpp:166
static constexpr ListLength ListLength_Min
列表长度类型最小值
定义 NBT_Type.hpp:150
int32_t Int
32位有符号整数
定义 NBT_Type.hpp:50
类型列表模板
定义 NBT_Type.hpp:79