23#include "tolua_generator.h"
25#include "exceptions.h"
27#include <utils/misc/string_conversions.h>
60 std::string directory,
61 std::string interface_name,
62 std::string config_basename,
65 std::string creation_date,
66 std::string data_comment,
67 const unsigned char * hash,
69 const std::vector<InterfaceConstant> & constants,
70 const std::vector<InterfaceEnumConstant> &enum_constants,
71 const std::vector<InterfaceField> & data_fields,
72 const std::vector<InterfacePseudoMap> & pseudo_maps,
73 const std::vector<InterfaceMessage> & messages)
75 this->dir = directory;
76 if (dir.find_last_of(
"/") != (dir.length() - 1)) {
79 this->author = author;
81 this->creation_date = creation_date;
82 this->data_comment = data_comment;
84 this->hash_size = hash_size;
85 this->constants = constants;
86 this->enum_constants = enum_constants;
87 this->data_fields = data_fields;
88 this->pseudo_maps = pseudo_maps;
89 this->messages = messages;
91 filename_tolua = config_basename +
".tolua";
92 filename_h = config_basename +
".h";
94 if (interface_name.find(
"Interface", 0) == string::npos) {
96 class_name = interface_name +
"Interface";
98 class_name = interface_name;
116 if (c_type ==
"uint8_t") {
117 return "unsigned char";
118 }
else if (c_type ==
"uint16_t") {
119 return "unsigned short";
120 }
else if (c_type ==
"uint32_t") {
121 return "unsigned int";
122 }
else if (c_type ==
"uint64_t") {
123#if __WORDSIZE == 64 || defined(__x86_64__)
124 return "unsigned long";
126 return "unsigned long long";
128 }
else if (c_type ==
"int8_t") {
130 }
else if (c_type ==
"int16_t") {
132 }
else if (c_type ==
"int32_t") {
134 }
else if (c_type ==
"int64_t") {
135#if __WORDSIZE == 64 || defined(__x86_64__)
140 }
else if (c_type ==
"uint8_t *") {
141 return "unsigned char *";
142 }
else if (c_type ==
"uint16_t *") {
143 return "unsigned short *";
144 }
else if (c_type ==
"uint32_t *") {
145 return "unsigned int *";
146 }
else if (c_type ==
"uint64_t *") {
147#if __WORDSIZE == 64 || defined(__x86_64__)
148 return "unsigned long *";
150 return "unsigned long long *";
152 }
else if (c_type ==
"int8_t *") {
154 }
else if (c_type ==
"int16_t *") {
156 }
else if (c_type ==
"int32_t *") {
158 }
else if (c_type ==
"int64_t *") {
159#if __WORDSIZE == 64 || defined(__x86_64__)
162 return "long long *";
165 return c_type.c_str();
176 fprintf(f,
"\n/***************************************************************************\n");
178 " * %s - Fawkes BlackBoard Interface - %s - tolua++ wrapper\n",
182 if (creation_date.length() > 0) {
183 fprintf(f,
" * Interface created: %s\n", creation_date.c_str());
185 fprintf(f,
" * Templated created: Thu Oct 12 10:49:19 2006\n");
187 " * Copyright %s %s\n",
189 ((author.length() > 0) ? author.c_str() :
"AllemaniACs RoboCup Team"));
191 fprintf(f,
" ****************************************************************************/\n\n");
193 fprintf(f,
" * This program is free software; you can redistribute it and/or modify\n");
194 fprintf(f,
" * it under the terms of the GNU General Public License as published by\n");
195 fprintf(f,
" * the Free Software Foundation; either version 2 of the License, or\n");
196 fprintf(f,
" * (at your option) any later version.\n");
198 fprintf(f,
" * This program is distributed in the hope that it will be useful,\n");
199 fprintf(f,
" * but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
200 fprintf(f,
" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
201 fprintf(f,
" * GNU Library General Public License for more details.\n");
203 fprintf(f,
" * You should have received a copy of the GNU General Public License\n");
204 fprintf(f,
" * along with this program; if not, write to the Free Software Foundation,\n");
205 fprintf(f,
" * Inc., 51 Franklin Street, Fifth floor, Boston, MA 02111-1307, USA.\n");
206 fprintf(f,
" */\n\n");
215 for (vector<InterfaceConstant>::iterator i = constants.begin(); i != constants.end(); ++i) {
216 fprintf(f,
" static const %s %s;\n",
convert_type(i->getType()), i->getName().c_str());
220 for (vector<InterfaceEnumConstant>::iterator i = enum_constants.begin();
221 i != enum_constants.end();
223 fprintf(f,
" typedef enum {\n");
224 vector<InterfaceEnumConstant::EnumItem> items = (*i).get_items();
225 vector<InterfaceEnumConstant::EnumItem>::iterator j = items.begin();
226 while (j != items.end()) {
227 if (j->has_custom_value) {
228 fprintf(f,
" %s = %i", j->name.c_str(), j->custom_value);
230 fprintf(f,
" %s", j->name.c_str());
233 if (j != items.end()) {
239 fprintf(f,
" } %s;\n\n", (*i).get_name().c_str());
249 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
251 " class %s : public Message\n"
253 (*i).getName().c_str());
258 fprintf(f,
" };\n\n");
270 std::string classname)
290 std::string classname,
291 std::vector<InterfaceField> fields)
293 vector<InterfaceField>::iterator i;
295 if (fields.size() > 0) {
296 fprintf(f,
"%s%s(", is.c_str(), classname.c_str());
299 while (i != fields.end()) {
300 fprintf(f,
"%s ini_%s",
convert_type(i->getAccessType()), i->getName().c_str());
302 if (i != fields.end()) {
320 " bool oftype(const char *interface_type) const;\n"
321 " const void * datachunk() const;\n"
322 " unsigned int datasize() const;\n"
323 " const char * type() const;\n"
324 " const char * id() const;\n"
325 " const char * uid() const;\n"
326 " fawkes::Uuid serial() const;\n"
327 " unsigned int mem_serial() const;\n"
328 " bool operator== (Interface &comp) const;\n"
329 " const unsigned char * hash() const;\n"
330 " int hash_size() const;\n"
331 " const char * hash_printable() const;\n"
332 " bool is_writer() const;\n"
334 " void set_from_chunk(void *chunk);\n"
336 " virtual fawkes::Message * create_message @ create_message_generic(const char *type) "
342 " bool has_writer() const;\n"
343 " unsigned int num_readers() const;\n"
345 " bool changed() const;\n"
346 " const fawkes::Time * timestamp() const;\n"
347 " void set_auto_timestamping(bool enabled);\n"
348 " void set_timestamp(const fawkes::Time *t);\n"
349 " void set_clock(fawkes::Clock *clock);\n"
351 " unsigned int msgq_enqueue_copy(Message *message);\n"
352 " void msgq_remove(Message *message);\n"
353 " void msgq_remove(unsigned int message_id);\n"
354 " unsigned int msgq_size();\n"
355 " void msgq_flush();\n"
356 " void msgq_lock();\n"
357 " bool msgq_try_lock();\n"
358 " void msgq_unlock();\n"
359 " void msgq_pop();\n"
360 " fawkes::Message * msgq_first @ msgq_first_generic();\n"
361 " bool msgq_empty();\n"
372 " unsigned int id() const;\n"
374 " fawkes::Uuid sender_id() const;\n"
375 " fawkes::Uuid source_id() const;\n"
376 " const char * sender_thread_name() const;\n"
377 " Interface * interface() const;\n"
378 " const char * type() const;\n"
380 " const void * datachunk() const;\n"
381 " unsigned int datasize() const;\n"
383 " void set_from_chunk(const void *chunk);\n"
385 " /* from RefCount */\n"
388 " unsigned int refcount();\n"
402 "assert(fawkes.Interface.msgq_first)\n"
403 "assert(fawkes.Interface.msgq_enqueue)\n"
404 "assert(fawkes.Interface.create_message)\n\n"
405 "fawkes.%s.msgq_first = fawkes.Interface.msgq_first\n"
406 "fawkes.%s.msgq_enqueue = fawkes.Interface.msgq_enqueue\n"
407 "fawkes.%s.create_message = fawkes.Interface.create_message\n"
422 std::vector<InterfaceField> fields)
424 for (vector<InterfaceField>::iterator i = fields.begin(); i != fields.end(); ++i) {
425 if ((i->getLengthValue() > 0) && (i->getType() !=
"string")) {
427 "%s%s %s%s(int index);\n",
429 (i->getType() ==
"byte") ?
"unsigned int" :
convert_type(i->getPlainAccessType()),
430 (((*i).getType() ==
"bool") ?
"is_" :
""),
431 (*i).getName().c_str());
434 "%svoid set_%s(unsigned int index, const %s new_%s);\n",
436 (*i).getName().c_str(),
438 i->getName().c_str());
444 (((*i).getType() ==
"bool") ?
"is_" :
""),
445 (*i).getName().c_str());
448 "%svoid set_%s(const %s new_%s);\n",
450 (*i).getName().c_str(),
452 i->getName().c_str());
454 fprintf(f,
"%sint maxlenof_%s() const;\n", is.c_str(), (*i).getName().c_str());
467 std::vector<InterfaceField> fields,
468 std::vector<InterfacePseudoMap> pseudo_maps)
472 for (vector<InterfacePseudoMap>::iterator i = pseudo_maps.begin(); i != pseudo_maps.end(); ++i) {
474 "%s%s %s(%s key) const;\n"
475 "%svoid set_%s(const %s key, const %s new_value);\n",
478 (*i).getName().c_str(),
481 (*i).getName().c_str(),
494 "$#include <interfaces/%s>\n"
495 "$#include <utils/time/time.h>\n"
496 "$#include <utils/time/clock.h>\n"
497 "$using namespace fawkes;\n"
498 "namespace fawkes {\n"
499 "class %s : public Interface\n"
509 fprintf(f,
"\n};\n\n");
520 struct tm timestruct;
521 time_t t = time(NULL);
522 localtime_r(&t, ×truct);
523 asctime_r(×truct, timestring);
524 gendate = timestring;
528 toluaf = fopen(
string(dir + filename_tolua).c_str(),
"w");
530 if (toluaf == NULL) {
531 printf(
"Cannot open tolua file %s%s\n", dir.c_str(), filename_tolua.c_str());
void write_methods_h(FILE *f, std::string is, std::vector< InterfaceField > fields)
Write methods to h file.
void write_lua_code(FILE *f, std::string classname)
Write additional Lua code to file.
void write_superclass_h(FILE *f)
Write superclass methods.
void write_messages_h(FILE *f)
Write messages to h file.
ToLuaInterfaceGenerator(std::string directory, std::string interface_name, std::string config_basename, std::string author, std::string year, std::string creation_date, std::string data_comment, const unsigned char *hash, size_t hash_size, const std::vector< InterfaceConstant > &constants, const std::vector< InterfaceEnumConstant > &enum_constants, const std::vector< InterfaceField > &data_fields, const std::vector< InterfacePseudoMap > &pseudo_maps, const std::vector< InterfaceMessage > &messages)
Constructor.
void write_header(FILE *f, std::string filename)
Write header to file.
~ToLuaInterfaceGenerator()
Destructor.
void write_message_superclass_h(FILE *f)
Write superclass methods.
void write_ctor_dtor_h(FILE *f, std::string is, std::string classname)
Write constructor and destructor to h file.
const char * convert_type(std::string c_type)
Convert C type to Lua type.
void write_message_ctor_dtor_h(FILE *f, std::string is, std::string classname, std::vector< InterfaceField > fields)
Write constructor and destructor for message to h file.
void write_constants_h(FILE *f)
Write constants to h file.
void write_toluaf(FILE *f)
Write h file.
void generate()
Generator cpp and h files.