23 #include "tolua_generator.h"
24 #include "exceptions.h"
26 #include <utils/misc/string_conversions.h>
60 std::string config_basename, std::string author,
61 std::string year, std::string creation_date,
62 std::string data_comment,
63 const unsigned char *hash,
size_t hash_size,
64 const std::vector<InterfaceConstant> &constants,
65 const std::vector<InterfaceEnumConstant> &enum_constants,
66 const std::vector<InterfaceField> &data_fields,
67 const std::vector<InterfacePseudoMap> &pseudo_maps,
68 const std::vector<InterfaceMessage> &messages
71 this->dir = directory;
72 if ( dir.find_last_of(
"/") != (dir.length() - 1) ) {
75 this->author = author;
77 this->creation_date = creation_date;
78 this->data_comment = data_comment;
80 this->hash_size = hash_size;
81 this->constants = constants;
82 this->enum_constants = enum_constants;
83 this->data_fields = data_fields;
84 this->pseudo_maps = pseudo_maps;
85 this->messages = messages;
87 filename_tolua = config_basename +
".tolua";
88 filename_h = config_basename +
".h";
90 if ( interface_name.find(
"Interface", 0) == string::npos ) {
92 class_name = interface_name +
"Interface";
94 class_name = interface_name;
114 if (c_type ==
"uint8_t") {
115 return "unsigned char";
116 }
else if (c_type ==
"uint16_t") {
117 return "unsigned short";
118 }
else if (c_type ==
"uint32_t") {
119 return "unsigned int";
120 }
else if (c_type ==
"uint64_t") {
122 return "unsigned long";
124 return "unsigned long long";
126 }
else if (c_type ==
"int8_t") {
128 }
else if (c_type ==
"int16_t") {
130 }
else if (c_type ==
"int32_t") {
132 }
else if (c_type ==
"int64_t") {
138 }
else if (c_type ==
"uint8_t *") {
139 return "unsigned char *";
140 }
else if (c_type ==
"uint16_t *") {
141 return "unsigned short *";
142 }
else if (c_type ==
"uint32_t *") {
143 return "unsigned int *";
144 }
else if (c_type ==
"uint64_t *") {
146 return "unsigned long *";
148 return "unsigned long long *";
150 }
else if (c_type ==
"int8_t *") {
152 }
else if (c_type ==
"int16_t *") {
154 }
else if (c_type ==
"int32_t *") {
156 }
else if (c_type ==
"int64_t *") {
160 return "long long *";
163 return c_type.c_str();
176 fprintf(f,
"\n/***************************************************************************\n");
177 fprintf(f,
" * %s - Fawkes BlackBoard Interface - %s - tolua++ wrapper\n", filename.c_str(), class_name.c_str());
179 if ( creation_date.length() > 0 ) {
180 fprintf(f,
" * Interface created: %s\n", creation_date.c_str());
182 fprintf(f,
" * Templated created: Thu Oct 12 10:49:19 2006\n");
183 fprintf(f,
" * Copyright %s %s\n", year.c_str(),
184 ((author.length() > 0) ? author.c_str() :
"AllemaniACs RoboCup Team") );
186 fprintf(f,
" ****************************************************************************/\n\n");
188 fprintf(f,
" * This program is free software; you can redistribute it and/or modify\n");
189 fprintf(f,
" * it under the terms of the GNU General Public License as published by\n");
190 fprintf(f,
" * the Free Software Foundation; either version 2 of the License, or\n");
191 fprintf(f,
" * (at your option) any later version.\n");
193 fprintf(f,
" * This program is distributed in the hope that it will be useful,\n");
194 fprintf(f,
" * but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
195 fprintf(f,
" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
196 fprintf(f,
" * GNU Library General Public License for more details.\n");
198 fprintf(f,
" * You should have received a copy of the GNU General Public License\n");
199 fprintf(f,
" * along with this program; if not, write to the Free Software Foundation,\n");
200 fprintf(f,
" * Inc., 51 Franklin Street, Fifth floor, Boston, MA 02111-1307, USA.\n");
201 fprintf(f,
" */\n\n");
211 for ( vector<InterfaceConstant>::iterator i = constants.begin(); i != constants.end(); ++i) {
212 fprintf(f,
" static const %s %s;\n", convert_type(i->getType()),
213 i->getName().c_str());
217 for ( vector<InterfaceEnumConstant>::iterator i = enum_constants.begin(); i != enum_constants.end(); ++i) {
218 fprintf(f,
" typedef enum {\n");
219 vector<InterfaceEnumConstant::EnumItem> items = (*i).get_items();
220 vector<InterfaceEnumConstant::EnumItem>::iterator j = items.begin();
221 while (j != items.end()) {
222 if (j->has_custom_value) {
223 fprintf(f,
" %s = %i", j->name.c_str(), j->custom_value);
225 fprintf(f,
" %s", j->name.c_str());
228 if ( j != items.end() ) {
234 fprintf(f,
" } %s;\n\n", (*i).get_name().c_str());
245 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
246 fprintf(f,
" class %s : public Message\n"
247 " {\n", (*i).getName().c_str());
248 write_message_ctor_dtor_h(f,
" ", (*i).getName(), (*i).getFields());
249 write_methods_h(f,
" ", (*i).getFields());
251 fprintf(f,
" };\n\n");
264 std::string classname)
269 is.c_str(), classname.c_str(),
270 is.c_str(), classname.c_str());
282 std::string classname,
283 std::vector<InterfaceField> fields)
285 vector<InterfaceField>::iterator i;
287 if ( fields.size() > 0 ) {
289 fprintf(f,
"%s%s(", is.c_str(), classname.c_str());
292 while (i != fields.end()) {
293 fprintf(f,
"%s ini_%s",
294 convert_type(i->getAccessType()), i->getName().c_str());
296 if ( i != fields.end() ) {
305 write_ctor_dtor_h(f, is, classname);
315 " bool oftype(const char *interface_type) const;\n"
316 " const void * datachunk() const;\n"
317 " unsigned int datasize() const;\n"
318 " const char * type() const;\n"
319 " const char * id() const;\n"
320 " const char * uid() const;\n"
321 " unsigned int serial() const;\n"
322 " unsigned int mem_serial() const;\n"
323 " bool operator== (Interface &comp) const;\n"
324 " const unsigned char * hash() const;\n"
325 " int hash_size() const;\n"
326 " const char * hash_printable() const;\n"
327 " bool is_writer() const;\n"
329 " void set_from_chunk(void *chunk);\n"
331 " virtual Message * create_message(const char *type) const = 0;\n"
336 " bool has_writer() const;\n"
337 " unsigned int num_readers() const;\n"
340 " unsigned int msgq_enqueue_copy(Message *message);\n"
341 " void msgq_remove(Message *message);\n"
342 " void msgq_remove(unsigned int message_id);\n"
343 " unsigned int msgq_size();\n"
344 " void msgq_flush();\n"
345 " void msgq_lock();\n"
346 " bool msgq_try_lock();\n"
347 " void msgq_unlock();\n"
348 " void msgq_pop();\n"
349 " Message * msgq_first();\n"
350 " bool msgq_empty();\n"
361 std::vector<InterfaceField> fields)
363 for (vector<InterfaceField>::iterator i = fields.begin(); i != fields.end(); ++i) {
365 if ( (i->getLengthValue() > 0) && (i->getType() !=
"string" ) ) {
367 "%s%s %s%s(int index);\n",
369 (i->getType() ==
"byte") ?
"unsigned int" : convert_type(i->getPlainAccessType()),
370 ( ((*i).getType() ==
"bool" ) ?
"is_" :
""),
371 (*i).getName().c_str());
374 "%svoid set_%s(unsigned int index, const %s new_%s);\n",
375 is.c_str(), (*i).getName().c_str(),
376 convert_type(i->getPlainAccessType()), i->getName().c_str());
380 is.c_str(), convert_type(i->getAccessType()),
381 ( ((*i).getType() ==
"bool" ) ?
"is_" :
""),
382 (*i).getName().c_str());
385 "%svoid set_%s(const %s new_%s);\n",
386 is.c_str(), (*i).getName().c_str(),
387 convert_type(i->getAccessType()), i->getName().c_str());
390 "%sint maxlenof_%s() const;\n",
391 is.c_str(), (*i).getName().c_str()
405 std::vector<InterfaceField> fields,
406 std::vector<InterfacePseudoMap> pseudo_maps)
408 write_methods_h(f, is, fields);
410 for (vector<InterfacePseudoMap>::iterator i = pseudo_maps.begin(); i != pseudo_maps.end(); ++i) {
412 "%s%s %s(%s key) const;\n"
413 "%svoid set_%s(const %s key, const %s new_value);\n",
414 is.c_str(), convert_type(i->getType()),
415 (*i).getName().c_str(), convert_type(i->getKeyType()),
416 is.c_str(), (*i).getName().c_str(),
417 convert_type(i->getKeyType()), convert_type(i->getType()));
429 "$#include <interfaces/%s>\n"
430 "$using namespace fawkes;\n"
431 "namespace fawkes {\n"
432 "class %s : public Interface\n"
437 write_constants_h(f);
440 write_methods_h(f,
" ", data_fields, pseudo_maps);
441 write_superclass_h(f);
442 fprintf(f,
"\n};\n\n}\n");
452 struct tm timestruct;
453 time_t t = time(NULL);
454 localtime_r(&t, ×truct);
455 asctime_r(×truct, timestring);
456 gendate = timestring;
460 toluaf = fopen(
string(dir + filename_tolua).c_str(),
"w");
462 if ( toluaf == NULL ) {
463 printf(
"Cannot open tolua file %s%s\n", dir.c_str(), filename_tolua.c_str());
466 write_toluaf(toluaf);