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
35 groupClass(const std::string &group) {
36 return std::string("cmd_group_")+group;
39 //! convert string to upper case
41 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
48 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
58 strip(std::string s, std::string prefix)
61 if ( (prefix.length()>s.length() ) || ( std::string(s,0,prefix.length())!=prefix ) ) return error;
62 return std::string(s, prefix.length(), s.length()-prefix.length());
65 //! get child element by id
67 \return pointer to element having id or null on error
68 \todo find libxmlpp pendant
70 const xmlpp::Element* get_element_by_id(const xmlpp::Element* element, const std::string &id)
72 const xmlpp::Attribute* cid = element->get_attribute("id");
73 if ( cid && ( cid->get_value() == id)) return element;
75 //Recurse through child nodes:
76 xmlpp::Node::NodeList list = element->get_children();
77 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
79 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;
89 get_file(const xmlpp::Element* root, const std::string &file_id)
92 const xmlpp::Element* e=get_element_by_id(root, file_id);
93 if ((!e)||(!e->get_attribute("name"))) return error;
94 return e->get_attribute("name")->get_value();
97 //! get namespace by id
99 \return namespace name or empty string on error
101 std::string get_namespace(const xmlpp::Element* root, const std::string &id)
104 const xmlpp::Element* element(get_element_by_id(root, id));
105 if ((!element)||(!element->get_attribute("name"))) return error;
106 return element->get_attribute("name")->get_value();
109 //! procedure marked for export?
111 is_marked(const std::string &attrs)
113 // todo: improve this
114 std::string to_match("gccxml(libt2n-");
115 std::string::size_type p(attrs.find(to_match));
116 return (p!=std::string::npos);
122 std::string noref_name;
123 bool operator==(const type_info& o) {return (name==o.name) && (noref_name == o.noref_name);}
124 std::string noref() const {return noref_name.empty() ? name : noref_name;}
127 std::ostream &operator<<(std::ostream &o, const type_info &t) {
132 struct parse_error : public std::runtime_error
134 parse_error(const std::string &file, unsigned line, const std::string &msg)
135 : std::runtime_error(file+":"+boost::lexical_cast<std::string>(line)+": error: "+msg)
141 \return type name or empty string on error
143 type_info get_type(const xmlpp::Element* root, const std::string &id)
146 const xmlpp::Element* element(get_element_by_id(root, id));
147 if (!element) return error;
149 // TODO: not yet complete
150 // if we recurse - when do we stop?
151 // if it is a typedef? yes? (hmm if the typedef is in the file parsed this will not work)
153 // TODO: const and reference types handling is a ugly hack
155 std::string tag(element->get_name());
156 if (tag=="ReferenceType") {
157 assert(element->get_attribute("type"));
158 type_info ret(get_type(root, element->get_attribute("type")->get_value()));
159 if (ret==error) return error;
160 // at the moment we only support const &
161 // todo: nice error message!
162 if ((ret.noref_name=strip(ret.name,"const ")).empty()) return error;
163 ret.name=ret.name+"&";
165 }else if (tag=="CvQualifiedType") {
166 assert(element->get_attribute("type"));
167 type_info ret(get_type(root, element->get_attribute("type")->get_value()));
168 if (ret==error) return error;
169 ret.name=std::string("const ")+ret.name;
171 }else if (tag=="PointerType") {
176 assert(element->get_attribute("name"));
178 if (element->get_attribute("context")) {
179 ret.name=get_namespace(root, element->get_attribute("context")->get_value());
183 // do not explicitely add ::
186 ret.name+=element->get_attribute("name")->get_value();
192 typedef std::list<std::pair<std::string, type_info> > Args;
199 std::string ret_classname() const {
200 return name+mangled+"_res";
202 std::string cmd_classname() const {
203 return name+mangled+"_cmd";
207 std::ostream &operator<<(std::ostream &o, const t2n_procedure::Args &args) {
208 for (t2n_procedure::Args::const_iterator it=args.begin();it!=args.end();++it) {
209 if (it!=args.begin()) o << ", ";
210 o << it->second << " " << it->first;
215 std::ostream &operator<<(std::ostream &o, const t2n_procedure &f) {
216 o << f.ret_type << " " << f.name << "(" << f.args << ")";
223 Parser(const std::string &fname) : m_fname(fname) {}
225 std::list<t2n_procedure> get_procedures() {
226 xmlpp::DomParser parser;
227 // parser.set_validate();
228 parser.set_substitute_entities(); //We just want the text to be resolved/unescaped automatically.
229 parser.parse_file(m_fname);
233 const xmlpp::Node* pNode = parser.get_document()->get_root_node(); //deleted by DomParser.
234 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 if (!is_marked(attributes->get_value())) return;
260 // we need the return type
261 f.ret_type=get_type(root, returns->get_value());
262 f.name=name->get_value();
263 f.mangled=mangled->get_value();
265 xmlpp::Node::NodeList list = node->get_children("Argument");
266 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
268 const xmlpp::Element* arg = dynamic_cast<const xmlpp::Element*>(*iter);
270 assert(arg->get_name() == "Argument");
271 assert(arg->get_attribute("name"));
272 assert(arg->get_attribute("type"));
273 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())));
274 // todo: ugly - could be any other error
275 if (f.args.back().second.name.empty()) {
276 assert(element->get_attribute("file"));
277 assert(element->get_attribute("line"));
278 throw parse_error(get_file(root, element->get_attribute("file")->get_value()),
279 boost::lexical_cast<unsigned>(element->get_attribute("line")->get_value())-1,
280 std::string("type of parameter `")+f.args.back().first+"' not (yet?) supported");
284 std::cerr << "Found function: " << f << std::endl;
285 m_procedures.push_back(f);
288 void visit_node(const xmlpp::Element* root, const xmlpp::Node* node = NULL, unsigned int indentation = 0)
290 if (!node) node=root;
292 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
293 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node);
294 const xmlpp::CommentNode* nodeComment = dynamic_cast<const xmlpp::CommentNode*>(node);
296 if(nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this.
299 std::string nodename = node->get_name();
301 if(!nodeText && !nodeComment && !nodename.empty()) //Let's not say "name: text".
303 if (node->get_name() == "Function") parse_function(root, node);
307 //Recurse through child nodes:
308 xmlpp::Node::NodeList list = node->get_children();
309 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
311 visit_node(root, *iter, indentation + 2); //recursive
317 void output_common_hpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs) {
318 o << "class " << groupClass(group) << " : public libt2n::command\n"
321 << " friend class boost::serialization::access;\n"
322 << " template<class Archive>\n"
323 << " void serialize(Archive & ar, const unsigned int /* version */)\n"
324 << " {ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);}\n"
327 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
328 o << "class " << it->ret_classname() << " : public libt2n::result\n"
331 << " " << it->ret_type << " res;\n"
332 << " friend class boost::serialization::access;\n"
333 << " template<class Archive>\n"
334 << " void serialize(Archive & ar, const unsigned int /* version */)\n"
336 << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);\n"
337 << " ar & BOOST_SERIALIZATION_NVP(res);\n"
340 << " " << it->ret_classname() << "() {}\n"
341 << " " << it->ret_classname() << "(const " << it->ret_type << " &_res) : res(_res) {}\n"
342 << " " << it->ret_type << " get_data() { return res; }\n"
345 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
346 o << "class " << it->cmd_classname() << " : public " << groupClass(group) << "\n"
349 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
350 o << " " << ait->second.noref() << " " << ait->first << ";\n";
352 o << " friend class boost::serialization::access;\n"
353 << " template<class Archive>\n"
354 << " void serialize(Archive & ar, const unsigned int /* version */)\n"
356 << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(" << groupClass(group) << ");\n";
357 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
358 o << " ar & BOOST_SERIALIZATION_NVP(" << ait->first << ");\n";
361 // default constructor
365 << " " << it->cmd_classname() << "() {}\n";
367 // constructor taking all arguments
368 o << " " << it->cmd_classname() << "(";
369 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
370 if (ait!=it->args.begin()) o << ", ";
371 o << ait->second << " _" << ait->first;
374 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
375 if (ait!=it->args.begin()) o << ", ";
376 o << ait->first << "(_" << ait->first << ")";
379 << " libt2n::result* operator()();\n"
384 void output_common_cpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs, const std::string &common_hpp) {
385 o << "#include \"" << common_hpp << "\"\n"
386 << "#include <boost/serialization/export.hpp>\n"
388 << "/* register types with boost serialization */\n";
389 o << "BOOST_CLASS_EXPORT(" << groupClass(group) << ")\n";
390 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
391 o << "BOOST_CLASS_EXPORT("<<it->ret_classname()<<")\n"
392 << "BOOST_CLASS_EXPORT("<<it->cmd_classname()<<")\n";
396 void output_client_hpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs) {
397 o << "#include <command_client.hxx>\n";
399 o << "class " << groupClass(group) << "_client : public libt2n::command_client\n"
402 << groupClass(group) << "_client(libt2n::client_connection &_c,\n"
403 << " long long _command_timeout_usec=command_timeout_usec_default,\n"
404 << " long long _hello_timeout_usec=hello_timeout_usec_default)\n"
405 << " : libt2n::command_client(_c,_command_timeout_usec,_hello_timeout_usec)\n"
407 for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit) {
408 o << " " << *pit << ";\n";
413 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) {
414 o << "#include \"" << client_hpp << "\"\n"
415 << "#include \"" << common_hpp << "\"\n"
417 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
418 o << "libt2n::result* " << it->cmd_classname() << "::operator()() { return NULL; }\n";
421 for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit) {
422 o << pit->ret_type << " " << groupClass(group) << "_client::" << pit->name << "(" << pit->args << ")\n"
424 << " libt2n::result_container rc;\n"
425 << " send_command(new " << pit->cmd_classname() << "(";
426 for (t2n_procedure::Args::const_iterator ait=pit->args.begin();ait!=pit->args.end();++ait) {
427 if (ait!=pit->args.begin()) o << ", ";
431 << " " << pit->ret_classname() << "* res=dynamic_cast<" << pit->ret_classname() << "*>(rc.get_result());\n"
432 << " if (!res) throw libt2n::t2n_communication_error(\"result object of wrong type\");\n"
433 << " return res->get_data();\n"
437 // include in this compilation unit to ensure the compilation unit is used
439 // http://www.google.de/search?q=g%2B%2B+static+initializer+in+static+library
440 o << "#include \"" << common_cpp << "\"\n";
443 void output_server_hpp(std::ostream &o, const std::string & /* group */, const std::list<t2n_procedure> &procs, const std::string &common_hpp) {
444 o << "#include \"" << common_hpp << "\"\n";
446 // output function declarations
447 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it)
451 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) {
452 o << "#include \"" << common_hpp << "\"\n";
454 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
456 o << "libt2n::result* " << it->cmd_classname() << "::operator()() { return new " << it->ret_classname() << "(" << it->name << "(";
457 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
458 if (ait!=it->args.begin()) o << ", ";
463 o << "#include \"" << common_cpp << "\"\n";
466 struct header_file : public std::ofstream
468 header_file(const char* fname) : std::ofstream(fname) {
469 std::cerr << "create header: '" << fname << "'" << std::endl;
470 std::string macro(replace(toupper(fname),'.','_'));
471 *this << "// automatically generated code (generated by libt2n-codegen " << VERSION << ") - do not edit\n" << std::endl;
472 *this << "#ifndef " << macro << "\n"
473 << "#define " << macro << "\n";
476 *this << "#endif" << std::endl;
480 struct cpp_file : public std::ofstream
482 cpp_file(const char* fname) : std::ofstream(fname) {
483 std::cerr << "create cpp: '" << fname << "'" << std::endl;
484 *this << "// automatically generated code - do not edit\n" << std::endl;
489 main(int argc, char* argv[])
491 // todo: maybe use getopt
492 if ((argc>1)&&(std::string(argv[1])=="--version")) {
493 std::cerr << VERSION << std::endl;
498 std::cerr << "Usage: " << argv[0] << "default-group gccxml-file1 gccxml-file2 ... " << std::endl;
503 std::string group(argv[1]);
504 std::list<std::string> xmlfiles;
505 for (int i=2;i<argc;++i)
506 xmlfiles.push_back(argv[i]);
508 std::string prefix=group+"_";
509 std::list<t2n_procedure> procedures;
510 for (std::list<std::string>::iterator it=xmlfiles.begin();it!=xmlfiles.end();++it) {
511 std::cerr << "Parse " << *it << std::endl;
513 const std::list<t2n_procedure> &p(parser.get_procedures());
514 std::copy(p.begin(), p.end(), std::back_inserter(procedures));
517 std::cerr << "Procedures:" << std::endl;
518 for (std::list<t2n_procedure>::const_iterator it=procedures.begin();it!=procedures.end();++it)
519 std::cerr << *it << ";" << std::endl;
521 std::string common_hpp_fname(prefix+"common.hxx");
522 std::string common_cpp_fname(prefix+"common.cpp");
523 std::string client_hpp_fname(prefix+"client.hxx");
524 std::string client_cpp_fname(prefix+"client.cpp");
525 std::string server_hpp_fname(prefix+"server.hxx");
526 std::string server_cpp_fname(prefix+"server.cpp");
528 header_file common_hpp(common_hpp_fname.c_str());
529 common_hpp << "// boost serialization is picky about order of include files => we have to include this one first\n"
530 << "#include \"codegen-stubhead.hxx\"\n"
531 << "#include \"" << group << ".hxx\"\n";
533 output_common_hpp(common_hpp, group, procedures);
535 cpp_file common_cpp(common_cpp_fname.c_str());
536 output_common_cpp(common_cpp, group, procedures, common_hpp_fname);
538 header_file client_hpp(client_hpp_fname.c_str());
539 client_hpp << "// boost serialization is picky about order of include files => we have to include this one first\n"
540 << "#include \"codegen-stubhead.hxx\"\n"
541 << "#include \"" << group << ".hxx\"\n";
542 output_client_hpp(client_hpp, group, procedures);
544 cpp_file client_cpp(client_cpp_fname.c_str());
545 output_client_cpp(client_cpp, group, procedures, common_hpp_fname, common_cpp_fname, client_hpp_fname);
547 header_file server_hpp(server_hpp_fname.c_str());
548 output_server_hpp(server_hpp, group, procedures, common_hpp_fname);
550 cpp_file server_cpp(server_cpp_fname.c_str());
551 output_server_cpp(server_cpp, group, procedures, common_hpp_fname, common_cpp_fname);
552 }catch(const parse_error &e){
553 std::cerr << e.what() << std::endl;