23 #include "cpp_generator.h"
25 #include "exceptions.h"
27 #include <utils/misc/string_conversions.h>
59 std::string directory,
60 std::string interface_name,
61 std::string config_basename,
64 std::string creation_date,
65 std::string data_comment,
66 const unsigned char * hash,
68 const std::vector<InterfaceConstant> & constants,
69 const std::vector<InterfaceEnumConstant> &enum_constants,
70 const std::vector<InterfaceField> & data_fields,
71 const std::vector<InterfacePseudoMap> & pseudo_maps,
72 const std::vector<InterfaceMessage> & messages)
74 this->dir = directory;
75 if (dir.find_last_of(
"/") != (dir.length() - 1)) {
78 this->author = author;
80 this->creation_date = creation_date;
81 this->data_comment = data_comment;
83 this->hash_size = hash_size;
84 this->constants = constants;
85 this->enum_constants = enum_constants;
86 this->data_fields = data_fields;
87 this->pseudo_maps = pseudo_maps;
88 this->messages = messages;
90 filename_cpp = config_basename +
".cpp";
91 filename_h = config_basename +
".h";
92 filename_o = config_basename +
".o";
94 if (interface_name.find(
"Interface", 0) == string::npos) {
96 class_name = interface_name +
"Interface";
98 class_name = interface_name;
129 std::vector<InterfaceField> fields)
134 "%s/** Internal data storage, do NOT modify! */\n"
135 "%stypedef struct {\n"
136 "%s int64_t timestamp_sec; /**< Interface Unix timestamp, seconds */\n"
137 "%s int64_t timestamp_usec; /**< Interface Unix timestamp, micro-seconds */\n",
143 for (vector<InterfaceField>::iterator i = fields.begin(); i != fields.end(); ++i) {
144 fprintf(f,
"%s %s %s", is.c_str(), (*i).getStructType().c_str(), (*i).getName().c_str());
145 if ((*i).getLength().length() > 0) {
146 fprintf(f,
"[%s]", (*i).getLength().c_str());
148 fprintf(f,
"; /**< %s */\n", (*i).getComment().c_str());
151 fprintf(f,
"%s} %s;\n\n", is.c_str(), name.c_str());
160 for (vector<InterfaceEnumConstant>::iterator i = enum_constants.begin();
161 i != enum_constants.end();
163 fprintf(f,
" interface_enum_map_t enum_map_%s;\n", i->get_name().c_str());
175 "\n/***************************************************************************\n"
176 " * %s - Fawkes BlackBoard Interface - %s\n"
179 " * Templated created: Thu Oct 12 10:49:19 2006\n"
180 " * Copyright %s %s\n"
182 " ****************************************************************************/\n\n"
183 "/* This program is free software; you can redistribute it and/or modify\n"
184 " * it under the terms of the GNU General Public License as published by\n"
185 " * the Free Software Foundation; either version 2 of the License, or\n"
186 " * (at your option) any later version. A runtime exception applies to\n"
187 " * this software (see LICENSE.GPL_WRE file mentioned below for details).\n"
189 " * This program is distributed in the hope that it will be useful,\n"
190 " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
191 " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
192 " * GNU Library General Public License for more details.\n"
194 " * Read the full text in the LICENSE.GPL_WRE file in the doc directory.\n"
198 (creation_date.length() > 0) ?
" * Interface created: " :
"",
199 (creation_date.length() > 0) ? creation_date.c_str() :
"",
200 (creation_date.length() > 0) ?
"\n" :
"",
202 (author.length() > 0) ? author.c_str() :
"AllemaniACs RoboCup Team");
211 fprintf(f,
"#ifndef %s\n", deflector.c_str());
212 fprintf(f,
"#define %s\n\n", deflector.c_str());
221 write_header(f, filename_cpp);
223 "#include <interfaces/%s>\n\n"
224 "#include <core/exceptions/software.h>\n\n"
226 "#include <string>\n"
227 "#include <cstring>\n"
228 "#include <cstdlib>\n\n"
229 "namespace fawkes {\n\n"
230 "/** @class %s <interfaces/%s>\n"
231 " * %s Fawkes BlackBoard Interface.\n"
233 " * @ingroup FawkesInterfaces\n"
239 data_comment.c_str());
240 write_constants_cpp(f);
241 write_ctor_dtor_cpp(f, class_name,
"Interface",
"", data_fields, messages);
242 write_enum_constants_tostring_cpp(f);
243 write_methods_cpp(f, class_name, class_name, data_fields, pseudo_maps,
"");
244 write_basemethods_cpp(f);
245 write_messages_cpp(f);
247 write_management_funcs_cpp(f);
249 fprintf(f,
"\n} // end namespace fawkes\n");
259 "/// @cond INTERNALS\n"
260 "EXPORT_INTERFACE(%s)\n"
271 for (vector<InterfaceConstant>::iterator i = constants.begin(); i != constants.end(); ++i) {
272 const char *type_suffix =
"";
273 if (i->getType() ==
"uint32_t") {
277 "/** %s constant */\n"
278 "const %s %s::%s = %s%s;\n",
279 (*i).getName().c_str(),
280 (*i).getType().c_str(),
282 i->getName().c_str(),
283 i->getValue().c_str(),
295 for (vector<InterfaceEnumConstant>::iterator i = enum_constants.begin();
296 i != enum_constants.end();
299 "/** Convert %s constant to string.\n"
300 " * @param value value to convert to string\n"
301 " * @return constant value as string.\n"
304 "%s::tostring_%s(%s value) const\n"
306 " switch (value) {\n",
307 i->get_name().c_str(),
309 i->get_name().c_str(),
310 i->get_name().c_str());
311 vector<InterfaceEnumConstant::EnumItem> items = i->get_items();
312 vector<InterfaceEnumConstant::EnumItem>::iterator j;
313 for (j = items.begin(); j != items.end(); ++j) {
314 fprintf(f,
" case %s: return \"%s\";\n", j->name.c_str(), j->name.c_str());
317 " default: return \"UNKNOWN\";\n"
329 fprintf(f,
" /* constants */\n");
330 for (vector<InterfaceConstant>::iterator i = constants.begin(); i != constants.end(); ++i) {
331 fprintf(f,
" static const %s %s;\n", (*i).getType().c_str(), (*i).getName().c_str());
335 for (vector<InterfaceEnumConstant>::iterator i = enum_constants.begin();
336 i != enum_constants.end();
341 (*i).get_comment().c_str());
342 vector<InterfaceEnumConstant::EnumItem> items = i->get_items();
343 vector<InterfaceEnumConstant::EnumItem>::iterator j = items.begin();
344 while (j != items.end()) {
345 if (j->has_custom_value) {
346 fprintf(f,
" %s = %i /**< %s */", j->name.c_str(), j->custom_value, j->comment.c_str());
348 fprintf(f,
" %s /**< %s */", j->name.c_str(), j->comment.c_str());
351 if (j != items.end()) {
357 fprintf(f,
" } %s;\n", (*i).get_name().c_str());
359 " const char * tostring_%s(%s value) const;\n\n",
360 i->get_name().c_str(),
361 i->get_name().c_str());
371 fprintf(f,
" /* messages */\n");
372 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
374 " class %s : public Message\n"
376 (*i).getName().c_str());
378 fprintf(f,
" private:\n");
379 write_struct(f, (*i).getName() +
"_data_t",
" ", (*i).getFields());
380 fprintf(f,
" %s_data_t *data;\n\n", (*i).getName().c_str());
382 write_enum_maps_h(f);
384 fprintf(f,
" public:\n");
385 write_message_ctor_dtor_h(f,
" ", (*i).getName(), (*i).getFields());
386 write_methods_h(f,
" ", (*i).getFields());
387 write_message_clone_method_h(f,
" ");
388 fprintf(f,
" };\n\n");
390 fprintf(f,
" virtual bool message_valid(const Message *message) const;\n");
399 fprintf(f,
"/* =========== messages =========== */\n");
400 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
402 "/** @class %s::%s <interfaces/%s>\n"
403 " * %s Fawkes BlackBoard Interface Message.\n"
407 (*i).getName().c_str(),
409 (*i).getName().c_str(),
410 (*i).getComment().c_str());
412 write_message_ctor_dtor_cpp(f, (*i).getName(),
"Message", class_name +
"::", (*i).getFields());
413 write_methods_cpp(f, class_name, (*i).getName(), (*i).getFields(), class_name +
"::",
false);
414 write_message_clone_method_cpp(f, (class_name +
"::" + (*i).getName()).c_str());
417 "/** Check if message is valid and can be enqueued.\n"
418 " * @param message Message to check\n"
419 " * @return true if the message is valid, false otherwise.\n"
422 "%s::message_valid(const Message *message) const\n"
426 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
428 " const %s *m%u = dynamic_cast<const %s *>(message);\n"
429 " if ( m%u != NULL ) {\n"
432 (*i).getName().c_str(),
434 (*i).getName().c_str(),
449 fprintf(f,
"/* =========== message create =========== */\n");
452 "%s::create_message(const char *type) const\n"
457 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
459 " %sif ( strncmp(\"%s\", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {\n"
460 " return new %s();\n",
461 first ?
"" :
"} else ",
462 i->getName().c_str(),
463 i->getName().c_str());
468 " throw UnknownTypeException(\"The given type '%%s' does not match any known \"\n"
469 " \"message type for this interface type.\", type);\n"
474 " throw UnknownTypeException(\"The given type '%%s' does not match any known \"\n"
475 " \"message type for this interface type.\", type);\n"
488 "/** Copy values from other interface.\n"
489 " * @param other other interface to copy values from\n"
492 "%s::copy_values(const Interface *other)\n"
494 " const %s *oi = dynamic_cast<const %s *>(other);\n"
495 " if (oi == NULL) {\n"
496 " throw TypeMismatchException(\"Can only copy values from interface of same type (%%s "
498 " type(), other->type());\n"
500 " memcpy(data, oi->data, sizeof(%s_data_t));\n"
516 "%s::enum_tostring(const char *enumtype, int val) const\n"
519 for (vector<InterfaceEnumConstant>::iterator i = enum_constants.begin();
520 i != enum_constants.end();
523 " if (strcmp(enumtype, \"%s\") == 0) {\n"
524 " return tostring_%s((%s)val);\n"
526 i->get_name().c_str(),
527 i->get_name().c_str(),
528 i->get_name().c_str());
531 " throw UnknownTypeException(\"Unknown enum type %%s\", enumtype);\n"
541 write_create_message_method_cpp(f);
542 write_copy_value_method_cpp(f);
543 write_enum_tostring_method_cpp(f);
554 std::string classname)
574 std::string classname,
575 std::vector<InterfaceField> fields)
577 vector<InterfaceField>::iterator i;
579 if (fields.size() > 0) {
580 fprintf(f,
"%s%s(", is.c_str(), classname.c_str());
583 while (i != fields.end()) {
584 fprintf(f,
"const %s ini_%s", (*i).getAccessType().c_str(), (*i).getName().c_str());
586 if (i != fields.end()) {
594 write_ctor_dtor_h(f, is, classname);
595 fprintf(f,
"%sexplicit %s(const %s *m);\n", is.c_str(), classname.c_str(), classname.c_str());
605 fprintf(f,
"%svirtual Message * clone() const;\n", is.c_str());
616 "/** Clone this message.\n"
617 " * Produces a message of the same type as this message and copies the\n"
618 " * data to the new message.\n"
619 " * @return clone of this message\n"
622 "%s::clone() const\n"
624 " return new %s(this);\n"
636 for (vector<InterfaceEnumConstant>::iterator i = enum_constants.begin();
637 i != enum_constants.end();
639 const std::vector<InterfaceEnumConstant::EnumItem> &enum_values = i->get_items();
641 std::vector<InterfaceEnumConstant::EnumItem>::const_iterator ef;
642 for (ef = enum_values.begin(); ef != enum_values.end(); ++ef) {
644 " enum_map_%s[(int)%s] = \"%s\";\n",
645 i->get_name().c_str(),
659 std::vector<InterfaceField>::iterator i;
660 for (i = fields.begin(); i != fields.end(); ++i) {
661 const char *type =
"";
662 const char *dataptr =
"&";
663 std::string enumtype;
665 if (i->getType() ==
"bool") {
667 }
else if (i->getType() ==
"int8") {
669 }
else if (i->getType() ==
"uint8") {
671 }
else if (i->getType() ==
"int16") {
673 }
else if (i->getType() ==
"uint16") {
675 }
else if (i->getType() ==
"int32") {
677 }
else if (i->getType() ==
"uint32") {
679 }
else if (i->getType() ==
"int64") {
681 }
else if (i->getType() ==
"uint64") {
683 }
else if (i->getType() ==
"byte") {
685 }
else if (i->getType() ==
"float") {
687 }
else if (i->getType() ==
"double") {
689 }
else if (i->getType() ==
"string") {
694 enumtype = i->getType();
698 " add_fieldinfo(IFT_%s, \"%s\", %u, %sdata->%s%s%s%s%s%s%s);\n",
700 i->getName().c_str(),
701 (i->getLengthValue() > 0) ? i->getLengthValue() : 1,
703 i->getName().c_str(),
704 enumtype.empty() ?
"" :
", \"",
705 enumtype.empty() ?
"" : enumtype.c_str(),
706 enumtype.empty() ?
"" :
"\"",
707 enumtype.empty() ?
"" :
", ",
708 enumtype.empty() ?
"" :
"&enum_map_",
709 enumtype.empty() ?
"" : enumtype.c_str());
723 std::string classname,
724 std::string super_class,
725 std::string inclusion_prefix,
726 std::vector<InterfaceField> fields,
727 std::vector<InterfaceMessage> messages)
730 "/** Constructor */\n"
731 "%s%s::%s() : %s()\n"
733 inclusion_prefix.c_str(),
736 super_class.c_str());
739 " data_size = sizeof(%s_data_t);\n"
740 " data_ptr = malloc(data_size);\n"
741 " data = (%s_data_t *)data_ptr;\n"
742 " data_ts = (interface_data_ts_t *)data_ptr;\n"
743 " memset(data_ptr, 0, data_size);\n",
747 write_enum_map_population(f);
748 write_add_fieldinfo_calls(f, fields);
750 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
751 fprintf(f,
" add_messageinfo(\"%s\");\n", i->getName().c_str());
754 fprintf(f,
" unsigned char tmp_hash[] = {");
755 for (
size_t st = 0; st < hash_size - 1; ++st) {
756 fprintf(f,
"%#02x, ", hash[st]);
758 fprintf(f,
"%#02x};\n", hash[hash_size - 1]);
759 fprintf(f,
" set_hash(tmp_hash);\n");
763 "/** Destructor */\n"
768 inclusion_prefix.c_str(),
782 std::string classname,
783 std::string super_class,
784 std::string inclusion_prefix,
785 std::vector<InterfaceField> fields)
787 vector<InterfaceField>::iterator i;
789 if (fields.size() > 0) {
790 fprintf(f,
"/** Constructor with initial values.\n");
792 for (i = fields.begin(); i != fields.end(); ++i) {
794 " * @param ini_%s initial value for %s\n",
795 (*i).getName().c_str(),
796 (*i).getName().c_str());
802 inclusion_prefix.c_str(),
807 while (i != fields.end()) {
808 fprintf(f,
"const %s ini_%s", (*i).getAccessType().c_str(), (*i).getName().c_str());
810 if (i != fields.end()) {
818 " data_size = sizeof(%s_data_t);\n"
819 " data_ptr = malloc(data_size);\n"
820 " memset(data_ptr, 0, data_size);\n"
821 " data = (%s_data_t *)data_ptr;\n"
822 " data_ts = (message_data_ts_t *)data_ptr;\n",
828 for (i = fields.begin(); i != fields.end(); ++i) {
829 if ((*i).getType() ==
"string") {
831 " strncpy(data->%s, ini_%s, %s-1);\n"
832 " data->%s[%s-1] = 0;\n",
833 (*i).getName().c_str(),
834 (*i).getName().c_str(),
835 (*i).getLength().c_str(),
836 (*i).getName().c_str(),
837 (*i).getLength().c_str());
838 }
else if (i->getLengthValue() > 1) {
840 " memcpy(data->%s, ini_%s, sizeof(%s) * %s);\n",
841 i->getName().c_str(),
842 i->getName().c_str(),
843 i->getPlainAccessType().c_str(),
844 i->getLength().c_str());
847 fprintf(f,
" data->%s = ini_%s;\n", (*i).getName().c_str(), (*i).getName().c_str());
851 write_enum_map_population(f);
852 write_add_fieldinfo_calls(f, fields);
858 "/** Constructor */\n"
859 "%s%s::%s() : %s(\"%s\")\n"
861 inclusion_prefix.c_str(),
868 " data_size = sizeof(%s_data_t);\n"
869 " data_ptr = malloc(data_size);\n"
870 " memset(data_ptr, 0, data_size);\n"
871 " data = (%s_data_t *)data_ptr;\n"
872 " data_ts = (message_data_ts_t *)data_ptr;\n",
876 write_enum_map_population(f);
877 write_add_fieldinfo_calls(f, fields);
881 "/** Destructor */\n"
886 inclusion_prefix.c_str(),
891 "/** Copy constructor.\n"
892 " * @param m message to copy from\n"
894 "%s%s::%s(const %s *m) : %s(m)\n"
896 inclusion_prefix.c_str(),
900 super_class.c_str());
903 " data_size = m->data_size;\n"
904 " data_ptr = malloc(data_size);\n"
905 " memcpy(data_ptr, m->data_ptr, data_size);\n"
906 " data = (%s_data_t *)data_ptr;\n"
907 " data_ts = (message_data_ts_t *)data_ptr;\n",
925 std::string interface_classname,
926 std::string classname,
927 std::vector<InterfaceField> fields,
928 std::string inclusion_prefix,
929 bool write_data_changed)
931 fprintf(f,
"/* Methods */\n");
932 for (vector<InterfaceField>::iterator i = fields.begin(); i != fields.end(); ++i) {
934 "/** Get %s value.\n"
936 " * @return %s value\n"
939 "%s%s::%s%s() const\n"
941 " return %sdata->%s;\n"
943 (*i).getName().c_str(),
944 (*i).getComment().c_str(),
945 (*i).getName().c_str(),
946 (*i).isEnumType() ? (interface_classname +
"::").c_str() :
"",
947 (*i).getAccessType().c_str(),
948 inclusion_prefix.c_str(),
950 (((*i).getType() ==
"bool") ?
"is_" :
""),
951 (*i).getName().c_str(),
953 ? (std::string(
"(") + interface_classname +
"::" + i->getAccessType() +
")").c_str()
955 (*i).getName().c_str());
957 if ((i->getLengthValue() > 0) && (i->getType() !=
"string")) {
960 "/** Get %s value at given index.\n"
962 " * @param index index of value\n"
963 " * @return %s value\n"
964 " * @exception Exception thrown if index is out of bounds\n"
967 "%s%s::%s%s(unsigned int index) const\n"
969 " if (index > %s) {\n"
970 " throw Exception(\"Index value %%u out of bounds (0..%s)\", index);\n"
972 " return %sdata->%s[index];\n"
974 (*i).getName().c_str(),
975 (*i).getComment().c_str(),
976 (*i).getName().c_str(),
977 (*i).isEnumType() ? (interface_classname +
"::").c_str() :
"",
978 (*i).getPlainAccessType().c_str(),
979 inclusion_prefix.c_str(),
981 (((*i).getType() ==
"bool") ?
"is_" :
""),
982 (*i).getName().c_str(),
983 i->getMaxIdx().c_str(),
984 i->getMaxIdx().c_str(),
986 ? (std::string(
"(") + interface_classname +
"::" + i->getPlainAccessType() +
")").c_str()
988 (*i).getName().c_str());
992 "/** Get maximum length of %s value.\n"
993 " * @return length of %s value, can be length of the array or number of \n"
994 " * maximum number of characters for a string\n"
997 "%s%s::maxlenof_%s() const\n"
1001 i->getName().c_str(),
1002 i->getName().c_str(),
1003 inclusion_prefix.c_str(),
1005 i->getName().c_str(),
1006 i->getLengthValue() > 0 ? i->getLength().c_str() :
"1");
1009 "/** Set %s value.\n"
1011 " * @param new_%s new %s value\n"
1014 "%s%s::set_%s(const %s new_%s)\n"
1016 (*i).getName().c_str(),
1017 (*i).getComment().c_str(),
1018 (*i).getName().c_str(),
1019 (*i).getName().c_str(),
1020 inclusion_prefix.c_str(),
1022 (*i).getName().c_str(),
1023 (*i).getAccessType().c_str(),
1024 (*i).getName().c_str());
1025 if ((*i).getType() ==
"string") {
1027 " strncpy(data->%s, new_%s, sizeof(data->%s)-1);\n"
1028 " data->%s[sizeof(data->%s)-1] = 0;\n",
1029 (*i).getName().c_str(),
1030 (*i).getName().c_str(),
1031 (*i).getName().c_str(),
1032 (*i).getName().c_str(),
1033 (*i).getName().c_str());
1034 }
else if ((*i).getLength() !=
"") {
1036 " memcpy(data->%s, new_%s, sizeof(%s) * %s);\n",
1037 (*i).getName().c_str(),
1038 (*i).getName().c_str(),
1039 (*i).getPlainAccessType().c_str(),
1040 (*i).getLength().c_str());
1042 fprintf(f,
" data->%s = new_%s;\n", (*i).getName().c_str(), (*i).getName().c_str());
1044 fprintf(f,
"%s}\n\n", write_data_changed ?
" data_changed = true;\n" :
"");
1046 if (((*i).getType() !=
"string") && ((*i).getLengthValue() > 0)) {
1048 "/** Set %s value at given index.\n"
1050 " * @param new_%s new %s value\n"
1051 " * @param index index for of the value\n"
1054 "%s%s::set_%s(unsigned int index, const %s new_%s)\n"
1056 " if (index > %s) {\n"
1057 " throw Exception(\"Index value %%u out of bounds (0..%s)\", index);\n"
1059 " data->%s[index] = new_%s;\n"
1062 (*i).getName().c_str(),
1063 (*i).getComment().c_str(),
1064 (*i).getName().c_str(),
1065 (*i).getName().c_str(),
1066 inclusion_prefix.c_str(),
1068 (*i).getName().c_str(),
1069 (*i).getPlainAccessType().c_str(),
1070 i->getName().c_str(),
1071 i->getMaxIdx().c_str(),
1072 i->getMaxIdx().c_str(),
1073 i->getName().c_str(),
1074 i->getName().c_str(),
1075 write_data_changed ?
" data_changed = true;\n" :
"");
1090 std::string interface_classname,
1091 std::string classname,
1092 std::vector<InterfaceField> fields,
1093 std::vector<InterfacePseudoMap> pseudo_maps,
1094 std::string inclusion_prefix)
1096 write_methods_cpp(f, interface_classname, classname, fields, inclusion_prefix,
true);
1098 for (vector<InterfacePseudoMap>::iterator i = pseudo_maps.begin(); i != pseudo_maps.end(); ++i) {
1100 "/** Get %s value.\n"
1102 " * @param key key of the value\n"
1103 " * @return %s value\n"
1106 "%s%s::%s(const %s key) const\n"
1108 (*i).getName().c_str(),
1109 (*i).getComment().c_str(),
1110 (*i).getName().c_str(),
1111 (*i).getType().c_str(),
1112 inclusion_prefix.c_str(),
1114 (*i).getName().c_str(),
1115 (*i).getKeyType().c_str());
1118 InterfacePseudoMap::RefList::iterator paref;
1120 for (paref = reflist.begin(); paref != reflist.end(); ++paref) {
1122 " %sif (key == %s) {\n"
1123 " return data->%s;\n",
1124 first ?
"" :
"} else ",
1125 paref->second.c_str(),
1126 paref->first.c_str());
1131 " throw Exception(\"Invalid key, cannot retrieve value\");\n"
1136 "/** Set %s value.\n"
1138 " * @param key key of the value\n"
1139 " * @param new_value new value\n"
1142 "%s%s::set_%s(const %s key, const %s new_value)\n"
1144 (*i).getName().c_str(),
1145 (*i).getComment().c_str(),
1146 inclusion_prefix.c_str(),
1148 (*i).getName().c_str(),
1149 (*i).getKeyType().c_str(),
1150 (*i).getType().c_str());
1153 for (paref = reflist.begin(); paref != reflist.end(); ++paref) {
1155 " %sif (key == %s) {\n"
1156 " data->%s = new_value;\n",
1157 first ?
"" :
"} else ",
1158 paref->second.c_str(),
1159 paref->first.c_str());
1177 std::vector<InterfaceField> fields)
1179 fprintf(f,
"%s/* Methods */\n", is.c_str());
1180 for (vector<InterfaceField>::iterator i = fields.begin(); i != fields.end(); ++i) {
1182 "%s%s %s%s() const;\n",
1184 (*i).getAccessType().c_str(),
1185 (((*i).getType() ==
"bool") ?
"is_" :
""),
1186 (*i).getName().c_str());
1188 if ((i->getLengthValue() > 0) && (i->getType() !=
"string")) {
1190 "%s%s %s%s(unsigned int index) const;\n"
1191 "%svoid set_%s(unsigned int index, const %s new_%s);\n",
1193 i->getPlainAccessType().c_str(),
1194 (((*i).getType() ==
"bool") ?
"is_" :
""),
1195 (*i).getName().c_str(),
1197 (*i).getName().c_str(),
1198 i->getPlainAccessType().c_str(),
1199 i->getName().c_str());
1203 "%svoid set_%s(const %s new_%s);\n"
1204 "%ssize_t maxlenof_%s() const;\n",
1206 (*i).getName().c_str(),
1207 i->getAccessType().c_str(),
1208 i->getName().c_str(),
1210 i->getName().c_str());
1223 std::vector<InterfaceField> fields,
1224 std::vector<InterfacePseudoMap> pseudo_maps)
1226 write_methods_h(f, is, fields);
1228 for (vector<InterfacePseudoMap>::iterator i = pseudo_maps.begin(); i != pseudo_maps.end(); ++i) {
1230 "%s%s %s(%s key) const;\n"
1231 "%svoid set_%s(const %s key, const %s new_value);\n",
1233 (*i).getType().c_str(),
1234 (*i).getName().c_str(),
1235 (*i).getKeyType().c_str(),
1237 (*i).getName().c_str(),
1238 i->getKeyType().c_str(),
1239 i->getType().c_str());
1251 "%svirtual Message * create_message(const char *type) const;\n\n"
1252 "%svirtual void copy_values(const Interface *other);\n"
1253 "%svirtual const char * enum_tostring(const char *enumtype, int val) const;\n",
1265 write_header(f, filename_h);
1269 "#include <interface/interface.h>\n"
1270 "#include <interface/message.h>\n"
1271 "#include <interface/field_iterator.h>\n\n"
1272 "namespace fawkes {\n\n"
1273 "class %s : public Interface\n"
1275 " /// @cond INTERNALS\n"
1276 " INTERFACE_MGMT_FRIENDS(%s)\n"
1280 class_name.c_str());
1282 write_constants_h(f);
1284 fprintf(f,
" private:\n");
1286 write_struct(f, class_name +
"_data_t",
" ", data_fields);
1288 fprintf(f,
" %s_data_t *data;\n\n", class_name.c_str());
1290 write_enum_maps_h(f);
1292 fprintf(f,
" public:\n");
1294 write_messages_h(f);
1295 fprintf(f,
" private:\n");
1296 write_ctor_dtor_h(f,
" ", class_name);
1297 fprintf(f,
" public:\n");
1298 write_methods_h(f,
" ", data_fields, pseudo_maps);
1299 write_basemethods_h(f,
" ");
1300 fprintf(f,
"\n};\n\n} // end namespace fawkes\n\n#endif\n");
1308 char timestring[26];
1309 struct tm timestruct;
1310 time_t t = time(NULL);
1311 localtime_r(&t, ×truct);
1312 asctime_r(×truct, timestring);
1313 gendate = timestring;
1318 cpp = fopen(
string(dir + filename_cpp).c_str(),
"w");
1319 h = fopen(
string(dir + filename_h).c_str(),
"w");
1322 printf(
"Cannot open cpp file %s%s\n", dir.c_str(), filename_cpp.c_str());
1325 printf(
"Cannot open h file %s%s\n", dir.c_str(), filename_h.c_str());