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 JT |
26 | #include <stdexcept> |
27 | #include <boost/lexical_cast.hpp> | |
3907d211 JT |
28 | #ifdef HAVE_CONFIG_H |
29 | #include "config.h" | |
30 | #endif | |
e50cc1d6 | 31 | |
2f0896ce JT |
32 | |
33 | //! map group to class name | |
1770469c GE |
34 | std::string groupClass(const std::string &group) |
35 | { | |
2f0896ce JT |
36 | return std::string("cmd_group_")+group; |
37 | } | |
38 | ||
52b6f93a | 39 | //! convert string to upper case |
1770469c GE |
40 | std::string toupper(std::string s) |
41 | { | |
42 | for (unsigned i=0; i<s.length(); ++i) s[i]=toupper(s[i]); | |
43 | return s; | |
e035276b JT |
44 | } |
45 | ||
52b6f93a | 46 | //! replace all characters f by r in string s |
1770469c GE |
47 | std::string replace(std::string s, char f, char r) |
48 | { | |
49 | for (unsigned i=0; i<s.length(); ++i) if (s[i]==f) s[i]=r; | |
50 | return s; | |
e035276b JT |
51 | } |
52 | ||
52b6f93a JT |
53 | //! strip prefix from string s |
54 | /*! | |
55 | \return string s without prefix or an empty string on error | |
56 | */ | |
1770469c | 57 | std::string strip(std::string s, std::string prefix) |
e035276b | 58 | { |
1770469c GE |
59 | std::string error; |
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()); | |
e035276b JT |
62 | } |
63 | ||
e50cc1d6 JT |
64 | //! get child element by id |
65 | /*! | |
66 | \return pointer to element having id or null on error | |
67 | \todo find libxmlpp pendant | |
68 | */ | |
69 | const xmlpp::Element* get_element_by_id(const xmlpp::Element* element, const std::string &id) | |
70 | { | |
1770469c GE |
71 | const xmlpp::Attribute* cid = element->get_attribute("id"); |
72 | if ( cid && ( cid->get_value() == id)) return element; | |
73 | ||
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) | |
77 | { | |
78 | const xmlpp::Element* element = dynamic_cast<const xmlpp::Element*>(*iter); | |
79 | if (element) | |
80 | { | |
81 | const xmlpp::Element* match = get_element_by_id(element, id); | |
82 | if (match) return match; | |
83 | } | |
84 | } | |
85 | return NULL; | |
e50cc1d6 JT |
86 | } |
87 | ||
1770469c | 88 | std::string get_file(const xmlpp::Element* root, const std::string &file_id) |
6be7b70f | 89 | { |
1770469c GE |
90 | std::string error; |
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(); | |
6be7b70f JT |
94 | } |
95 | ||
e50cc1d6 JT |
96 | //! get namespace by id |
97 | /*! | |
98 | \return namespace name or empty string on error | |
99 | */ | |
100 | std::string get_namespace(const xmlpp::Element* root, const std::string &id) | |
101 | { | |
1770469c GE |
102 | std::string error; |
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(); | |
e50cc1d6 JT |
107 | } |
108 | ||
ef7b2923 | 109 | //! procedure marked for export? |
1770469c | 110 | bool is_marked(const std::string &attrs) |
d340d435 | 111 | { |
1770469c GE |
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); | |
d340d435 JT |
116 | } |
117 | ||
e035276b JT |
118 | struct type_info |
119 | { | |
1770469c GE |
120 | std::string name; |
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";} | |
e035276b JT |
125 | }; |
126 | ||
1770469c GE |
127 | std::ostream &operator<<(std::ostream &o, const type_info &t) |
128 | { | |
129 | o << t.name; | |
130 | return o; | |
e035276b JT |
131 | } |
132 | ||
6be7b70f JT |
133 | struct parse_error : public std::runtime_error |
134 | { | |
1770469c GE |
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) | |
137 | {} | |
6be7b70f JT |
138 | }; |
139 | ||
e50cc1d6 JT |
140 | //! get type by id |
141 | /*! | |
142 | \return type name or empty string on error | |
143 | */ | |
e035276b | 144 | type_info get_type(const xmlpp::Element* root, const std::string &id) |
e50cc1d6 | 145 | { |
1770469c GE |
146 | type_info error; |
147 | const xmlpp::Element* element(get_element_by_id(root, id)); | |
148 | if (!element) return error; | |
149 | ||
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) | |
153 | ||
154 | // TODO: const and reference types handling is a ugly hack | |
155 | ||
156 | std::string tag(element->get_name()); | |
157 | if (tag=="ReferenceType") | |
158 | { | |
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+"&"; | |
166 | return ret; | |
167 | } | |
168 | else if (tag=="CvQualifiedType") | |
169 | { | |
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; | |
174 | return ret; | |
175 | } | |
176 | else if (tag=="PointerType") | |
177 | { | |
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+"*"; | |
186 | return ret; | |
187 | } | |
188 | ||
189 | assert(element->get_attribute("name")); | |
190 | type_info ret; | |
191 | if (element->get_attribute("context")) | |
192 | { | |
193 | ret.name=get_namespace(root, element->get_attribute("context")->get_value()); | |
194 | if (ret.name!="::") | |
195 | ret.name+="::"; | |
196 | else | |
197 | // do not explicitely add :: | |
198 | ret.name=""; | |
199 | } | |
200 | ret.name+=element->get_attribute("name")->get_value(); | |
201 | return ret; | |
e50cc1d6 JT |
202 | } |
203 | ||
73eb04f2 GE |
204 | struct Arg |
205 | { | |
206 | std::string name; | |
207 | type_info type; | |
208 | std::string defaultArg; | |
209 | }; | |
210 | ||
060fbd87 JT |
211 | struct t2n_procedure |
212 | { | |
73eb04f2 | 213 | typedef std::list<Arg> Args; |
1770469c GE |
214 | |
215 | type_info ret_type; | |
216 | std::string name; | |
217 | std::string mangled; | |
218 | Args args; | |
219 | ||
220 | std::string ret_classname() const | |
221 | { | |
222 | return name+mangled+"_res"; | |
223 | } | |
224 | std::string cmd_classname() const | |
225 | { | |
226 | return name+mangled+"_cmd"; | |
227 | } | |
228 | bool hasReturn() const {return !ret_type.isVoid();} | |
060fbd87 | 229 | }; |
e50cc1d6 | 230 | |
1770469c GE |
231 | std::ostream &operator<<(std::ostream &o, const t2n_procedure::Args &args) |
232 | { | |
233 | for (t2n_procedure::Args::const_iterator it=args.begin();it!=args.end();++it) | |
234 | { | |
235 | if (it!=args.begin()) o << ", "; | |
73eb04f2 GE |
236 | o << it->type << " " << it->name; |
237 | ||
238 | if (!it->defaultArg.empty()) | |
239 | o << "=" << it->defaultArg; | |
1770469c GE |
240 | } |
241 | return o; | |
060fbd87 | 242 | } |
e50cc1d6 | 243 | |
1770469c GE |
244 | std::ostream &operator<<(std::ostream &o, const t2n_procedure &f) |
245 | { | |
246 | o << f.ret_type << " " << f.name << "(" << f.args << ")"; | |
247 | return o; | |
060fbd87 | 248 | } |
e50cc1d6 | 249 | |
3b7c93a8 JT |
250 | std::pair<std::string, unsigned> |
251 | get_file_and_line(const xmlpp::Element* root, const xmlpp::Element* element) | |
252 | { | |
1770469c GE |
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); | |
3b7c93a8 JT |
255 | } |
256 | ||
1770469c | 257 | std::string get_file_and_line_as_string(const xmlpp::Element* root, const xmlpp::Element* element) |
3b7c93a8 | 258 | { |
1770469c GE |
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); | |
3b7c93a8 JT |
261 | } |
262 | ||
060fbd87 JT |
263 | class Parser |
264 | { | |
265 | public: | |
1770469c GE |
266 | Parser(const std::string &fname) : m_fname(fname) {} |
267 | ||
268 | std::list<t2n_procedure> get_procedures() | |
269 | { | |
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); | |
274 | if (parser) | |
275 | { | |
276 | //Walk the tree: | |
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); | |
279 | assert(root); | |
280 | visit_node(root); | |
281 | } | |
282 | return m_procedures; | |
283 | } | |
060fbd87 | 284 | protected: |
1770469c GE |
285 | std::string m_fname; |
286 | std::list<t2n_procedure> m_procedures; | |
287 | ||
288 | void parse_function(const xmlpp::Element* root, const xmlpp::Node* node) | |
289 | { | |
290 | const xmlpp::Element* element = dynamic_cast<const xmlpp::Element*>(node); | |
291 | if (!element) return; | |
292 | ||
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; | |
298 | ||
299 | // check wether the procedure is marked (TODO: improve) | |
300 | // attributes are speparated by spaces? | |
301 | ||
302 | t2n_procedure f; | |
303 | if (!is_marked(attributes->get_value())) return; | |
304 | ||
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(); | |
309 | ||
310 | xmlpp::Node::NodeList list = node->get_children("Argument"); | |
311 | for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter) | |
312 | { | |
313 | const xmlpp::Element* arg = dynamic_cast<const xmlpp::Element*>(*iter); | |
314 | if ( arg ) | |
315 | { | |
73eb04f2 GE |
316 | struct Arg a; |
317 | ||
1770469c | 318 | assert(arg->get_name() == "Argument"); |
73eb04f2 | 319 | |
1770469c | 320 | assert(arg->get_attribute("name")); |
73eb04f2 GE |
321 | a.name=arg->get_attribute("name")->get_value(); |
322 | ||
1770469c | 323 | assert(arg->get_attribute("type")); |
73eb04f2 GE |
324 | a.type=get_type(root, arg->get_attribute("type")->get_value()); |
325 | ||
326 | if(arg->get_attribute("default")) | |
327 | a.defaultArg=arg->get_attribute("default")->get_value(); | |
328 | ||
1770469c | 329 | // todo: ugly - could be any other error |
73eb04f2 | 330 | if (a.type.name.empty()) |
1770469c GE |
331 | { |
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, | |
73eb04f2 | 337 | std::string("type of parameter '")+a.name+"' not (yet?) supported"); |
1770469c | 338 | } |
73eb04f2 GE |
339 | |
340 | f.args.push_back(a); | |
1770469c GE |
341 | } |
342 | } | |
343 | std::cerr << get_file_and_line_as_string(root, element) << ":\texport procedure: " << f << std::endl; | |
344 | m_procedures.push_back(f); | |
345 | } | |
346 | ||
347 | void visit_node(const xmlpp::Element* root, const xmlpp::Node* node = NULL, unsigned int indentation = 0) | |
348 | { | |
349 | if (!node) node=root; | |
350 | ||
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); | |
354 | ||
355 | if (nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this. | |
356 | return; | |
357 | ||
358 | std::string nodename = node->get_name(); | |
359 | ||
360 | if (!nodeText && !nodeComment && !nodename.empty()) //Let's not say "name: text". | |
361 | { | |
362 | if (node->get_name() == "Function") parse_function(root, node); | |
363 | } | |
364 | if (!nodeContent) | |
365 | { | |
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) | |
369 | { | |
370 | visit_node(root, *iter, indentation + 2); //recursive | |
371 | } | |
372 | } | |
373 | } | |
060fbd87 JT |
374 | }; |
375 | ||
1770469c GE |
376 | void output_common_hpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs) |
377 | { | |
2f0896ce | 378 | o << "class " << groupClass(group) << " : public libt2n::command\n" |
1770469c GE |
379 | << "{\n" |
380 | << "private:\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" | |
385 | << "};\n"; | |
386 | ||
387 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) | |
388 | { | |
389 | o << "class " << it->ret_classname() << " : public libt2n::result\n" | |
390 | << "{\n" | |
391 | << "private:\n"; | |
392 | if (it->hasReturn()) | |
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" | |
397 | << " {\n" | |
398 | << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);\n"; | |
399 | if (it->hasReturn()) | |
400 | o << " ar & BOOST_SERIALIZATION_NVP(res);\n"; | |
401 | o << " }\n" | |
402 | << "public:\n" | |
403 | << " " << it->ret_classname() << "() {}\n"; | |
404 | if (it->hasReturn()) | |
405 | { | |
406 | o << " " << it->ret_classname() << "(const " << it->ret_type << " &_res) : res(_res) {}\n" | |
407 | << " " << it->ret_type << " get_data() { return res; }\n"; | |
408 | } | |
409 | o << "};\n"; | |
d340d435 | 410 | } |
1770469c GE |
411 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) |
412 | { | |
413 | o << "class " << it->cmd_classname() << " : public " << groupClass(group) << "\n" | |
414 | << "{\n" | |
415 | << "private:\n"; | |
416 | for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) | |
417 | { | |
73eb04f2 | 418 | o << " " << ait->type.noref() << " " << ait->name << ";\n"; |
1770469c GE |
419 | } |
420 | o << " friend class boost::serialization::access;\n" | |
421 | << " template<class Archive>\n" | |
422 | << " void serialize(Archive & ar, const unsigned int /* version */)\n" | |
423 | << " {\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) | |
426 | { | |
73eb04f2 | 427 | o << " ar & BOOST_SERIALIZATION_NVP(" << ait->name << ");\n"; |
1770469c GE |
428 | } |
429 | ||
430 | // default constructor | |
431 | o << " }\n" | |
432 | << "\n" | |
433 | << "public:\n" | |
434 | << " " << it->cmd_classname() << "() {}\n"; | |
435 | ||
436 | // constructor taking all arguments | |
437 | if (!it->args.empty()) | |
438 | { | |
439 | o << " " << it->cmd_classname() << "("; | |
440 | for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) | |
441 | { | |
442 | if (ait!=it->args.begin()) o << ", "; | |
73eb04f2 | 443 | o << ait->type << " _" << ait->name; |
1770469c GE |
444 | } |
445 | o << ") : "; | |
446 | for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) | |
447 | { | |
448 | if (ait!=it->args.begin()) o << ", "; | |
449 | // pointers are const pointers and must be dereferenced | |
73eb04f2 | 450 | o << ait->name << "(" << ((ait->type.name.find_first_of('*')!=std::string::npos) ? "*" : "" ) << "_" << ait->name << ")"; |
1770469c GE |
451 | } |
452 | o << " {}\n"; | |
453 | } | |
454 | o << " libt2n::result* operator()();\n" | |
455 | << "};\n"; | |
d340d435 | 456 | } |
060fbd87 JT |
457 | } |
458 | ||
1770469c GE |
459 | void output_common_cpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs, const std::string &common_hpp) |
460 | { | |
461 | o << "#include \"" << common_hpp << "\"\n" | |
462 | << "#include <boost/serialization/export.hpp>\n" | |
463 | << "\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) | |
467 | { | |
468 | o << "BOOST_CLASS_EXPORT("<<it->ret_classname()<<")\n" | |
469 | << "BOOST_CLASS_EXPORT("<<it->cmd_classname()<<")\n"; | |
470 | } | |
060fbd87 | 471 | } |
e50cc1d6 | 472 | |
1770469c GE |
473 | void output_client_hpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs) |
474 | { | |
475 | o << "#include <command_client.hxx>\n"; | |
476 | ||
477 | o << "class " << groupClass(group) << "_client : public libt2n::command_client\n" | |
478 | << "{\n" | |
479 | << "public:\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" | |
484 | << " {}\n"; | |
485 | for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit) | |
486 | { | |
487 | o << " " << *pit << ";\n"; | |
488 | } | |
489 | o << "};\n"; | |
060fbd87 | 490 | } |
e50cc1d6 | 491 | |
1770469c GE |
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) |
493 | { | |
494 | o << "#include \"" << client_hpp << "\"\n" | |
495 | << "#include \"" << common_hpp << "\"\n" | |
496 | << "// fake\n"; | |
497 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) | |
498 | { | |
499 | o << "libt2n::result* " << it->cmd_classname() << "::operator()() { return NULL; }\n"; | |
500 | } | |
501 | ||
502 | for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit) | |
503 | { | |
73eb04f2 GE |
504 | o << pit->ret_type << " " << groupClass(group) << "_client::" << pit->name << "("; |
505 | ||
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) | |
508 | { | |
509 | if (xit!=pit->args.begin()) | |
510 | o << ", "; | |
511 | o << xit->type << " " << xit->name; | |
512 | } | |
513 | ||
514 | o << ")\n" | |
1770469c GE |
515 | << "{\n" |
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) | |
519 | { | |
520 | if (ait!=pit->args.begin()) o << ", "; | |
73eb04f2 | 521 | o << ait->name; |
1770469c GE |
522 | } |
523 | o << "), rc);\n" | |
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"; | |
528 | o << "}\n"; | |
529 | } | |
530 | ||
531 | // include in this compilation unit to ensure the compilation unit is used | |
532 | // see also: | |
533 | // http://www.google.de/search?q=g%2B%2B+static+initializer+in+static+library | |
534 | o << "#include \"" << common_cpp << "\"\n"; | |
060fbd87 JT |
535 | } |
536 | ||
1770469c GE |
537 | void output_server_hpp(std::ostream &o, const std::string & /* group */, const std::list<t2n_procedure> &procs, const std::string &common_hpp) |
538 | { | |
539 | o << "#include \"" << common_hpp << "\"\n"; | |
a96ab628 | 540 | |
1770469c GE |
541 | // output function declarations |
542 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) | |
543 | o << *it << ";\n"; | |
a96ab628 JT |
544 | } |
545 | ||
1770469c GE |
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) |
547 | { | |
548 | o << "#include \"" << common_hpp << "\"\n"; | |
549 | ||
550 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) | |
551 | { | |
552 | o << *it << ";\n"; | |
553 | o << "libt2n::result* " << it->cmd_classname() << "::operator()() { "; | |
554 | ||
555 | if (it->hasReturn()) | |
556 | o << "return new " << it->ret_classname() << "("; | |
557 | ||
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) | |
561 | { | |
562 | if (ait!=it->args.begin()) o << ", "; | |
563 | // get pointer | |
73eb04f2 | 564 | if (ait->type.name.find_first_of('*')!=std::string::npos) |
1770469c | 565 | o << '&'; |
73eb04f2 | 566 | o << ait->name; |
1770469c GE |
567 | } |
568 | ||
569 | if (it->hasReturn()) | |
570 | o << "));"; | |
571 | else | |
9c627fab TJ |
572 | o << "); return new " << it->ret_classname() << "();"; |
573 | ||
1770469c GE |
574 | o << " }\n"; |
575 | } | |
576 | o << "#include \"" << common_cpp << "\"\n"; | |
060fbd87 JT |
577 | } |
578 | ||
060fbd87 JT |
579 | struct header_file : public std::ofstream |
580 | { | |
1770469c GE |
581 | header_file(const char* fname) : std::ofstream(fname) |
582 | { | |
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"; | |
588 | } | |
589 | ~header_file() | |
590 | { | |
591 | *this << "#endif" << std::endl; | |
592 | } | |
060fbd87 JT |
593 | }; |
594 | ||
595 | struct cpp_file : public std::ofstream | |
596 | { | |
1770469c GE |
597 | cpp_file(const char* fname) : std::ofstream(fname) |
598 | { | |
599 | std::cerr << "create cpp: '" << fname << "'" << std::endl; | |
600 | *this << "// automatically generated code - do not edit\n" << std::endl; | |
601 | } | |
060fbd87 JT |
602 | }; |
603 | ||
604 | int | |
605 | main(int argc, char* argv[]) | |
e50cc1d6 | 606 | { |
3907d211 | 607 | // todo: maybe use getopt |
1770469c GE |
608 | if ((argc>1)&&(std::string(argv[1])=="--version")) |
609 | { | |
610 | std::cerr << VERSION << std::endl; | |
611 | return 0; | |
3907d211 JT |
612 | } |
613 | if (argc < 3) | |
614 | { | |
1770469c GE |
615 | std::cerr << "Usage: " << argv[0] << "default-group gccxml-file1 gccxml-file2 ... " << std::endl; |
616 | return 1; | |
3907d211 | 617 | } |
060fbd87 | 618 | |
1770469c GE |
619 | try |
620 | { | |
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]); | |
625 | ||
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) | |
629 | { | |
630 | std::cerr << "Parse " << *it << std::endl; | |
631 | Parser parser(*it); | |
632 | const std::list<t2n_procedure> &p(parser.get_procedures()); | |
633 | std::copy(p.begin(), p.end(), std::back_inserter(procedures)); | |
634 | } | |
635 | ||
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; | |
639 | ||
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"); | |
646 | ||
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"; | |
651 | ||
652 | output_common_hpp(common_hpp, group, procedures); | |
653 | ||
654 | cpp_file common_cpp(common_cpp_fname.c_str()); | |
655 | output_common_cpp(common_cpp, group, procedures, common_hpp_fname); | |
656 | ||
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); | |
662 | ||
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); | |
665 | ||
666 | header_file server_hpp(server_hpp_fname.c_str()); | |
667 | output_server_hpp(server_hpp, group, procedures, common_hpp_fname); | |
668 | ||
669 | cpp_file server_cpp(server_cpp_fname.c_str()); | |
670 | output_server_cpp(server_cpp, group, procedures, common_hpp_fname, common_cpp_fname); | |
671 | } | |
672 | catch (const parse_error &e) | |
673 | { | |
674 | std::cerr << e.what() << std::endl; | |
675 | return EXIT_FAILURE; | |
676 | } | |
677 | return EXIT_SUCCESS; | |
e50cc1d6 | 678 | } |