* 生成代码步进
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
#include "dl_json_config.h"
|
||||
|
||||
namespace dl
|
||||
{
|
||||
|
||||
std::string JsonConfig::GenerateCode()
|
||||
{
|
||||
std::string str;
|
||||
std::size_t tab_num = 0;// 当前tab数
|
||||
|
||||
// 写入换行
|
||||
auto endl = [&]() {
|
||||
str.push_back('\n');
|
||||
};
|
||||
// 写入tab
|
||||
auto tab = [&]() {
|
||||
for (std::size_t i = 0; i < tab_num; ++i)
|
||||
str += '\t';
|
||||
};
|
||||
|
||||
|
||||
auto fn_value = [&](auto&& fn_value, Value& v)->void {
|
||||
if (v.GetType() == ValueType::STRUCT)
|
||||
{
|
||||
ValueStruct s = v.ToStruct();
|
||||
tab(); str += fmt::format("//! {} {}", s["name"].ToString(), s["desc"].ToString()); endl();
|
||||
tab(); str += fmt::format("struct {}", s["type"].ToString()); endl();
|
||||
tab(); str += "{"; endl(); ++tab_num;
|
||||
|
||||
fn_value(fn_value, s["value"]);
|
||||
|
||||
tab(); str += "}"; endl(); --tab_num;
|
||||
}
|
||||
else
|
||||
{
|
||||
tab(); str += v.GetString(); endl();
|
||||
}
|
||||
};
|
||||
|
||||
fn_value(fn_value, _root);
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
dl::Value JsonConfig::_parse_node(json& j, std::string_view var)
|
||||
{
|
||||
ValueStruct s;
|
||||
|
||||
Node node;
|
||||
node._var = var;
|
||||
Json::Get(j, "name", node._name);
|
||||
Json::Get(j, "desc", node._desc);
|
||||
|
||||
std::string type;
|
||||
Json::Get(j, "type", type);
|
||||
|
||||
json j_value;
|
||||
Json::Get(j, "value", j_value);
|
||||
node._value = _parse_value(j_value, type);
|
||||
|
||||
s["var"] = var;
|
||||
s["type"] = type;
|
||||
s["name"] = node._name;
|
||||
s["desc"] = node._desc;
|
||||
s["value"] = node._value;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
dl::Value JsonConfig::_parse_value(json& j, std::string_view type)
|
||||
{
|
||||
ValueType value_type = String2ValueType(type);
|
||||
try
|
||||
{
|
||||
switch (value_type)
|
||||
{
|
||||
case dl::ValueType::BOOL:
|
||||
return j.get<bool>();
|
||||
case dl::ValueType::CHAR:
|
||||
return j.get<char>();
|
||||
case dl::ValueType::UCHAR:
|
||||
return j.get<unsigned char>();
|
||||
case dl::ValueType::SHORT:
|
||||
return j.get<short>();
|
||||
case dl::ValueType::USHORT:
|
||||
return j.get<unsigned short>();
|
||||
case dl::ValueType::INT:
|
||||
return j.get<int>();
|
||||
case dl::ValueType::UINT:
|
||||
return j.get<unsigned int>();
|
||||
case dl::ValueType::INT64:
|
||||
return j.get<int64_t>();
|
||||
case dl::ValueType::UINT64:
|
||||
return j.get<uint64_t>();
|
||||
case dl::ValueType::FLOAT:
|
||||
return j.get<float>();
|
||||
case dl::ValueType::DOUBLE:
|
||||
return j.get<double>();
|
||||
case dl::ValueType::STRUCT:
|
||||
{
|
||||
ValueStruct s;
|
||||
for (auto& iter : j.items())
|
||||
{
|
||||
Value v = _parse_node(iter.value(), iter.key());
|
||||
s[iter.key()] = v;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
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:
|
||||
default:
|
||||
assert(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (nlohmann::detail::exception& e)
|
||||
{
|
||||
log_err("json类型转换失败,请检查配置:({})\n"
|
||||
"{}\n"
|
||||
"{}", type, j.dump(4), e.what());
|
||||
}
|
||||
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -42,11 +42,7 @@ public:
|
||||
if (j.is_null())
|
||||
return false;
|
||||
|
||||
std::string type;
|
||||
if (!Json::Get(j, "type", type))
|
||||
return false;
|
||||
|
||||
//_nodeRoot = _parse_node(j);
|
||||
_root = _parse_node(j, "root");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -54,92 +50,15 @@ public:
|
||||
* @brief 生成C++代码
|
||||
* @note
|
||||
*/
|
||||
std::string GenerateCode() { return "TODO"; }
|
||||
std::string GenerateCode();
|
||||
|
||||
private:
|
||||
Node* _nodeRoot;
|
||||
Value _root;
|
||||
|
||||
Node* _parse_node(json& j, std::string_view var)
|
||||
{
|
||||
Node* node = new Node;
|
||||
node->_var = var;
|
||||
Json::Get(j, "name", node->_name);
|
||||
Json::Get(j, "desc", node->_desc);
|
||||
json j_value;
|
||||
std::string type;
|
||||
Json::Get(j, "type", type);
|
||||
node->_value = _parse_value(j_value, type);
|
||||
return node;
|
||||
}
|
||||
//! json、变量名(Node) -> Value
|
||||
Value _parse_node(json& j, std::string_view var);
|
||||
|
||||
Value _parse_value(json& j, std::string_view type)
|
||||
{
|
||||
Value ret;
|
||||
ValueType value_type = String2ValueType(type);
|
||||
|
||||
try
|
||||
{
|
||||
switch (value_type)
|
||||
{
|
||||
case dl::ValueType::BOOL:
|
||||
ret.FromInt(j.get<bool>());
|
||||
break;
|
||||
case dl::ValueType::CHAR:
|
||||
ret.FromInt(j.get<char>());
|
||||
break;
|
||||
case dl::ValueType::UCHAR:
|
||||
break;
|
||||
case dl::ValueType::SHORT:
|
||||
break;
|
||||
case dl::ValueType::USHORT:
|
||||
break;
|
||||
case dl::ValueType::INT:
|
||||
break;
|
||||
case dl::ValueType::UINT:
|
||||
break;
|
||||
case dl::ValueType::INT64:
|
||||
break;
|
||||
case dl::ValueType::UINT64:
|
||||
break;
|
||||
case dl::ValueType::FLOAT:
|
||||
break;
|
||||
case dl::ValueType::DOUBLE:
|
||||
break;
|
||||
case dl::ValueType::STRUCT:
|
||||
{
|
||||
std::vector<Value> vec_node;
|
||||
for (auto& iter : j)
|
||||
{
|
||||
Node* node = _parse_node(iter, "struct_x");
|
||||
Value v;
|
||||
v.FromPointer(node);
|
||||
vec_node.push_back(v);
|
||||
}
|
||||
ret.FromStruct(vec_node);
|
||||
}
|
||||
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:
|
||||
default:
|
||||
assert(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (nlohmann::detail::exception& e)
|
||||
{
|
||||
log_err("json类型转换失败,请检查配置:({})\n"
|
||||
"{}\n"
|
||||
"{}", type, j.dump(4), e.what());
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
//! json、变量类型 -> Value
|
||||
Value _parse_value(json& j, std::string_view type);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
#include "dl_value.h"
|
||||
|
||||
namespace dl
|
||||
{
|
||||
|
||||
std::string to_string(const Value& v)
|
||||
{
|
||||
return v.GetString();
|
||||
}
|
||||
|
||||
std::string Value::GetString() const
|
||||
{
|
||||
switch (_type)
|
||||
{
|
||||
case dl::ValueType::BOOL:
|
||||
break;
|
||||
case dl::ValueType::CHAR:
|
||||
break;
|
||||
case dl::ValueType::UCHAR:
|
||||
break;
|
||||
case dl::ValueType::SHORT:
|
||||
break;
|
||||
case dl::ValueType::USHORT:
|
||||
break;
|
||||
case dl::ValueType::INT:
|
||||
return std::to_string(ToInt());
|
||||
case dl::ValueType::UINT:
|
||||
break;
|
||||
case dl::ValueType::INT64:
|
||||
break;
|
||||
case dl::ValueType::UINT64:
|
||||
break;
|
||||
case dl::ValueType::FLOAT:
|
||||
break;
|
||||
case dl::ValueType::DOUBLE:
|
||||
break;
|
||||
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:
|
||||
default:
|
||||
break;
|
||||
}return "null";
|
||||
}
|
||||
|
||||
|
||||
void Value::_log_cast_failed(ValueType type) const
|
||||
{
|
||||
log_err("不能转换到类型:{} -> {}", to_string(_type), to_string(type));
|
||||
}
|
||||
|
||||
}
|
||||
+170
-54
@@ -12,6 +12,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "io/dl_log.h"
|
||||
|
||||
@@ -40,60 +42,6 @@ namespace dl
|
||||
MAP,
|
||||
};
|
||||
|
||||
// 任意类型
|
||||
class Value
|
||||
{
|
||||
private:
|
||||
union
|
||||
{
|
||||
bool _bool;
|
||||
int _int;
|
||||
float _float;
|
||||
void* _pointer;
|
||||
|
||||
std::string* _string;
|
||||
std::vector<Value*>* _vector;
|
||||
};
|
||||
ValueType _type;
|
||||
public:
|
||||
Value() :_type{ ValueType::NONE }
|
||||
{}
|
||||
|
||||
ValueType GetType() { return _type; }
|
||||
|
||||
void FromInt(int v)
|
||||
{
|
||||
_type = ValueType::INT;
|
||||
_int = v;
|
||||
}
|
||||
void FromFloat(float v)
|
||||
{
|
||||
_type = ValueType::FLOAT;
|
||||
_float = v;
|
||||
}
|
||||
void FromPointer(void* v)
|
||||
{
|
||||
_type = ValueType::POINTER;
|
||||
_pointer = v;
|
||||
}
|
||||
void FromStruct(const std::vector<Value>& vec)
|
||||
{
|
||||
_type = ValueType::STRUCT;
|
||||
//_vector = new(vec;
|
||||
}
|
||||
|
||||
int ToInt() const
|
||||
{
|
||||
assert(_type == ValueType::INT);
|
||||
return _int;
|
||||
}
|
||||
float ToFloat() const
|
||||
{
|
||||
assert(_type == ValueType::FLOAT);
|
||||
return _float;
|
||||
}
|
||||
};
|
||||
|
||||
inline ValueType String2ValueType(std::string_view str)
|
||||
{
|
||||
if (str == "int")
|
||||
@@ -107,6 +55,172 @@ namespace dl
|
||||
else
|
||||
return ValueType::STRUCT;
|
||||
}
|
||||
class Value;
|
||||
using ValueStruct = std::unordered_map<std::string, Value>;
|
||||
|
||||
// 任意类型
|
||||
class Value
|
||||
{
|
||||
|
||||
private:
|
||||
ValueType _type;
|
||||
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;
|
||||
std::vector<Value>* _vector;
|
||||
ValueStruct* _umap;
|
||||
};
|
||||
public:
|
||||
Value() :_type{ ValueType::NONE }
|
||||
{}
|
||||
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 std::string& str)
|
||||
{
|
||||
_type = ValueType::STRING;
|
||||
_string = new std::string(str);
|
||||
}
|
||||
Value(const std::string_view& str)
|
||||
{
|
||||
_type = ValueType::STRING;
|
||||
_string = new std::string(str);
|
||||
}
|
||||
Value(const ValueStruct& s)
|
||||
{
|
||||
_type = ValueType::STRUCT;
|
||||
_umap = new ValueStruct(s);
|
||||
}
|
||||
|
||||
ValueType GetType() { return _type; }
|
||||
|
||||
operator bool() const
|
||||
{
|
||||
return ToBool();
|
||||
}
|
||||
|
||||
std::string GetString() 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;
|
||||
}
|
||||
int ToInt() const
|
||||
{
|
||||
int ret;
|
||||
if (!_type_cast(ret, ValueType::INT))
|
||||
return false;
|
||||
return ret;
|
||||
}
|
||||
std::string ToString() const
|
||||
{
|
||||
if (_type != ValueType::STRING)
|
||||
{
|
||||
_log_cast_failed(ValueType::STRING);
|
||||
return {};
|
||||
}
|
||||
return *_string;
|
||||
}
|
||||
ValueStruct ToStruct() const
|
||||
{
|
||||
if (_type != ValueType::STRUCT)
|
||||
{
|
||||
_log_cast_failed(ValueType::STRUCT);
|
||||
return {};
|
||||
}
|
||||
return *_umap;
|
||||
}
|
||||
private:
|
||||
void _log_cast_failed(ValueType type) const;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
inline std::string_view to_string(ValueType v)
|
||||
{
|
||||
@@ -153,4 +267,6 @@ namespace dl
|
||||
}
|
||||
return "void";
|
||||
}
|
||||
|
||||
std::string to_string(const Value& v);
|
||||
}
|
||||
@@ -63,28 +63,15 @@ int main()
|
||||
//str += "};\n";
|
||||
//File::SaveString(str, "E:/logo.h");
|
||||
|
||||
std::string_view str_u8 = R"(老师的咖啡机收到了咖啡机
|
||||
收到冯老师肯德基菲拉斯
|
||||
Value v = 100;
|
||||
|
||||
第三方计算逻辑<><><> < |||\\\; ':"":";????///.,.!@#$%^&*()
|
||||
~
|
||||
`12234567890- = []\{}';\,*/-+
|
||||
鲁大师快捷方式~@@##¥ % …… % …… & *())—— + “ | ”:L ? > < M《》
|
||||
??。、。、、《《、、、||||
|
||||
胜多负少
|
||||
水电费垃圾分类第三方
|
||||
log_msg("{}", to_string(v));
|
||||
|
||||
阿斯蒂芬索拉卡到付件啊
|
||||
水电费水电费
|
||||
适得府君书)";
|
||||
std::string str = CodeCvt::Utf8ToMultiByte(str_u8);
|
||||
log_msg0(CodeCvt::MultiByteToUtf8(str));
|
||||
std::string_view str_sub = CodeCvt::ANSIExtend::GbkSub(str, 265);
|
||||
|
||||
log_msg0(CodeCvt::MultiByteToUtf8(str_sub));
|
||||
JsonConfig config;
|
||||
if (config.Load("json_config.json"))
|
||||
config.GenerateCode();
|
||||
{
|
||||
File::SaveString(config.GenerateCode(), "D:/out.h");
|
||||
}
|
||||
|
||||
Engine::Release();
|
||||
return 0;
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
"type": "tagSetting",
|
||||
"name": "设置配置",
|
||||
"desc": "客户端基本配置",
|
||||
"member": {
|
||||
"value": {
|
||||
"use_item": {
|
||||
"type": "bool",
|
||||
"value": false
|
||||
},
|
||||
"mail": {
|
||||
"type": "tagMail",
|
||||
"member": {
|
||||
"value": {
|
||||
"show_mail": {
|
||||
"type": "bool",
|
||||
"value": true,
|
||||
|
||||
Reference in New Issue
Block a user