Commit | Line | Data |
---|---|---|
0e627fe2 JT |
1 | /* |
2 | Copyright (C) 2006 | |
3 | intra2net.com | |
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 | |
34 | std::string | |
35 | groupClass(const std::string &group) { | |
36 | return std::string("cmd_group_")+group; | |
37 | } | |
38 | ||
52b6f93a | 39 | //! convert string to upper case |
e035276b JT |
40 | std::string |
41 | toupper(std::string s) { | |
42 | for (unsigned i=0; i<s.length(); ++i) s[i]=toupper(s[i]); | |
43 | return s; | |
44 | } | |
45 | ||
52b6f93a | 46 | //! replace all characters f by r in string s |
e035276b JT |
47 | std::string |
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; | |
50 | return s; | |
51 | } | |
52 | ||
52b6f93a JT |
53 | //! strip prefix from string s |
54 | /*! | |
55 | \return string s without prefix or an empty string on error | |
56 | */ | |
e035276b JT |
57 | std::string |
58 | strip(std::string s, std::string prefix) | |
59 | { | |
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()); | |
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 | { | |
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 | const xmlpp::Element* match = get_element_by_id(element, id); | |
82 | if (match) return match; | |
83 | } | |
84 | } | |
85 | return NULL; | |
86 | } | |
87 | ||
6be7b70f JT |
88 | std::string |
89 | get_file(const xmlpp::Element* root, const std::string &file_id) | |
90 | { | |
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(); | |
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 | { | |
103 | std::string error; | |
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(); | |
107 | } | |
108 | ||
ef7b2923 JT |
109 | //! procedure marked for export? |
110 | bool | |
111 | is_marked(const std::string &attrs) | |
d340d435 JT |
112 | { |
113 | // todo: improve this | |
d340d435 JT |
114 | std::string to_match("gccxml(libt2n-"); |
115 | std::string::size_type p(attrs.find(to_match)); | |
ef7b2923 | 116 | return (p!=std::string::npos); |
d340d435 JT |
117 | } |
118 | ||
e035276b JT |
119 | struct type_info |
120 | { | |
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);} | |
9d3993e5 | 124 | std::string noref() const {return noref_name.empty() ? name : noref_name;} |
5f5fa5fc | 125 | bool isVoid() const {return name=="void";} |
e035276b JT |
126 | }; |
127 | ||
128 | std::ostream &operator<<(std::ostream &o, const type_info &t) { | |
129 | o << t.name; | |
130 | return o; | |
131 | } | |
132 | ||
6be7b70f JT |
133 | struct parse_error : public std::runtime_error |
134 | { | |
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 | {} | |
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 | { |
e035276b | 146 | type_info error; |
e50cc1d6 JT |
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) | |
060fbd87 | 153 | |
9d3993e5 | 154 | // TODO: const and reference types handling is a ugly hack |
060fbd87 | 155 | |
e50cc1d6 JT |
156 | std::string tag(element->get_name()); |
157 | if (tag=="ReferenceType") { | |
158 | assert(element->get_attribute("type")); | |
e035276b | 159 | type_info ret(get_type(root, element->get_attribute("type")->get_value())); |
e50cc1d6 | 160 | if (ret==error) return error; |
e035276b JT |
161 | // at the moment we only support const & |
162 | // todo: nice error message! | |
163 | if ((ret.noref_name=strip(ret.name,"const ")).empty()) return error; | |
164 | ret.name=ret.name+"&"; | |
165 | return ret; | |
e50cc1d6 JT |
166 | }else if (tag=="CvQualifiedType") { |
167 | assert(element->get_attribute("type")); | |
e035276b | 168 | type_info ret(get_type(root, element->get_attribute("type")->get_value())); |
e50cc1d6 | 169 | if (ret==error) return error; |
e035276b JT |
170 | ret.name=std::string("const ")+ret.name; |
171 | return ret; | |
6be7b70f | 172 | }else if (tag=="PointerType") { |
2fad99db JT |
173 | // todo: nearly the same as reference type handling |
174 | assert(element->get_attribute("type")); | |
175 | type_info ret(get_type(root, element->get_attribute("type")->get_value())); | |
176 | if (ret==error) return error; | |
177 | // at the moment we only support const & | |
178 | // todo: nice error message! | |
179 | if ((ret.noref_name=strip(ret.name,"const ")).empty()) return error; | |
180 | ret.name=ret.name+"*"; | |
181 | return ret; | |
e50cc1d6 JT |
182 | } |
183 | ||
184 | assert(element->get_attribute("name")); | |
e035276b | 185 | type_info ret; |
fb5c13c6 JT |
186 | if (element->get_attribute("context")) { |
187 | ret.name=get_namespace(root, element->get_attribute("context")->get_value()); | |
188 | if (ret.name!="::") | |
189 | ret.name+="::"; | |
190 | else | |
191 | // do not explicitely add :: | |
192 | ret.name=""; | |
193 | } | |
9d3993e5 | 194 | ret.name+=element->get_attribute("name")->get_value(); |
e035276b | 195 | return ret; |
e50cc1d6 JT |
196 | } |
197 | ||
060fbd87 JT |
198 | struct t2n_procedure |
199 | { | |
9d3993e5 | 200 | typedef std::list<std::pair<std::string, type_info> > Args; |
e50cc1d6 | 201 | |
e035276b | 202 | type_info ret_type; |
060fbd87 | 203 | std::string name; |
52b6f93a | 204 | std::string mangled; |
060fbd87 | 205 | Args args; |
e50cc1d6 | 206 | |
060fbd87 | 207 | std::string ret_classname() const { |
52b6f93a | 208 | return name+mangled+"_res"; |
060fbd87 JT |
209 | } |
210 | std::string cmd_classname() const { | |
52b6f93a | 211 | return name+mangled+"_cmd"; |
060fbd87 | 212 | } |
5f5fa5fc | 213 | bool hasReturn() const {return !ret_type.isVoid();} |
060fbd87 | 214 | }; |
e50cc1d6 | 215 | |
060fbd87 JT |
216 | std::ostream &operator<<(std::ostream &o, const t2n_procedure::Args &args) { |
217 | for (t2n_procedure::Args::const_iterator it=args.begin();it!=args.end();++it) { | |
218 | if (it!=args.begin()) o << ", "; | |
219 | o << it->second << " " << it->first; | |
220 | } | |
221 | return o; | |
222 | } | |
e50cc1d6 | 223 | |
060fbd87 JT |
224 | std::ostream &operator<<(std::ostream &o, const t2n_procedure &f) { |
225 | o << f.ret_type << " " << f.name << "(" << f.args << ")"; | |
226 | return o; | |
227 | } | |
e50cc1d6 | 228 | |
3b7c93a8 JT |
229 | std::pair<std::string, unsigned> |
230 | get_file_and_line(const xmlpp::Element* root, const xmlpp::Element* element) | |
231 | { | |
232 | return std::pair<std::string, unsigned>(get_file(root, element->get_attribute("file")->get_value()), | |
233 | boost::lexical_cast<unsigned>(element->get_attribute("line")->get_value())-1); | |
234 | } | |
235 | ||
236 | std::string | |
237 | get_file_and_line_as_string(const xmlpp::Element* root, const xmlpp::Element* element) | |
238 | { | |
239 | std::pair<std::string, unsigned> fl(get_file_and_line(root,element)); | |
240 | return std::string(fl.first)+":"+boost::lexical_cast<std::string>(fl.second); | |
241 | } | |
242 | ||
060fbd87 JT |
243 | class Parser |
244 | { | |
245 | public: | |
d340d435 | 246 | Parser(const std::string &fname) : m_fname(fname) {} |
e50cc1d6 | 247 | |
060fbd87 JT |
248 | std::list<t2n_procedure> get_procedures() { |
249 | xmlpp::DomParser parser; | |
250 | // parser.set_validate(); | |
251 | parser.set_substitute_entities(); //We just want the text to be resolved/unescaped automatically. | |
252 | parser.parse_file(m_fname); | |
253 | if(parser) | |
254 | { | |
255 | //Walk the tree: | |
256 | const xmlpp::Node* pNode = parser.get_document()->get_root_node(); //deleted by DomParser. | |
257 | const xmlpp::Element* root = dynamic_cast<const xmlpp::Element*>(pNode); | |
258 | assert(root); | |
259 | visit_node(root); | |
e50cc1d6 | 260 | } |
060fbd87 | 261 | return m_procedures; |
e50cc1d6 | 262 | } |
060fbd87 JT |
263 | protected: |
264 | std::string m_fname; | |
265 | std::list<t2n_procedure> m_procedures; | |
266 | ||
267 | void parse_function(const xmlpp::Element* root, const xmlpp::Node* node) { | |
268 | const xmlpp::Element* element = dynamic_cast<const xmlpp::Element*>(node); | |
269 | if (!element) return; | |
270 | ||
271 | const xmlpp::Attribute* attributes = element->get_attribute("attributes"); | |
272 | const xmlpp::Attribute* name = element->get_attribute("name"); | |
52b6f93a | 273 | const xmlpp::Attribute* mangled = element->get_attribute("mangled"); |
060fbd87 | 274 | const xmlpp::Attribute* returns = element->get_attribute("returns"); |
52b6f93a | 275 | if ((!attributes)||(!name)||(!mangled)||(!returns)) return; |
060fbd87 JT |
276 | |
277 | // check wether the procedure is marked (TODO: improve) | |
278 | // attributes are speparated by spaces? | |
060fbd87 | 279 | |
060fbd87 | 280 | t2n_procedure f; |
ef7b2923 | 281 | if (!is_marked(attributes->get_value())) return; |
e50cc1d6 | 282 | |
060fbd87 JT |
283 | // we need the return type |
284 | f.ret_type=get_type(root, returns->get_value()); | |
285 | f.name=name->get_value(); | |
52b6f93a | 286 | f.mangled=mangled->get_value(); |
060fbd87 JT |
287 | |
288 | xmlpp::Node::NodeList list = node->get_children("Argument"); | |
289 | for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter) | |
290 | { | |
291 | const xmlpp::Element* arg = dynamic_cast<const xmlpp::Element*>(*iter); | |
292 | if ( arg ) { | |
293 | assert(arg->get_name() == "Argument"); | |
294 | assert(arg->get_attribute("name")); | |
295 | assert(arg->get_attribute("type")); | |
9d3993e5 | 296 | 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()))); |
6be7b70f JT |
297 | // todo: ugly - could be any other error |
298 | if (f.args.back().second.name.empty()) { | |
299 | assert(element->get_attribute("file")); | |
300 | assert(element->get_attribute("line")); | |
3b7c93a8 JT |
301 | std::pair<std::string, unsigned> file_and_line(get_file_and_line(root, element)); |
302 | throw parse_error(file_and_line.first, | |
303 | file_and_line.second, | |
6be7b70f JT |
304 | std::string("type of parameter `")+f.args.back().first+"' not (yet?) supported"); |
305 | } | |
060fbd87 JT |
306 | } |
307 | } | |
fe41e3c5 | 308 | std::cerr << get_file_and_line_as_string(root, element) << ":\texport procedure: " << f << std::endl; |
060fbd87 | 309 | m_procedures.push_back(f); |
e50cc1d6 | 310 | } |
e50cc1d6 | 311 | |
060fbd87 JT |
312 | void visit_node(const xmlpp::Element* root, const xmlpp::Node* node = NULL, unsigned int indentation = 0) |
313 | { | |
314 | if (!node) node=root; | |
060fbd87 JT |
315 | |
316 | const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node); | |
317 | const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node); | |
318 | const xmlpp::CommentNode* nodeComment = dynamic_cast<const xmlpp::CommentNode*>(node); | |
e50cc1d6 | 319 | |
060fbd87 JT |
320 | if(nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this. |
321 | return; | |
e50cc1d6 | 322 | |
060fbd87 | 323 | std::string nodename = node->get_name(); |
e50cc1d6 | 324 | |
060fbd87 JT |
325 | if(!nodeText && !nodeComment && !nodename.empty()) //Let's not say "name: text". |
326 | { | |
52b6f93a | 327 | if (node->get_name() == "Function") parse_function(root, node); |
060fbd87 | 328 | } |
060fbd87 JT |
329 | if(!nodeContent) |
330 | { | |
331 | //Recurse through child nodes: | |
332 | xmlpp::Node::NodeList list = node->get_children(); | |
333 | for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter) | |
334 | { | |
335 | visit_node(root, *iter, indentation + 2); //recursive | |
336 | } | |
337 | } | |
e50cc1d6 | 338 | } |
060fbd87 JT |
339 | }; |
340 | ||
d340d435 | 341 | void output_common_hpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs) { |
2f0896ce | 342 | o << "class " << groupClass(group) << " : public libt2n::command\n" |
d340d435 JT |
343 | << "{\n" |
344 | << "private:\n" | |
345 | << " friend class boost::serialization::access;\n" | |
346 | << " template<class Archive>\n" | |
347 | << " void serialize(Archive & ar, const unsigned int /* version */)\n" | |
348 | << " {ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);}\n" | |
349 | << "};\n"; | |
060fbd87 | 350 | |
d340d435 JT |
351 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) { |
352 | o << "class " << it->ret_classname() << " : public libt2n::result\n" | |
353 | << "{\n" | |
1ef8a135 | 354 | << "private:\n"; |
5f5fa5fc | 355 | if (it->hasReturn()) |
1ef8a135 JT |
356 | o << " " << it->ret_type << " res;\n"; |
357 | o << " friend class boost::serialization::access;\n" | |
d340d435 JT |
358 | << " template<class Archive>\n" |
359 | << " void serialize(Archive & ar, const unsigned int /* version */)\n" | |
360 | << " {\n" | |
1ef8a135 | 361 | << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);\n"; |
5f5fa5fc | 362 | if (it->hasReturn()) |
1ef8a135 JT |
363 | o << " ar & BOOST_SERIALIZATION_NVP(res);\n"; |
364 | o << " }\n" | |
d340d435 | 365 | << "public:\n" |
1ef8a135 | 366 | << " " << it->ret_classname() << "() {}\n"; |
5f5fa5fc | 367 | if (it->hasReturn()) { |
1ef8a135 JT |
368 | o << " " << it->ret_classname() << "(const " << it->ret_type << " &_res) : res(_res) {}\n" |
369 | << " " << it->ret_type << " get_data() { return res; }\n"; | |
370 | } | |
371 | o << "};\n"; | |
d340d435 JT |
372 | } |
373 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) { | |
2f0896ce | 374 | o << "class " << it->cmd_classname() << " : public " << groupClass(group) << "\n" |
d340d435 JT |
375 | << "{\n" |
376 | << "private:\n"; | |
377 | for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) { | |
378 | o << " " << ait->second.noref() << " " << ait->first << ";\n"; | |
379 | } | |
380 | o << " friend class boost::serialization::access;\n" | |
381 | << " template<class Archive>\n" | |
382 | << " void serialize(Archive & ar, const unsigned int /* version */)\n" | |
383 | << " {\n" | |
2f0896ce | 384 | << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(" << groupClass(group) << ");\n"; |
d340d435 JT |
385 | for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) { |
386 | o << " ar & BOOST_SERIALIZATION_NVP(" << ait->first << ");\n"; | |
387 | } | |
388 | ||
389 | // default constructor | |
390 | o << " }\n" | |
391 | << "\n" | |
392 | << "public:\n" | |
393 | << " " << it->cmd_classname() << "() {}\n"; | |
394 | ||
395 | // constructor taking all arguments | |
1ef8a135 JT |
396 | if (!it->args.empty()) { |
397 | o << " " << it->cmd_classname() << "("; | |
398 | for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) { | |
d340d435 JT |
399 | if (ait!=it->args.begin()) o << ", "; |
400 | o << ait->second << " _" << ait->first; | |
1ef8a135 JT |
401 | } |
402 | o << ") : "; | |
403 | for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) { | |
d340d435 | 404 | if (ait!=it->args.begin()) o << ", "; |
2fad99db JT |
405 | // pointers are const pointers and must be dereferenced |
406 | o << ait->first << "(" << ((ait->second.name.find_first_of('*')!=std::string::npos) ? "*" : "" ) << "_" << ait->first << ")"; | |
1ef8a135 JT |
407 | } |
408 | o << " {}\n"; | |
d340d435 | 409 | } |
1ef8a135 | 410 | o << " libt2n::result* operator()();\n" |
d340d435 JT |
411 | << "};\n"; |
412 | } | |
060fbd87 JT |
413 | } |
414 | ||
d340d435 | 415 | void output_common_cpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs, const std::string &common_hpp) { |
060fbd87 | 416 | o << "#include \"" << common_hpp << "\"\n" |
e035276b | 417 | << "#include <boost/serialization/export.hpp>\n" |
060fbd87 JT |
418 | << "\n" |
419 | << "/* register types with boost serialization */\n"; | |
2f0896ce | 420 | o << "BOOST_CLASS_EXPORT(" << groupClass(group) << ")\n"; |
060fbd87 JT |
421 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) { |
422 | o << "BOOST_CLASS_EXPORT("<<it->ret_classname()<<")\n" | |
423 | << "BOOST_CLASS_EXPORT("<<it->cmd_classname()<<")\n"; | |
e50cc1d6 | 424 | } |
060fbd87 | 425 | } |
e50cc1d6 | 426 | |
d340d435 | 427 | void output_client_hpp(std::ostream &o, const std::string &group, const std::list<t2n_procedure> &procs) { |
060fbd87 JT |
428 | o << "#include <command_client.hxx>\n"; |
429 | ||
2f0896ce | 430 | o << "class " << groupClass(group) << "_client : public libt2n::command_client\n" |
d340d435 JT |
431 | << "{\n" |
432 | << "public:\n" | |
2f0896ce | 433 | << groupClass(group) << "_client(libt2n::client_connection &_c,\n" |
d340d435 JT |
434 | << " long long _command_timeout_usec=command_timeout_usec_default,\n" |
435 | << " long long _hello_timeout_usec=hello_timeout_usec_default)\n" | |
436 | << " : libt2n::command_client(_c,_command_timeout_usec,_hello_timeout_usec)\n" | |
437 | << " {}\n"; | |
438 | for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit) { | |
439 | o << " " << *pit << ";\n"; | |
060fbd87 | 440 | } |
d340d435 | 441 | o << "};\n"; |
060fbd87 | 442 | } |
e50cc1d6 | 443 | |
d340d435 | 444 | 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) { |
060fbd87 JT |
445 | o << "#include \"" << client_hpp << "\"\n" |
446 | << "#include \"" << common_hpp << "\"\n" | |
447 | << "// fake\n"; | |
448 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) { | |
449 | o << "libt2n::result* " << it->cmd_classname() << "::operator()() { return NULL; }\n"; | |
450 | } | |
451 | ||
d340d435 | 452 | for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit) { |
2f0896ce | 453 | o << pit->ret_type << " " << groupClass(group) << "_client::" << pit->name << "(" << pit->args << ")\n" |
d340d435 JT |
454 | << "{\n" |
455 | << " libt2n::result_container rc;\n" | |
456 | << " send_command(new " << pit->cmd_classname() << "("; | |
457 | for (t2n_procedure::Args::const_iterator ait=pit->args.begin();ait!=pit->args.end();++ait) { | |
458 | if (ait!=pit->args.begin()) o << ", "; | |
459 | o << ait->first; | |
460 | } | |
461 | o << "), rc);\n" | |
462 | << " " << pit->ret_classname() << "* res=dynamic_cast<" << pit->ret_classname() << "*>(rc.get_result());\n" | |
1ef8a135 | 463 | << " if (!res) throw libt2n::t2n_communication_error(\"result object of wrong type\");\n"; |
5f5fa5fc | 464 | if (pit->hasReturn()) |
1ef8a135 JT |
465 | o << " return res->get_data();\n"; |
466 | o << "}\n"; | |
e50cc1d6 | 467 | } |
060fbd87 JT |
468 | |
469 | // include in this compilation unit to ensure the compilation unit is used | |
470 | // see also: | |
471 | // http://www.google.de/search?q=g%2B%2B+static+initializer+in+static+library | |
472 | o << "#include \"" << common_cpp << "\"\n"; | |
473 | } | |
474 | ||
d340d435 | 475 | void output_server_hpp(std::ostream &o, const std::string & /* group */, const std::list<t2n_procedure> &procs, const std::string &common_hpp) { |
a96ab628 JT |
476 | o << "#include \"" << common_hpp << "\"\n"; |
477 | ||
478 | // output function declarations | |
479 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) | |
480 | o << *it << ";\n"; | |
481 | } | |
482 | ||
d340d435 | 483 | 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) { |
060fbd87 JT |
484 | o << "#include \"" << common_hpp << "\"\n"; |
485 | ||
486 | for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) { | |
487 | o << *it << ";\n"; | |
1ef8a135 | 488 | o << "libt2n::result* " << it->cmd_classname() << "::operator()() { "; |
9c627fab TJ |
489 | |
490 | if (it->hasReturn()) | |
491 | o << "return new " << it->ret_classname() << "("; | |
492 | ||
493 | // output function name and args | |
494 | o << it->name << "("; | |
495 | for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) { | |
496 | if (ait!=it->args.begin()) o << ", "; | |
497 | // get pointer | |
498 | if (ait->second.name.find_first_of('*')!=std::string::npos) | |
499 | o << '&'; | |
500 | o << ait->first; | |
e50cc1d6 | 501 | } |
9c627fab TJ |
502 | |
503 | if (it->hasReturn()) | |
504 | o << "));"; | |
505 | else | |
506 | o << "); return new " << it->ret_classname() << "();"; | |
507 | ||
1ef8a135 | 508 | o << " }\n"; |
e50cc1d6 | 509 | } |
060fbd87 JT |
510 | o << "#include \"" << common_cpp << "\"\n"; |
511 | } | |
512 | ||
060fbd87 JT |
513 | struct header_file : public std::ofstream |
514 | { | |
515 | header_file(const char* fname) : std::ofstream(fname) { | |
516 | std::cerr << "create header: '" << fname << "'" << std::endl; | |
517 | std::string macro(replace(toupper(fname),'.','_')); | |
3907d211 | 518 | *this << "// automatically generated code (generated by libt2n-codegen " << VERSION << ") - do not edit\n" << std::endl; |
060fbd87 JT |
519 | *this << "#ifndef " << macro << "\n" |
520 | << "#define " << macro << "\n"; | |
521 | } | |
522 | ~header_file() { | |
523 | *this << "#endif" << std::endl; | |
524 | } | |
525 | }; | |
526 | ||
527 | struct cpp_file : public std::ofstream | |
528 | { | |
529 | cpp_file(const char* fname) : std::ofstream(fname) { | |
530 | std::cerr << "create cpp: '" << fname << "'" << std::endl; | |
531 | *this << "// automatically generated code - do not edit\n" << std::endl; | |
532 | } | |
533 | }; | |
534 | ||
535 | int | |
536 | main(int argc, char* argv[]) | |
e50cc1d6 | 537 | { |
3907d211 JT |
538 | // todo: maybe use getopt |
539 | if ((argc>1)&&(std::string(argv[1])=="--version")) { | |
540 | std::cerr << VERSION << std::endl; | |
541 | return 0; | |
542 | } | |
543 | if (argc < 3) | |
544 | { | |
545 | std::cerr << "Usage: " << argv[0] << "default-group gccxml-file1 gccxml-file2 ... " << std::endl; | |
546 | return 1; | |
547 | } | |
060fbd87 | 548 | |
3907d211 | 549 | try{ |
85106017 | 550 | std::string group(argv[1]); |
0cfa3fb2 | 551 | std::list<std::string> xmlfiles; |
85106017 | 552 | for (int i=2;i<argc;++i) |
0cfa3fb2 JT |
553 | xmlfiles.push_back(argv[i]); |
554 | ||
a96ab628 | 555 | std::string prefix=group+"_"; |
0cfa3fb2 JT |
556 | std::list<t2n_procedure> procedures; |
557 | for (std::list<std::string>::iterator it=xmlfiles.begin();it!=xmlfiles.end();++it) { | |
d340d435 JT |
558 | std::cerr << "Parse " << *it << std::endl; |
559 | Parser parser(*it); | |
560 | const std::list<t2n_procedure> &p(parser.get_procedures()); | |
561 | std::copy(p.begin(), p.end(), std::back_inserter(procedures)); | |
0cfa3fb2 | 562 | } |
6be7b70f | 563 | |
fe41e3c5 | 564 | std::cerr << "All procedures:" << std::endl; |
6be7b70f JT |
565 | for (std::list<t2n_procedure>::const_iterator it=procedures.begin();it!=procedures.end();++it) |
566 | std::cerr << *it << ";" << std::endl; | |
567 | ||
6be7b70f JT |
568 | std::string common_hpp_fname(prefix+"common.hxx"); |
569 | std::string common_cpp_fname(prefix+"common.cpp"); | |
570 | std::string client_hpp_fname(prefix+"client.hxx"); | |
571 | std::string client_cpp_fname(prefix+"client.cpp"); | |
a96ab628 | 572 | std::string server_hpp_fname(prefix+"server.hxx"); |
6be7b70f JT |
573 | std::string server_cpp_fname(prefix+"server.cpp"); |
574 | ||
575 | header_file common_hpp(common_hpp_fname.c_str()); | |
576 | common_hpp << "// boost serialization is picky about order of include files => we have to include this one first\n" | |
85106017 JT |
577 | << "#include \"codegen-stubhead.hxx\"\n" |
578 | << "#include \"" << group << ".hxx\"\n"; | |
579 | ||
d340d435 | 580 | output_common_hpp(common_hpp, group, procedures); |
6be7b70f | 581 | |
25924cae | 582 | cpp_file common_cpp(common_cpp_fname.c_str()); |
d340d435 | 583 | output_common_cpp(common_cpp, group, procedures, common_hpp_fname); |
6be7b70f | 584 | |
25924cae JT |
585 | header_file client_hpp(client_hpp_fname.c_str()); |
586 | client_hpp << "// boost serialization is picky about order of include files => we have to include this one first\n" | |
85106017 JT |
587 | << "#include \"codegen-stubhead.hxx\"\n" |
588 | << "#include \"" << group << ".hxx\"\n"; | |
d340d435 | 589 | output_client_hpp(client_hpp, group, procedures); |
6be7b70f | 590 | |
25924cae | 591 | cpp_file client_cpp(client_cpp_fname.c_str()); |
d340d435 | 592 | output_client_cpp(client_cpp, group, procedures, common_hpp_fname, common_cpp_fname, client_hpp_fname); |
6be7b70f | 593 | |
25924cae | 594 | header_file server_hpp(server_hpp_fname.c_str()); |
d340d435 | 595 | output_server_hpp(server_hpp, group, procedures, common_hpp_fname); |
a96ab628 | 596 | |
25924cae | 597 | cpp_file server_cpp(server_cpp_fname.c_str()); |
d340d435 | 598 | output_server_cpp(server_cpp, group, procedures, common_hpp_fname, common_cpp_fname); |
6be7b70f | 599 | }catch(const parse_error &e){ |
25924cae JT |
600 | std::cerr << e.what() << std::endl; |
601 | return EXIT_FAILURE; | |
6be7b70f JT |
602 | } |
603 | return EXIT_SUCCESS; | |
e50cc1d6 | 604 | } |