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