2 Copyright (C) 2006-2008
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <libxml++/libxml++.h>
27 #include <boost/lexical_cast.hpp>
33 //! map group to class name
34 std::string groupClass(const std::string &group)
36 return std::string("cmd_group_")+group;
39 //! convert string to upper case
40 std::string toupper(std::string s)
42 for (unsigned i=0; i<s.length(); ++i) s[i]=toupper(s[i]);
46 //! replace all characters f by r in string s
47 std::string replace(std::string s, char f, char r)
49 for (unsigned i=0; i<s.length(); ++i) if (s[i]==f) s[i]=r;
53 //! strip prefix from string s
55 \return string s without prefix or an empty string on error
57 std::string strip(std::string s, std::string prefix)
60 if ( (prefix.length()>s.length() ) || ( std::string(s,0,prefix.length())!=prefix ) ) return error;
61 return std::string(s, prefix.length(), s.length()-prefix.length());
64 //! get child element by id
66 \return pointer to element having id or null on error
67 \todo find libxmlpp pendant
69 const xmlpp::Element* get_element_by_id(const xmlpp::Element* element, const std::string &id)
71 const xmlpp::Attribute* cid = element->get_attribute("id");
72 if ( cid && ( cid->get_value() == id)) return element;
74 //Recurse through child nodes:
75 xmlpp::Node::NodeList list = element->get_children();
76 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
78 const xmlpp::Element* element = dynamic_cast<const xmlpp::Element*>(*iter);
81 const xmlpp::Element* match = get_element_by_id(element, id);
82 if (match) return match;
88 std::string get_file(const xmlpp::Element* root, const std::string &file_id)
91 const xmlpp::Element* e=get_element_by_id(root, file_id);
92 if ((!e)||(!e->get_attribute("name"))) return error;
93 return e->get_attribute("name")->get_value();
96 //! get namespace by id
98 \return namespace name or empty string on error
100 std::string get_namespace(const xmlpp::Element* root, const std::string &id)
103 const xmlpp::Element* element(get_element_by_id(root, id));
104 // [RP:20071024]: use "demangled" attribute instead of "name" to cover nested namespaces:
105 if ((!element)||(!element->get_attribute("demangled"))) return error;
106 return element->get_attribute("demangled")->get_value();
109 //! procedure marked for export?
110 bool is_marked(const std::string &attrs)
112 // todo: improve this
113 std::string to_match("gccxml(libt2n-");
114 std::string::size_type p(attrs.find(to_match));
115 return (p!=std::string::npos);
121 std::string noref_name;
122 bool operator==(const type_info& o) {return (name==o.name) && (noref_name == o.noref_name);}
123 std::string noref() const {return noref_name.empty() ? name : noref_name;}
124 bool isVoid() const {return name=="void";}
127 std::ostream &operator<<(std::ostream &o, const type_info &t)
133 struct parse_error : public std::runtime_error
135 parse_error(const std::string &file, unsigned line, const std::string &msg)
136 : std::runtime_error(file+":"+boost::lexical_cast<std::string>(line)+": error: "+msg)
142 \return type name or empty string on error
144 type_info get_type(const xmlpp::Element* root, const std::string &id)
147 const xmlpp::Element* element(get_element_by_id(root, id));
148 if (!element) return error;
150 // TODO: not yet complete
151 // if we recurse - when do we stop?
152 // if it is a typedef? yes? (hmm if the typedef is in the file parsed this will not work)
154 // TODO: const and reference types handling is a ugly hack
156 std::string tag(element->get_name());
157 if (tag=="ReferenceType")
159 assert(element->get_attribute("type"));
160 type_info ret(get_type(root, element->get_attribute("type")->get_value()));
161 if (ret==error) return error;
162 // at the moment we only support const &
163 // todo: nice error message!
164 if ((ret.noref_name=strip(ret.name,"const ")).empty()) return error;
165 ret.name=ret.name+"&";
168 else if (tag=="CvQualifiedType")
170 assert(element->get_attribute("type"));
171 type_info ret(get_type(root, element->get_attribute("type")->get_value()));
172 if (ret==error) return error;
173 ret.name=std::string("const ")+ret.name;
176 else if (tag=="PointerType")
178 // todo: nearly the same as reference type handling
179 assert(element->get_attribute("type"));
180 type_info ret(get_type(root, element->get_attribute("type")->get_value()));
181 if (ret==error) return error;
182 // at the moment we only support const &
183 // todo: nice error message!
184 if ((ret.noref_name=strip(ret.name,"const ")).empty()) return error;
185 ret.name=ret.name+"*";
189 assert(element->get_attribute("name"));
191 if (element->get_attribute("context"))
193 ret.name=get_namespace(root, element->get_attribute("context")->get_value());
197 // do not explicitely add ::
200 ret.name+=element->get_attribute("name")->get_value();
208 std::string defaultArg;
213 typedef std::list<Arg> Args;
220 std::string ret_classname() const
222 return name+mangled+"_res";
224 std::string cmd_classname() const
226 return name+mangled+"_cmd";
228 bool hasReturn() const {return !ret_type.isVoid();}
231 std::ostream &operator<<(std::ostream &o, const t2n_procedure::Args &args)
233 for (t2n_procedure::Args::const_iterator it=args.begin();it!=args.end();++it)
235 if (it!=args.begin()) o << ", ";
236 o << it->type << " " << it->name;
238 if (!it->defaultArg.empty())
239 o << "=" << it->defaultArg;
244 std::ostream &operator<<(std::ostream &o, const t2n_procedure &f)
246 o << f.ret_type << " " << f.name << "(" << f.args << ")";
250 std::pair<std::string, unsigned>
251 get_file_and_line(const xmlpp::Element* root, const xmlpp::Element* element)
253 return std::pair<std::string, unsigned>(get_file(root, element->get_attribute("file")->get_value()),
254 boost::lexical_cast<unsigned>(element->get_attribute("line")->get_value())-1);
257 std::string get_file_and_line_as_string(const xmlpp::Element* root, const xmlpp::Element* element)
259 std::pair<std::string, unsigned> fl(get_file_and_line(root,element));
260 return std::string(fl.first)+":"+boost::lexical_cast<std::string>(fl.second);
266 Parser(const std::string &fname) : m_fname(fname) {}
268 std::list<t2n_procedure> get_procedures()
270 xmlpp::DomParser parser;
271 // parser.set_validate();
272 parser.set_substitute_entities(); //We just want the text to be resolved/unescaped automatically.
273 parser.parse_file(m_fname);
277 const xmlpp::Node* pNode = parser.get_document()->get_root_node(); //deleted by DomParser.
278 const xmlpp::Element* root = dynamic_cast<const xmlpp::Element*>(pNode);
286 std::list<t2n_procedure> m_procedures;
288 void parse_function(const xmlpp::Element* root, const xmlpp::Node* node)
290 const xmlpp::Element* element = dynamic_cast<const xmlpp::Element*>(node);
291 if (!element) return;
293 const xmlpp::Attribute* attributes = element->get_attribute("attributes");
294 const xmlpp::Attribute* name = element->get_attribute("name");
295 const xmlpp::Attribute* mangled = element->get_attribute("mangled");
296 const xmlpp::Attribute* returns = element->get_attribute("returns");
297 if ((!attributes)||(!name)||(!mangled)||(!returns)) return;
299 // check wether the procedure is marked (TODO: improve)
300 // attributes are speparated by spaces?
303 if (!is_marked(attributes->get_value())) return;
305 // we need the return type
306 f.ret_type=get_type(root, returns->get_value());
307 f.name=name->get_value();
308 f.mangled=mangled->get_value();
310 xmlpp::Node::NodeList list = node->get_children("Argument");
311 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
313 const xmlpp::Element* arg = dynamic_cast<const xmlpp::Element*>(*iter);
318 assert(arg->get_name() == "Argument");
320 assert(arg->get_attribute("name"));
321 a.name=arg->get_attribute("name")->get_value();
323 assert(arg->get_attribute("type"));
324 a.type=get_type(root, arg->get_attribute("type")->get_value());
326 if(arg->get_attribute("default"))
327 a.defaultArg=arg->get_attribute("default")->get_value();
329 // todo: ugly - could be any other error
330 if (a.type.name.empty())
332 assert(element->get_attribute("file"));
333 assert(element->get_attribute("line"));
334 std::pair<std::string, unsigned> file_and_line(get_file_and_line(root, element));
335 throw parse_error(file_and_line.first,
336 file_and_line.second,
337 std::string("type of parameter '")+a.name+"' not (yet?) supported");
343 std::cerr << get_file_and_line_as_string(root, element) << ":\texport procedure: " << f << std::endl;
344 m_procedures.push_back(f);
347 void visit_node(const xmlpp::Element* root, const xmlpp::Node* node = NULL, unsigned int indentation = 0)
349 if (!node) node=root;
351 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
352 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node);
353 const xmlpp::CommentNode* nodeComment = dynamic_cast<const xmlpp::CommentNode*>(node);
355 if (nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this.
358 std::string nodename = node->get_name();
360 if (!nodeText && !nodeComment && !nodename.empty()) //Let's not say "name: text".
362 if (node->get_name() == "Function") parse_function(root, node);
366 //Recurse through child nodes:
367 xmlpp::Node::NodeList list = node->get_children();
368 for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
370 visit_node(root, *iter, indentation + 2); //recursive
376 void output_common_hpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs)
378 o << "class " << groupClass(group) << " : public libt2n::command\n"
381 << " friend class boost::serialization::access;\n"
382 << " template<class Archive>\n"
383 << " void serialize(Archive & ar, const unsigned int /* version */)\n"
384 << " {ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);}\n"
387 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it)
389 o << "class " << it->ret_classname() << " : public libt2n::result\n"
393 o << " " << it->ret_type << " res;\n";
394 o << " friend class boost::serialization::access;\n"
395 << " template<class Archive>\n"
396 << " void serialize(Archive & ar, const unsigned int /* version */)\n"
398 << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);\n";
400 o << " ar & BOOST_SERIALIZATION_NVP(res);\n";
403 << " " << it->ret_classname() << "() {}\n";
406 o << " " << it->ret_classname() << "(const " << it->ret_type << " &_res) : res(_res) {}\n"
407 << " " << it->ret_type << " get_data() { return res; }\n";
411 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it)
413 o << "class " << it->cmd_classname() << " : public " << groupClass(group) << "\n"
416 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait)
418 o << " " << ait->type.noref() << " " << ait->name << ";\n";
420 o << " friend class boost::serialization::access;\n"
421 << " template<class Archive>\n"
422 << " void serialize(Archive & ar, const unsigned int /* version */)\n"
424 << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(" << groupClass(group) << ");\n";
425 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait)
427 o << " ar & BOOST_SERIALIZATION_NVP(" << ait->name << ");\n";
430 // default constructor
434 << " " << it->cmd_classname() << "() {}\n";
436 // constructor taking all arguments
437 if (!it->args.empty())
439 o << " " << it->cmd_classname() << "(";
440 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait)
442 if (ait!=it->args.begin()) o << ", ";
443 o << ait->type << " _" << ait->name;
446 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait)
448 if (ait!=it->args.begin()) o << ", ";
449 // pointers are const pointers and must be dereferenced
450 o << ait->name << "(" << ((ait->type.name.find_first_of('*')!=std::string::npos) ? "*" : "" ) << "_" << ait->name << ")";
454 o << " libt2n::result* operator()();\n"
459 void output_common_cpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs, const std::string &common_hpp)
461 o << "#include \"" << common_hpp << "\"\n"
462 << "#include <boost/serialization/export.hpp>\n"
464 << "/* register types with boost serialization */\n";
465 o << "BOOST_CLASS_EXPORT(" << groupClass(group) << ")\n";
466 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it)
468 o << "BOOST_CLASS_EXPORT("<<it->ret_classname()<<")\n"
469 << "BOOST_CLASS_EXPORT("<<it->cmd_classname()<<")\n";
473 void output_client_hpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs)
475 o << "#include <command_client.hxx>\n";
477 o << "class " << groupClass(group) << "_client : public libt2n::command_client\n"
480 << groupClass(group) << "_client(libt2n::client_connection &_c,\n"
481 << " long long _command_timeout_usec=command_timeout_usec_default,\n"
482 << " long long _hello_timeout_usec=hello_timeout_usec_default)\n"
483 << " : libt2n::command_client(_c,_command_timeout_usec,_hello_timeout_usec)\n"
485 for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit)
487 o << " " << *pit << ";\n";
492 void output_client_cpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs, const std::string &common_hpp, const std::string &common_cpp, const std::string &client_hpp)
494 o << "#include \"" << client_hpp << "\"\n"
495 << "#include \"" << common_hpp << "\"\n"
497 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it)
499 o << "libt2n::result* " << it->cmd_classname() << "::operator()() { return NULL; }\n";
502 for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit)
504 o << pit->ret_type << " " << groupClass(group) << "_client::" << pit->name << "(";
506 // we need to do this by hand here because we don't want default arguments within the cpp
507 for (t2n_procedure::Args::const_iterator xit=pit->args.begin();xit!=pit->args.end();++xit)
509 if (xit!=pit->args.begin())
511 o << xit->type << " " << xit->name;
516 << " libt2n::result_container rc;\n"
517 << " send_command(new " << pit->cmd_classname() << "(";
518 for (t2n_procedure::Args::const_iterator ait=pit->args.begin();ait!=pit->args.end();++ait)
520 if (ait!=pit->args.begin()) o << ", ";
524 << " " << pit->ret_classname() << "* res=dynamic_cast<" << pit->ret_classname() << "*>(rc.get_result());\n"
525 << " if (!res) throw libt2n::t2n_communication_error(\"result object of wrong type\");\n";
526 if (pit->hasReturn())
527 o << " return res->get_data();\n";
531 // include in this compilation unit to ensure the compilation unit is used
533 // http://www.google.de/search?q=g%2B%2B+static+initializer+in+static+library
534 o << "#include \"" << common_cpp << "\"\n";
537 void output_server_hpp(std::ostream &o, const std::string & /* group */, const std::list<t2n_procedure> &procs, const std::string &common_hpp)
539 o << "#include \"" << common_hpp << "\"\n";
541 // output function declarations
542 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it)
546 void output_server_cpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs, const std::string &common_hpp, const std::string &common_cpp)
548 o << "#include \"" << common_hpp << "\"\n";
550 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it)
553 o << "libt2n::result* " << it->cmd_classname() << "::operator()() { ";
556 o << "return new " << it->ret_classname() << "(";
558 // output function name and args
559 o << it->name << "(";
560 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait)
562 if (ait!=it->args.begin()) o << ", ";
564 if (ait->type.name.find_first_of('*')!=std::string::npos)
572 o << "); return new " << it->ret_classname() << "();";
576 o << "#include \"" << common_cpp << "\"\n";
579 struct header_file : public std::ofstream
581 header_file(const char* fname) : std::ofstream(fname)
583 std::cerr << "create header: '" << fname << "'" << std::endl;
584 std::string macro(replace(toupper(fname),'.','_'));
585 *this << "// automatically generated code (generated by libt2n-codegen " << VERSION << ") - do not edit\n" << std::endl;
586 *this << "#ifndef " << macro << "\n"
587 << "#define " << macro << "\n";
591 *this << "#endif" << std::endl;
595 struct cpp_file : public std::ofstream
597 cpp_file(const char* fname) : std::ofstream(fname)
599 std::cerr << "create cpp: '" << fname << "'" << std::endl;
600 *this << "// automatically generated code - do not edit\n" << std::endl;
605 main(int argc, char* argv[])
607 // todo: maybe use getopt
608 if ((argc>1)&&(std::string(argv[1])=="--version"))
610 std::cerr << VERSION << std::endl;
615 std::cerr << "Usage: " << argv[0] << "default-group gccxml-file1 gccxml-file2 ... " << std::endl;
621 std::string group(argv[1]);
622 std::list<std::string> xmlfiles;
623 for (int i=2;i<argc;++i)
624 xmlfiles.push_back(argv[i]);
626 std::string prefix=group+"_";
627 std::list<t2n_procedure> procedures;
628 for (std::list<std::string>::iterator it=xmlfiles.begin();it!=xmlfiles.end();++it)
630 std::cerr << "Parse " << *it << std::endl;
632 const std::list<t2n_procedure> &p(parser.get_procedures());
633 std::copy(p.begin(), p.end(), std::back_inserter(procedures));
636 std::cerr << "All procedures:" << std::endl;
637 for (std::list<t2n_procedure>::const_iterator it=procedures.begin();it!=procedures.end();++it)
638 std::cerr << *it << ";" << std::endl;
640 std::string common_hpp_fname(prefix+"common.hxx");
641 std::string common_cpp_fname(prefix+"common.cpp");
642 std::string client_hpp_fname(prefix+"client.hxx");
643 std::string client_cpp_fname(prefix+"client.cpp");
644 std::string server_hpp_fname(prefix+"server.hxx");
645 std::string server_cpp_fname(prefix+"server.cpp");
647 header_file common_hpp(common_hpp_fname.c_str());
648 common_hpp << "// boost serialization is picky about order of include files => we have to include this one first\n"
649 << "#include \"codegen-stubhead.hxx\"\n"
650 << "#include \"" << group << ".hxx\"\n";
652 output_common_hpp(common_hpp, group, procedures);
654 cpp_file common_cpp(common_cpp_fname.c_str());
655 output_common_cpp(common_cpp, group, procedures, common_hpp_fname);
657 header_file client_hpp(client_hpp_fname.c_str());
658 client_hpp << "// boost serialization is picky about order of include files => we have to include this one first\n"
659 << "#include \"codegen-stubhead.hxx\"\n"
660 << "#include \"" << group << ".hxx\"\n";
661 output_client_hpp(client_hpp, group, procedures);
663 cpp_file client_cpp(client_cpp_fname.c_str());
664 output_client_cpp(client_cpp, group, procedures, common_hpp_fname, common_cpp_fname, client_hpp_fname);
666 header_file server_hpp(server_hpp_fname.c_str());
667 output_server_hpp(server_hpp, group, procedures, common_hpp_fname);
669 cpp_file server_cpp(server_cpp_fname.c_str());
670 output_server_cpp(server_cpp, group, procedures, common_hpp_fname, common_cpp_fname);
672 catch (const parse_error &e)
674 std::cerr << e.what() << std::endl;