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 std::string* _string; // 除了string需要处理深拷贝,基础类型和智能指针不需要
172 SharedVector* _vector;
173 SharedStruct* _struct;
174 };
175 public:
176 Value() :_type{ ValueType::NONE }
177 {}
180 {
181 switch (_type)
182 {
184 delete _string;
185 break;
187 delete _struct;
188 break;
191 delete _vector;
192 break;
194
195 break;
196 default:
197 break;
198 }
199 }
200 Value(const Value& b);
201 Value(bool v) { _type = ValueType::BOOL; _bool = v; }
202 Value(char v) { _type = ValueType::CHAR; _char = v; }
203 Value(short v) { _type = ValueType::SHORT; _short = v; }
204 Value(int v) { _type = ValueType::INT; _int = v; }
205 Value(unsigned char v) { _type = ValueType::UCHAR; _uchar = v; }
206 Value(unsigned short v) { _type = ValueType::USHORT; _ushort = v; }
207 Value(unsigned int v) { _type = ValueType::UINT; _uint = v; }
208 Value(int64_t v) { _type = ValueType::INT64; _i64 = v; }
209 Value(uint64_t v) { _type = ValueType::UINT64; _uint64 = v; }
210 Value(float v) { _type = ValueType::FLOAT; _float = v; }
211 Value(double v) { _type = ValueType::DOUBLE; _double = v; }
212 Value(void* v) { _type = ValueType::POINTER; _pointer = v; }
213 Value(const char* str)
214 {
215 _type = ValueType::STRING;
216 _string = new std::string(str);
217 }
218 Value(const std::string& str)
219 {
220 _type = ValueType::STRING;
221 _string = new std::string(str);
222 }
223 Value(std::string_view str)
224 {
225 _type = ValueType::STRING;
226 _string = new std::string(str);
227 }
228 Value(const ValueVector& vec)
229 {
230 _type = ValueType::VECTOR;
231 _vector = new SharedVector(std::make_shared<ValueVector>(vec));
232 }
234 {
235 _type = ValueType::STRUCT;
236 _struct = new SharedStruct(std::make_shared<ValueStruct>(s));
237 }
238 template<typename T, size_t N>
239 Value(const std::array<T, N>& arr)
240 {
241 _type = ValueType::ARRAY;
242 _vector = new SharedVector(std::make_shared<ValueVector>(
244
245 for (size_t i = 0; i < N; ++i)
246 (*_vector)->GetVector().push_back(arr[i]);
247 }
248
249 ValueType GetType() { return _type; }
250
251 operator bool() const
252 {
253 if (_type == ValueType::NONE)
254 return false;
255 if(_type == ValueType::BOOL)
256 return ToBool();
257 return true;
258 }
259
262 std::string GetString(bool add_quotes = true) const;
263
264 json GetJson() const;
265 public:
266 template <typename T>
267 bool _type_cast(T& ret, ValueType type) const
268 {
269 switch (_type)
270 {
272 ret = static_cast<T>(_bool);
273 return true;
275 ret = static_cast<T>(_char);
276 return true;
278 ret = static_cast<T>(_uchar);
279 return true;
281 ret = static_cast<T>(_short);
282 return true;
284 ret = static_cast<T>(_ushort);
285 return true;
287 ret = static_cast<T>(_int);
288 return true;
290 ret = static_cast<T>(_uint);
291 return true;
293 ret = static_cast<T>(_i64);
294 return true;
296 ret = static_cast<T>(_uint64);
297 return true;
299 ret = static_cast<T>(_float);
300 return true;
302 ret = static_cast<T>(_double);
303 return true;
305 break;
307 break;
309 break;
311 break;
313 break;
315 break;
317 break;
318 default:
319 break;
320 }
321 _log_cast_failed(type);
322 return false;
323 }
324
325
326 bool ToBool() const
327 {
328 bool ret;
329 if (!_type_cast(ret, ValueType::BOOL))
330 return false;
331 return ret;
332 }
333 char ToChar() const
334 {
335 char ret;
336 if (!_type_cast(ret, ValueType::CHAR))
337 return 0;
338 return ret;
339 }
340 unsigned char ToUChar() const
341 {
342 unsigned char ret;
343 if (!_type_cast(ret, ValueType::UCHAR))
344 return 0;
345 return ret;
346 }
347 short ToShort() const
348 {
349 short ret;
350 if (!_type_cast(ret, ValueType::SHORT))
351 return 0;
352 return ret;
353 }
354 unsigned short ToUShort() const
355 {
356 unsigned short ret;
357 if (!_type_cast(ret, ValueType::USHORT))
358 return 0;
359 return ret;
360 }
361 int ToInt() const
362 {
363 int ret;
364 if (!_type_cast(ret, ValueType::INT))
365 return 0;
366 return ret;
367 }
368 unsigned ToUInt() const
369 {
370 unsigned ret;
371 if (!_type_cast(ret, ValueType::UINT))
372 return 0;
373 return ret;
374 }
375 int64_t ToInt64() const
376 {
377 int64_t ret;
378 if (!_type_cast(ret, ValueType::INT64))
379 return 0;
380 return ret;
381 }
382 uint64_t ToUInt64() const
383 {
384 uint64_t ret;
385 if (!_type_cast(ret, ValueType::UINT64))
386 return 0;
387 return ret;
388 }
389 float ToFloat() const
390 {
391 float ret;
392 if (!_type_cast(ret, ValueType::FLOAT))
393 return 0;
394 return ret;
395 }
396 double ToDouble() const
397 {
398 double ret;
399 if (!_type_cast(ret, ValueType::DOUBLE))
400 return 0;
401 return ret;
402 }
403 void* ToPointer() const
404 {
405 if (_type != ValueType::POINTER)
406 {
407 _log_cast_failed(ValueType::POINTER);
408 return nullptr;
409 }
410 return _pointer;
411 }
412 std::string ToString() const
413 {
414 if (_type != ValueType::STRING)
415 {
416 _log_cast_failed(ValueType::STRING);
417 return {};
418 }
419 return *_string;
420 }
422 {
423 if (_type != ValueType::STRUCT)
424 {
425 _log_cast_failed(ValueType::STRUCT);
426 return NULL_VALUE_STRUCT;
427 }
428 return **_struct;
429 }
431 {
432 if (_type != ValueType::VECTOR)
433 {
434 _log_cast_failed(ValueType::VECTOR);
435 return NULL_VALUE_VECTOR;
436 }
437 return **_vector;
438 }
439 template<size_t N>
440 std::array<Value, N> ToArray() const
441 {
442 if (_type != ValueType::ARRAY
443 || N != (*_vector)->GetVector().size())
444 {
445 _log_cast_failed(ValueType::ARRAY);
446 return {};
447 }
448 std::array<Value, N> ret{};
449 for (size_t i = 0; i < N && i < (*_vector)->GetVector().size(); ++i)
450 ret[i] = *(_vector[i]);
451 return ret;
452 }
453 private:
454 void _log_cast_failed(ValueType type) const;
455
456 };
457
458
460 std::string_view to_string(ValueType v);
461
463 std::string to_string(const Value& v);
464
466 std::string_view Type2CppType(ValueType v);
467 std::string Type2CppTypeEx(ValueType v0, ValueType v1, size_t length);
468}
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(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