1 #include <libxml++/libxml++.h>
8 void print_indentation(unsigned int indentation)
10 for(unsigned int i = 0; i < indentation; ++i)
15 toupper(std::string s) {
16 for (unsigned i=0; i<s.length(); ++i) s[i]=toupper(s[i]);
21 replace(std::string s, char f, char r) {
22 for (unsigned i=0; i<s.length(); ++i) if (s[i]==f) s[i]=r;
27 strip(std::string s, std::string prefix)
30 if ( (prefix.length()>s.length() ) || ( std::string(s,0,prefix.length())!=prefix ) ) return error;
31 return std::string(s, prefix.length(), s.length()-prefix.length());
34 //! get child element by id
36 \return pointer to element having id or null on error
37 \todo find libxmlpp pendant
39 const xmlpp::Element* get_element_by_id(const xmlpp::Element* element, const std::string &id)
41 const xmlpp::Attribute* cid = element->get_attribute("id");
42 if ( cid && ( cid->get_value() == id)) return element;
44 //Recurse through child nodes:
45 xmlpp::Node::NodeList list = element->get_children();
46 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
48 const xmlpp::Element* element = dynamic_cast<const xmlpp::Element*>(*iter);
50 const xmlpp::Element* match = get_element_by_id(element, id);
51 if (match) return match;
57 //! get namespace by id
59 \return namespace name or empty string on error
61 std::string get_namespace(const xmlpp::Element* root, const std::string &id)
64 const xmlpp::Element* element(get_element_by_id(root, id));
65 if ((!element)||(!element->get_attribute("name"))) return error;
66 return element->get_attribute("name")->get_value();
73 std::string noref_name;
74 bool operator==(const type_info& o) {return (name==o.name) && (noref_name == o.noref_name);}
75 std::string noref() const {return noref_name.empty() ? name : noref_name;}
78 std::ostream &operator<<(std::ostream &o, const type_info &t) {
85 \return type name or empty string on error
87 type_info get_type(const xmlpp::Element* root, const std::string &id)
90 const xmlpp::Element* element(get_element_by_id(root, id));
91 if (!element) return error;
93 // TODO: not yet complete
94 // if we recurse - when do we stop?
95 // if it is a typedef? yes? (hmm if the typedef is in the file parsed this will not work)
97 // TODO: const and reference types handling is a ugly hack
99 std::string tag(element->get_name());
100 if (tag=="ReferenceType") {
101 assert(element->get_attribute("type"));
102 type_info ret(get_type(root, element->get_attribute("type")->get_value()));
103 if (ret==error) return error;
104 // at the moment we only support const &
105 // todo: nice error message!
106 if ((ret.noref_name=strip(ret.name,"const ")).empty()) return error;
107 ret.name=ret.name+"&";
109 }else if (tag=="CvQualifiedType") {
110 assert(element->get_attribute("type"));
111 type_info ret(get_type(root, element->get_attribute("type")->get_value()));
112 if (ret==error) return error;
113 ret.name=std::string("const ")+ret.name;
117 assert(element->get_attribute("name"));
119 if (element->get_attribute("context"))
120 ret.name=get_namespace(root, element->get_attribute("context")->get_value())+"::";
121 ret.name+=element->get_attribute("name")->get_value();
127 typedef std::list<std::pair<std::string, type_info> > Args;
134 std::string ret_classname() const {
137 std::string cmd_classname() const {
142 std::ostream &operator<<(std::ostream &o, const t2n_procedure::Args &args) {
143 for (t2n_procedure::Args::const_iterator it=args.begin();it!=args.end();++it) {
144 if (it!=args.begin()) o << ", ";
145 o << it->second << " " << it->first;
150 std::ostream &operator<<(std::ostream &o, const t2n_procedure &f) {
151 o << f.ret_type << " " << f.name << "(" << f.args << ")";
158 Parser(const std::string &fname) : m_fname(fname) {}
160 std::list<t2n_procedure> get_procedures() {
161 xmlpp::DomParser parser;
162 // parser.set_validate();
163 parser.set_substitute_entities(); //We just want the text to be resolved/unescaped automatically.
164 parser.parse_file(m_fname);
168 const xmlpp::Node* pNode = parser.get_document()->get_root_node(); //deleted by DomParser.
169 const xmlpp::Element* root = dynamic_cast<const xmlpp::Element*>(pNode);
177 std::list<t2n_procedure> m_procedures;
179 void parse_function(const xmlpp::Element* root, const xmlpp::Node* node) {
180 const xmlpp::Element* element = dynamic_cast<const xmlpp::Element*>(node);
181 if (!element) return;
183 const xmlpp::Attribute* attributes = element->get_attribute("attributes");
184 const xmlpp::Attribute* name = element->get_attribute("name");
185 const xmlpp::Attribute* returns = element->get_attribute("returns");
186 if ((!attributes)||(!name)||(!returns)) return;
188 // check wether the procedure is marked (TODO: improve)
189 // attributes are speparated by spaces?
190 if (attributes->get_value().find("gccxml(libt2n")==std::string::npos) return;
192 // TODO: extract command group
193 // something like: sed 's,gccxml(libt2n-\([a-z]*}),\1,'
197 // we need the return type
198 f.ret_type=get_type(root, returns->get_value());
199 f.name=name->get_value();
201 xmlpp::Node::NodeList list = node->get_children("Argument");
202 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
204 const xmlpp::Element* arg = dynamic_cast<const xmlpp::Element*>(*iter);
206 assert(arg->get_name() == "Argument");
207 assert(arg->get_attribute("name"));
208 assert(arg->get_attribute("type"));
209 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())));
212 std::cerr << "Found function: " << f << std::endl;
213 m_procedures.push_back(f);
216 void visit_node(const xmlpp::Element* root, const xmlpp::Node* node = NULL, unsigned int indentation = 0)
218 if (!node) node=root;
220 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
221 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node);
222 const xmlpp::CommentNode* nodeComment = dynamic_cast<const xmlpp::CommentNode*>(node);
224 if(nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this.
227 std::string nodename = node->get_name();
229 if(!nodeText && !nodeComment && !nodename.empty()) //Let's not say "name: text".
231 // print_indentation(indentation);
232 // std::cout << "Node name = " << node->get_name() << std::endl;
233 // std::cout << "Node name = " << nodename << std::endl;
234 if (node->get_name() == "Function") {
235 parse_function(root, node);
239 else if(nodeText) //Let's say when it's text. - e.g. let's say what that white space is.
241 print_indentation(indentation);
242 std::cout << "Text Node" << std::endl;
245 //Treat the various node types differently:
248 print_indentation(indentation);
249 std::cout << "text = \"" << nodeText->get_content() << "\"" << std::endl;
253 print_indentation(indentation);
254 std::cout << "comment = " << nodeComment->get_content() << std::endl;
258 print_indentation(indentation);
259 std::cout << "content = " << nodeContent->get_content() << std::endl;
261 else if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node))
263 //A normal Element node:
265 //line() works only for ElementNodes.
266 print_indentation(indentation);
267 std::cout << " line = " << node->get_line() << std::endl;
270 const xmlpp::Element::AttributeList& attributes = nodeElement->get_attributes();
271 for(xmlpp::Element::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter)
273 const xmlpp::Attribute* attribute = *iter;
274 print_indentation(indentation);
275 std::cout << " Attribute " << attribute->get_name() << " = " << attribute->get_value() << std::endl;
278 const xmlpp::Attribute* attribute = nodeElement->get_attribute("title");
281 std::cout << "title found: =" << attribute->get_value() << std::endl;
288 //Recurse through child nodes:
289 xmlpp::Node::NodeList list = node->get_children();
290 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
292 visit_node(root, *iter, indentation + 2); //recursive
299 std::set<std::string>
300 used_groups(const std::list<t2n_procedure> &funcs) {
301 typedef std::set<std::string> Groups;
303 for (std::list<t2n_procedure>::const_iterator it=funcs.begin();it!=funcs.end();++it)
304 // since we use std::set each group is inserted only once
305 groups.insert(it->group);
309 void output_common_hpp(std::ostream &o, const std::list<t2n_procedure> &procs) {
310 std::set<std::string> groups(used_groups(procs));
312 o << "#include \"codegen-stubhead.hxx\"\n";
313 for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
314 o << "class cmd_group_" << *it << " : public libt2n::command\n"
317 << " friend class boost::serialization::access;\n"
318 << " template<class Archive>\n"
319 << " void serialize(Archive & ar, const unsigned int version)\n"
320 << " {ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);}\n"
324 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
325 o << "class " << it->ret_classname() << " : public libt2n::result\n"
328 << " " << it->ret_type << " res;\n"
329 << " friend class boost::serialization::access;\n"
330 << " template<class Archive>\n"
331 << " void serialize(Archive & ar, const unsigned int version)\n"
333 << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);\n"
334 << " ar & BOOST_SERIALIZATION_NVP(res);\n"
337 << " " << it->ret_classname() << "() {}\n"
338 << " " << it->ret_classname() << "(const " << it->ret_type << " &_res) : res(_res) {}\n"
339 << " " << it->ret_type << " get_data() { return res; }\n"
342 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
343 o << "class " << it->cmd_classname() << " : public " << "cmd_group_" << it->group << "\n"
346 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
347 o << " " << ait->second.noref() << " " << ait->first << ";\n";
349 o << " friend class boost::serialization::access;\n"
350 << " template<class Archive>\n"
351 << " void serialize(Archive & ar, const unsigned int version)\n"
353 << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(cmd_group_" << it->group << ");\n";
354 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
355 o << " ar & BOOST_SERIALIZATION_NVP(" << ait->first << ");\n";
358 // default constructor
362 << " " << it->cmd_classname() << "() {}\n";
364 // constructor taking all arguments
365 o << " " << it->cmd_classname() << "(";
366 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
367 if (ait!=it->args.begin()) o << ", ";
368 o << ait->second << " _" << ait->first;
371 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
372 if (ait!=it->args.begin()) o << ", ";
373 o << ait->first << "(_" << ait->first << ")";
376 << " libt2n::result* operator()();\n"
381 void output_common_cpp(std::ostream &o, const std::list<t2n_procedure> &procs, const std::string &common_hpp) {
382 std::set<std::string> groups(used_groups(procs));
384 o << "#include \"" << common_hpp << "\"\n"
385 << "#include <boost/serialization/export.hpp>\n"
387 << "/* register types with boost serialization */\n";
388 for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
389 o << "BOOST_CLASS_EXPORT(cmd_group_" << *it << ")\n";
391 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
392 o << "BOOST_CLASS_EXPORT("<<it->ret_classname()<<")\n"
393 << "BOOST_CLASS_EXPORT("<<it->cmd_classname()<<")\n";
397 void output_client_hpp(std::ostream &o, const std::list<t2n_procedure> &procs) {
398 std::set<std::string> groups(used_groups(procs));
400 o << "#include <command_client.hxx>\n";
402 for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
403 o << "class cmd_group_" << *it << "_client : public libt2n::command_client\n"
406 << "cmd_group_" << *it << "_client(libt2n::client_connection &_c,\n"
407 << " long long _command_timeout_usec=command_timeout_usec_default,\n"
408 << " long long _hello_timeout_usec=hello_timeout_usec_default)\n"
409 << " : libt2n::command_client(_c,_command_timeout_usec,_hello_timeout_usec)\n"
411 for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit) {
412 if (pit->group==*it) {
413 o << " " << *pit << ";\n";
420 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) {
421 std::set<std::string> groups(used_groups(procs));
423 o << "#include \"" << client_hpp << "\"\n"
424 << "#include \"" << common_hpp << "\"\n"
426 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
427 o << "libt2n::result* " << it->cmd_classname() << "::operator()() { return NULL; }\n";
430 for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
431 for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit) {
432 if (pit->group==*it) {
433 o << pit->ret_type << " cmd_group_" << *it << "_client::" << pit->name << "(" << pit->args << ")\n"
435 << " libt2n::result_container rc;\n"
436 << " send_command(new " << pit->cmd_classname() << "(";
437 for (t2n_procedure::Args::const_iterator ait=pit->args.begin();ait!=pit->args.end();++ait) {
438 if (ait!=pit->args.begin()) o << ", ";
442 << " " << pit->ret_classname() << "* res=dynamic_cast<" << pit->ret_classname() << "*>(rc.get_result());\n"
443 << " if (!res) throw libt2n::t2n_communication_error(\"result object of wrong type\");\n"
444 << " return res->get_data();\n"
450 // include in this compilation unit to ensure the compilation unit is used
452 // http://www.google.de/search?q=g%2B%2B+static+initializer+in+static+library
453 o << "#include \"" << common_cpp << "\"\n";
456 void output_server_cpp(std::ostream &o, const std::list<t2n_procedure> &procs, const std::string &common_hpp, const std::string &common_cpp) {
457 o << "#include \"" << common_hpp << "\"\n";
459 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
461 o << "libt2n::result* " << it->cmd_classname() << "::operator()() { return new " << it->ret_classname() << "(" << it->name << "(";
462 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
463 if (ait!=it->args.begin()) o << ", ";
468 o << "#include \"" << common_cpp << "\"\n";
471 struct header_file : public std::ofstream
473 header_file(const char* fname) : std::ofstream(fname) {
474 std::cerr << "create header: '" << fname << "'" << std::endl;
475 std::string macro(replace(toupper(fname),'.','_'));
476 *this << "// automatically generated code - do not edit\n" << std::endl;
477 *this << "#ifndef " << macro << "\n"
478 << "#define " << macro << "\n";
481 *this << "#endif" << std::endl;
485 struct cpp_file : public std::ofstream
487 cpp_file(const char* fname) : std::ofstream(fname) {
488 std::cerr << "create cpp: '" << fname << "'" << std::endl;
489 *this << "// automatically generated code - do not edit\n" << std::endl;
494 main(int argc, char* argv[])
497 std::cerr << "Usage: " << argv[0] << " gccxml-file outputprefix" << std::endl;
500 std::string filepath(argv[1]);
501 std::string prefix(argv[2]);
503 Parser parser(filepath);
504 std::list<t2n_procedure> procedures(parser.get_procedures());
506 std::cerr << "Procedures:" << std::endl;
507 for (std::list<t2n_procedure>::const_iterator it=procedures.begin();it!=procedures.end();++it)
508 std::cerr << *it << ";" << std::endl;
510 std::set<std::string> groups(used_groups(procedures));
511 std::cerr << "Used groups:" << std::endl;
512 for (std::set<std::string>::const_iterator it=groups.begin();it!=groups.end();++it)
513 std::cerr << *it << std::endl;
515 std::string common_hpp_fname(prefix+"common.hxx");
516 std::string common_cpp_fname(prefix+"common.cpp");
517 std::string client_hpp_fname(prefix+"client.hxx");
518 std::string client_cpp_fname(prefix+"client.cpp");
519 std::string server_cpp_fname(prefix+"server.cpp");
521 header_file common_hpp(common_hpp_fname.c_str());
522 output_common_hpp(common_hpp, procedures);
524 cpp_file common_cpp(common_cpp_fname.c_str());
525 output_common_cpp(common_cpp, procedures, common_hpp_fname);
527 header_file client_hpp(client_hpp_fname.c_str());
528 output_client_hpp(client_hpp, procedures);
530 cpp_file client_cpp(client_cpp_fname.c_str());
531 output_client_cpp(client_cpp, procedures, common_hpp_fname, common_cpp_fname, client_hpp_fname);
533 cpp_file server_cpp(server_cpp_fname.c_str());
534 output_server_cpp(server_cpp, procedures, common_hpp_fname, common_cpp_fname);