110 lines
2.0 KiB
C++
110 lines
2.0 KiB
C++
/**
|
|
* @file dl_code_writer.h
|
|
* @brief 根据一定规则生成C++代码
|
|
*
|
|
*
|
|
* @version 1.0
|
|
* @author lveyou
|
|
* @date 23-08-27
|
|
*
|
|
* @note
|
|
*/
|
|
#pragma once
|
|
|
|
#include "io/dl_log.h"
|
|
#include "base/dl_vector2.h"
|
|
#include "base/dl_range_find.h"
|
|
|
|
namespace dl::CodeWriter
|
|
{
|
|
// 变量类型
|
|
enum class VariableType
|
|
{
|
|
BASE, // 基础
|
|
USER, // 自定义
|
|
ARRAY, // array
|
|
SINGLE, // 单值容器
|
|
PAIR, // 键值对
|
|
};
|
|
//
|
|
// int a = 3;
|
|
// Student a = {a,a};
|
|
// std::array<int, 3> a = {};
|
|
// std::vector<int> a = {};
|
|
// std::map<int, int> a = {};
|
|
|
|
//! 是标准库类型
|
|
constexpr bool IsStdType(std::string_view str)
|
|
{
|
|
return str == "string";
|
|
}
|
|
|
|
//! 如果是标准库类型,则添加std前缀
|
|
inline void AddStdNamespace(std::string& str)
|
|
{
|
|
if (IsStdType(str))
|
|
str = "std::" + str;
|
|
}
|
|
|
|
//! 类型转写
|
|
inline void ReplaceType(std::string& str)
|
|
{
|
|
if (str == "uchar")
|
|
str = "unsigned char";
|
|
else if (str == "int32")
|
|
str = "int32_t";
|
|
else if (str == "int64")
|
|
str = "int64_t";
|
|
}
|
|
|
|
// 变量
|
|
struct Variable
|
|
{
|
|
VariableType _typeVariable;// 变量类型
|
|
std::string _type;// 类型 map<int, int>
|
|
std::string _name;// 变量名 a
|
|
std::string _comment;// 注释
|
|
std::string _value;// 缺省值 1,2 3,4
|
|
};
|
|
// 结构体
|
|
struct Struct
|
|
{
|
|
std::string _comment;// 注释
|
|
std::string _name;// 类名
|
|
std::string _value;// 变量名
|
|
std::vector<Variable> _allVariable;// 所有成员
|
|
|
|
// 使所有变量对齐
|
|
void FormatVariable();
|
|
|
|
Variable* GetVariable(std::string_view name)
|
|
{
|
|
return Find::FirstYes(_allVariable, [name](const Variable& v) {
|
|
return v._name == name;
|
|
});
|
|
}
|
|
};
|
|
// 一段代码
|
|
struct Segment
|
|
{
|
|
void Output(std::string_view path_name);
|
|
Struct* GetStruct(std::string_view name)
|
|
{
|
|
return Find::FirstYes(_allStruct, [name](const Struct& s) {
|
|
return s._name == name;
|
|
});
|
|
}
|
|
Variable* GetVariable(std::string_view name0, std::string_view name1)
|
|
{
|
|
Struct* s = GetStruct(name0);
|
|
if (!s)
|
|
return nullptr;
|
|
return s->GetVariable(name1);
|
|
}
|
|
|
|
std::vector<Struct> _allStruct;// 所有成员
|
|
};
|
|
|
|
Segment* Create(const Vector2<std::string, size_t>& vec2);
|
|
|
|
} |