dl
dl_value.h
浏览该文件的文档.
1
12#pragma once
13
14#include <vector>
15#include <cassert>
16#include <unordered_map>
17
18#include "io/dl_log.h"
19#include "base/dl_string.h"
20#include "io/dl_json.h"
21
22namespace dl
23{
24 // 实际类型
25 enum class ValueType
26 {
42 ARRAY, // 底层为vector,表现为长度固定,有默认值
43 VECTOR, // 为空时类型不确定
45 };
46
48 {
51 size_t _length;
52 std::string _strType1;
53
57 _length{ 0 }
58 {}
59 };
60
61 ValueType String2ValueType(std::string_view str);
62 ValueTypeInfo String2ValueTypeEx(std::string_view str);
63
64 template<typename T>
66 {
67 if constexpr (std::is_same<T, bool>::value)
68 return ValueType::BOOL;
69 else
70 return ValueType::STRUCT;
71 }
72
73
74 class Value;
75 using ValueTable = std::unordered_map<std::string, Value, string_hash, std::equal_to<>>;
76
79 {
80 public:
81 void RegisterStruct(std::string_view struct_name, const ValueTable& meta_table = {})
82 {
83 _metaStruct[std::string{ struct_name }] = meta_table;
84 }
85 ValueTable& GetStructMeta(std::string_view struct_name)
86 {
87 return _metaStruct[std::string{ struct_name }];
88 }
89 private:
90 std::unordered_map<std::string, ValueTable, string_hash, std::equal_to<>> _metaStruct;
91 };
92 extern ValueSystem g_value;
93
94
97 {
98 public:
99 ValueStruct(std::string_view name) :
100 _name{ name }
101 {
102
103 }
104 Value& operator[](std::string_view key)
105 {
106 return GetMember(key);
107 }
108
109 Value& GetMember(std::string_view key);
110 void AddMember(std::string_view key, Value& v);
111 ValueTable& GetMemberAll() { return _member; }
112
113 std::string GetName() { return _name; }
114 ValueTable& GetMeta() { return g_value.GetStructMeta(_name); }
115 private:
116 std::string _name;
117 ValueTable _member;
118 };
119
122 {
123 public:
125 _type{ type }
126 {
127
128 }
129 Value& operator[](size_t i)
130 {
131 return _vec[i];
132 }
133
134
135 ValueType GetType() { return _type; }
136 std::vector<Value>& GetVector() { return _vec; }
137
138 void PushBack(const Value& v) { _vec.push_back(v); }
139 private:
140 ValueType _type;
141 std::vector<Value> _vec;
142 };
143
144
145 const ValueStruct NULL_VALUE_STRUCT = { "null struct" };
147
148 // 任意类型
149 class Value
150 {
151 private:
152 using SharedVector = std::shared_ptr<ValueVector>;
153 using SharedStruct = std::shared_ptr<ValueStruct>;
154 ValueType _type;
155 uint32_t _pad;
156 union
157 {
158 bool _bool;
159 char _char;
160 short _short;
161 int _int;
162 unsigned char _uchar;
163 unsigned short _ushort;
164 unsigned int _uint;
165 int64_t _i64;
166 uint64_t _uint64;
167 float _float;
168 double _double;
169 void* _pointer;
170
171 // 智能指针本身也在堆上,需要深拷贝
172 std::string* _string;
173 SharedVector* _vector;
174 SharedStruct* _struct;
175 };
176 public:
177 Value() :_type{ ValueType::NONE }
178 {}
181 {
182 switch (_type)
183 {
185 delete _string;
186 break;
188 delete _struct;
189 break;
192 delete _vector;
193 break;
195
196 break;
197 default:
198 break;
199 }
200 }
202 Value(const Value& b);
203 Value(bool v) { _type = ValueType::BOOL; _bool = v; }
204 Value(char v) { _type = ValueType::CHAR; _char = v; }
205 Value(short v) { _type = ValueType::SHORT; _short = v; }
206 Value(int v) { _type = ValueType::INT; _int = v; }
207 Value(unsigned char v) { _type = ValueType::UCHAR; _uchar = v; }
208 Value(unsigned short v) { _type = ValueType::USHORT; _ushort = v; }
209 Value(unsigned int v) { _type = ValueType::UINT; _uint = v; }
210 Value(int64_t v) { _type = ValueType::INT64; _i64 = v; }
211 Value(uint64_t v) { _type = ValueType::UINT64; _uint64 = v; }
212 Value(float v) { _type = ValueType::FLOAT; _float = v; }
213 Value(double v) { _type = ValueType::DOUBLE; _double = v; }
214 Value(void* v) { _type = ValueType::POINTER; _pointer = v; }
215 Value(const char* str)
216 {
217 _type = ValueType::STRING;
218 _string = new std::string(str);
219 }
220 Value(const std::string& str)
221 {
222 _type = ValueType::STRING;
223 _string = new std::string(str);
224 }
225 Value(std::string_view str)
226 {
227 _type = ValueType::STRING;
228 _string = new std::string(str);
229 }
230 Value(const ValueVector& vec)
231 {
232 _type = ValueType::VECTOR;
233 _vector = new SharedVector(std::make_shared<ValueVector>(vec));
234 }
236 {
237 _type = ValueType::STRUCT;
238 _struct = new SharedStruct(std::make_shared<ValueStruct>(s));
239 }
240 template<typename T, size_t N>
241 Value(const std::array<T, N>& arr)
242 {
243 _type = ValueType::ARRAY;
244 _vector = new SharedVector(std::make_shared<ValueVector>(
246
247 for (size_t i = 0; i < N; ++i)
248 (*_vector)->GetVector().push_back(arr[i]);
249 }
250
251 ValueType GetType() { return _type; }
252
253 operator bool() const
254 {
255 if (_type == ValueType::NONE)
256 return false;
257 if(_type == ValueType::BOOL)
258 return ToBool();
259 return true;
260 }
261
264 std::string GetString(bool add_quotes = true) const;
265
266 json GetJson() const;
267 public:
268 template <typename T>
269 bool _type_cast(T& ret, ValueType type) const
270 {
271 switch (_type)
272 {
274 ret = static_cast<T>(_bool);
275 return true;
277 ret = static_cast<T>(_char);
278 return true;
280 ret = static_cast<T>(_uchar);
281 return true;
283 ret = static_cast<T>(_short);
284 return true;
286 ret = static_cast<T>(_ushort);
287 return true;
289 ret = static_cast<T>(_int);
290 return true;
292 ret = static_cast<T>(_uint);
293 return true;
295 ret = static_cast<T>(_i64);
296 return true;
298 ret = static_cast<T>(_uint64);
299 return true;
301 ret = static_cast<T>(_float);
302 return true;
304 ret = static_cast<T>(_double);
305 return true;
307 break;
309 break;
311 break;
313 break;
315 break;
317 break;
319 break;
320 default:
321 break;
322 }
323 _log_cast_failed(type);
324 return false;
325 }
326
327
328 bool ToBool() const
329 {
330 bool ret;
331 if (!_type_cast(ret, ValueType::BOOL))
332 return false;
333 return ret;
334 }
335 char ToChar() const
336 {
337 char ret;
338 if (!_type_cast(ret, ValueType::CHAR))
339 return 0;
340 return ret;
341 }
342 unsigned char ToUChar() const
343 {
344 unsigned char ret;
345 if (!_type_cast(ret, ValueType::UCHAR))
346 return 0;
347 return ret;
348 }
349 short ToShort() const
350 {
351 short ret;
352 if (!_type_cast(ret, ValueType::SHORT))
353 return 0;
354 return ret;
355 }
356 unsigned short ToUShort() const
357 {
358 unsigned short ret;
359 if (!_type_cast(ret, ValueType::USHORT))
360 return 0;
361 return ret;
362 }
363 int ToInt() const
364 {
365 int ret;
366 if (!_type_cast(ret, ValueType::INT))
367 return 0;
368 return ret;
369 }
370 unsigned ToUInt() const
371 {
372 unsigned ret;
373 if (!_type_cast(ret, ValueType::UINT))
374 return 0;
375 return ret;
376 }
377 int64_t ToInt64() const
378 {
379 int64_t ret;
380 if (!_type_cast(ret, ValueType::INT64))
381 return 0;
382 return ret;
383 }
384 uint64_t ToUInt64() const
385 {
386 uint64_t ret;
387 if (!_type_cast(ret, ValueType::UINT64))
388 return 0;
389 return ret;
390 }
391 float ToFloat() const
392 {
393 float ret;
394 if (!_type_cast(ret, ValueType::FLOAT))
395 return 0;
396 return ret;
397 }
398 double ToDouble() const
399 {
400 double ret;
401 if (!_type_cast(ret, ValueType::DOUBLE))
402 return 0;
403 return ret;
404 }
405 void* ToPointer() const
406 {
407 if (_type != ValueType::POINTER)
408 {
409 _log_cast_failed(ValueType::POINTER);
410 return nullptr;
411 }
412 return _pointer;
413 }
414 std::string ToString() const
415 {
416 if (_type != ValueType::STRING)
417 {
418 _log_cast_failed(ValueType::STRING);
419 return {};
420 }
421 return *_string;
422 }
424 {
425 if (_type != ValueType::STRUCT)
426 {
427 _log_cast_failed(ValueType::STRUCT);
428 return NULL_VALUE_STRUCT;
429 }
430 return **_struct;
431 }
433 {
434 if (_type != ValueType::VECTOR)
435 {
436 _log_cast_failed(ValueType::VECTOR);
437 return NULL_VALUE_VECTOR;
438 }
439 return **_vector;
440 }
441 template<size_t N>
442 std::array<Value, N> ToArray() const
443 {
444 if (_type != ValueType::ARRAY
445 || N != (*_vector)->GetVector().size())
446 {
447 _log_cast_failed(ValueType::ARRAY);
448 return {};
449 }
450 std::array<Value, N> ret{};
451 for (size_t i = 0; i < N && i < (*_vector)->GetVector().size(); ++i)
452 ret[i] = *(_vector[i]);
453 return ret;
454 }
455 private:
456 void _log_cast_failed(ValueType type) const;
457
458 };
459
460
462 std::string_view to_string(ValueType v);
463
465 std::string to_string(const Value& v);
466
468 std::string_view Type2CppType(ValueType v);
469 std::string Type2CppTypeEx(ValueType v0, ValueType v1, size_t length);
470}
ValueStruct ToStruct() const
Value(int64_t v)
Value(float v)
unsigned short _ushort
unsigned char ToUChar() const
std::string * _string
int ToInt() const
unsigned ToUInt() const
void * ToPointer() const
Value(unsigned char v)
char ToChar() const
std::string ToString() const
Value(unsigned int v)
ValueVector ToVector() const
double ToDouble() const
short ToShort() const
unsigned char _uchar
Value(std::string_view str)
float ToFloat() const
json GetJson() const
uint64_t _uint64
bool _type_cast(T &ret, ValueType type) const
Value(const ValueStruct &s)
unsigned int _uint
ValueType GetType()
void * _pointer
unsigned short ToUShort() const
Value(double v)
int64_t ToInt64() const
Value(unsigned short v)
Value(const std::array< T, N > &arr)
Value(const ValueVector &vec)
SharedVector * _vector
uint64_t ToUInt64() const
Value(ValueType type)
Value(uint64_t v)
SharedStruct * _struct
Value & operator=(const Value &b)
Value(const char *str)
Value(const std::string &str)
bool ToBool() const
std::string GetString(bool add_quotes=true) const
Value(const Value &b)
Value(bool v)
Value(short v)
Value(char v)
std::array< Value, N > ToArray() const
Value(void *v)
ValueTable & GetMeta()
std::string GetName()
ValueStruct(std::string_view name)
Value & GetMember(std::string_view key)
Value & operator[](std::string_view key)
void AddMember(std::string_view key, Value &v)
ValueTable & GetMemberAll()
管理器,注册类型
ValueTable & GetStructMeta(std::string_view struct_name)
void RegisterStruct(std::string_view struct_name, const ValueTable &meta_table={})
变长数组(由于为空时类型丢失,需要成类)
std::vector< Value > & GetVector()
ValueVector(ValueType type)
Value & operator[](size_t i)
ValueType GetType()
void PushBack(const Value &v)
json格式相关
nlohmann::json json
日志系统
字符串相关
std::string_view Type2CppType(ValueType v)
类型转C++类型字符串
std::unordered_map< std::string, Value, string_hash, std::equal_to<> > ValueTable
std::string to_string(T v)
to_string包装
consteval ValueType ClassType2ValueType()
ValueTypeInfo String2ValueTypeEx(std::string_view str)
ValueSystem g_value
std::string Type2CppTypeEx(ValueType v0, ValueType v1, size_t length)
ValueType String2ValueType(std::string_view str)
const ValueVector NULL_VALUE_VECTOR
const ValueStruct NULL_VALUE_STRUCT
std::string _strType1