454 lines
9.3 KiB
C++
454 lines
9.3 KiB
C++
/**
|
|
* @file dl_value.h
|
|
* @brief 任意类型
|
|
*
|
|
*
|
|
* @version 1.0
|
|
* @author lveyou
|
|
* @date 24-11-21
|
|
*
|
|
* @note 统一表示类型
|
|
*/
|
|
#pragma once
|
|
|
|
#include <vector>
|
|
#include <cassert>
|
|
#include <unordered_map>
|
|
|
|
#include "io/dl_log.h"
|
|
#include "base/dl_string.h"
|
|
#include "io/dl_json.h"
|
|
|
|
namespace dl
|
|
{
|
|
//! 实际类型
|
|
enum class ValueType
|
|
{
|
|
NONE,
|
|
BOOL,
|
|
CHAR,
|
|
UCHAR,
|
|
SHORT,
|
|
USHORT,
|
|
INT,
|
|
UINT,
|
|
INT64,
|
|
UINT64,
|
|
FLOAT,
|
|
DOUBLE,
|
|
POINTER,
|
|
STRUCT,
|
|
STRING,
|
|
ARRAY, // 底层为vector,表现为长度固定,有默认值
|
|
VECTOR, // 为空时类型不确定
|
|
MAP,
|
|
};
|
|
//! 类型信息
|
|
struct ValueTypeInfo
|
|
{
|
|
ValueType _type0;
|
|
ValueType _type1;
|
|
size_t _length;
|
|
std::string _strType1;
|
|
|
|
ValueTypeInfo() :
|
|
_type0{ ValueType::NONE },
|
|
_type1{ ValueType::NONE },
|
|
_length{ 0 }
|
|
{}
|
|
};
|
|
|
|
ValueType String2ValueType(std::string_view str);
|
|
ValueTypeInfo String2ValueTypeEx(std::string_view str);
|
|
|
|
template<typename T>
|
|
consteval ValueType ClassType2ValueType()
|
|
{
|
|
if constexpr (std::is_same<T, bool>::value)
|
|
return ValueType::BOOL;
|
|
else
|
|
return ValueType::STRUCT;
|
|
}
|
|
|
|
class ValueVector;
|
|
class ValueStruct;
|
|
//! 任意类型
|
|
class Value
|
|
{
|
|
private:
|
|
using SharedVector = std::shared_ptr<ValueVector>;
|
|
using SharedStruct = std::shared_ptr<ValueStruct>;
|
|
ValueType _type;
|
|
uint32_t _pad;
|
|
union
|
|
{
|
|
bool _bool;
|
|
char _char;
|
|
short _short;
|
|
int _int;
|
|
unsigned char _uchar;
|
|
unsigned short _ushort;
|
|
unsigned int _uint;
|
|
int64_t _i64;
|
|
uint64_t _uint64;
|
|
float _float;
|
|
double _double;
|
|
void* _pointer;
|
|
|
|
// 智能指针本身也在堆上,需要深拷贝
|
|
std::string* _string;
|
|
SharedVector* _vector;
|
|
SharedStruct* _struct;
|
|
};
|
|
public:
|
|
Value() :_type{ ValueType::NONE }
|
|
{}
|
|
Value(ValueType type);
|
|
~Value()
|
|
{
|
|
switch (_type)
|
|
{
|
|
case dl::ValueType::STRING:
|
|
delete _string;
|
|
break;
|
|
case dl::ValueType::STRUCT:
|
|
delete _struct;
|
|
break;
|
|
case dl::ValueType::VECTOR:
|
|
case dl::ValueType::ARRAY:
|
|
delete _vector;
|
|
break;
|
|
case dl::ValueType::MAP:
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
Value& operator=(const Value& b);
|
|
Value(const Value& b);
|
|
Value(bool v) { _type = ValueType::BOOL; _bool = v; }
|
|
Value(char v) { _type = ValueType::CHAR; _char = v; }
|
|
Value(short v) { _type = ValueType::SHORT; _short = v; }
|
|
Value(int v) { _type = ValueType::INT; _int = v; }
|
|
Value(unsigned char v) { _type = ValueType::UCHAR; _uchar = v; }
|
|
Value(unsigned short v) { _type = ValueType::USHORT; _ushort = v; }
|
|
Value(unsigned int v) { _type = ValueType::UINT; _uint = v; }
|
|
Value(int64_t v) { _type = ValueType::INT64; _i64 = v; }
|
|
Value(uint64_t v) { _type = ValueType::UINT64; _uint64 = v; }
|
|
Value(float v) { _type = ValueType::FLOAT; _float = v; }
|
|
Value(double v) { _type = ValueType::DOUBLE; _double = v; }
|
|
Value(void* v) { _type = ValueType::POINTER; _pointer = v; }
|
|
Value(const char* str)
|
|
{
|
|
_type = ValueType::STRING;
|
|
_string = new std::string(str);
|
|
}
|
|
Value(const std::string& str)
|
|
{
|
|
_type = ValueType::STRING;
|
|
_string = new std::string(str);
|
|
}
|
|
Value(std::string_view str)
|
|
{
|
|
_type = ValueType::STRING;
|
|
_string = new std::string(str);
|
|
}
|
|
Value(const ValueVector& vec)
|
|
{
|
|
_type = ValueType::VECTOR;
|
|
_vector = new SharedVector(std::make_shared<ValueVector>(vec));
|
|
}
|
|
Value(const ValueStruct& s)
|
|
{
|
|
_type = ValueType::STRUCT;
|
|
_struct = new SharedStruct(std::make_shared<ValueStruct>(s));
|
|
}
|
|
template<typename T, size_t N>
|
|
Value(const std::array<T, N>& arr)
|
|
{
|
|
_type = ValueType::ARRAY;
|
|
_vector = new SharedVector(std::make_shared<ValueVector>(
|
|
ClassType2ValueType<T>()));
|
|
|
|
std::array<Value, N> arr_value;
|
|
for (size_t i = 0; i < N; ++i)
|
|
arr_value[i] = arr[i];
|
|
_from_array(arr_value);
|
|
}
|
|
|
|
ValueType GetType() { return _type; }
|
|
|
|
operator bool() const
|
|
{
|
|
if (_type == ValueType::NONE)
|
|
return false;
|
|
if(_type == ValueType::BOOL)
|
|
return ToBool();
|
|
return true;
|
|
}
|
|
|
|
//! 值的显示字符串
|
|
//! add_quotes 是否给字符串加上双引号
|
|
std::string GetString(bool add_quotes = true) const;
|
|
|
|
json GetJson() const;
|
|
public:
|
|
template <typename T>
|
|
bool _type_cast(T& ret, ValueType type) const
|
|
{
|
|
switch (_type)
|
|
{
|
|
case dl::ValueType::BOOL:
|
|
ret = static_cast<T>(_bool);
|
|
return true;
|
|
case dl::ValueType::CHAR:
|
|
ret = static_cast<T>(_char);
|
|
return true;
|
|
case dl::ValueType::UCHAR:
|
|
ret = static_cast<T>(_uchar);
|
|
return true;
|
|
case dl::ValueType::SHORT:
|
|
ret = static_cast<T>(_short);
|
|
return true;
|
|
case dl::ValueType::USHORT:
|
|
ret = static_cast<T>(_ushort);
|
|
return true;
|
|
case dl::ValueType::INT:
|
|
ret = static_cast<T>(_int);
|
|
return true;
|
|
case dl::ValueType::UINT:
|
|
ret = static_cast<T>(_uint);
|
|
return true;
|
|
case dl::ValueType::INT64:
|
|
ret = static_cast<T>(_i64);
|
|
return true;
|
|
case dl::ValueType::UINT64:
|
|
ret = static_cast<T>(_uint64);
|
|
return true;
|
|
case dl::ValueType::FLOAT:
|
|
ret = static_cast<T>(_float);
|
|
return true;
|
|
case dl::ValueType::DOUBLE:
|
|
ret = static_cast<T>(_double);
|
|
return true;
|
|
case dl::ValueType::POINTER:
|
|
break;
|
|
case dl::ValueType::STRUCT:
|
|
break;
|
|
case dl::ValueType::STRING:
|
|
break;
|
|
case dl::ValueType::ARRAY:
|
|
break;
|
|
case dl::ValueType::VECTOR:
|
|
break;
|
|
case dl::ValueType::MAP:
|
|
break;
|
|
case dl::ValueType::NONE:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
_log_cast_failed(type);
|
|
return false;
|
|
}
|
|
|
|
|
|
bool ToBool() const
|
|
{
|
|
bool ret;
|
|
if (!_type_cast(ret, ValueType::BOOL))
|
|
return false;
|
|
return ret;
|
|
}
|
|
char ToChar() const
|
|
{
|
|
char ret;
|
|
if (!_type_cast(ret, ValueType::CHAR))
|
|
return 0;
|
|
return ret;
|
|
}
|
|
unsigned char ToUChar() const
|
|
{
|
|
unsigned char ret;
|
|
if (!_type_cast(ret, ValueType::UCHAR))
|
|
return 0;
|
|
return ret;
|
|
}
|
|
short ToShort() const
|
|
{
|
|
short ret;
|
|
if (!_type_cast(ret, ValueType::SHORT))
|
|
return 0;
|
|
return ret;
|
|
}
|
|
unsigned short ToUShort() const
|
|
{
|
|
unsigned short ret;
|
|
if (!_type_cast(ret, ValueType::USHORT))
|
|
return 0;
|
|
return ret;
|
|
}
|
|
int ToInt() const
|
|
{
|
|
int ret;
|
|
if (!_type_cast(ret, ValueType::INT))
|
|
return 0;
|
|
return ret;
|
|
}
|
|
unsigned ToUInt() const
|
|
{
|
|
unsigned ret;
|
|
if (!_type_cast(ret, ValueType::UINT))
|
|
return 0;
|
|
return ret;
|
|
}
|
|
int64_t ToInt64() const
|
|
{
|
|
int64_t ret;
|
|
if (!_type_cast(ret, ValueType::INT64))
|
|
return 0;
|
|
return ret;
|
|
}
|
|
uint64_t ToUInt64() const
|
|
{
|
|
uint64_t ret;
|
|
if (!_type_cast(ret, ValueType::UINT64))
|
|
return 0;
|
|
return ret;
|
|
}
|
|
float ToFloat() const
|
|
{
|
|
float ret;
|
|
if (!_type_cast(ret, ValueType::FLOAT))
|
|
return 0;
|
|
return ret;
|
|
}
|
|
double ToDouble() const
|
|
{
|
|
double ret;
|
|
if (!_type_cast(ret, ValueType::DOUBLE))
|
|
return 0;
|
|
return ret;
|
|
}
|
|
void* ToPointer() const
|
|
{
|
|
if (_type != ValueType::POINTER)
|
|
{
|
|
_log_cast_failed(ValueType::POINTER);
|
|
return nullptr;
|
|
}
|
|
return _pointer;
|
|
}
|
|
std::string ToString() const
|
|
{
|
|
if (_type != ValueType::STRING)
|
|
{
|
|
_log_cast_failed(ValueType::STRING);
|
|
return {};
|
|
}
|
|
return *_string;
|
|
}
|
|
const ValueStruct& ToStruct() const;
|
|
const ValueVector& ToVector() const;
|
|
template<size_t N>
|
|
std::array<Value, N> ToArray() const
|
|
{
|
|
if (!_check_array(N))
|
|
return {};
|
|
std::array<Value, N> ret{};
|
|
_to_array(ret);
|
|
return ret;
|
|
}
|
|
private:
|
|
void _log_cast_failed(ValueType type) const;
|
|
|
|
void _from_array(const std::span<Value>& arr);
|
|
void _to_array(const std::span<Value>& arr)const;
|
|
bool _check_array(size_t size) const;
|
|
};
|
|
|
|
|
|
using ValueTable = std::unordered_map<std::string, Value, string_hash, std::equal_to<>>;
|
|
|
|
//! 管理器,注册类型
|
|
class ValueSystem
|
|
{
|
|
public:
|
|
void RegisterStruct(std::string_view struct_name, const ValueTable& meta_table = {})
|
|
{
|
|
_metaStruct[std::string{ struct_name }] = meta_table;
|
|
}
|
|
ValueTable& GetStructMeta(std::string_view struct_name)
|
|
{
|
|
return _metaStruct[std::string{ struct_name }];
|
|
}
|
|
private:
|
|
std::unordered_map<std::string, ValueTable, string_hash, std::equal_to<>> _metaStruct;
|
|
};
|
|
extern ValueSystem g_value;
|
|
|
|
|
|
//! 结构体
|
|
class ValueStruct
|
|
{
|
|
public:
|
|
ValueStruct(std::string_view name) :
|
|
_name{ name }
|
|
{
|
|
|
|
}
|
|
Value& operator[](std::string_view key)
|
|
{
|
|
return GetMember(key);
|
|
}
|
|
|
|
Value& GetMember(std::string_view key);
|
|
void AddMember(std::string_view key, Value& v);
|
|
ValueTable& GetMemberAll() { return _member; }
|
|
|
|
std::string GetName() { return _name; }
|
|
ValueTable& GetMeta() { return g_value.GetStructMeta(_name); }
|
|
private:
|
|
std::string _name;
|
|
ValueTable _member;
|
|
};
|
|
|
|
//! 变长数组(由于为空时类型丢失,需要成类)
|
|
class ValueVector
|
|
{
|
|
public:
|
|
ValueVector(ValueType type) :
|
|
_type{ type }
|
|
{
|
|
|
|
}
|
|
Value& operator[](size_t i)
|
|
{
|
|
return _vec[i];
|
|
}
|
|
|
|
|
|
ValueType GetType() { return _type; }
|
|
std::vector<Value>& GetVector() { return _vec; }
|
|
|
|
void PushBack(const Value& v) { _vec.push_back(v); }
|
|
private:
|
|
ValueType _type;
|
|
std::vector<Value> _vec;
|
|
};
|
|
|
|
|
|
const ValueStruct NULL_VALUE_STRUCT = { "null struct" };
|
|
const ValueVector NULL_VALUE_VECTOR = { ValueType::NONE };
|
|
|
|
//! 类型转字符串
|
|
std::string_view to_string(ValueType v);
|
|
|
|
//! 值转字符串
|
|
std::string to_string(const Value& v);
|
|
|
|
//! 类型转C++类型字符串
|
|
std::string_view Type2CppType(ValueType v);
|
|
std::string Type2CppTypeEx(ValueType v0, ValueType v1, size_t length);
|
|
} |