Commit | Line | Data |
---|---|---|
0e627fe2 | 1 | /* |
1770469c GE |
2 | Copyright (C) 2006-2008 |
3 | intra2net.com | |
0e627fe2 JT |
4 | |
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. | |
9 | ||
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. | |
14 | ||
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 | |
18 | */ | |
19 | ||
e50cc1d6 JT |
20 | #include <libxml++/libxml++.h> |
21 | #include <cassert> | |
22 | #include <iostream> | |
060fbd87 JT |
23 | #include <set> |
24 | #include <fstream> | |
9d3993e5 | 25 | #include <list> |
6be7b70f | 26 | #include <stdexcept> |
7799fec7 | 27 | #include <string> |
6be7b70f | 28 | #include <boost/lexical_cast.hpp> |
3907d211 JT |
29 | #ifdef HAVE_CONFIG_H |
30 | #include "config.h" | |
31 | #endif | |
e50cc1d6 | 32 | |
2f0896ce JT |
33 | |
34 | //! map group to class name | |
1770469c GE |
35 | std::string groupClass(const std::string &group) |
36 | { | |
2f0896ce JT |
37 | return std::string("cmd_group_")+group; |
38 | } | |
39 | ||
52b6f93a | 40 | //! convert string to upper case |
1770469c GE |
41 | std::string toupper(std::string s) |
42 | { | |
43 | for (unsigned i=0; i<s.length(); ++i) s[i]=toupper(s[i]); | |
44 | return s; | |
e035276b JT |
45 | } |
46 | ||
52b6f93a | 47 | //! replace all characters f by r in string s |
1770469c GE |
48 | std::string replace(std::string s, char f, char r) |
49 | { | |
50 | for (unsigned i=0; i<s.length(); ++i) if (s[i]==f) s[i]=r; | |
51 | return s; | |
e035276b JT |
52 | } |
53 | ||
52b6f93a JT |
54 | //! strip prefix from string s |
55 | /*! | |
56 | \return string s without prefix or an empty string on error | |
57 | */ | |
1770469c | 58 | std::string strip(std::string s, std::string prefix) |
e035276b | 59 | { |
1770469c GE |
60 | std::string error; |
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()); | |
e035276b JT |
63 | } |
64 | ||
e50cc1d6 JT |
65 | //! get child element by id |
66 | /*! | |
67 | \return pointer to element having id or null on error | |
68 | \todo find libxmlpp pendant | |
69 | */ | |
70 | const xmlpp::Element* get_element_by_id(const xmlpp::Element* element, const std::string &id) | |
71 | { | |
1770469c GE |
72 | const xmlpp::Attribute* cid = element->get_attribute("id"); |
73 | if ( cid && ( cid->get_value() == id)) return element; | |
74 | ||
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) | |
78 | { | |
79 | const xmlpp::Element* element = dynamic_cast<const xmlpp::Element*>(*iter); | |
80 | if (element) | |
81 | { | |
82 | const xmlpp::Element* match = get_element_by_id(element, id); | |
83 | if (match) return match; | |
84 | } | |
85 | } | |
86 | return NULL; | |
e50cc1d6 JT |
87 | } |
88 | ||
1770469c | 89 | std::string get_file(const xmlpp::Element* root, const std::string &file_id) |
6be7b70f | 90 | { |
1770469c GE |
91 | std::string error; |
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(); | |
6be7b70f JT |
95 | } |
96 | ||
e50cc1d6 JT |
97 | //! get namespace by id |
98 | /*! | |
99 | \return namespace name or empty string on error | |
100 | */ | |
101 | std::string get_namespace(const xmlpp::Element* root, const std::string &id) | |
102 | { | |
1770469c GE |
103 | std::string error; |
104 | const xmlpp::Element* element(get_element_by_id(root, id)); | |
105 | // [RP:20071024]: use "demangled" attribute instead of "name" to cover nested namespaces: | |
106 | if ((!element)||(!element->get_attribute("demangled"))) return error; | |
107 | return element->get_attribute("demangled")->get_value(); | |
e50cc1d6 JT |
108 | } |
109 | ||
ef7b2923 | 110 | //! procedure marked for export? |
1770469c | 111 | bool is_marked(const std::string &attrs) |
d340d435 | 112 | { |
1770469c GE |
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); | |
d340d435 JT |
117 | } |
118 | ||
e035276b JT |
119 | struct type_info |
120 | { | |
1770469c GE |
121 | std::string name; |
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;} | |
125 | bool isVoid() const {return name=="void";} | |
e035276b JT |
126 | }; |
127 | ||
1770469c GE |
128 | std::ostream &operator<<(std::ostream &o, const type_info &t) |
129 | { | |
130 | o << t.name; | |
131 | return o; | |
e035276b JT |
132 | } |
133 | ||
6be7b70f JT |
134 | struct parse_error : public std::runtime_error |
135 | { | |
1770469c GE |
136 | parse_error(const std::string &file, unsigned line, const std::string &msg) |
137 | : std::runtime_error(file+":"+boost::lexical_cast<std::string>(line)+": error: "+msg) | |
138 | {} | |
6be7b70f JT |
139 | }; |
140 | ||
e50cc1d6 JT |
141 | //! get type by id |
142 | /*! | |
143 | \return type name or empty string on error | |
144 | */ | |
e035276b | 145 | type_info get_type(const xmlpp::Element* root, const std::string &id) |
e50cc1d6 | 146 | { |
1770469c GE |
147 | type_info error; |
148 | const xmlpp::Element* element(get_element_by_id(root, id)); | |
149 | if (!element) return error; | |
150 | ||
151 | // TODO: not yet complete | |
152 | // if we recurse - when do we stop? | |
153 | // if it is a typedef? yes? (hmm if the typedef is in the file parsed this will not work) | |
154 | ||
155 | // TODO: const and reference types handling is a ugly hack | |
156 | ||
157 | std::string tag(element->get_name()); | |
158 | if (tag=="ReferenceType") | |
159 | { | |
160 | assert(element->get_attribute("type")); | |
161 | type_info ret(get_type(root, element->get_attribute("type")->get_value())); | |
162 | if (ret==error) return error; | |
163 | // at the moment we only support const & | |
164 | // todo: nice error message! | |
165 | if ((ret.noref_name=strip(ret.name,"const ")).empty()) return error; | |
166 | ret.name=ret.name+"&"; | |
167 | return ret; | |
168 | } | |
169 | else if (tag=="CvQualifiedType") | |
170 | { | |
171 | assert(element->get_attribute("type")); | |
172 | type_info ret(get_type(root, element->get_attribute("type")->get_value())); | |
173 | if (ret==error) return error; | |
174 | ret.name=std::string("const ")+ret.name; | |
175 | return ret; | |
176 | } | |
177 | else if (tag=="PointerType") | |
178 | { | |
179 | // todo: nearly the same as reference type handling | |
180 | assert(element->get_attribute("type")); | |
181 | type_info ret(get_type(root, element->get_attribute("type")->get_value())); | |
182 | if (ret==error) return error; | |
183 | // at the moment we only support const & | |
184 | // todo: nice error message! | |
185 | if ((ret.noref_name=strip(ret.name,"const ")).empty()) return error; | |
186 | ret.name=ret.name+"*"; | |
187 | return ret; | |
188 | } | |
189 | ||
190 | assert(element->get_attribute("name")); | |
191 | type_info ret; | |
192 | if (element->get_attribute("context")) | |
193 | { | |
194 | ret.name=get_namespace(root, element->get_attribute("context")->get_value()); | |
195 | if (ret.name!="::") | |
196 | ret.name+="::"; | |
197 | else | |
198 | // do not explicitely add :: | |
199 | ret.name=""; | |
200 | } | |
201 | ret.name+=element->get_attribute("name")->get_value(); | |
202 | return ret; | |
e50cc1d6 JT |
203 | } |
204 | ||
73eb04f2 GE |
205 | struct Arg |
206 | { | |
207 | std::string name; | |
208 | type_info type; | |
209 | std::string defaultArg; | |
210 | }; | |
211 | ||
060fbd87 JT |
212 | struct t2n_procedure |
213 | { | |
73eb04f2 | 214 | typedef std::list<Arg> Args; |
1770469c GE |
215 | |
216 | type_info ret_type; | |
217 | std::string name; | |
218 | std::string mangled; | |
219 | Args args; | |
220 | ||
221 | std::string ret_classname() const | |
222 | { | |
223 | return name+mangled+"_res"; | |
224 | } | |
225 | std::string cmd_classname() const | |
226 | { | |
227 | return name+mangled+"_cmd"; | |
228 | } | |
229 | bool hasReturn() const {return !ret_type.isVoid();} | |
060fbd87 | 230 | }; |
e50cc1d6 | 231 | |
1770469c GE |
232 | std::ostream &operator<<(std::ostream &o, const t2n_procedure::Args &args) |
233 | { | |
234 | for (t2n_procedure::Args::const_iterator it=args.begin();it!=args.end();++it) | |
235 | { | |
236 | if (it!=args.begin()) o << ", "; | |
73eb04f2 GE |
237 | o << it->type << " " << it->name; |
238 | ||
239 | if (!it->defaultArg.empty()) | |
240 | o << "=" << it->defaultArg; | |
1770469c GE |
241 | } |
242 | return o; | |
060fbd87 | 243 | } |
e50cc1d6 | 244 | |
1770469c GE |
245 | std::ostream &operator<<(std::ostream &o, const t2n_procedure &f) |
246 | { | |
247 | o << f.ret_type << " " << f.name << "(" << f.args << ")"; | |
248 | return o; | |
060fbd87 | 249 | } |
e50cc1d6 | 250 | |
3b7c93a8 JT |
251 | std::pair<std::string, unsigned> |
252 | get_file_and_line(const xmlpp::Element* root, const xmlpp::Element* element) | |
253 | { | |
1770469c GE |
254 | return std::pair<std::string, unsigned>(get_file(root, element->get_attribute("file")->get_value()), |
255 | boost::lexical_cast<unsigned>(element->get_attribute("line")->get_value())-1); | |
3b7c93a8 JT |
256 | } |
257 | ||
1770469c | 258 | std::string get_file_and_line_as_string(const xmlpp::Element* root, const xmlpp::Element* element) |
3b7c93a8 | 259 | { |
1770469c GE |
260 | std::pair<std::string, unsigned> fl(get_file_and_line(root,element)); |
261 | return std::string(fl.first)+":"+boost::lexical_cast<std::string>(fl.second); | |
3b7c93a8 JT |
262 | } |
263 | ||
060fbd87 JT |
264 | class Parser |
265 | { | |
266 | public: | |
1770469c GE |
267 | Parser(const std::string &fname) : m_fname(fname) {} |
268 | ||
269 | std::list<t2n_procedure> get_procedures() | |
270 | { | |
271 | xmlpp::DomParser parser; | |
272 | // parser.set_validate(); | |
273 | parser.set_substitute_entities(); //We just want the text to be resolved/unescaped automatically. | |
274 | parser.parse_file(m_fname); | |
275 | if (parser) | |
276 | { | |
277 | //Walk the tree: | |
278 | const xmlpp::Node* pNode = parser.get_document()->get_root_node(); //deleted by DomParser. | |
279 | const xmlpp::Element* root = dynamic_cast<const xmlpp::Element*>(pNode); | |
280 | assert(root); | |
281 | visit_node(root); | |
282 | } | |
283 | return m_procedures; | |
284 | } | |
060fbd87 | 285 | protected: |
1770469c GE |
286 | std::string m_fname; |
287 | std::list<t2n_procedure> m_procedures; | |
288 | ||
289 | void parse_function(const xmlpp::Element* root, const xmlpp::Node* node) | |
290 | { | |
291 | const xmlpp::Element* element = dynamic_cast<const xmlpp::Element*>(node); | |
292 | if (!element) return; | |
293 | ||
294 | const xmlpp::Attribute* attributes = element->get_attribute("attributes"); | |
295 | const xmlpp::Attribute* name = element->get_attribute("name"); | |
296 | const xmlpp::Attribute* mangled = element->get_attribute("mangled"); | |
297 | const xmlpp::Attribute* returns = element->get_attribute("returns"); | |
298 | if ((!attributes)||(!name)||(!mangled)||(!returns)) return; | |
299 | ||
300 | // check wether the procedure is marked (TODO: improve) | |
301 | // attributes are speparated by spaces? | |
302 | ||
303 | t2n_procedure f; | |
304 | if (!is_marked(attributes->get_value())) return; | |
305 | ||
306 | // we need the return type | |
307 | f.ret_type=get_type(root, returns->get_value()); | |
308 | f.name=name->get_value(); | |
309 | f.mangled=mangled->get_value(); | |
310 | ||
311 | xmlpp::Node::NodeList list = node->get_children("Argument"); | |
312 | for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter) | |
313 | { | |
314 | const xmlpp::Element* arg = dynamic_cast<const xmlpp::Element*>(*iter); | |
315 | if ( arg ) | |
316 | { | |
73eb04f2 GE |
317 | struct Arg a; |
318 | ||
1770469c | 319 | assert(arg->get_name() == "Argument"); |
73eb04f2 | 320 | |
1770469c | 321 | assert(arg->get_attribute("name")); |
73eb04f2 GE |
322 | a.name=arg->get_attribute("name")->get_value(); |
323 | ||
1770469c | 324 | assert(arg->get_attribute("type")); |
73eb04f2 GE |
325 | a.type=get_type(root, arg->get_attribute("type")->get_value()); |
326 | ||
7799fec7 GE |
327 | // this would be the nice way of solving the problem of default arguments |
328 | // but currently the output of gccxml is unreliable (e.g. namespace only | |
329 | // sometimes available) | |
330 | // if(arg->get_attribute("default")) | |
331 | // a.defaultArg=arg->get_attribute("default")->get_value(); | |
332 | ||
333 | // so we need to get the def. arg. via attribute | |
334 | // which is previously set by the macro LIBT2N_DEFAULT_ARG(type,value) | |
335 | if(arg->get_attribute("attributes")) | |
336 | { | |
337 | std::string attr=arg->get_attribute("attributes")->get_value(); | |
338 | const std::string look_for = "gccxml(libt2n-default-arg,"; | |
339 | ||
340 | if (attr.compare(0,look_for.size(),look_for) == 0) | |
341 | a.defaultArg=attr.substr(look_for.size(),attr.size()-look_for.size()-1); | |
342 | } | |
73eb04f2 | 343 | |
1770469c | 344 | // todo: ugly - could be any other error |
73eb04f2 | 345 | if (a.type.name.empty()) |
1770469c GE |
346 | { |
347 | assert(element->get_attribute("file")); | |
348 | assert(element->get_attribute("line")); | |
349 | std::pair<std::string, unsigned> file_and_line(get_file_and_line(root, element)); | |
350 | throw parse_error(file_and_line.first, | |
351 | file_and_line.second, | |
73eb04f2 | 352 | std::string("type of parameter '")+a.name+"' not (yet?) supported"); |
1770469c | 353 | } |
73eb04f2 GE |
354 | |
355 | f.args.push_back(a); | |
1770469c GE |
356 | } |
357 | } | |
358 | std::cerr << get_file_and_line_as_string(root, element) << ":\texport procedure: " << f << std::endl; | |
359 | m_procedures.push_back(f); | |
360 | } | |
361 | ||
362 | void visit_node(const xmlpp::Element* root, const xmlpp::Node* node = NULL, unsigned int indentation = 0) | |
363 | { | |
364 | if (!node) node=root; | |
365 | ||
366 | const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node); | |
367 | const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node); | |
368 | const xmlpp::CommentNode* nodeComment = dynamic_cast<const xmlpp::CommentNode*>(node); | |
369 | ||
370 | if (nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this. | |
371 | return; | |
372 | ||
373 | std::string nodename = node->get_name(); | |
374 | ||
375 | if (!nodeText && !nodeComment && !nodename.empty()) //Let's not say "name: text". | |
376 | { | |
377 | if (node->get_name() == "Function") parse_function(root, node); | |
378 | } | |
379 | if (!nodeContent) | |
380 | { | |
381 | //Recurse through child nodes: | |
382 | xmlpp::Node::NodeList list = node->get_children(); | |
383 | for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter) | |
384 | { | |
385 | visit_node(root, *iter, indentation + 2); //recursive | |
386 | } | |
387 | } | |
388 | } | |
060fbd87 JT |
389 | }; |
390 | ||
1770469c GE |
391 | void output_common_hpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs) |
392 | { | |
2f0896ce | 393 | o << "class " << groupClass(group) << " : public libt2n::command\n" |
1770469c GE |
394 | << "{\n" |
395 | << "private:\n" | |
396 | << " friend class boost::serialization::access;\n" | |
397 | << " template<class Archive>\n" | |
398 | << " void serialize(Archive & ar, const unsigned int /* version */)\n" | |
399 | << " {ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);}\n" | |
400 | << "};\n"; | |
401 | ||
402 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) | |
403 | { | |
404 | o << "class " << it->ret_classname() << " : public libt2n::result\n" | |
405 | << "{\n" | |
406 | << "private:\n"; | |
407 | if (it->hasReturn()) | |
408 | o << " " << it->ret_type << " res;\n"; | |
409 | o << " friend class boost::serialization::access;\n" | |
410 | << " template<class Archive>\n" | |
411 | << " void serialize(Archive & ar, const unsigned int /* version */)\n" | |
412 | << " {\n" | |
413 | << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);\n"; | |
414 | if (it->hasReturn()) | |
415 | o << " ar & BOOST_SERIALIZATION_NVP(res);\n"; | |
416 | o << " }\n" | |
417 | << "public:\n" | |
418 | << " " << it->ret_classname() << "() {}\n"; | |
419 | if (it->hasReturn()) | |
420 | { | |
421 | o << " " << it->ret_classname() << "(const " << it->ret_type << " &_res) : res(_res) {}\n" | |
422 | << " " << it->ret_type << " get_data() { return res; }\n"; | |
423 | } | |
424 | o << "};\n"; | |
d340d435 | 425 | } |
1770469c GE |
426 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) |
427 | { | |
428 | o << "class " << it->cmd_classname() << " : public " << groupClass(group) << "\n" | |
429 | << "{\n" | |
430 | << "private:\n"; | |
431 | for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) | |
432 | { | |
73eb04f2 | 433 | o << " " << ait->type.noref() << " " << ait->name << ";\n"; |
1770469c GE |
434 | } |
435 | o << " friend class boost::serialization::access;\n" | |
436 | << " template<class Archive>\n" | |
437 | << " void serialize(Archive & ar, const unsigned int /* version */)\n" | |
438 | << " {\n" | |
439 | << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(" << groupClass(group) << ");\n"; | |
440 | for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) | |
441 | { | |
73eb04f2 | 442 | o << " ar & BOOST_SERIALIZATION_NVP(" << ait->name << ");\n"; |
1770469c GE |
443 | } |
444 | ||
445 | // default constructor | |
446 | o << " }\n" | |
447 | << "\n" | |
448 | << "public:\n" | |
449 | << " " << it->cmd_classname() << "() {}\n"; | |
450 | ||
451 | // constructor taking all arguments | |
452 | if (!it->args.empty()) | |
453 | { | |
454 | o << " " << it->cmd_classname() << "("; | |
455 | for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) | |
456 | { | |
457 | if (ait!=it->args.begin()) o << ", "; | |
73eb04f2 | 458 | o << ait->type << " _" << ait->name; |
1770469c GE |
459 | } |
460 | o << ") : "; | |
461 | for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) | |
462 | { | |
463 | if (ait!=it->args.begin()) o << ", "; | |
464 | // pointers are const pointers and must be dereferenced | |
73eb04f2 | 465 | o << ait->name << "(" << ((ait->type.name.find_first_of('*')!=std::string::npos) ? "*" : "" ) << "_" << ait->name << ")"; |
1770469c GE |
466 | } |
467 | o << " {}\n"; | |
468 | } | |
469 | o << " libt2n::result* operator()();\n" | |
470 | << "};\n"; | |
d340d435 | 471 | } |
060fbd87 JT |
472 | } |
473 | ||
1770469c GE |
474 | void output_common_cpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs, const std::string &common_hpp) |
475 | { | |
476 | o << "#include \"" << common_hpp << "\"\n" | |
477 | << "#include <boost/serialization/export.hpp>\n" | |
478 | << "\n" | |
479 | << "/* register types with boost serialization */\n"; | |
480 | o << "BOOST_CLASS_EXPORT(" << groupClass(group) << ")\n"; | |
481 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) | |
482 | { | |
483 | o << "BOOST_CLASS_EXPORT("<<it->ret_classname()<<")\n" | |
484 | << "BOOST_CLASS_EXPORT("<<it->cmd_classname()<<")\n"; | |
485 | } | |
060fbd87 | 486 | } |
e50cc1d6 | 487 | |
1770469c GE |
488 | void output_client_hpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs) |
489 | { | |
490 | o << "#include <command_client.hxx>\n"; | |
491 | ||
492 | o << "class " << groupClass(group) << "_client : public libt2n::command_client\n" | |
493 | << "{\n" | |
494 | << "public:\n" | |
495 | << groupClass(group) << "_client(libt2n::client_connection &_c,\n" | |
496 | << " long long _command_timeout_usec=command_timeout_usec_default,\n" | |
497 | << " long long _hello_timeout_usec=hello_timeout_usec_default)\n" | |
498 | << " : libt2n::command_client(_c,_command_timeout_usec,_hello_timeout_usec)\n" | |
499 | << " {}\n"; | |
500 | for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit) | |
501 | { | |
502 | o << " " << *pit << ";\n"; | |
503 | } | |
504 | o << "};\n"; | |
060fbd87 | 505 | } |
e50cc1d6 | 506 | |
1770469c GE |
507 | 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) |
508 | { | |
509 | o << "#include \"" << client_hpp << "\"\n" | |
510 | << "#include \"" << common_hpp << "\"\n" | |
511 | << "// fake\n"; | |
512 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) | |
513 | { | |
514 | o << "libt2n::result* " << it->cmd_classname() << "::operator()() { return NULL; }\n"; | |
515 | } | |
516 | ||
517 | for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit) | |
518 | { | |
73eb04f2 GE |
519 | o << pit->ret_type << " " << groupClass(group) << "_client::" << pit->name << "("; |
520 | ||
521 | // we need to do this by hand here because we don't want default arguments within the cpp | |
522 | for (t2n_procedure::Args::const_iterator xit=pit->args.begin();xit!=pit->args.end();++xit) | |
523 | { | |
524 | if (xit!=pit->args.begin()) | |
525 | o << ", "; | |
526 | o << xit->type << " " << xit->name; | |
527 | } | |
528 | ||
529 | o << ")\n" | |
1770469c GE |
530 | << "{\n" |
531 | << " libt2n::result_container rc;\n" | |
532 | << " send_command(new " << pit->cmd_classname() << "("; | |
533 | for (t2n_procedure::Args::const_iterator ait=pit->args.begin();ait!=pit->args.end();++ait) | |
534 | { | |
535 | if (ait!=pit->args.begin()) o << ", "; | |
73eb04f2 | 536 | o << ait->name; |
1770469c GE |
537 | } |
538 | o << "), rc);\n" | |
539 | << " " << pit->ret_classname() << "* res=dynamic_cast<" << pit->ret_classname() << "*>(rc.get_result());\n" | |
540 | << " if (!res) throw libt2n::t2n_communication_error(\"result object of wrong type\");\n"; | |
541 | if (pit->hasReturn()) | |
542 | o << " return res->get_data();\n"; | |
543 | o << "}\n"; | |
544 | } | |
545 | ||
546 | // include in this compilation unit to ensure the compilation unit is used | |
547 | // see also: | |
548 | // http://www.google.de/search?q=g%2B%2B+static+initializer+in+static+library | |
549 | o << "#include \"" << common_cpp << "\"\n"; | |
060fbd87 JT |
550 | } |
551 | ||
1770469c GE |
552 | void output_server_hpp(std::ostream &o, const std::string & /* group */, const std::list<t2n_procedure> &procs, const std::string &common_hpp) |
553 | { | |
554 | o << "#include \"" << common_hpp << "\"\n"; | |
a96ab628 | 555 | |
1770469c GE |
556 | // output function declarations |
557 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) | |
558 | o << *it << ";\n"; | |
a96ab628 JT |
559 | } |
560 | ||
1770469c GE |
561 | 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) |
562 | { | |
563 | o << "#include \"" << common_hpp << "\"\n"; | |
564 | ||
565 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) | |
566 | { | |
567 | o << *it << ";\n"; | |
568 | o << "libt2n::result* " << it->cmd_classname() << "::operator()() { "; | |
569 | ||
570 | if (it->hasReturn()) | |
571 | o << "return new " << it->ret_classname() << "("; | |
572 | ||
573 | // output function name and args | |
574 | o << it->name << "("; | |
575 | for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) | |
576 | { | |
577 | if (ait!=it->args.begin()) o << ", "; | |
578 | // get pointer | |
73eb04f2 | 579 | if (ait->type.name.find_first_of('*')!=std::string::npos) |
1770469c | 580 | o << '&'; |
73eb04f2 | 581 | o << ait->name; |
1770469c GE |
582 | } |
583 | ||
584 | if (it->hasReturn()) | |
585 | o << "));"; | |
586 | else | |
9c627fab TJ |
587 | o << "); return new " << it->ret_classname() << "();"; |
588 | ||
1770469c GE |
589 | o << " }\n"; |
590 | } | |
591 | o << "#include \"" << common_cpp << "\"\n"; | |
060fbd87 JT |
592 | } |
593 | ||
060fbd87 JT |
594 | struct header_file : public std::ofstream |
595 | { | |
1770469c GE |
596 | header_file(const char* fname) : std::ofstream(fname) |
597 | { | |
598 | std::cerr << "create header: '" << fname << "'" << std::endl; | |
599 | std::string macro(replace(toupper(fname),'.','_')); | |
600 | *this << "// automatically generated code (generated by libt2n-codegen " << VERSION << ") - do not edit\n" << std::endl; | |
601 | *this << "#ifndef " << macro << "\n" | |
602 | << "#define " << macro << "\n"; | |
603 | } | |
604 | ~header_file() | |
605 | { | |
606 | *this << "#endif" << std::endl; | |
607 | } | |
060fbd87 JT |
608 | }; |
609 | ||
610 | struct cpp_file : public std::ofstream | |
611 | { | |
1770469c GE |
612 | cpp_file(const char* fname) : std::ofstream(fname) |
613 | { | |
614 | std::cerr << "create cpp: '" << fname << "'" << std::endl; | |
615 | *this << "// automatically generated code - do not edit\n" << std::endl; | |
616 | } | |
060fbd87 JT |
617 | }; |
618 | ||
619 | int | |
620 | main(int argc, char* argv[]) | |
e50cc1d6 | 621 | { |
3907d211 | 622 | // todo: maybe use getopt |
1770469c GE |
623 | if ((argc>1)&&(std::string(argv[1])=="--version")) |
624 | { | |
625 | std::cerr << VERSION << std::endl; | |
626 | return 0; | |
3907d211 JT |
627 | } |
628 | if (argc < 3) | |
629 | { | |
1770469c GE |
630 | std::cerr << "Usage: " << argv[0] << "default-group gccxml-file1 gccxml-file2 ... " << std::endl; |
631 | return 1; | |
3907d211 | 632 | } |
060fbd87 | 633 | |
1770469c GE |
634 | try |
635 | { | |
636 | std::string group(argv[1]); | |
637 | std::list<std::string> xmlfiles; | |
638 | for (int i=2;i<argc;++i) | |
639 | xmlfiles.push_back(argv[i]); | |
640 | ||
641 | std::string prefix=group+"_"; | |
642 | std::list<t2n_procedure> procedures; | |
643 | for (std::list<std::string>::iterator it=xmlfiles.begin();it!=xmlfiles.end();++it) | |
644 | { | |
645 | std::cerr << "Parse " << *it << std::endl; | |
646 | Parser parser(*it); | |
647 | const std::list<t2n_procedure> &p(parser.get_procedures()); | |
648 | std::copy(p.begin(), p.end(), std::back_inserter(procedures)); | |
649 | } | |
650 | ||
651 | std::cerr << "All procedures:" << std::endl; | |
652 | for (std::list<t2n_procedure>::const_iterator it=procedures.begin();it!=procedures.end();++it) | |
653 | std::cerr << *it << ";" << std::endl; | |
654 | ||
655 | std::string common_hpp_fname(prefix+"common.hxx"); | |
656 | std::string common_cpp_fname(prefix+"common.cpp"); | |
657 | std::string client_hpp_fname(prefix+"client.hxx"); | |
658 | std::string client_cpp_fname(prefix+"client.cpp"); | |
659 | std::string server_hpp_fname(prefix+"server.hxx"); | |
660 | std::string server_cpp_fname(prefix+"server.cpp"); | |
661 | ||
662 | header_file common_hpp(common_hpp_fname.c_str()); | |
663 | common_hpp << "// boost serialization is picky about order of include files => we have to include this one first\n" | |
664 | << "#include \"codegen-stubhead.hxx\"\n" | |
665 | << "#include \"" << group << ".hxx\"\n"; | |
666 | ||
667 | output_common_hpp(common_hpp, group, procedures); | |
668 | ||
669 | cpp_file common_cpp(common_cpp_fname.c_str()); | |
670 | output_common_cpp(common_cpp, group, procedures, common_hpp_fname); | |
671 | ||
672 | header_file client_hpp(client_hpp_fname.c_str()); | |
673 | client_hpp << "// boost serialization is picky about order of include files => we have to include this one first\n" | |
674 | << "#include \"codegen-stubhead.hxx\"\n" | |
675 | << "#include \"" << group << ".hxx\"\n"; | |
676 | output_client_hpp(client_hpp, group, procedures); | |
677 | ||
678 | cpp_file client_cpp(client_cpp_fname.c_str()); | |
679 | output_client_cpp(client_cpp, group, procedures, common_hpp_fname, common_cpp_fname, client_hpp_fname); | |
680 | ||
681 | header_file server_hpp(server_hpp_fname.c_str()); | |
682 | output_server_hpp(server_hpp, group, procedures, common_hpp_fname); | |
683 | ||
684 | cpp_file server_cpp(server_cpp_fname.c_str()); | |
685 | output_server_cpp(server_cpp, group, procedures, common_hpp_fname, common_cpp_fname); | |
686 | } | |
687 | catch (const parse_error &e) | |
688 | { | |
689 | std::cerr << e.what() << std::endl; | |
690 | return EXIT_FAILURE; | |
691 | } | |
692 | return EXIT_SUCCESS; | |
e50cc1d6 | 693 | } |