git-svn-id: svn://db.shs.com.ru/pip@587 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2017-12-22 19:01:00 +00:00
parent 39d35731a1
commit 2353b3b33f
5 changed files with 114 additions and 50 deletions

View File

@@ -22,6 +22,7 @@
using namespace PICoutManipulators;
PICodeParser parser;
void usage() {
piCout << Bold << "PIP Code model generator";
@@ -133,22 +134,98 @@ void makeEnumInfo(PIFile & f, const PICodeParser::Enum * e) {
}
void makeClassStream(PIFile & f, const PICodeParser::Entity * e) {
PIStringList ml;
void writeClassStreamMembersOut(PIFile & f, const PICodeParser::Entity * e, int & cnt) {
PIVector<PICodeParser::Member> ml;
piForeachC (PICodeParser::Member & m, e->members) {
if (m.is_type_ptr) continue;
ml << m.name;
ml << m;
}
if (ml.isEmpty()) return;
bool is_union = e->type == "union";
piForeachC (PICodeParser::Member & m, ml) {
if (is_union && m.isBitfield())
continue;
++cnt;
if (m.dims.isEmpty()) {
f << "\tcs << cs.chunk(" << cnt << ", ";
if (parser.isEnum(m.type))
f << "(int)";
f << "v." << m.name << ");\n";
} else {
PIString ptype = m.type.left(m.type.find('['));
f << "\tcs << cs.chunk(" << cnt << ", PIVector<" << ptype << " >((const " << ptype << " *)(v." << m.name << "), ";
for (int i = 0; i < m.dims.size_s(); ++i) {
if (i > 0) f << " * ";
f << m.dims[i];
}
f << "));\n";
}
if (is_union)
piBreak;
}
if (is_union) return;
piForeachC (PICodeParser::Entity * ce, e->children) {
if (ce->has_name) continue;
writeClassStreamMembersOut(f, ce, cnt);
}
}
void writeClassStreamMembersIn(PIFile & f, const PICodeParser::Entity * e, int & cnt) {
PIVector<PICodeParser::Member> ml;
piForeachC (PICodeParser::Member & m, e->members) {
if (m.is_type_ptr) continue;
ml << m;
}
bool is_union = e->type == "union";
piForeachC (PICodeParser::Member & m, ml) {
if (is_union && m.isBitfield())
continue;
++cnt;
if (m.dims.isEmpty()) {
f << "\t\tcase " << cnt << ": cs.get(";
if (parser.isEnum(m.type))
f << "(int&)";
f << "v." << m.name << "); break;\n";
} else {
PIString ptype = m.type.left(m.type.find('['));
f << "\t\tcase " << cnt << ": {\n\t\t\tPIVector<" << ptype << " > d; cs.get(d);\n";
f << "\t\t\tint cnt = piMini(d.size_s(), ";
for (int i = 0; i < m.dims.size_s(); ++i) {
if (i > 0) f << " * ";
f << m.dims[i];
}
f << ");\n";
f << "\t\t\tfor (int i = 0; i < cnt; ++i)\n";
f << "\t\t\t\t((" << ptype << " *)(v." << m.name << "))[i] = d[i];\n";
f << "\t\t\t}\n";
}
if (is_union)
piBreak;
}
if (is_union) return;
piForeachC (PICodeParser::Entity * ce, e->children) {
if (ce->has_name) continue;
writeClassStreamMembersIn(f, ce, cnt);
}
}
void makeClassStream(PIFile & f, const PICodeParser::Entity * e) {
f << "\nPIByteArray & operator <<(PIByteArray & s, const " << e->name << " & v) {\n";
f << "\ts";
piForeachC (PIString & m, ml)
f << " << v." << m << "\n\t";
f << ";\n}\nPIByteArray & operator >>(PIByteArray & s, " << e->name << " & v) {\n";
f << "\ts";
piForeachC (PIString & m, ml)
f << " >> v." << m << "\n\t";
f << ";\n}\n";
f << "\tPIChunkStream cs;\n";
int cnt = 0;
writeClassStreamMembersOut(f, e, cnt);
f << "\ts << cs.data();\n\treturn s;\n}\n";
f << "PIByteArray & operator >>(PIByteArray & s, " << e->name << " & v) {\n";
f << "\tif (s.size_s() < 4) return s;\n";
f << "\tPIByteArray csba; s >> csba;\n";
f << "\tPIChunkStream cs(csba);\n";
f << "\twhile (!cs.atEnd()) {\n";
f << "\t\tswitch (cs.read()) {\n";
cnt = 0;
writeClassStreamMembersIn(f, e, cnt);
f << "\t\t}\n\t}\n";
f << "\treturn s;\n}\n";
}
@@ -177,15 +254,11 @@ void writeModel(PICodeParser & parser, PICLI & cli, const PIString out, bool met
f.clear();
f.open(PIIODevice::WriteOnly);
f << "// Generated by \"PIP Code model generator\" " << PIDateTime::current().toString("dd.MM.yyyy hh:mm:ss\n\n");
f << "#include <string.h>\n#include \"" << out << ".h\"\n\nusing namespace PICodeInfo;\n\n";
if (streams || texts) {
PIVector<PIString> incf = inc_files.toVector();
piForeachC (PIString & i, incf) {
if (i != parser.mainFile())
f << "#include \"" << i << "\"\n";
}
f << "\n";
}
f << "#include <string.h>\n";
if (streams || texts)
f << "#include <pichunkstream.h>\n";
f << "#include \"" << out << ".h\"\n";
f << "\nusing namespace PICodeInfo;\n\n";
/*
PIString entry, argtype, rettype, args;
piForeachC (PICodeParser::Entity * e, parser.entities) {
@@ -261,7 +334,7 @@ void writeModel(PICodeParser & parser, PICLI & cli, const PIString out, bool met
if (streams) {
f << "\n\n// Stream operators\n";
piForeachC (PICodeParser::Entity * e, parser.entities) {
if (e->name.startsWith("_PI")) continue;
if (!e->has_name || e->name.startsWith("_PI")) continue;
makeClassStream(f, e);
}
}
@@ -279,6 +352,13 @@ void writeModel(PICodeParser & parser, PICLI & cli, const PIString out, bool met
f << "\n";
f << "#ifndef " << defname << "\n#define " << defname << "\n\n";
f << "#include \"pivariant.h\"\n#include \"picodeinfo.h\"";
if (streams || texts) {
PIVector<PIString> incf = inc_files.toVector();
piForeachC (PIString & i, incf) {
if (i != parser.mainFile())
f << "\n#include \"" << i << "\"";
}
}
/*
f << "\n\n";
piForeachC (PICodeParser::Entity * e, ventities)
@@ -297,7 +377,7 @@ const PIVariant & arg1 = PIVariant(), const PIVariant & arg2 = PIVariant(), cons
if (streams) {
f << "\n\n// Stream operators\n";
piForeachC (PICodeParser::Entity * e, parser.entities) {
if (e->name.startsWith("_PI")) continue;
if (!e->has_name || e->name.startsWith("_PI")) continue;
makeClassStreamHeader(f, e);
}
}
@@ -326,7 +406,6 @@ int main(int argc, char * argv[]) {
return 0;
}
piDebug = !cli.hasArgument("quiet");
PICodeParser parser;
piForeachC (PIString & a, cli.rawArguments()) {
if (a.startsWith("-I")) parser.includeDirectory(a.mid(2));
if (a.startsWith("-D")) parser.addDefine(a.mid(2), PIString());