428 lines
9.5 KiB
C++
428 lines
9.5 KiB
C++
#include "dl_value.h"
|
|
|
|
namespace dl
|
|
{
|
|
|
|
ValueSystem* g_value;
|
|
|
|
dl::ValueType String2ValueType(std::string_view str)
|
|
{
|
|
if (str == "bool")
|
|
return ValueType::BOOL;
|
|
else if (str == "char")
|
|
return ValueType::CHAR;
|
|
else if (str == "uchar")
|
|
return ValueType::UCHAR;
|
|
else if (str == "short")
|
|
return ValueType::SHORT;
|
|
else if (str == "ushort")
|
|
return ValueType::USHORT;
|
|
else if (str == "int")
|
|
return ValueType::INT;
|
|
else if (str == "uint")
|
|
return ValueType::UINT;
|
|
else if (str == "int64")
|
|
return ValueType::INT64;
|
|
else if (str == "uint64")
|
|
return ValueType::UINT64;
|
|
else if (str == "float")
|
|
return ValueType::FLOAT;
|
|
else if (str == "double")
|
|
return ValueType::DOUBLE;
|
|
else if (str == "pointer")
|
|
return ValueType::POINTER;
|
|
else if (str == "string")
|
|
return ValueType::STRING;
|
|
else if (String::IsBegin(str, "array<"))
|
|
return ValueType::ARRAY;
|
|
else if (String::IsBegin(str, "vector<"))
|
|
return ValueType::VECTOR;
|
|
else
|
|
return ValueType::STRUCT;
|
|
}
|
|
|
|
|
|
ValueTypeInfo String2ValueTypeEx(std::string_view str)
|
|
{
|
|
ValueTypeInfo ret;
|
|
std::vector<std::string_view> vec = String::SplitView(str, '<');
|
|
if (vec.size() < 2)
|
|
return ret;
|
|
// 记录主类型
|
|
ret._type0 = String2ValueType(str);
|
|
if (vec[1].empty() || vec[1].back() != '>')
|
|
return ret;
|
|
vec[1] = vec[1].substr(0, vec[1].size() - 1);
|
|
|
|
std::vector<std::string_view> vec1 = String::SplitView(vec[1], ',');
|
|
if (vec1.size() > 0)
|
|
{
|
|
ret._strType1 = vec1[0];
|
|
ret._type1 = String2ValueType(vec1[0]);
|
|
}
|
|
if (vec1.size() > 1)
|
|
{
|
|
ret._length = std::atoi(vec1[1].data());
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
std::string to_string(const Value& v)
|
|
{
|
|
return v.GetString();
|
|
}
|
|
|
|
std::string_view to_string(ValueType v)
|
|
{
|
|
switch (v)
|
|
{
|
|
case dl::ValueType::NONE:
|
|
return "null";
|
|
case dl::ValueType::BOOL:
|
|
return "bool";
|
|
case dl::ValueType::CHAR:
|
|
return "char";
|
|
case dl::ValueType::UCHAR:
|
|
return "uchar";
|
|
case dl::ValueType::SHORT:
|
|
return "short";
|
|
case dl::ValueType::USHORT:
|
|
return "ushort";
|
|
case dl::ValueType::INT:
|
|
return "int";
|
|
case dl::ValueType::UINT:
|
|
return "uint";
|
|
case dl::ValueType::INT64:
|
|
return "int64";
|
|
case dl::ValueType::UINT64:
|
|
return "uint64";
|
|
case dl::ValueType::FLOAT:
|
|
return "float";
|
|
case dl::ValueType::DOUBLE:
|
|
return "double";
|
|
case dl::ValueType::POINTER:
|
|
return "pointer";
|
|
case dl::ValueType::STRUCT:
|
|
return "struct";
|
|
case dl::ValueType::STRING:
|
|
return "string";
|
|
case dl::ValueType::ARRAY:
|
|
return "array";
|
|
case dl::ValueType::VECTOR:
|
|
return "vector";
|
|
case dl::ValueType::MAP:
|
|
return "map";
|
|
default:
|
|
break;
|
|
}
|
|
return "void";
|
|
}
|
|
|
|
std::string_view Type2CppType(ValueType v)
|
|
{
|
|
switch (v)
|
|
{
|
|
case dl::ValueType::NONE:
|
|
return "void";
|
|
case dl::ValueType::BOOL:
|
|
return "bool";
|
|
case dl::ValueType::CHAR:
|
|
return "char";
|
|
case dl::ValueType::UCHAR:
|
|
return "unsigned char";
|
|
case dl::ValueType::SHORT:
|
|
return "short";
|
|
case dl::ValueType::USHORT:
|
|
return "unsigned short";
|
|
case dl::ValueType::INT:
|
|
return "int";
|
|
case dl::ValueType::UINT:
|
|
return "unsigned";
|
|
case dl::ValueType::INT64:
|
|
return "int64_t";
|
|
case dl::ValueType::UINT64:
|
|
return "uint64_t";
|
|
case dl::ValueType::FLOAT:
|
|
return "float";
|
|
case dl::ValueType::DOUBLE:
|
|
return "double";
|
|
case dl::ValueType::POINTER:
|
|
return "void*";
|
|
case dl::ValueType::STRUCT:
|
|
return "struct";
|
|
case dl::ValueType::STRING:
|
|
return "std::string";
|
|
case dl::ValueType::ARRAY:
|
|
return "std::array";
|
|
case dl::ValueType::VECTOR:
|
|
return "std::vector";
|
|
case dl::ValueType::MAP:
|
|
return "std::map";
|
|
default:
|
|
break;
|
|
}
|
|
return "void";
|
|
}
|
|
|
|
std::string Type2CppTypeEx(ValueType v0, ValueType v1, size_t length)
|
|
{
|
|
if(length == 0)
|
|
return fmt::format("{}<{}>", Type2CppType(v0), Type2CppType(v1));
|
|
else
|
|
return fmt::format("{}<{}, {}>", Type2CppType(v0), Type2CppType(v1), length);
|
|
}
|
|
|
|
Value& Value::operator=(const Value& b)
|
|
{
|
|
_type = b._type;
|
|
switch (_type)
|
|
{
|
|
case dl::ValueType::NONE:
|
|
case dl::ValueType::BOOL:
|
|
case dl::ValueType::CHAR:
|
|
case dl::ValueType::UCHAR:
|
|
case dl::ValueType::SHORT:
|
|
case dl::ValueType::USHORT:
|
|
case dl::ValueType::INT:
|
|
case dl::ValueType::UINT:
|
|
case dl::ValueType::INT64:
|
|
case dl::ValueType::UINT64:
|
|
case dl::ValueType::FLOAT:
|
|
case dl::ValueType::DOUBLE:
|
|
case dl::ValueType::POINTER:
|
|
case dl::ValueType::MAP:
|
|
_i64 = b._i64;
|
|
break;
|
|
case dl::ValueType::STRUCT:
|
|
_struct = new SharedStruct(*b._struct);
|
|
break;
|
|
case dl::ValueType::ARRAY:
|
|
case dl::ValueType::VECTOR:
|
|
_vector = new SharedVector(*b._vector);
|
|
break;
|
|
case dl::ValueType::STRING:
|
|
_string = new std::string(*b._string);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
Value::Value(ValueType type) :_type{ type }
|
|
{
|
|
switch (_type)
|
|
{
|
|
case dl::ValueType::BOOL:
|
|
case dl::ValueType::CHAR:
|
|
case dl::ValueType::UCHAR:
|
|
case dl::ValueType::SHORT:
|
|
case dl::ValueType::USHORT:
|
|
case dl::ValueType::INT:
|
|
case dl::ValueType::UINT:
|
|
case dl::ValueType::INT64:
|
|
case dl::ValueType::UINT64:
|
|
case dl::ValueType::FLOAT:
|
|
case dl::ValueType::DOUBLE:
|
|
case dl::ValueType::POINTER:
|
|
_i64 = 0;
|
|
break;
|
|
case dl::ValueType::STRUCT:
|
|
*this = new ValueStruct(NULL_VALUE_STRUCT);
|
|
break;
|
|
case dl::ValueType::STRING:
|
|
*this = new std::string("");
|
|
break;
|
|
case dl::ValueType::ARRAY:
|
|
*this = new ValueVector(NULL_VALUE_VECTOR);
|
|
break;
|
|
case dl::ValueType::VECTOR:
|
|
*this = new ValueVector(NULL_VALUE_VECTOR);
|
|
break;
|
|
case dl::ValueType::MAP:
|
|
break;
|
|
case dl::ValueType::NONE:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
Value::Value(const Value& b)
|
|
{
|
|
this->operator=(b);
|
|
}
|
|
|
|
std::string Value::GetString(bool add_quotes) const
|
|
{
|
|
switch (_type)
|
|
{
|
|
case dl::ValueType::BOOL:
|
|
return _bool ? "true" : "false";
|
|
case dl::ValueType::CHAR:
|
|
return std::to_string(_char);
|
|
case dl::ValueType::UCHAR:
|
|
return std::to_string(_uchar);
|
|
case dl::ValueType::SHORT:
|
|
return std::to_string(_short);
|
|
case dl::ValueType::USHORT:
|
|
return std::to_string(_ushort);
|
|
case dl::ValueType::INT:
|
|
return std::to_string(_int);
|
|
case dl::ValueType::UINT:
|
|
return std::to_string(_uint);
|
|
case dl::ValueType::INT64:
|
|
return std::to_string(_i64);
|
|
case dl::ValueType::UINT64:
|
|
return std::to_string(_uint64);
|
|
case dl::ValueType::FLOAT:
|
|
{
|
|
std::string ret = fmt::format("{:g}", _float);
|
|
if (Any::Equal(ret, '.'))
|
|
ret += 'f';
|
|
return ret;
|
|
}
|
|
case dl::ValueType::DOUBLE:
|
|
return std::to_string(_double);
|
|
case dl::ValueType::POINTER:
|
|
return "TODO this is pointer!";
|
|
case dl::ValueType::STRUCT:
|
|
return "TODO this is struct!";
|
|
case dl::ValueType::STRING:
|
|
return add_quotes ? String::AddQuotes(*_string) : *_string;
|
|
case dl::ValueType::ARRAY:
|
|
case dl::ValueType::VECTOR:
|
|
{
|
|
if ((*_vector)->GetVector().empty())
|
|
return "{}";
|
|
std::string ret;
|
|
ret += '{';
|
|
for (auto& iter : (*_vector)->GetVector())
|
|
{
|
|
ret += ' ';
|
|
ret += iter.GetString();
|
|
ret += ',';
|
|
}
|
|
ret.pop_back();
|
|
ret += " }";
|
|
return ret;
|
|
}
|
|
case dl::ValueType::MAP:
|
|
return "TODO this is map!";
|
|
case dl::ValueType::NONE:
|
|
default:
|
|
break;
|
|
}return "null";
|
|
}
|
|
|
|
|
|
json Value::GetJson() const
|
|
{
|
|
switch (_type)
|
|
{
|
|
case dl::ValueType::BOOL:
|
|
return ToBool();
|
|
case dl::ValueType::CHAR:
|
|
return ToChar();
|
|
case dl::ValueType::UCHAR:
|
|
return ToUChar();
|
|
case dl::ValueType::SHORT:
|
|
return ToShort();
|
|
case dl::ValueType::USHORT:
|
|
return ToUShort();
|
|
case dl::ValueType::INT:
|
|
return ToInt();
|
|
case dl::ValueType::UINT:
|
|
return ToUInt();
|
|
case dl::ValueType::INT64:
|
|
return ToInt64();
|
|
case dl::ValueType::UINT64:
|
|
return ToUInt64();
|
|
case dl::ValueType::FLOAT:
|
|
return ToFloat();
|
|
case dl::ValueType::DOUBLE:
|
|
return ToDouble();
|
|
case dl::ValueType::STRING:
|
|
return ToString();
|
|
case dl::ValueType::ARRAY:
|
|
{
|
|
json ret = json::array();
|
|
for (auto& iter : (*_vector)->GetVector())
|
|
ret.push_back(iter.GetJson());
|
|
return ret;
|
|
}
|
|
case dl::ValueType::VECTOR:
|
|
{
|
|
json ret = json::array();
|
|
for (auto& iter : (*_vector)->GetVector())
|
|
ret.push_back(iter.GetJson());
|
|
return ret;
|
|
}
|
|
case dl::ValueType::POINTER:
|
|
case dl::ValueType::STRUCT:
|
|
case dl::ValueType::MAP:
|
|
case dl::ValueType::NONE:
|
|
default:
|
|
break;
|
|
}
|
|
_log_cast_failed(_type);
|
|
return false;
|
|
}
|
|
|
|
void Value::_log_cast_failed(ValueType type) const
|
|
{
|
|
log_err("不能转换到类型:{} {} -> {}", GetString(), to_string(_type), to_string(type));
|
|
}
|
|
|
|
const ValueStruct& Value::ToStruct() const
|
|
{
|
|
if (_type != ValueType::STRUCT)
|
|
{
|
|
_log_cast_failed(ValueType::STRUCT);
|
|
return NULL_VALUE_STRUCT;
|
|
}
|
|
return **_struct;
|
|
}
|
|
const ValueVector& Value::ToVector() const
|
|
{
|
|
if (_type != ValueType::VECTOR)
|
|
{
|
|
_log_cast_failed(ValueType::VECTOR);
|
|
return NULL_VALUE_VECTOR;
|
|
}
|
|
return **_vector;
|
|
}
|
|
|
|
void Value::_from_array(const std::span<Value>& arr)
|
|
{
|
|
for (size_t i = 0; i < arr.size(); ++i)
|
|
(*_vector)->GetVector().push_back(arr[i]);
|
|
}
|
|
void Value::_to_array(const std::span<Value>& arr)const
|
|
{
|
|
for (size_t i = 0; i < arr.size() && i < (*_vector)->GetVector().size(); ++i)
|
|
arr[i] = (*_vector)->GetVector()[i];
|
|
}
|
|
bool Value::_check_array(size_t size) const
|
|
{
|
|
if (_type != ValueType::ARRAY
|
|
|| size != (*_vector)->GetVector().size())
|
|
{
|
|
_log_cast_failed(ValueType::ARRAY);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
dl::Value& ValueStruct::GetMember(std::string_view key)
|
|
{
|
|
return _member[std::string{ key }];
|
|
}
|
|
|
|
void ValueStruct::AddMember(std::string_view key, Value& v)
|
|
{
|
|
_member[std::string{ key }] = v;
|
|
}
|
|
|
|
|
|
} |