📄 codegenerator.cpp
字号:
for (method_it = c->methods.begin(); method_it != c->methods.end(); ++method_it) { const std::vector<std::string> &arg_types = (*method_it)->arg_types; for (arg_it = arg_types.begin(); arg_it != arg_types.end(); ++arg_it) { string argName = *arg_it; if ((sp = argName.find("ClassType_tmpl<")) != string::npos){ sp = sp + strlen("ClassType_tmpl<") - 1; ep = sp + 1; while ((argName[ep] != '>') && (argName[ep] != '&')) ep++; cout << "#include \"" + argName.substr(sp + 1, ep - sp - 1) + "_reflection.hpp\"\n"; } } } cout << "namespace reflcpp {\n\n" << "template <typename Bottom_TP, typename Der_TP, int N>\n" << "class Bases<Bottom_TP, Der_TP, " + c->name + ", N> : public BaseList<Bottom_TP, " << c->name; if (!c->baseClass.empty()) cout << "," + c->baseClass; cout << " > {};\n" << "\n\nstruct " + c->name + "_strings {\n" << "\tstatic const char name[];\n"; for (p = c->fields.begin(); p != c->fields.end(); ++p) cout << "\tstatic const char f_" + (*p)->name + "[];\n"; vector<Xct_Method*>::const_iterator m; for (m = c->methods.begin(); m != c->methods.end(); ++m) cout << "\tstatic const char m_" + (*m)->name + "[];\n"; cout << "};\n" << "\ntemplate <>\n" << "class Members <" + c->name + ">\n" << " : public MemberList<\n\t" + c->name + ",\n" << "\t" + c->name + "_strings::name\n"; for (p = c->fields.begin(); p != c->fields.end(); ++p) { // Only public fields can be accessible if ((*p)->access == "") { cout << "\t, DataMemberDcl<" + c->name + "," + (*p)->type << ", &" + c->name + "::" + (*p)->name + ", " + c->name + "_strings::f_"+ (*p)->name + ">\n"; } } for (m = c->methods.begin(); m != c->methods.end(); ++m) { // Only public methods can be accessible if ((*m)->access == "") { cout << "\t," << getMemberFunctionDcl(c->name,*m) << "\n"; } } cout << "\t>\n{};\n\n}\n\n#endif\n";} /** * given the id of the class * generate the Head file for the class */static void genHpp(char* cid){ xmlNode *n = searchNodeById(cid); if (n == NULL) { cerr << "Can't find the node with id " << cid << endl; return; } if (strcmp(getTag(n),"Class") != 0) { cerr << "non-Class definiation found" << endl << "You may need to run gccxml with -fxml-start option" << endl; return; } Xct_Class* c = buildClass(n); genHpp(c); // FIXME, the memory leaking for methods and fields delete c;}static void genCpp (Xct_Class* c) { string className = c->name; fprintf(stdout,"#include \"%s_reflection.hpp\"\n\n",className.c_str()); fprintf(stdout,"namespace reflcpp {\n\n"); // Inserter fprintf(stdout,"namespace {\n"); fprintf(stdout," ClassType_tmpl<%s> inserter;\n", className.c_str()); fprintf(stdout,"}\n\n"); fprintf(stdout,"const char %s_strings::name[] = \"%s\";\n", className.c_str(),className.c_str()); for (vector<Xct_Field*>::iterator it = (c->fields).begin(); it != (c->fields).end(); ++it) { fprintf(stdout,"const char %s_strings::f_%s[] = \"%s\";\n", className.c_str(),(*it)->name.c_str(),(*it)->name.c_str()); } for (vector<Xct_Method*>::iterator it = c->methods.begin(); it != c->methods.end(); ++it) { fprintf(stdout,"const char %s_strings::m_%s[] = \"%s\";\n", className.c_str(),(*it)->name.c_str(),(*it)->name.c_str()); } fprintf(stdout,"} // namespace reflcpp\n");}/** * given the id of the class * generate the cpp file for the class */static void genCpp(char* cid){ xmlNode *n; char *_tagName, *className, *_members,*_fid,*_fname; n = searchNodeById(cid); if (n == NULL) { cerr << "Can't find the node with id " << cid << endl; return; } if (strcmp(getTag(n),"Class") != 0) { cerr << "non-Class definiation found" << endl << "You may need to run gccxml with -fxml-start option" << endl; return; } Xct_Class* c = buildClass(n); genCpp(c); delete c;}// --------------------------------------------------------------------------// generate c++ code from the DOM// --------------------------------------------------------------------------static void genCode(char *nid){// char *_members, *_base,*_name,*_id,*_tagName,*_retId,*_typeId,*_access,*_virtual;// xmlNode *n,*child;// n = searchNodeById(nid);// if (n == NULL) return;// _tagName = XMLString::transcode(n->getTagName());// if (strcmp(_tagName, "Class") == 0) {// _name = XMLString::transcode(n->getAttribute(XMLString::transcode("name")));// printf("Class %s {\n",_name); // _members = XMLString::transcode(n->getAttribute(XMLString::transcode("members")));// _id = strtok(_members, " ");// while (_id != NULL) {// genCode(_id);// _id = strtok(NULL, " ");// }// printf("}\n");// }// if (strcmp(_tagName, "Field") == 0) {// _name = XMLString::transcode(n->getAttribute(XMLString::transcode("name")));// _typeId = XMLString::transcode(n->getAttribute(XMLString::transcode("type")));// _access = XMLString::transcode(n->getAttribute(XMLString::transcode("access")));// printf("%s ",_access);// genCode(_typeId);// printf(" : %s\n", _name);// }// if (strcmp(_tagName, "FundamentalType") == 0) {// _name = XMLString::transcode(n->getAttribute(XMLString::transcode("name")));// printf("%s ",_name);// }// if ((strcmp(_tagName, "Method") == 0) || (strcmp(_tagName, "Constructor") == 0) || (strcmp(_tagName, "Destructor") == 0) ) {// _name = XMLString::transcode(n->getAttribute(XMLString::transcode("name")));// _access = XMLString::transcode(n->getAttribute(XMLString::transcode("access")));// _retId = XMLString::transcode(n->getAttribute(XMLString::transcode("return")));// _virtual = XMLString::transcode(n->getAttribute(XMLString::transcode("virtual"))); // printf("%s ",_access);// if (strcmp(_virtual,"1") == 0 )// printf(" virtual ");// genCode(_retId);// if (strcmp(_tagName, "Destructor") == 0)// printf(" ~%s(",_name);// else// printf(" %s(",_name);// for (child = (DOMElement*) n->getFirstChild(); child != 0; child= (DOMElement *) child->getNextSibling()) {// _tagName = XMLString::transcode(child->getTagName());// if (strcmp(_tagName, "Argument") == 0) {// _name = XMLString::transcode(child->getAttribute(XMLString::transcode("name")));// _typeId = XMLString::transcode(child->getAttribute(XMLString::transcode("type")));// genCode(_typeId);// printf(" %s,",_name);// }// }// printf(")\n ");// }}// ---------------------------------------------------------------------------//// main//// ---------------------------------------------------------------------------int main(int argC, char* argV[]){ // Check command line and extract arguments. if (argC < 2) { usage(); return 1; } LIBXML_TEST_VERSION int fileType = HEAD; int argInd; for (argInd = 1; argInd < argC; argInd++) { // Break out on first parm not starting with a dash if (argV[argInd][0] != '-') break; // Watch for special case help request if (!strncmp(argV[argInd], "-h",2)) { usage(); return 2; } // generation type else if (!strncmp(argV[argInd], "-t=", 3) || !strncmp(argV[argInd], "-t=", 3)) { const char* const parm = &argV[argInd][3]; if (!strcmp(parm, "hpp")) fileType = HEAD; else if (!strcmp(parm, "cpp")) fileType = CPP; else { cerr << "Unknown -t= value: " << parm << endl; return 2; } } else { cerr << "Unknown option '" << argV[argInd] << "', ignoring it\n" << endl; } } // // There should be only one and only one parameter left, and that // should be the file name. // if (argInd != argC - 1) { usage(); return 1; } xmlDocPtr doc = xmlParseFile(argV[argInd]); if (doc) { gRootNode = (xmlNode *) xmlDocGetRootElement(doc); if (fileType == HEAD) genHpp("_1"); else genCpp("_1"); } else { cerr << "bad xml file" << endl; } xmlCleanupParser(); xmlFreeDoc(doc); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -