1 #include <libxml++/libxml++.h>
8 #include <boost/lexical_cast.hpp>
10 //! convert string to upper case
12 toupper(std::string s) {
13 for (unsigned i=0; i<s.length(); ++i) s[i]=toupper(s[i]);
17 //! replace all characters f by r in string s
19 replace(std::string s, char f, char r) {
20 for (unsigned i=0; i<s.length(); ++i) if (s[i]==f) s[i]=r;
24 //! strip prefix from string s
26 \return string s without prefix or an empty string on error
29 strip(std::string s, std::string prefix)
32 if ( (prefix.length()>s.length() ) || ( std::string(s,0,prefix.length())!=prefix ) ) return error;
33 return std::string(s, prefix.length(), s.length()-prefix.length());
36 //! extract group from attributes
38 extract_group(const std::string &attrs)
42 std::string to_match("gccxml(libt2n-");
43 std::string::size_type p(attrs.find(to_match));
44 if (p==std::string::npos) return error;
45 std::string group(attrs, p+to_match.length(), attrs.length());
46 p=group.find_first_of(')');
47 assert(p!=std::string::npos);
48 return std::string(group,0,p);
51 //! get child element by id
53 \return pointer to element having id or null on error
54 \todo find libxmlpp pendant
56 const xmlpp::Element* get_element_by_id(const xmlpp::Element* element, const std::string &id)
58 const xmlpp::Attribute* cid = element->get_attribute("id");
59 if ( cid && ( cid->get_value() == id)) return element;
61 //Recurse through child nodes:
62 xmlpp::Node::NodeList list = element->get_children();
63 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
65 const xmlpp::Element* element = dynamic_cast<const xmlpp::Element*>(*iter);
67 const xmlpp::Element* match = get_element_by_id(element, id);
68 if (match) return match;
75 get_file(const xmlpp::Element* root, const std::string &file_id)
78 const xmlpp::Element* e=get_element_by_id(root, file_id);
79 if ((!e)||(!e->get_attribute("name"))) return error;
80 return e->get_attribute("name")->get_value();
83 //! get namespace by id
85 \return namespace name or empty string on error
87 std::string get_namespace(const xmlpp::Element* root, const std::string &id)
90 const xmlpp::Element* element(get_element_by_id(root, id));
91 if ((!element)||(!element->get_attribute("name"))) return error;
92 return element->get_attribute("name")->get_value();
96 std::string get_default_group(const xmlpp::Element* root) {
99 if ( (root->get_attribute("name"))
100 && (root->get_attribute("attributes"))
101 && (root->get_name() == "Typedef")
102 && (root->get_attribute("name")->get_value()=="libt2n_default_group"))
103 return extract_group(root->get_attribute("attributes")->get_value());
105 //Recurse through child nodes:
106 xmlpp::Node::NodeList list = root->get_children();
107 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
109 if (const xmlpp::Element* elem=dynamic_cast<const xmlpp::Element*>(*iter)) {
110 std::string ret(get_default_group(elem));
111 if (!ret.empty()) return ret;
120 std::string noref_name;
121 bool operator==(const type_info& o) {return (name==o.name) && (noref_name == o.noref_name);}
122 std::string noref() const {return noref_name.empty() ? name : noref_name;}
125 std::ostream &operator<<(std::ostream &o, const type_info &t) {
130 struct parse_error : public std::runtime_error
132 parse_error(const std::string &file, unsigned line, const std::string &msg)
133 : std::runtime_error(file+":"+boost::lexical_cast<std::string>(line)+": error: "+msg)
139 \return type name or empty string on error
141 type_info get_type(const xmlpp::Element* root, const std::string &id)
144 const xmlpp::Element* element(get_element_by_id(root, id));
145 if (!element) return error;
147 // TODO: not yet complete
148 // if we recurse - when do we stop?
149 // if it is a typedef? yes? (hmm if the typedef is in the file parsed this will not work)
151 // TODO: const and reference types handling is a ugly hack
153 std::string tag(element->get_name());
154 if (tag=="ReferenceType") {
155 assert(element->get_attribute("type"));
156 type_info ret(get_type(root, element->get_attribute("type")->get_value()));
157 if (ret==error) return error;
158 // at the moment we only support const &
159 // todo: nice error message!
160 if ((ret.noref_name=strip(ret.name,"const ")).empty()) return error;
161 ret.name=ret.name+"&";
163 }else if (tag=="CvQualifiedType") {
164 assert(element->get_attribute("type"));
165 type_info ret(get_type(root, element->get_attribute("type")->get_value()));
166 if (ret==error) return error;
167 ret.name=std::string("const ")+ret.name;
169 }else if (tag=="PointerType") {
174 assert(element->get_attribute("name"));
176 if (element->get_attribute("context")) {
177 ret.name=get_namespace(root, element->get_attribute("context")->get_value());
181 // do not explicitely add ::
184 ret.name+=element->get_attribute("name")->get_value();
190 typedef std::list<std::pair<std::string, type_info> > Args;
198 std::string ret_classname() const {
199 return name+mangled+"_res";
201 std::string cmd_classname() const {
202 return name+mangled+"_cmd";
206 std::ostream &operator<<(std::ostream &o, const t2n_procedure::Args &args) {
207 for (t2n_procedure::Args::const_iterator it=args.begin();it!=args.end();++it) {
208 if (it!=args.begin()) o << ", ";
209 o << it->second << " " << it->first;
214 std::ostream &operator<<(std::ostream &o, const t2n_procedure &f) {
215 o << f.ret_type << " " << f.name << "(" << f.args << ")";
222 Parser(const std::string &fname, const std::string &group) : m_fname(fname), m_group(group) {}
224 std::list<t2n_procedure> get_procedures() {
225 xmlpp::DomParser parser;
226 // parser.set_validate();
227 parser.set_substitute_entities(); //We just want the text to be resolved/unescaped automatically.
228 parser.parse_file(m_fname);
232 const xmlpp::Node* pNode = parser.get_document()->get_root_node(); //deleted by DomParser.
233 const xmlpp::Element* root = dynamic_cast<const xmlpp::Element*>(pNode);
242 std::list<t2n_procedure> m_procedures;
244 void parse_function(const xmlpp::Element* root, const xmlpp::Node* node) {
245 const xmlpp::Element* element = dynamic_cast<const xmlpp::Element*>(node);
246 if (!element) return;
248 const xmlpp::Attribute* attributes = element->get_attribute("attributes");
249 const xmlpp::Attribute* name = element->get_attribute("name");
250 const xmlpp::Attribute* mangled = element->get_attribute("mangled");
251 const xmlpp::Attribute* returns = element->get_attribute("returns");
252 if ((!attributes)||(!name)||(!mangled)||(!returns)) return;
254 // check wether the procedure is marked (TODO: improve)
255 // attributes are speparated by spaces?
258 f.group=extract_group(attributes->get_value());
259 if (f.group.empty()) return;
260 // todo: handle default group
261 if (f.group=="default") f.group=m_group;
263 // we need the return type
264 f.ret_type=get_type(root, returns->get_value());
265 f.name=name->get_value();
266 f.mangled=mangled->get_value();
268 xmlpp::Node::NodeList list = node->get_children("Argument");
269 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
271 const xmlpp::Element* arg = dynamic_cast<const xmlpp::Element*>(*iter);
273 assert(arg->get_name() == "Argument");
274 assert(arg->get_attribute("name"));
275 assert(arg->get_attribute("type"));
276 f.args.push_back(std::pair<std::string, type_info>(arg->get_attribute("name")->get_value(), get_type(root, arg->get_attribute("type")->get_value())));
277 // todo: ugly - could be any other error
278 if (f.args.back().second.name.empty()) {
279 assert(element->get_attribute("file"));
280 assert(element->get_attribute("line"));
281 throw parse_error(get_file(root, element->get_attribute("file")->get_value()),
282 boost::lexical_cast<unsigned>(element->get_attribute("line")->get_value())-1,
283 std::string("type of parameter `")+f.args.back().first+"' not (yet?) supported");
287 std::cerr << "Found function: " << f << std::endl;
288 m_procedures.push_back(f);
291 void visit_node(const xmlpp::Element* root, const xmlpp::Node* node = NULL, unsigned int indentation = 0)
293 if (!node) node=root;
295 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
296 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node);
297 const xmlpp::CommentNode* nodeComment = dynamic_cast<const xmlpp::CommentNode*>(node);
299 if(nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this.
302 std::string nodename = node->get_name();
304 if(!nodeText && !nodeComment && !nodename.empty()) //Let's not say "name: text".
306 if (node->get_name() == "Function") parse_function(root, node);
310 //Recurse through child nodes:
311 xmlpp::Node::NodeList list = node->get_children();
312 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
314 visit_node(root, *iter, indentation + 2); //recursive
321 std::set<std::string>
322 used_groups(const std::list<t2n_procedure> &funcs) {
323 typedef std::set<std::string> Groups;
325 for (std::list<t2n_procedure>::const_iterator it=funcs.begin();it!=funcs.end();++it)
326 // since we use std::set each group is inserted only once
327 groups.insert(it->group);
331 void output_common_hpp(std::ostream &o, const std::list<t2n_procedure> &procs) {
332 std::set<std::string> groups(used_groups(procs));
334 for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
335 o << "class cmd_group_" << *it << " : public libt2n::command\n"
338 << " friend class boost::serialization::access;\n"
339 << " template<class Archive>\n"
340 << " void serialize(Archive & ar, const unsigned int /* version */)\n"
341 << " {ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);}\n"
345 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
346 o << "class " << it->ret_classname() << " : public libt2n::result\n"
349 << " " << it->ret_type << " res;\n"
350 << " friend class boost::serialization::access;\n"
351 << " template<class Archive>\n"
352 << " void serialize(Archive & ar, const unsigned int /* version */)\n"
354 << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);\n"
355 << " ar & BOOST_SERIALIZATION_NVP(res);\n"
358 << " " << it->ret_classname() << "() {}\n"
359 << " " << it->ret_classname() << "(const " << it->ret_type << " &_res) : res(_res) {}\n"
360 << " " << it->ret_type << " get_data() { return res; }\n"
363 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
364 o << "class " << it->cmd_classname() << " : public " << "cmd_group_" << it->group << "\n"
367 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
368 o << " " << ait->second.noref() << " " << ait->first << ";\n";
370 o << " friend class boost::serialization::access;\n"
371 << " template<class Archive>\n"
372 << " void serialize(Archive & ar, const unsigned int /* version */)\n"
374 << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(cmd_group_" << it->group << ");\n";
375 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
376 o << " ar & BOOST_SERIALIZATION_NVP(" << ait->first << ");\n";
379 // default constructor
383 << " " << it->cmd_classname() << "() {}\n";
385 // constructor taking all arguments
386 o << " " << it->cmd_classname() << "(";
387 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
388 if (ait!=it->args.begin()) o << ", ";
389 o << ait->second << " _" << ait->first;
392 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
393 if (ait!=it->args.begin()) o << ", ";
394 o << ait->first << "(_" << ait->first << ")";
397 << " libt2n::result* operator()();\n"
402 void output_common_cpp(std::ostream &o, const std::list<t2n_procedure> &procs, const std::string &common_hpp) {
403 std::set<std::string> groups(used_groups(procs));
405 o << "#include \"" << common_hpp << "\"\n"
406 << "#include <boost/serialization/export.hpp>\n"
408 << "/* register types with boost serialization */\n";
409 for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
410 o << "BOOST_CLASS_EXPORT(cmd_group_" << *it << ")\n";
412 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
413 o << "BOOST_CLASS_EXPORT("<<it->ret_classname()<<")\n"
414 << "BOOST_CLASS_EXPORT("<<it->cmd_classname()<<")\n";
418 void output_client_hpp(std::ostream &o, const std::list<t2n_procedure> &procs) {
419 std::set<std::string> groups(used_groups(procs));
421 o << "#include <command_client.hxx>\n";
423 for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
424 o << "class cmd_group_" << *it << "_client : public libt2n::command_client\n"
427 << "cmd_group_" << *it << "_client(libt2n::client_connection &_c,\n"
428 << " long long _command_timeout_usec=command_timeout_usec_default,\n"
429 << " long long _hello_timeout_usec=hello_timeout_usec_default)\n"
430 << " : libt2n::command_client(_c,_command_timeout_usec,_hello_timeout_usec)\n"
432 for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit) {
433 if (pit->group==*it) {
434 o << " " << *pit << ";\n";
441 void output_client_cpp(std::ostream &o, const std::list<t2n_procedure> &procs, const std::string &common_hpp, const std::string &common_cpp, const std::string &client_hpp) {
442 std::set<std::string> groups(used_groups(procs));
444 o << "#include \"" << client_hpp << "\"\n"
445 << "#include \"" << common_hpp << "\"\n"
447 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
448 o << "libt2n::result* " << it->cmd_classname() << "::operator()() { return NULL; }\n";
451 for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
452 for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit) {
453 if (pit->group==*it) {
454 o << pit->ret_type << " cmd_group_" << *it << "_client::" << pit->name << "(" << pit->args << ")\n"
456 << " libt2n::result_container rc;\n"
457 << " send_command(new " << pit->cmd_classname() << "(";
458 for (t2n_procedure::Args::const_iterator ait=pit->args.begin();ait!=pit->args.end();++ait) {
459 if (ait!=pit->args.begin()) o << ", ";
463 << " " << pit->ret_classname() << "* res=dynamic_cast<" << pit->ret_classname() << "*>(rc.get_result());\n"
464 << " if (!res) throw libt2n::t2n_communication_error(\"result object of wrong type\");\n"
465 << " return res->get_data();\n"
471 // include in this compilation unit to ensure the compilation unit is used
473 // http://www.google.de/search?q=g%2B%2B+static+initializer+in+static+library
474 o << "#include \"" << common_cpp << "\"\n";
477 void output_server_hpp(std::ostream &o, const std::list<t2n_procedure> &procs, const std::string &common_hpp) {
478 o << "#include \"" << common_hpp << "\"\n";
480 // output function declarations
481 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it)
485 void output_server_cpp(std::ostream &o, const std::list<t2n_procedure> &procs, const std::string &common_hpp, const std::string &common_cpp) {
486 o << "#include \"" << common_hpp << "\"\n";
488 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
490 o << "libt2n::result* " << it->cmd_classname() << "::operator()() { return new " << it->ret_classname() << "(" << it->name << "(";
491 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
492 if (ait!=it->args.begin()) o << ", ";
497 o << "#include \"" << common_cpp << "\"\n";
500 struct header_file : public std::ofstream
502 header_file(const char* fname) : std::ofstream(fname) {
503 std::cerr << "create header: '" << fname << "'" << std::endl;
504 std::string macro(replace(toupper(fname),'.','_'));
505 *this << "// automatically generated code - do not edit\n" << std::endl;
506 *this << "#ifndef " << macro << "\n"
507 << "#define " << macro << "\n";
510 *this << "#endif" << std::endl;
514 struct cpp_file : public std::ofstream
516 cpp_file(const char* fname) : std::ofstream(fname) {
517 std::cerr << "create cpp: '" << fname << "'" << std::endl;
518 *this << "// automatically generated code - do not edit\n" << std::endl;
522 std::list<std::string>
523 get_includes(const std::string &fname)
525 // grep "#include" fname
526 std::ifstream in(fname.c_str());
528 std::list<std::string> ret;
529 while (std::getline(in,line)) {
530 if (line.find("#include")!=std::string::npos)
537 paste_includes(std::ostream &o, std::list<std::string> &i)
540 << "// copied includes begin" << std::endl;
541 for (std::list<std::string>::const_iterator it=i.begin(); it!=i.end(); ++it)
542 o << *it << std::endl;
543 o << "// copied includes end" << std::endl
547 struct RemoveGenerated
549 RemoveGenerated(const std::string &_prefix) : prefix(_prefix) {}
550 bool operator()(const std::string &s) const {
551 return (s.find(prefix+"common.hxx")!=std::string::npos);
553 const std::string &prefix;
557 main(int argc, char* argv[])
560 std::cerr << "Usage: " << argv[0] << " header-file gccxml-file group" << std::endl;
565 std::string headerfile(argv[1]);
566 std::string xmlfile(argv[2]);
567 std::string group(argv[3]);
568 std::string prefix=group+"_";
569 std::list<std::string> includes(get_includes(headerfile));
570 remove_if(includes.begin(), includes.end(), RemoveGenerated(prefix));
571 includes.erase(remove_if(includes.begin(), includes.end(), RemoveGenerated(prefix)), includes.end());
573 //paste_includes(std::cerr, includes);
575 Parser parser(xmlfile, group);
576 std::list<t2n_procedure> procedures(parser.get_procedures());
578 std::cerr << "Procedures:" << std::endl;
579 for (std::list<t2n_procedure>::const_iterator it=procedures.begin();it!=procedures.end();++it)
580 std::cerr << *it << ";" << std::endl;
582 std::set<std::string> groups(used_groups(procedures));
583 std::cerr << "Used groups:" << std::endl;
584 for (std::set<std::string>::const_iterator it=groups.begin();it!=groups.end();++it)
585 std::cerr << *it << std::endl;
587 std::string common_hpp_fname(prefix+"common.hxx");
588 std::string common_cpp_fname(prefix+"common.cpp");
589 std::string client_hpp_fname(prefix+"client.hxx");
590 std::string client_cpp_fname(prefix+"client.cpp");
591 std::string server_hpp_fname(prefix+"server.hxx");
592 std::string server_cpp_fname(prefix+"server.cpp");
594 header_file common_hpp(common_hpp_fname.c_str());
595 common_hpp << "// boost serialization is picky about order of include files => we have to include this one first\n"
596 << "#include \"codegen-stubhead.hxx\"\n";
597 paste_includes(common_hpp, includes);
599 output_common_hpp(common_hpp, procedures);
601 cpp_file common_cpp(common_cpp_fname.c_str());
602 output_common_cpp(common_cpp, procedures, common_hpp_fname);
604 header_file client_hpp(client_hpp_fname.c_str());
605 client_hpp << "// boost serialization is picky about order of include files => we have to include this one first\n"
606 << "#include \"codegen-stubhead.hxx\"\n";
607 paste_includes(client_hpp, includes);
608 output_client_hpp(client_hpp, procedures);
610 cpp_file client_cpp(client_cpp_fname.c_str());
611 output_client_cpp(client_cpp, procedures, common_hpp_fname, common_cpp_fname, client_hpp_fname);
613 header_file server_hpp(server_hpp_fname.c_str());
614 output_server_hpp(server_hpp, procedures, common_hpp_fname);
616 cpp_file server_cpp(server_cpp_fname.c_str());
617 output_server_cpp(server_cpp, procedures, common_hpp_fname, common_cpp_fname);
618 }catch(const parse_error &e){
619 std::cerr << e.what() << std::endl;