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{
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 class ValueVector;
74 class ValueStruct;
76 class Value
77 {
78 private:
79 using SharedVector = std::shared_ptr<ValueVector>;
80 using SharedStruct = std::shared_ptr<ValueStruct>;
81 ValueType _type;
82 uint32_t _pad;
83 union
84 {
85 bool _bool;
86 char _char;
87 short _short;
88 int _int;
89 unsigned char _uchar;
90 unsigned short _ushort;
91 unsigned int _uint;
92 int64_t _i64;
93 uint64_t _uint64;
94 float _float;
95 double _double;
96 void* _pointer;
97
98 // 智能指针本身也在堆上,需要深拷贝
99 std::string* _string;
100 SharedVector* _vector;
101 SharedStruct* _struct;
102 };
103 public:
104 Value() :_type{ ValueType::NONE }
105 {}
108 {
109 switch (_type)
110 {
112 delete _string;
113 break;
115 delete _struct;
116 break;
119 delete _vector;
120 break;
122
123 break;
124 default:
125 break;
126 }
127 }
129 Value(const Value& b);
130 Value(bool v) { _type = ValueType::BOOL; _bool = v; }
131 Value(char v) { _type = ValueType::CHAR; _char = v; }
132 Value(short v) { _type = ValueType::SHORT; _short = v; }
133 Value(int v) { _type = ValueType::INT; _int = v; }
134 Value(unsigned char v) { _type = ValueType::UCHAR; _uchar = v; }
135 Value(unsigned short v) { _type = ValueType::USHORT; _ushort = v; }
136 Value(unsigned int v) { _type = ValueType::UINT; _uint = v; }
137 Value(int64_t v) { _type = ValueType::INT64; _i64 = v; }
138 Value(uint64_t v) { _type = ValueType::UINT64; _uint64 = v; }
139 Value(float v) { _type = ValueType::FLOAT; _float = v; }
140 Value(double v) { _type = ValueType::DOUBLE; _double = v; }
141 Value(void* v) { _type = ValueType::POINTER; _pointer = v; }
142 Value(const char* str)
143 {
144 _type = ValueType::STRING;
145 _string = new std::string(str);
146 }
147 Value(const std::string& str)
148 {
149 _type = ValueType::STRING;
150 _string = new std::string(str);
151 }
152 Value(std::string_view str)
153 {
154 _type = ValueType::STRING;
155 _string = new std::string(str);
156 }
157 Value(const ValueVector& vec)
158 {
159 _type = ValueType::VECTOR;
160 _vector = new SharedVector(std::make_shared<ValueVector>(vec));
161 }
163 {
164 _type = ValueType::STRUCT;
165 _struct = new SharedStruct(std::make_shared<ValueStruct>(s));
166 }
167 template<typename T, size_t N>
168 Value(const std::array<T, N>& arr)
169 {
170 _type = ValueType::ARRAY;
171 _vector = new SharedVector(std::make_shared<ValueVector>(
173
174 std::array<Value, N> arr_value;
175 for (size_t i = 0; i < N; ++i)
176 arr_value[i] = arr[i];
177 _from_array(arr_value);
178 }
179
180 ValueType GetType() { return _type; }
181
182 operator bool() const
183 {
184 if (_type == ValueType::NONE)
185 return false;
186 if(_type == ValueType::BOOL)
187 return ToBool();
188 return true;
189 }
190
193 std::string GetString(bool add_quotes = true) const;
194
195 json GetJson() const;
196 public:
197 template <typename T>
198 bool _type_cast(T& ret, ValueType type) const
199 {
200 switch (_type)
201 {
203 ret = static_cast<T>(_bool);
204 return true;
206 ret = static_cast<T>(_char);
207 return true;
209 ret = static_cast<T>(_uchar);
210 return true;
212 ret = static_cast<T>(_short);
213 return true;
215 ret = static_cast<T>(_ushort);
216 return true;
218 ret = static_cast<T>(_int);
219 return true;
221 ret = static_cast<T>(_uint);
222 return true;
224 ret = static_cast<T>(_i64);
225 return true;
227 ret = static_cast<T>(_uint64);
228 return true;
230 ret = static_cast<T>(_float);
231 return true;
233 ret = static_cast<T>(_double);
234 return true;
236 break;
238 break;
240 break;
242 break;
244 break;
246 break;
248 break;
249 default:
250 break;
251 }
252 _log_cast_failed(type);
253 return false;
254 }
255
256
257 bool ToBool() const
258 {
259 bool ret;
260 if (!_type_cast(ret, ValueType::BOOL))
261 return false;
262 return ret;
263 }
264 char ToChar() const
265 {
266 char ret;
267 if (!_type_cast(ret, ValueType::CHAR))
268 return 0;
269 return ret;
270 }
271 unsigned char ToUChar() const
272 {
273 unsigned char ret;
274 if (!_type_cast(ret, ValueType::UCHAR))
275 return 0;
276 return ret;
277 }
278 short ToShort() const
279 {
280 short ret;
281 if (!_type_cast(ret, ValueType::SHORT))
282 return 0;
283 return ret;
284 }
285 unsigned short ToUShort() const
286 {
287 unsigned short ret;
288 if (!_type_cast(ret, ValueType::USHORT))
289 return 0;
290 return ret;
291 }
292 int ToInt() const
293 {
294 int ret;
295 if (!_type_cast(ret, ValueType::INT))
296 return 0;
297 return ret;
298 }
299 unsigned ToUInt() const
300 {
301 unsigned ret;
302 if (!_type_cast(ret, ValueType::UINT))
303 return 0;
304 return ret;
305 }
306 int64_t ToInt64() const
307 {
308 int64_t ret;
309 if (!_type_cast(ret, ValueType::INT64))
310 return 0;
311 return ret;
312 }
313 uint64_t ToUInt64() const
314 {
315 uint64_t ret;
316 if (!_type_cast(ret, ValueType::UINT64))
317 return 0;
318 return ret;
319 }
320 float ToFloat() const
321 {
322 float ret;
323 if (!_type_cast(ret, ValueType::FLOAT))
324 return 0;
325 return ret;
326 }
327 double ToDouble() const
328 {
329 double ret;
330 if (!_type_cast(ret, ValueType::DOUBLE))
331 return 0;
332 return ret;
333 }
334 void* ToPointer() const
335 {
336 if (_type != ValueType::POINTER)
337 {
338 _log_cast_failed(ValueType::POINTER);
339 return nullptr;
340 }
341 return _pointer;
342 }
343 std::string ToString() const
344 {
345 if (_type != ValueType::STRING)
346 {
347 _log_cast_failed(ValueType::STRING);
348 return {};
349 }
350 return *_string;
351 }
352 const ValueStruct& ToStruct() const;
353 const ValueVector& ToVector() const;
354 template<size_t N>
355 std::array<Value, N> ToArray() const
356 {
357 if (!_check_array(N))
358 return {};
359 std::array<Value, N> ret{};
360 _to_array(ret);
361 return ret;
362 }
363 private:
364 void _log_cast_failed(ValueType type) const;
365
366 void _from_array(const std::span<Value>& arr);
367 void _to_array(const std::span<Value>& arr)const;
368 bool _check_array(size_t size) const;
369 };
370
371
372 using ValueTable = std::unordered_map<std::string, Value, string_hash, std::equal_to<>>;
373
376 {
377 public:
378 void RegisterStruct(std::string_view struct_name, const ValueTable& meta_table = {})
379 {
380 _metaStruct[std::string{ struct_name }] = meta_table;
381 }
382 ValueTable& GetStructMeta(std::string_view struct_name)
383 {
384 return _metaStruct[std::string{ struct_name }];
385 }
386 private:
387 std::unordered_map<std::string, ValueTable, string_hash, std::equal_to<>> _metaStruct;
388 };
389 // 安卓定义到全局区会有问题
390 extern ValueSystem* g_value;
391
392
395 {
396 public:
397 ValueStruct(std::string_view name) :
398 _name{ name }
399 {
400
401 }
402 Value& operator[](std::string_view key)
403 {
404 return GetMember(key);
405 }
406
407 Value& GetMember(std::string_view key);
408 void AddMember(std::string_view key, Value& v);
409 ValueTable& GetMemberAll() { return _member; }
410
411 std::string GetName() { return _name; }
412 ValueTable& GetMeta() { return g_value->GetStructMeta(_name); }
413 private:
414 std::string _name;
415 ValueTable _member;
416 };
417
420 {
421 public:
423 _type{ type }
424 {
425
426 }
427 Value& operator[](size_t i)
428 {
429 return _vec[i];
430 }
431
432
433 ValueType GetType() { return _type; }
434 std::vector<Value>& GetVector() { return _vec; }
435
436 void PushBack(const Value& v) { _vec.push_back(v); }
437 private:
438 ValueType _type;
439 std::vector<Value> _vec;
440 };
441
442
443 const ValueStruct NULL_VALUE_STRUCT = { "null struct" };
445
447 std::string_view to_string(ValueType v);
448
450 std::string to_string(const Value& v);
451
453 std::string_view Type2CppType(ValueType v);
454 std::string Type2CppTypeEx(ValueType v0, ValueType v1, size_t length);
455}
任意类型
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)
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
int64_t _i64
const ValueVector & ToVector() 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)
const ValueStruct & ToStruct() const
Value(uint64_t v)
SharedStruct * _struct
double _double
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++类型字符串
ValueType
实际类型
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)
std::string Type2CppTypeEx(ValueType v0, ValueType v1, size_t length)
ValueType String2ValueType(std::string_view str)
const ValueVector NULL_VALUE_VECTOR
ValueSystem * g_value
const ValueStruct NULL_VALUE_STRUCT
std::string _strType1