23 #include "cpp_generator.h"
24 #include "exceptions.h"
26 #include <utils/misc/string_conversions.h>
59 std::string config_basename, std::string author,
60 std::string year, std::string creation_date,
61 std::string data_comment,
62 const unsigned char *hash,
size_t hash_size,
63 const std::vector<InterfaceConstant> &constants,
64 const std::vector<InterfaceEnumConstant> &enum_constants,
65 const std::vector<InterfaceField> &data_fields,
66 const std::vector<InterfacePseudoMap> &pseudo_maps,
67 const std::vector<InterfaceMessage> &messages
70 this->dir = directory;
71 if ( dir.find_last_of(
"/") != (dir.length() - 1) ) {
74 this->author = author;
76 this->creation_date = creation_date;
77 this->data_comment = data_comment;
79 this->hash_size = hash_size;
80 this->constants = constants;
81 this->enum_constants = enum_constants;
82 this->data_fields = data_fields;
83 this->pseudo_maps = pseudo_maps;
84 this->messages = messages;
86 filename_cpp = config_basename +
".cpp";
87 filename_h = config_basename +
".h";
88 filename_o = config_basename +
".o";
90 if ( interface_name.find(
"Interface", 0) == string::npos ) {
92 class_name = interface_name +
"Interface";
94 class_name = interface_name;
126 std::vector<InterfaceField> fields)
132 "#pragma pack(push,4)\n"
133 "%s/** Internal data storage, do NOT modify! */\n"
134 "%stypedef struct {\n"
135 "%s int64_t timestamp_sec; /**< Interface Unix timestamp, seconds */\n"
136 "%s int64_t timestamp_usec; /**< Interface Unix timestamp, micro-seconds */\n", is.c_str(), is.c_str(), is.c_str(), is.c_str());
138 for (vector<InterfaceField>::iterator i = fields.begin(); i != fields.end(); ++i) {
139 fprintf(f,
"%s %s %s", is.c_str(), (*i).getStructType().c_str(), (*i).getName().c_str());
140 if ( (*i).getLength().length() > 0 ) {
141 fprintf(f,
"[%s]", (*i).getLength().c_str());
143 fprintf(f,
"; /**< %s */\n", (*i).getComment().c_str());
146 fprintf(f,
"%s} %s;\n"
147 "#pragma pack(pop)\n\n", is.c_str(), name.c_str());
159 "\n/***************************************************************************\n"
160 " * %s - Fawkes BlackBoard Interface - %s\n"
163 " * Templated created: Thu Oct 12 10:49:19 2006\n"
164 " * Copyright %s %s\n"
166 " ****************************************************************************/\n\n"
167 "/* This program is free software; you can redistribute it and/or modify\n"
168 " * it under the terms of the GNU General Public License as published by\n"
169 " * the Free Software Foundation; either version 2 of the License, or\n"
170 " * (at your option) any later version. A runtime exception applies to\n"
171 " * this software (see LICENSE.GPL_WRE file mentioned below for details).\n"
173 " * This program is distributed in the hope that it will be useful,\n"
174 " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
175 " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
176 " * GNU Library General Public License for more details.\n"
178 " * Read the full text in the LICENSE.GPL_WRE file in the doc directory.\n"
180 filename.c_str(), class_name.c_str(),
181 (creation_date.length() > 0 ) ?
" * Interface created: " :
"",
182 (creation_date.length() > 0 ) ? creation_date.c_str() :
"",
183 (creation_date.length() > 0 ) ?
"\n" :
"",
184 year.c_str(), (author.length() > 0) ? author.c_str() :
"AllemaniACs RoboCup Team"
195 fprintf(f,
"#ifndef %s\n", deflector.c_str());
196 fprintf(f,
"#define %s\n\n", deflector.c_str());
206 write_header(f, filename_cpp);
208 "#include <interfaces/%s>\n\n"
209 "#include <core/exceptions/software.h>\n\n"
210 "#include <cstring>\n"
211 "#include <cstdlib>\n\n"
212 "namespace fawkes {\n\n"
213 "/** @class %s <interfaces/%s>\n"
214 " * %s Fawkes BlackBoard Interface.\n"
216 " * @ingroup FawkesInterfaces\n"
218 filename_h.c_str(), class_name.c_str(), filename_h.c_str(),
219 class_name.c_str(), data_comment.c_str());
220 write_constants_cpp(f);
221 write_ctor_dtor_cpp(f, class_name,
"Interface",
"", data_fields, messages);
222 write_enum_constants_tostring_cpp(f);
223 write_methods_cpp(f, class_name, class_name, data_fields, pseudo_maps,
"");
224 write_basemethods_cpp(f);
225 write_messages_cpp(f);
227 write_management_funcs_cpp(f);
229 fprintf(f,
"\n} // end namespace fawkes\n");
240 "/// @cond INTERNALS\n"
241 "EXPORT_INTERFACE(%s)\n"
253 for ( vector<InterfaceConstant>::iterator i = constants.begin(); i != constants.end(); ++i) {
254 const char *type_suffix =
"";
255 if (i->getType() ==
"uint32_t") {
259 "/** %s constant */\n"
260 "const %s %s::%s = %s%s;\n",
261 (*i).getName().c_str(),
262 (*i).getType().c_str(),
263 class_name.c_str(), i->getName().c_str(),
264 i->getValue().c_str(), type_suffix);
276 for ( vector<InterfaceEnumConstant>::iterator i = enum_constants.begin(); i != enum_constants.end(); ++i) {
278 "/** Convert %s constant to string.\n"
279 " * @param value value to convert to string\n"
280 " * @return constant value as string.\n"
283 "%s::tostring_%s(%s value) const\n"
285 " switch (value) {\n",
286 i->get_name().c_str(), class_name.c_str(), i->get_name().c_str(),
287 i->get_name().c_str());
288 vector<InterfaceEnumConstant::EnumItem> items = i->get_items();
289 vector<InterfaceEnumConstant::EnumItem>::iterator j;
290 for (j = items.begin(); j != items.end(); ++j) {
291 fprintf(f,
" case %s: return \"%s\";\n",
292 j->name.c_str(), j->name.c_str());
295 " default: return \"UNKNOWN\";\n"
307 fprintf(f,
" /* constants */\n");
308 for ( vector<InterfaceConstant>::iterator i = constants.begin(); i != constants.end(); ++i) {
309 fprintf(f,
" static const %s %s;\n", (*i).getType().c_str(), (*i).getName().c_str());
313 for ( vector<InterfaceEnumConstant>::iterator i = enum_constants.begin(); i != enum_constants.end(); ++i) {
317 (*i).get_comment().c_str());
318 vector<InterfaceEnumConstant::EnumItem> items = i->get_items();
319 vector<InterfaceEnumConstant::EnumItem>::iterator j = items.begin();
320 while (j != items.end()) {
321 if (j->has_custom_value) {
322 fprintf(f,
" %s = %i /**< %s */", j->name.c_str(),
323 j->custom_value, j->comment.c_str());
325 fprintf(f,
" %s /**< %s */", j->name.c_str(), j->comment.c_str());
328 if ( j != items.end() ) {
334 fprintf(f,
" } %s;\n", (*i).get_name().c_str());
335 fprintf(f,
" const char * tostring_%s(%s value) const;\n\n",
336 i->get_name().c_str(), i->get_name().c_str());
347 fprintf(f,
" /* messages */\n");
348 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
349 fprintf(f,
" class %s : public Message\n"
350 " {\n", (*i).getName().c_str());
352 fprintf(f,
" private:\n");
353 write_struct(f, (*i).getName() +
"_data_t",
" ", (*i).getFields());
355 " %s_data_t *data;\n\n",
356 (*i).getName().c_str());
358 fprintf(f,
" public:\n");
359 write_message_ctor_dtor_h(f,
" ", (*i).getName(), (*i).getFields());
360 write_methods_h(f,
" ", (*i).getFields());
361 write_message_clone_method_h(f,
" ");
362 fprintf(f,
" };\n\n");
364 fprintf(f,
" virtual bool message_valid(const Message *message) const;\n");
375 fprintf(f,
"/* =========== messages =========== */\n");
376 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
378 "/** @class %s::%s <interfaces/%s>\n"
379 " * %s Fawkes BlackBoard Interface Message.\n"
382 class_name.c_str(), (*i).getName().c_str(), filename_h.c_str(),
383 (*i).getName().c_str(), (*i).getComment().c_str());
385 write_message_ctor_dtor_cpp(f, (*i).getName(),
"Message", class_name +
"::",
387 write_methods_cpp(f, class_name, (*i).getName(), (*i).getFields(), class_name +
"::",
false);
388 write_message_clone_method_cpp(f, (class_name +
"::" + (*i).getName()).c_str());
391 "/** Check if message is valid and can be enqueued.\n"
392 " * @param message Message to check\n"
393 " * @return true if the message is valid, false otherwise.\n"
396 "%s::message_valid(const Message *message) const\n"
397 "{\n", class_name.c_str());
399 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
401 " const %s *m%u = dynamic_cast<const %s *>(message);\n"
402 " if ( m%u != NULL ) {\n"
405 (*i).getName().c_str(), n, (*i).getName().c_str(), n);
420 fprintf(f,
"/* =========== message create =========== */\n");
423 "%s::create_message(const char *type) const\n"
424 "{\n", class_name.c_str());
427 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
429 " %sif ( strncmp(\"%s\", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {\n"
430 " return new %s();\n",
431 first ?
"" :
"} else ", i->getName().c_str(), i->getName().c_str());
436 " throw UnknownTypeException(\"The given type '%%s' does not match any known \"\n"
437 " \"message type for this interface type.\", type);\n"
442 " throw UnknownTypeException(\"The given type '%%s' does not match any known \"\n"
443 " \"message type for this interface type.\", type);\n"
457 "/** Copy values from other interface.\n"
458 " * @param other other interface to copy values from\n"
461 "%s::copy_values(const Interface *other)\n"
463 " const %s *oi = dynamic_cast<const %s *>(other);\n"
464 " if (oi == NULL) {\n"
465 " throw TypeMismatchException(\"Can only copy values from interface of same type (%%s vs. %%s)\",\n"
466 " type(), other->type());\n"
468 " memcpy(data, oi->data, sizeof(%s_data_t));\n"
470 class_name.c_str(), class_name.c_str(), class_name.c_str(), class_name.c_str());
482 "%s::enum_tostring(const char *enumtype, int val) const\n"
483 "{\n", class_name.c_str());
484 for ( vector<InterfaceEnumConstant>::iterator i = enum_constants.begin(); i != enum_constants.end(); ++i) {
486 " if (strcmp(enumtype, \"%s\") == 0) {\n"
487 " return tostring_%s((%s)val);\n"
489 i->get_name().c_str(), i->get_name().c_str(), i->get_name().c_str());
492 " throw UnknownTypeException(\"Unknown enum type %%s\", enumtype);\n"
503 write_create_message_method_cpp(f);
504 write_copy_value_method_cpp(f);
505 write_enum_tostring_method_cpp(f);
516 std::string classname)
521 is.c_str(), classname.c_str(),
522 is.c_str(), classname.c_str());
534 std::string classname,
535 std::vector<InterfaceField> fields)
537 vector<InterfaceField>::iterator i;
539 if ( fields.size() > 0 ) {
541 fprintf(f,
"%s%s(", is.c_str(), classname.c_str());
544 while (i != fields.end()) {
545 fprintf(f,
"const %s ini_%s",
546 (*i).getAccessType().c_str(), (*i).getName().c_str());
548 if ( i != fields.end() ) {
556 write_ctor_dtor_h(f, is, classname);
557 fprintf(f,
"%s%s(const %s *m);\n", is.c_str(), classname.c_str(), classname.c_str());
569 fprintf(f,
"%svirtual Message * clone() const;\n", is.c_str());
581 "/** Clone this message.\n"
582 " * Produces a message of the same type as this message and copies the\n"
583 " * data to the new message.\n"
584 " * @return clone of this message\n"
587 "%s::clone() const\n"
589 " return new %s(this);\n"
590 "}\n", classname.c_str(), classname.c_str());
600 std::vector<InterfaceField>::iterator i;
601 for (i = fields.begin(); i != fields.end(); ++i) {
602 const char *type =
"";
603 const char *dataptr =
"&";
604 const char *enumtype = 0;
606 if ( i->getType() ==
"bool" ) {
608 }
else if ( i->getType() ==
"int8" ) {
610 }
else if ( i->getType() ==
"uint8" ) {
612 }
else if ( i->getType() ==
"int16" ) {
614 }
else if ( i->getType() ==
"uint16" ) {
616 }
else if ( i->getType() ==
"int32" ) {
618 }
else if ( i->getType() ==
"uint32" ) {
620 }
else if ( i->getType() ==
"int64" ) {
622 }
else if ( i->getType() ==
"uint64" ) {
624 }
else if ( i->getType() ==
"byte" ) {
626 }
else if ( i->getType() ==
"float" ) {
628 }
else if ( i->getType() ==
"double" ) {
630 }
else if ( i->getType() ==
"string" ) {
635 enumtype = i->getType().c_str();
638 fprintf(f,
" add_fieldinfo(IFT_%s, \"%s\", %u, %sdata->%s%s%s%s);\n",
639 type, i->getName().c_str(),
640 (i->getLengthValue() > 0) ? i->getLengthValue() : 1,
641 dataptr, i->getName().c_str(),
642 enumtype ?
", \"" :
"",
643 enumtype ? enumtype :
"",
660 std::string classname, std::string super_class,
661 std::string inclusion_prefix,
662 std::vector<InterfaceField> fields,
663 std::vector<InterfaceMessage> messages)
666 "/** Constructor */\n"
667 "%s%s::%s() : %s()\n"
669 inclusion_prefix.c_str(), classname.c_str(),
670 classname.c_str(), super_class.c_str());
673 " data_size = sizeof(%s_data_t);\n"
674 " data_ptr = malloc(data_size);\n"
675 " data = (%s_data_t *)data_ptr;\n"
676 " data_ts = (interface_data_ts_t *)data_ptr;\n"
677 " memset(data_ptr, 0, data_size);\n",
678 classname.c_str(), classname.c_str());
680 write_add_fieldinfo_calls(f, fields);
682 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
683 fprintf(f,
" add_messageinfo(\"%s\");\n", i->getName().c_str());
686 fprintf(f,
" unsigned char tmp_hash[] = {");
687 for (
size_t st = 0; st < hash_size-1; ++st) {
688 fprintf(f,
"%#02x, ", hash[st]);
690 fprintf(f,
"%#02x};\n", hash[hash_size-1]);
691 fprintf(f,
" set_hash(tmp_hash);\n");
695 "/** Destructor */\n"
700 inclusion_prefix.c_str(), classname.c_str(), classname.c_str()
714 std::string classname, std::string super_class,
715 std::string inclusion_prefix,
716 std::vector<InterfaceField> fields)
718 vector<InterfaceField>::iterator i;
720 if ( fields.size() > 0 ) {
722 "/** Constructor with initial values.\n");
724 for (i = fields.begin(); i != fields.end(); ++i) {
725 fprintf(f,
" * @param ini_%s initial value for %s\n",
726 (*i).getName().c_str(), (*i).getName().c_str());
732 inclusion_prefix.c_str(), classname.c_str(), classname.c_str());
735 while (i != fields.end()) {
736 fprintf(f,
"const %s ini_%s",
737 (*i).getAccessType().c_str(), (*i).getName().c_str());
739 if ( i != fields.end() ) {
744 fprintf(f,
") : %s(\"%s\")\n"
746 " data_size = sizeof(%s_data_t);\n"
747 " data_ptr = malloc(data_size);\n"
748 " memset(data_ptr, 0, data_size);\n"
749 " data = (%s_data_t *)data_ptr;\n"
750 " data_ts = (message_data_ts_t *)data_ptr;\n",
751 super_class.c_str(), classname.c_str(), classname.c_str(), classname.c_str());
753 for (i = fields.begin(); i != fields.end(); ++i) {
754 if ( (*i).getType() ==
"string" ) {
755 fprintf(f,
" strncpy(data->%s, ini_%s, %s);\n",
756 (*i).getName().c_str(), (*i).getName().c_str(),
757 (*i).getLength().c_str());
758 }
else if (i->getLengthValue() > 1) {
759 fprintf(f,
" memcpy(data->%s, ini_%s, sizeof(%s) * %s);\n",
760 i->getName().c_str(), i->getName().c_str(),
761 i->getPlainAccessType().c_str(), i->getLength().c_str());
765 fprintf(f,
" data->%s = ini_%s;\n",
766 (*i).getName().c_str(), (*i).getName().c_str());
770 write_add_fieldinfo_calls(f, fields);
776 "/** Constructor */\n"
777 "%s%s::%s() : %s(\"%s\")\n"
779 inclusion_prefix.c_str(), classname.c_str(),
780 classname.c_str(), super_class.c_str(), classname.c_str());
783 " data_size = sizeof(%s_data_t);\n"
784 " data_ptr = malloc(data_size);\n"
785 " memset(data_ptr, 0, data_size);\n"
786 " data = (%s_data_t *)data_ptr;\n"
787 " data_ts = (message_data_ts_t *)data_ptr;\n",
788 classname.c_str(), classname.c_str());
790 write_add_fieldinfo_calls(f, fields);
794 "/** Destructor */\n"
799 inclusion_prefix.c_str(), classname.c_str(), classname.c_str());
802 "/** Copy constructor.\n"
803 " * @param m message to copy from\n"
805 "%s%s::%s(const %s *m) : %s(\"%s\")\n"
807 inclusion_prefix.c_str(), classname.c_str(), classname.c_str(),
808 classname.c_str(), super_class.c_str(), classname.c_str());
811 " data_size = m->data_size;\n"
812 " data_ptr = malloc(data_size);\n"
813 " memcpy(data_ptr, m->data_ptr, data_size);\n"
814 " data = (%s_data_t *)data_ptr;\n"
815 " data_ts = (message_data_ts_t *)data_ptr;\n",
835 std::string classname,
836 std::vector<InterfaceField> fields,
837 std::string inclusion_prefix,
838 bool write_data_changed)
840 fprintf(f,
"/* Methods */\n");
841 for (vector<InterfaceField>::iterator i = fields.begin(); i != fields.end(); ++i) {
843 "/** Get %s value.\n"
845 " * @return %s value\n"
848 "%s%s::%s%s() const\n"
850 " return %sdata->%s;\n"
852 (*i).getName().c_str(),
853 (*i).getComment().c_str(),
854 (*i).getName().c_str(),
855 (*i).isEnumType() ? (interface_classname +
"::").c_str() :
"",
856 (*i).getAccessType().c_str(),
857 inclusion_prefix.c_str(), classname.c_str(), ( ((*i).getType() ==
"bool" ) ?
"is_" :
""), (*i).getName().c_str(),
858 (*i).isEnumType() ? (std::string(
"(") + interface_classname +
"::" +
859 i->getAccessType() +
")").c_str() :
"",
860 (*i).getName().c_str() );
862 if ( (i->getLengthValue() > 0) && (i->getType() !=
"string") ) {
864 "/** Get %s value at given index.\n"
866 " * @param index index of value\n"
867 " * @return %s value\n"
868 " * @exception Exception thrown if index is out of bounds\n"
871 "%s%s::%s%s(unsigned int index) const\n"
873 " if (index > %s) {\n"
874 " throw Exception(\"Index value %%u out of bounds (0..%s)\", index);\n"
876 " return %sdata->%s[index];\n"
878 (*i).getName().c_str(),
879 (*i).getComment().c_str(),
880 (*i).getName().c_str(),
881 (*i).isEnumType() ? (interface_classname +
"::").c_str() :
"",
882 (*i).getPlainAccessType().c_str(),
883 inclusion_prefix.c_str(), classname.c_str(),
884 ( ((*i).getType() ==
"bool" ) ?
"is_" :
""), (*i).getName().c_str(),
885 i->getLength().c_str(), i->getLength().c_str(),
886 (*i).isEnumType() ? (std::string(
"(") + interface_classname +
"::" +
887 i->getPlainAccessType() +
")").c_str() :
"",
888 (*i).getName().c_str() );
892 "/** Get maximum length of %s value.\n"
893 " * @return length of %s value, can be length of the array or number of \n"
894 " * maximum number of characters for a string\n"
897 "%s%s::maxlenof_%s() const\n"
901 i->getName().c_str(), i->getName().c_str(), inclusion_prefix.c_str(),
902 classname.c_str(), i->getName().c_str(),
903 i->getLengthValue() > 0 ? i->getLength().c_str() :
"1" );
906 "/** Set %s value.\n"
908 " * @param new_%s new %s value\n"
911 "%s%s::set_%s(const %s new_%s)\n"
913 (*i).getName().c_str(),
914 (*i).getComment().c_str(),
915 (*i).getName().c_str(), (*i).getName().c_str(),
916 inclusion_prefix.c_str(), classname.c_str(), (*i).getName().c_str(), (*i).getAccessType().c_str(), (*i).getName().c_str()
918 if ( (*i).getType() ==
"string" ) {
920 " strncpy(data->%s, new_%s, sizeof(data->%s));\n",
921 (*i).getName().c_str(), (*i).getName().c_str(), (*i).getName().c_str());
922 }
else if ( (*i).getLength() !=
"" ) {
924 " memcpy(data->%s, new_%s, sizeof(%s) * %s);\n",
925 (*i).getName().c_str(), (*i).getName().c_str(),
926 (*i).getPlainAccessType().c_str(), (*i).getLength().c_str());
929 " data->%s = new_%s;\n",
930 (*i).getName().c_str(), (*i).getName().c_str());
932 fprintf(f,
"%s}\n\n", write_data_changed ?
" data_changed = true;\n" :
"");
934 if ( ((*i).getType() !=
"string") && ((*i).getLengthValue() > 0) ) {
936 "/** Set %s value at given index.\n"
938 " * @param new_%s new %s value\n"
939 " * @param index index for of the value\n"
942 "%s%s::set_%s(unsigned int index, const %s new_%s)\n"
944 " if (index > %s) {\n"
945 " throw Exception(\"Index value %%u out of bounds (0..%s)\", index);\n"
947 " data->%s[index] = new_%s;\n"
950 (*i).getName().c_str(),
951 (*i).getComment().c_str(),
952 (*i).getName().c_str(), (*i).getName().c_str(),
953 inclusion_prefix.c_str(), classname.c_str(), (*i).getName().c_str(),
954 (*i).getPlainAccessType().c_str(), i->getName().c_str(),
955 i->getLength().c_str(), i->getLength().c_str(),
956 i->getName().c_str(), i->getName().c_str(),
957 write_data_changed ?
" data_changed = true;\n" :
"");
973 std::string classname,
974 std::vector<InterfaceField> fields,
975 std::vector<InterfacePseudoMap> pseudo_maps,
976 std::string inclusion_prefix)
978 write_methods_cpp(f, interface_classname, classname, fields,
979 inclusion_prefix,
true);
981 for (vector<InterfacePseudoMap>::iterator i = pseudo_maps.begin(); i != pseudo_maps.end(); ++i) {
983 "/** Get %s value.\n"
985 " * @param key key of the value\n"
986 " * @return %s value\n"
989 "%s%s::%s(const %s key) const\n"
991 (*i).getName().c_str(),
992 (*i).getComment().c_str(),
993 (*i).getName().c_str(),
994 (*i).getType().c_str(),
995 inclusion_prefix.c_str(), classname.c_str(), (*i).getName().c_str(),
996 (*i).getKeyType().c_str() );
999 InterfacePseudoMap::RefList::iterator paref;
1001 for (paref = reflist.begin(); paref != reflist.end(); ++paref) {
1002 fprintf(f,
" %sif (key == %s) {\n"
1003 " return data->%s;\n",
1004 first ?
"" :
"} else ",
1005 paref->second.c_str(), paref->first.c_str());
1008 fprintf(f,
" } else {\n"
1009 " throw Exception(\"Invalid key, cannot retrieve value\");\n"
1014 "/** Set %s value.\n"
1016 " * @param key key of the value\n"
1017 " * @param new_value new value\n"
1020 "%s%s::set_%s(const %s key, const %s new_value)\n"
1022 (*i).getName().c_str(),
1023 (*i).getComment().c_str(),
1024 inclusion_prefix.c_str(), classname.c_str(), (*i).getName().c_str(),
1025 (*i).getKeyType().c_str(), (*i).getType().c_str());
1028 for (paref = reflist.begin(); paref != reflist.end(); ++paref) {
1029 fprintf(f,
" %sif (key == %s) {\n"
1030 " data->%s = new_value;\n",
1031 first ?
"" :
"} else ",
1032 paref->second.c_str(), paref->first.c_str());
1050 std::vector<InterfaceField> fields)
1052 fprintf(f,
"%s/* Methods */\n", is.c_str());
1053 for (vector<InterfaceField>::iterator i = fields.begin(); i != fields.end(); ++i) {
1055 "%s%s %s%s() const;\n",
1056 is.c_str(), (*i).getAccessType().c_str(),
1057 ( ((*i).getType() ==
"bool" ) ?
"is_" :
""),
1058 (*i).getName().c_str());
1060 if ((i->getLengthValue() > 0) && (i->getType() !=
"string")) {
1062 "%s%s %s%s(unsigned int index) const;\n"
1063 "%svoid set_%s(unsigned int index, const %s new_%s);\n",
1064 is.c_str(), i->getPlainAccessType().c_str(),
1065 ( ((*i).getType() ==
"bool" ) ?
"is_" :
""),
1066 (*i).getName().c_str(),
1067 is.c_str(), (*i).getName().c_str(),
1068 i->getPlainAccessType().c_str(), i->getName().c_str());
1072 "%svoid set_%s(const %s new_%s);\n"
1073 "%ssize_t maxlenof_%s() const;\n",
1074 is.c_str(), (*i).getName().c_str(),
1075 i->getAccessType().c_str(), i->getName().c_str(),
1076 is.c_str(), i->getName().c_str()
1090 std::vector<InterfaceField> fields,
1091 std::vector<InterfacePseudoMap> pseudo_maps)
1093 write_methods_h(f, is, fields);
1095 for (vector<InterfacePseudoMap>::iterator i = pseudo_maps.begin(); i != pseudo_maps.end(); ++i) {
1097 "%s%s %s(%s key) const;\n"
1098 "%svoid set_%s(const %s key, const %s new_value);\n",
1099 is.c_str(), (*i).getType().c_str(),
1100 (*i).getName().c_str(), (*i).getKeyType().c_str(),
1101 is.c_str(), (*i).getName().c_str(),
1102 i->getKeyType().c_str(), i->getType().c_str());
1115 "%svirtual Message * create_message(const char *type) const;\n\n"
1116 "%svirtual void copy_values(const Interface *other);\n"
1117 "%svirtual const char * enum_tostring(const char *enumtype, int val) const;\n",
1118 is.c_str(), is.c_str(), is.c_str());
1127 write_header(f, filename_h);
1131 "#include <interface/interface.h>\n"
1132 "#include <interface/message.h>\n"
1133 "#include <interface/field_iterator.h>\n\n"
1134 "namespace fawkes {\n\n"
1135 "class %s : public Interface\n"
1137 " /// @cond INTERNALS\n"
1138 " INTERFACE_MGMT_FRIENDS(%s)\n"
1142 class_name.c_str());
1144 write_constants_h(f);
1146 fprintf(f,
" private:\n");
1148 write_struct(f, class_name +
"_data_t",
" ", data_fields);
1150 fprintf(f,
" %s_data_t *data;\n"
1151 "\n public:\n", class_name.c_str());
1153 write_messages_h(f);
1154 fprintf(f,
" private:\n");
1155 write_ctor_dtor_h(f,
" ", class_name);
1156 fprintf(f,
" public:\n");
1157 write_methods_h(f,
" ", data_fields, pseudo_maps);
1158 write_basemethods_h(f,
" ");
1159 fprintf(f,
"\n};\n\n} // end namespace fawkes\n\n#endif\n");
1168 char timestring[26];
1169 struct tm timestruct;
1170 time_t t = time(NULL);
1171 localtime_r(&t, ×truct);
1172 asctime_r(×truct, timestring);
1173 gendate = timestring;
1178 cpp = fopen(
string(dir + filename_cpp).c_str(),
"w");
1179 h = fopen(
string(dir + filename_h).c_str(),
"w");
1181 if ( cpp == NULL ) {
1182 printf(
"Cannot open cpp file %s%s\n", dir.c_str(), filename_cpp.c_str());
1185 printf(
"Cannot open h file %s%s\n", dir.c_str(), filename_h.c_str());