c/c++ 都用的宏定义,乱七八糟的动态生成类,函数,方法等等, 看的乱七八糟的, 眼花缭乱,稀里糊涂, 实在懒得折腾了,而且宏定义太死板了,不能人性化处理, 虽然功能强大,但是累, 各种语言任性一点都是类生成器, 各种骚操作,都21世纪了,应该改改了, 向前看,所以操刀杀入重围,自己弄了个主要实体对象生成处理类, 懒得敲代码,累, 个别地方稍微改改,加加就行了,生成标准版就行了.下面来一起见证奇迹吧,哈哈哈…
/* *****************************************************************
* 类功能说明: Class 构建器[C/C++标准版]
* @author : lch
***************************************************************** */
#pragma once
#include
#include
#include "class/builder/InfoEntitys.h"
#include
#include
using namespace std;
class ClassBuilder {
public:
ClassBuilder(std::string className, std::string author,
std::string rooPath, std::string headPack, std::string cppPack,
vector<string>* includeList, vector<InfoEntity*>* list,
vector<string>* extend = NULL,
bool isBuilderAll = true, bool isToString = true,
bool isCppFile = true, bool isCover = true, bool isChain = false,
bool isDefine = false, bool isAnnotation = true,
string charset = "GB2312",
std::string cppReplaceIncludeString = "include/"
);
~ClassBuilder();
// 换行
static std::string BR;
// Tab
static std::string TAB;
// 设置 ClassBuilder 扩展回调接口
void setClassBuilderExtend(ClassBuilderExtend* classBuilderExtend);
// 创建c/c++文件
void create();
// 创建 h 文件
void h();
// 创建cpp文件
void cpp();
// 保存文件
void sava();
// 保存文件
void savaFile(string& str, string& path, string& charset);
///
/// 批量创建class entity
///
/// ClassBuilder信息
/// 批量类对象
static void listBuilder(ClassBuilder* classBuilder, vector<InfoEntitys*>* list);
// 默认值格式化
string defValFomat(InfoEntity* entity);
// c++字符串格式化
string valFormat(InfoEntity* entity);
// 文件是否操作
bool isFileExists(string& name);
// 创建多级目录
bool mkdirAll(string& path);
// 检查文件和目录是否操作,不存在创建
void checkedFileCreate(string& path, string& fileName);
// 获取格式化当前日期
string getNowTime();
// 首字母大写
string toFristUpper(std::string& str);
// 查找当前列表是否存在指定字符串
bool find(vector<string>& list, string& str);
// 查找当前列表是否存在指定字符串
bool find(vector<string>* list, string* str);
private:
// 类名
std::string className;
// 用户
std::string author;
// 根瘤菌
std::string rooPath;
// .h 文件包目录
std::string headPack;
// .cpp 文件包目录
std::string cppPack;
// .cpp 文件引入.h文件要替换文字 默认:include/
std::string cppReplaceIncludeString = "include/";
// 类继承
vector<string>* extend;
// 引入头
vector<string>* includeList;
// 字段信息
vector<InfoEntity*>* list;
// 是否构建全属性
bool isBuilderAll = true;
// 构建 toString 方法
bool isToString = true;
// 是否创建cpp文件
bool isCppFile = true;
// 文件是否覆盖
bool isCover = false;
// set 方法是否链式调用
bool isChain = false;
// 是否创建 define 避免重复编译即: #pragma once 或: #ifndef _SOMEFILE_H_ #define _SOMEFILE_H_ ... #endif
bool isDefine = false;
// 注释
bool isAnnotation = true;
//保存c++ 保存编码格式 默认 GB2312
string charset = "GB2312";
// .h 文件
std::string* hString;
// .cpp 文件
std::string* cppString;
// 设置ClassBuilder扩展实现类
ClassBuilderExtend* classBuilderExtend;
};
#include "class/builder/ClassBuilder.h"
#include "service/Logger.h"
#include
#include
#include
#include
#include
#include
#include
string ClassBuilder::BR = "\n";
string ClassBuilder::TAB = "\t";
ClassBuilder::ClassBuilder(string className, string author,
string rooPath, string headPack, string cppPack,
vector<string>* includeList, vector<InfoEntity*>* list,
vector<string>* extend,
bool isBuilderAll, bool isToString, bool isCppFile,
bool isCover, bool isChain, bool isDefine, bool isAnnotation,
string charset,
string cppReplaceIncludeString) {
this->className = className;
this->author = author;
this->rooPath = rooPath;
this->headPack = headPack;
this->cppPack = cppPack;
this->includeList = includeList;
this->list = list;
this->charset = charset;
this->cppReplaceIncludeString = cppReplaceIncludeString;
this->extend = extend;
this->isBuilderAll = isBuilderAll;
this->isToString = isToString;
this->isCppFile = isCppFile;
this->isCover = isCover;
this->isChain = isChain;
this->isDefine = isDefine;
this->isAnnotation = isAnnotation;
}
ClassBuilder::~ClassBuilder() {
delete hString;
delete cppString;
delete includeList;
delete list;
}
void ClassBuilder::setClassBuilderExtend(ClassBuilderExtend* classBuilderExtend) {
this->classBuilderExtend = classBuilderExtend;
}
void ClassBuilder::create() {
hString = new string();
if (this->className.empty()) {
assert("className 不能为null");
return;
}
// 构建h文件数据信息
this->h();
// 构建cpp文件数据信息
this->cpp();
}
void ClassBuilder::h() {
// 去重
//wxArrayString* s1 = new wxArrayString();
//int i = 0, j = 0, len = includeList->GetCount();
//s1->Add(includeList->Item(0));
//int counter = 1; // 记录t数组中存了多少元素
//includeList->Sort([](wxString* first,wxString* second) {
// return first->compare(second->c_str());
//});
去重
//while (i < len-1) {
// if (includeList->Item(i) != includeList->Item(++j)) {
// i = j;
// s1->Add(includeList->Item(i)); // 不重复的存入t数组,然后counter加一
// counter++;
// }
//}
hString->append(ClassBuilder::BR);
/* *****************************************************************
* 类功能说明: h 头文件定义
* @author : lch
***************************************************************** */
// 创建头部
hString->append("/* *****************************************************************" + ClassBuilder::BR);
hString->append("* 类功能说明: " + className + " 头文件定义" + ClassBuilder::BR);
hString->append("* @author : " + author + ClassBuilder::BR);
hString->append("* @date : " + getNowTime() + ClassBuilder::BR);
hString->append("***************************************************************** */" + ClassBuilder::BR);
hString->append(ClassBuilder::BR);
if (isDefine) {
//hString->append("#ifndef __SOMEFILE_H__" + ClassBuilder::BR);
//hString->append("#define __SOMEFILE_H__" + ClassBuilder::BR + ClassBuilder::BR);
hString->append("#pragma once" + ClassBuilder::BR + ClassBuilder::BR);
}
// 头部#include
for (int i = 0; i < includeList->size(); i++) {
if (includeList->at(i).find("<") == 0) {
hString->append("#include " + includeList->at(i) + ClassBuilder::BR);
}
else {
hString->append("#include \"" + includeList->at(i) + "\"" + ClassBuilder::BR);
}
}
// 引入StringUtil文件
if (isToString) {
hString->append("#include \"class/builder/StringUtil.h\"" + ClassBuilder::BR);
}
// 用户扩展
if (classBuilderExtend != NULL) {
classBuilderExtend->include(hString, className);
}
if (!find(includeList, new string("" ))) {
hString->append("#include " + ClassBuilder::BR);
hString->append("using namespace std;" + ClassBuilder::BR);
}
else {
hString->append("using namespace std;" + ClassBuilder::BR);
}
hString->append(ClassBuilder::BR);
// 创建类注释
if (isAnnotation) {
hString->append("/**" + ClassBuilder::BR);
hString->append("* " + className + ClassBuilder::BR);
hString->append("* @author :" + author + ClassBuilder::BR);
hString->append("* @date :" + getNowTime() + ClassBuilder::BR);
hString->append("*/" + ClassBuilder::BR);
}
// 创建类
hString->append("class " + className);
if (extend != NULL && !extend->empty()) {
hString->append(" : ");
for (int i = 0; i < extend->size(); i++) {
hString->append(extend->at(i) + (i < extend->size() - 1 ? ", " : ""));
}
}
hString->append(" { " + ClassBuilder::BR);
// 创建构造器(无参数)
hString->append("public:" + ClassBuilder::BR);
if (isAnnotation) {
hString->append(ClassBuilder::TAB + " // " + className + " 默认构造器" + ClassBuilder::BR);
}
hString->append(ClassBuilder::TAB + className + "();" + ClassBuilder::BR);
// 创建构造器(全参数)
InfoEntity* entity;
if (isAnnotation) {
hString->append(ClassBuilder::TAB + " // " + className + " 全参数构造器" + ClassBuilder::BR);
}
hString->append(ClassBuilder::TAB + className + "(");
for (int i = 0; i < list->size(); i++) {
entity = list->at(i);
hString->append(entity->Gettype() + " " + entity->Getname() + defValFomat(entity) + (i < list->size() - 1 ? "," : ""));
}
hString->append(");" + ClassBuilder::BR);
// 创建析构函数
if (isAnnotation) {
hString->append(ClassBuilder::TAB + " // " + className + " 析构函数");
}
hString->append(ClassBuilder::TAB + "~" + className + "();" + ClassBuilder::BR);
// 用户扩展
if (classBuilderExtend != NULL) {
classBuilderExtend->constructor(hString, className, isAnnotation);
}
//创建get/set
string str;
hString->append(ClassBuilder::BR);
for (int i = 0; i < list->size(); i++) {
entity = list->at(i);
str = toFristUpper(entity->Getname());
// get
if (isAnnotation) {
hString->append(ClassBuilder::TAB + " // " + className + "::" + " Get" + str + ClassBuilder::BR);
}
hString->append(ClassBuilder::TAB + entity->Gettype() + " Get" + str + "();" + ClassBuilder::BR);
// set
if (isChain) {
if (isAnnotation) {
hString->append(ClassBuilder::TAB + " // 链式函数 " + className + "::" + " Set" + str + ClassBuilder::BR);
}
hString->append(ClassBuilder::TAB + className + "* Set" + str + "(");
hString->append(entity->Gettype() + " " + entity->Getname());
hString->append(");" + ClassBuilder::BR);
}
else {
if (isAnnotation) {
hString->append(ClassBuilder::TAB + " // " + className + "::" + " Set" + str + ClassBuilder::BR);
}
hString->append(ClassBuilder::TAB + "void Set" + str + "(");
hString->append(entity->Gettype() + " " + entity->Getname());
hString->append(");" + ClassBuilder::BR);
}
}
// ToString
if (isToString) {
if (isAnnotation) {
hString->append(ClassBuilder::BR + ClassBuilder::TAB + " // " + className + "::" + " ToString()");
}
hString->append(ClassBuilder::BR);
hString->append(ClassBuilder::TAB + "string* ToString();" + ClassBuilder::BR);
}
// 用户扩展
if (classBuilderExtend != NULL) {
classBuilderExtend->function(hString, className, isAnnotation);
}
//属性定义
hString->append(ClassBuilder::BR);
hString->append("private:" + ClassBuilder::BR);
for (int i = 0; i < list->size(); i++) {
entity = list->at(i);
if (isAnnotation) {
hString->append(ClassBuilder::TAB + " // 参数/属性 " + className + "::" + entity->Getname() + ClassBuilder::BR);
}
hString->append(ClassBuilder::TAB + entity->Gettype() + " " + entity->Getname() + ";" + ClassBuilder::BR);
}
// 用户扩展
if (classBuilderExtend != NULL) {
classBuilderExtend->variable(hString, className, isAnnotation);
}
// 结束类
hString->append("}; " + ClassBuilder::BR);
if (isDefine) {
// hString->append(ClassBuilder::BR + "#endif");
}
// 测试 写入日志
// Logs::logger().Files(hString->c_str());
str.clear();
entity = NULL;
}
void ClassBuilder::cpp() {
/* *****************************************************************
* 类功能说明: cpp 头文件定义
* @author : lch
***************************************************************** */
cppString = new string();
// 不创建cpp文件
if (!isCppFile) {
return;
}
string s = std::regex_replace(headPack, std::regex("[\\|\\\\]"), "/");
if (cppReplaceIncludeString != "" && !cppReplaceIncludeString.empty()) {
s = std::regex_replace(s, std::regex("include\/"), "");
}
// 头部#include
cppString->append("#include \"" + s + className + ".h" + "\"" + ClassBuilder::BR);
cppString->append(ClassBuilder::BR);
// 用户扩展
if (classBuilderExtend != NULL) {
classBuilderExtend->cppVariable(cppString, className, isAnnotation);
}
// 创建构造器(无参数)
cppString->append(className + "::" + className + "() { }" + ClassBuilder::BR);
cppString->append(ClassBuilder::BR);
// 创建构造器(全参数)
InfoEntity* entity;
cppString->append(className + "::" + className + "(");
for (int i = 0; i < list->size(); i++) {
entity = list->at(i);
cppString->append(entity->Gettype() + " " + entity->Getname() + (i < list->size() - 1 ? "," : ""));
}
cppString->append("){" + ClassBuilder::BR);
for (int i = 0; i < list->size(); i++) {
entity = list->at(i);
cppString->append(ClassBuilder::TAB + "this->" + entity->Getname() + " = " + entity->Getname() + ";" + ClassBuilder::BR);
}
cppString->append("}");
cppString->append(ClassBuilder::BR);
// 创建析构函数
cppString->append(ClassBuilder::BR);
cppString->append(className + "::" + "~" + className + "() { }" + ClassBuilder::BR);
cppString->append(ClassBuilder::BR);
// get/set 函数
string str;
for (int i = 0; i < list->size(); i++) {
entity = list->at(i);
str = toFristUpper(entity->Getname());
// get
cppString->append(entity->Gettype() + " " + className + "::Get" + str + "(){" + ClassBuilder::BR);
cppString->append(ClassBuilder::TAB + "return this->" + entity->Getname() + ";" + ClassBuilder::BR);
cppString->append("}" + ClassBuilder::BR);
// set
cppString->append(ClassBuilder::BR);
if (isChain) {
cppString->append(className + "* " + className + "::" + " Set" + str + "(");
cppString->append(entity->Gettype() + " " + entity->Getname());
cppString->append("){" + ClassBuilder::BR);
cppString->append(ClassBuilder::TAB + "this->" + entity->Getname() + " = " + entity->Getname() + ";" + ClassBuilder::BR);
cppString->append(ClassBuilder::TAB + "return this" + ";" + ClassBuilder::BR);
cppString->append("}" + ClassBuilder::BR);
}
else {
cppString->append("void " + className + "::Set" + str + "(");
cppString->append(entity->Gettype() + " " + entity->Getname());
cppString->append("){" + ClassBuilder::BR);
cppString->append(ClassBuilder::TAB + "this->" + entity->Getname() + " = " + entity->Getname() + ";" + ClassBuilder::BR);
cppString->append("}" + ClassBuilder::BR);
}
}
// ToString
if (isToString) {
cppString->append(ClassBuilder::BR);
cppString->append("string* " + className + "::ToString(){" + ClassBuilder::BR);
cppString->append(ClassBuilder::TAB + "return new string(" + ClassBuilder::BR);
cppString->append(ClassBuilder::TAB + ClassBuilder::TAB + "\"" + className + "::{");
for (int i = 0; i < list->size(); i++) {
entity = list->at(i);
str = defValFomat(entity);
cppString->append(valFormat(entity) + (i == list->size() - 1 ? "" : "\","));
}
cppString->append("\"}\"" + ClassBuilder::BR + ClassBuilder::TAB + ");" + ClassBuilder::BR);
cppString->append("}" + ClassBuilder::BR);
cppString->append(ClassBuilder::BR);
}
// 用户扩展
if (classBuilderExtend != NULL) {
classBuilderExtend->cppFunction(cppString, className, list);
}
str.clear();
entity = NULL;
// 测试 写入日志
// Logs::logger().Files(cppString->c_str());
}
void ClassBuilder::sava() {
// 保存 h文件
if (hString->empty()) {
return;
}
// 创建h 文件
string path, allPath, fileName;
path = rooPath + headPack;
fileName = className + ".h";
allPath = path + fileName;
// 编码定义
string cvGB2312("GB2312");
if (isFileExists(allPath) && !isCover) {
Logger::LOG.msg(fileName + "文件不允许覆盖,忽略操作.", DEBUG);
return;
}
//保存文件
checkedFileCreate(path, fileName);
savaFile(*hString, allPath, cvGB2312);
hString->clear();
Logger::LOG.msg(fileName + ":文件生成成功,位置:" + allPath);
// ..............................................................
// 保存 cpp文件
if (!isCppFile || cppString->empty()) {
Logger::LOG.msg(className + "不生成cpp文件.");
return;
}
// 创建cpp文件
path = rooPath + cppPack;
fileName = className + ".cpp";
allPath = path + fileName;
if (isFileExists(allPath) && !isCover) {
Logger::LOG.msg(fileName + "文件不允许覆盖,忽略操作.", DEBUG);
return;
}
//保存文件
checkedFileCreate(path, fileName);
savaFile(*cppString, allPath, cvGB2312);
cppString->clear();
Logger::LOG.msg(fileName + ":文件生成成功,位置:" + allPath);
path.clear();
}
void ClassBuilder::savaFile(string& str, string& path, string& charset) {
if (str.empty() || !isFileExists(path)) {
return;
}
ofstream fout(path);
if (fout.is_open()) {
fout.write(str.c_str(), str.length());
}
else {
Logger::LOG.msg("文件保存失败: " + path, ERRORS);
}
fout.flush();
fout.close();
}
void ClassBuilder::listBuilder(ClassBuilder* classBuilder, vector<InfoEntitys*>* list) {
InfoEntitys* infoEntitys;
for (int i = 0; i < list->size(); i++) {
infoEntitys = list->at(i);
classBuilder->className = infoEntitys->GetclassName();
classBuilder->includeList = infoEntitys->Gethead();
classBuilder->list = infoEntitys->Getlist();
classBuilder->extend = infoEntitys->Getextend();
// 设置默认值排序
InfoEntityListSort(classBuilder->list);
// 创建数据文件
classBuilder->create();
classBuilder->sava();
}
infoEntitys = NULL;
}
string ClassBuilder::defValFomat(InfoEntity* entity) {
if (entity->GetdefVal() != "" && !entity->GetdefVal().empty()) {
string str;
str.append("=");
if (entity->Gettype() == "string" ||
entity->Gettype() == "string*" ||
entity->Gettype() == "std::string" ||
entity->Gettype() == "std::string*") {
str.append("\"" + entity->GetdefVal() + "\"");
}
else {
str.append(entity->GetdefVal());
}
return str;
}
else {
return "";
}
}
string ClassBuilder::valFormat(InfoEntity* entity) {
// 指定默认值
if (entity->GetdefToString() != "" && !entity->GetdefToString().empty()) {
return entity->Getname() + ":\"+this->" + entity->Getname() + entity->GetdefToString() + "+";
}
if (entity->Gettype() == "string" ||
entity->Gettype() == "string*" ||
entity->Gettype() == "std::string" ||
entity->Gettype() == "std::string*") {
return entity->Getname() + ":\"+this->" + entity->Getname() + "+";
}
else {
string::size_type index = entity->Gettype().find_last_of("\*");
// 指针类型
if (index != string::npos) {
return entity->Getname() + ":\"+StringUtil::pointAddress(this->" + entity->Getname() + ")+";
}
// 其他
else {
return entity->Getname() + ":\"+std::to_string(this->" + entity->Getname() + ")+";
}
}
}
bool ClassBuilder::isFileExists(string& name) {
ifstream f(name.c_str());
return f.good();
}
bool ClassBuilder::mkdirAll(string& path) {
int m = 0, n;
// 去除盘符 window
string str1 = path;
string str2 = str1.substr(0, 2);
str1 = str1.substr(3, str1.size());
while (m >= 0) {
m = str1.find('\\');
str2 += '\\' + str1.substr(0, m);
n = _access(str2.c_str(), 0); //判断该目录是否存在
if (n == -1) {
if (_mkdir(str2.c_str()) != 0) //创建目录
{
return false;
}
}
str1 = str1.substr(m + 1, str1.size());
}
return true;
}
void ClassBuilder::checkedFileCreate(string& path, string& fileName) {
bool result = isFileExists(path + fileName);
if (!result) {
// 创建目录
if (!isFileExists(path)) {
/*
string::size_type index = path.find_last_of("\\");
if (index != string::npos) {
path = path.substr(0, index - 1);
}
*/
// result = _mkdir(path.c_str());
result = mkdirAll(path);
if (result) {
Logger::LOG.msg("mkdir create dir succ: " + path);
}
else {
Logger::LOG.msg("创建文件夹失败:" + path);
return;
}
}
// 创建文件
ofstream fout(path + fileName, ios::app);
if (fout.is_open()) {
Logger::LOG.msg("创建文件成功: " + path + fileName);
}
else {
Logger::LOG.msg("创建文件失败!!!: " + path + fileName, ERRORS);
}
fout.close();
}
}
string ClassBuilder::getNowTime() {
stringstream strtime;
std::time_t currenttime = std::time(0);
char tAll[255];
std::strftime(tAll, sizeof(tAll), "%Y-%m-%d %H:%M:%S", std::localtime(¤ttime));
strtime << tAll;
return strtime.str();
}
string ClassBuilder::toFristUpper(string& str) {
if (str.length() == 0) {
return "";
}
str[0] = std::toupper(str[0]);
return str;
}
bool ClassBuilder::find(vector<string>& list, string& str) {
for (int i = 0; i < list.size(); i++) {
if (list.at(i)._Equal(str)) {
return true;
}
}
return false;
}
bool ClassBuilder::find(vector<string>* list, string* str) {
for (int i = 0; i < list->size(); i++) {
if (list->at(i)._Equal(str->c_str())) {
return true;
}
}
return false;
}
#include
#include "service/CSqliteOperator.h"
#include
#include
#include
#include
#include "service/WebAnalyzingAddressService.h"
#include
#include
#include "class/builder/EasySQLiteClassBuilderExtendImpl.h"
using namespace std;
// 公共头引用
vector<string>* head = new vector<string>();
// head->push_back("");
// head->push_back("wx/wx.h");
// head->push_back(" class/builder/StringUtil.h");
// 公共继承
vector<string>* extend = new vector<string>();
// extend->push_back("public wxObject");
// extend->push_back("public StringUtil");
// 公共参数配置
ClassBuilder* classBuilder = new ClassBuilder(
"User",
"lch",
"E:\\CProjects\\train_auto_systems\\train_auto_systems_service\\train_auto_systems_service\\",
"include\\entity\\",
"src\\entity\\",
head,
NULL,
NULL,
true,//是否构建全属性
true,//构建 toString 方法
true,//是否创建cpp文件
true, // 是否覆盖
false, // set 是否链式
false, // 是否创建 define 引入只有一份
false //创建注释
);
// 配置扩展器
ClassBuilderExtend* classBuilderExtend = new EasySQLiteClassBuilderExtendImpl();
classBuilder->setClassBuilderExtend(classBuilderExtend);
/** ******************************************************************
* 创建实体 WebAnalyzingAddress
****************************************************************** */
head = new vector<string>();
head->push_back("easySQLite/SqlCommon.h");
vector<InfoEntity*>* info = new vector<InfoEntity*>();
info->push_back(new InfoEntity("int", "id"));
info->push_back(new InfoEntity("string", "name"));
info->push_back(new InfoEntity("string", "url"));
info->push_back(new InfoEntity("sql::time", "datatime", "", ".asTimeString()"));
vector<InfoEntitys*>* infoEntitysList = new vector<InfoEntitys*>();
infoEntitysList->push_back(new InfoEntitys("WebAnalyzingAddress", head, info, extend));
/** ******************************************************************
* 创建实体
****************************************************************** */
// 多个文件创建方式 2
ClassBuilder::listBuilder(classBuilder, infoEntitysList);