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 __attribute__((packed)) {\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_) == 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,
"%s%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(\"%s\")\n" 896 inclusion_prefix.c_str(),
904 " data_size = m->data_size;\n" 905 " data_ptr = malloc(data_size);\n" 906 " memcpy(data_ptr, m->data_ptr, data_size);\n" 907 " data = (%s_data_t *)data_ptr;\n" 908 " data_ts = (message_data_ts_t *)data_ptr;\n",
926 std::string interface_classname,
927 std::string classname,
928 std::vector<InterfaceField> fields,
929 std::string inclusion_prefix,
930 bool write_data_changed)
932 fprintf(f,
"/* Methods */\n");
933 for (vector<InterfaceField>::iterator i = fields.begin(); i != fields.end(); ++i) {
935 "/** Get %s value.\n" 937 " * @return %s value\n" 940 "%s%s::%s%s() const\n" 942 " return %sdata->%s;\n" 944 (*i).getName().c_str(),
945 (*i).getComment().c_str(),
946 (*i).getName().c_str(),
947 (*i).isEnumType() ? (interface_classname +
"::").c_str() :
"",
948 (*i).getAccessType().c_str(),
949 inclusion_prefix.c_str(),
951 (((*i).getType() ==
"bool") ?
"is_" :
""),
952 (*i).getName().c_str(),
954 ? (std::string(
"(") + interface_classname +
"::" + i->getAccessType() +
")").c_str()
956 (*i).getName().c_str());
958 if ((i->getLengthValue() > 0) && (i->getType() !=
"string")) {
961 "/** Get %s value at given index.\n" 963 " * @param index index of value\n" 964 " * @return %s value\n" 965 " * @exception Exception thrown if index is out of bounds\n" 968 "%s%s::%s%s(unsigned int index) const\n" 970 " if (index > %s) {\n" 971 " throw Exception(\"Index value %%u out of bounds (0..%s)\", index);\n" 973 " return %sdata->%s[index];\n" 975 (*i).getName().c_str(),
976 (*i).getComment().c_str(),
977 (*i).getName().c_str(),
978 (*i).isEnumType() ? (interface_classname +
"::").c_str() :
"",
979 (*i).getPlainAccessType().c_str(),
980 inclusion_prefix.c_str(),
982 (((*i).getType() ==
"bool") ?
"is_" :
""),
983 (*i).getName().c_str(),
984 i->getLength().c_str(),
985 i->getLength().c_str(),
987 ? (std::string(
"(") + interface_classname +
"::" + i->getPlainAccessType() +
")").c_str()
989 (*i).getName().c_str());
993 "/** Get maximum length of %s value.\n" 994 " * @return length of %s value, can be length of the array or number of \n" 995 " * maximum number of characters for a string\n" 998 "%s%s::maxlenof_%s() const\n" 1002 i->getName().c_str(),
1003 i->getName().c_str(),
1004 inclusion_prefix.c_str(),
1006 i->getName().c_str(),
1007 i->getLengthValue() > 0 ? i->getLength().c_str() :
"1");
1010 "/** Set %s value.\n" 1012 " * @param new_%s new %s value\n" 1015 "%s%s::set_%s(const %s new_%s)\n" 1017 (*i).getName().c_str(),
1018 (*i).getComment().c_str(),
1019 (*i).getName().c_str(),
1020 (*i).getName().c_str(),
1021 inclusion_prefix.c_str(),
1023 (*i).getName().c_str(),
1024 (*i).getAccessType().c_str(),
1025 (*i).getName().c_str());
1026 if ((*i).getType() ==
"string") {
1028 " strncpy(data->%s, new_%s, sizeof(data->%s)-1);\n" 1029 " data->%s[sizeof(data->%s)-1] = 0;\n",
1030 (*i).getName().c_str(),
1031 (*i).getName().c_str(),
1032 (*i).getName().c_str(),
1033 (*i).getName().c_str(),
1034 (*i).getName().c_str());
1035 }
else if ((*i).getLength() !=
"") {
1037 " memcpy(data->%s, new_%s, sizeof(%s) * %s);\n",
1038 (*i).getName().c_str(),
1039 (*i).getName().c_str(),
1040 (*i).getPlainAccessType().c_str(),
1041 (*i).getLength().c_str());
1043 fprintf(f,
" data->%s = new_%s;\n", (*i).getName().c_str(), (*i).getName().c_str());
1045 fprintf(f,
"%s}\n\n", write_data_changed ?
" data_changed = true;\n" :
"");
1047 if (((*i).getType() !=
"string") && ((*i).getLengthValue() > 0)) {
1049 "/** Set %s value at given index.\n" 1051 " * @param new_%s new %s value\n" 1052 " * @param index index for of the value\n" 1055 "%s%s::set_%s(unsigned int index, const %s new_%s)\n" 1057 " if (index > %s) {\n" 1058 " throw Exception(\"Index value %%u out of bounds (0..%s)\", index);\n" 1060 " data->%s[index] = new_%s;\n" 1063 (*i).getName().c_str(),
1064 (*i).getComment().c_str(),
1065 (*i).getName().c_str(),
1066 (*i).getName().c_str(),
1067 inclusion_prefix.c_str(),
1069 (*i).getName().c_str(),
1070 (*i).getPlainAccessType().c_str(),
1071 i->getName().c_str(),
1072 i->getLength().c_str(),
1073 i->getLength().c_str(),
1074 i->getName().c_str(),
1075 i->getName().c_str(),
1076 write_data_changed ?
" data_changed = true;\n" :
"");
1091 std::string interface_classname,
1092 std::string classname,
1093 std::vector<InterfaceField> fields,
1094 std::vector<InterfacePseudoMap> pseudo_maps,
1095 std::string inclusion_prefix)
1097 write_methods_cpp(f, interface_classname, classname, fields, inclusion_prefix,
true);
1099 for (vector<InterfacePseudoMap>::iterator i = pseudo_maps.begin(); i != pseudo_maps.end(); ++i) {
1101 "/** Get %s value.\n" 1103 " * @param key key of the value\n" 1104 " * @return %s value\n" 1107 "%s%s::%s(const %s key) const\n" 1109 (*i).getName().c_str(),
1110 (*i).getComment().c_str(),
1111 (*i).getName().c_str(),
1112 (*i).getType().c_str(),
1113 inclusion_prefix.c_str(),
1115 (*i).getName().c_str(),
1116 (*i).getKeyType().c_str());
1119 InterfacePseudoMap::RefList::iterator paref;
1121 for (paref = reflist.begin(); paref != reflist.end(); ++paref) {
1123 " %sif (key == %s) {\n" 1124 " return data->%s;\n",
1125 first ?
"" :
"} else ",
1126 paref->second.c_str(),
1127 paref->first.c_str());
1132 " throw Exception(\"Invalid key, cannot retrieve value\");\n" 1137 "/** Set %s value.\n" 1139 " * @param key key of the value\n" 1140 " * @param new_value new value\n" 1143 "%s%s::set_%s(const %s key, const %s new_value)\n" 1145 (*i).getName().c_str(),
1146 (*i).getComment().c_str(),
1147 inclusion_prefix.c_str(),
1149 (*i).getName().c_str(),
1150 (*i).getKeyType().c_str(),
1151 (*i).getType().c_str());
1154 for (paref = reflist.begin(); paref != reflist.end(); ++paref) {
1156 " %sif (key == %s) {\n" 1157 " data->%s = new_value;\n",
1158 first ?
"" :
"} else ",
1159 paref->second.c_str(),
1160 paref->first.c_str());
1178 std::vector<InterfaceField> fields)
1180 fprintf(f,
"%s/* Methods */\n", is.c_str());
1181 for (vector<InterfaceField>::iterator i = fields.begin(); i != fields.end(); ++i) {
1183 "%s%s %s%s() const;\n",
1185 (*i).getAccessType().c_str(),
1186 (((*i).getType() ==
"bool") ?
"is_" :
""),
1187 (*i).getName().c_str());
1189 if ((i->getLengthValue() > 0) && (i->getType() !=
"string")) {
1191 "%s%s %s%s(unsigned int index) const;\n" 1192 "%svoid set_%s(unsigned int index, const %s new_%s);\n",
1194 i->getPlainAccessType().c_str(),
1195 (((*i).getType() ==
"bool") ?
"is_" :
""),
1196 (*i).getName().c_str(),
1198 (*i).getName().c_str(),
1199 i->getPlainAccessType().c_str(),
1200 i->getName().c_str());
1204 "%svoid set_%s(const %s new_%s);\n" 1205 "%ssize_t maxlenof_%s() const;\n",
1207 (*i).getName().c_str(),
1208 i->getAccessType().c_str(),
1209 i->getName().c_str(),
1211 i->getName().c_str());
1224 std::vector<InterfaceField> fields,
1225 std::vector<InterfacePseudoMap> pseudo_maps)
1227 write_methods_h(f, is, fields);
1229 for (vector<InterfacePseudoMap>::iterator i = pseudo_maps.begin(); i != pseudo_maps.end(); ++i) {
1231 "%s%s %s(%s key) const;\n" 1232 "%svoid set_%s(const %s key, const %s new_value);\n",
1234 (*i).getType().c_str(),
1235 (*i).getName().c_str(),
1236 (*i).getKeyType().c_str(),
1238 (*i).getName().c_str(),
1239 i->getKeyType().c_str(),
1240 i->getType().c_str());
1252 "%svirtual Message * create_message(const char *type) const;\n\n" 1253 "%svirtual void copy_values(const Interface *other);\n" 1254 "%svirtual const char * enum_tostring(const char *enumtype, int val) const;\n",
1266 write_header(f, filename_h);
1270 "#include <interface/interface.h>\n" 1271 "#include <interface/message.h>\n" 1272 "#include <interface/field_iterator.h>\n\n" 1273 "namespace fawkes {\n\n" 1274 "class %s : public Interface\n" 1276 " /// @cond INTERNALS\n" 1277 " INTERFACE_MGMT_FRIENDS(%s)\n" 1281 class_name.c_str());
1283 write_constants_h(f);
1285 fprintf(f,
" private:\n");
1287 write_struct(f, class_name +
"_data_t",
" ", data_fields);
1289 fprintf(f,
" %s_data_t *data;\n\n", class_name.c_str());
1291 write_enum_maps_h(f);
1293 fprintf(f,
" public:\n");
1295 write_messages_h(f);
1296 fprintf(f,
" private:\n");
1297 write_ctor_dtor_h(f,
" ", class_name);
1298 fprintf(f,
" public:\n");
1299 write_methods_h(f,
" ", data_fields, pseudo_maps);
1300 write_basemethods_h(f,
" ");
1301 fprintf(f,
"\n};\n\n} // end namespace fawkes\n\n#endif\n");
1309 char timestring[26];
1310 struct tm timestruct;
1311 time_t t = time(NULL);
1312 localtime_r(&t, ×truct);
1313 asctime_r(×truct, timestring);
1314 gendate = timestring;
1319 cpp = fopen(
string(dir + filename_cpp).c_str(),
"w");
1320 h = fopen(
string(dir + filename_h).c_str(),
"w");
1323 printf(
"Cannot open cpp file %s%s\n", dir.c_str(), filename_cpp.c_str());
1326 printf(
"Cannot open h file %s%s\n", dir.c_str(), filename_h.c_str());
CppInterfaceGenerator(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_add_fieldinfo_calls(FILE *f, std::vector< InterfaceField > &fields)
Write the add_fieldinfo() calls.
void write_message_ctor_dtor_cpp(FILE *f, std::string classname, std::string super_class, std::string inclusion_prefix, std::vector< InterfaceField > fields)
Write constructor and destructor for message to cpp file.
static std::string to_upper(std::string str)
Convert string to all-uppercase string.
void write_enum_map_population(FILE *f)
Write enum maps.
void write_messages_cpp(FILE *f)
Write messages to cpp file.
void write_management_funcs_cpp(FILE *f)
Write management functions.
void write_deflector(FILE *f)
Write header deflector.
void write_struct(FILE *f, std::string name, std::string is, std::vector< InterfaceField > fields)
Write optimized struct.
void write_basemethods_h(FILE *f, std::string is)
Write base methods header entries.
void write_constants_h(FILE *f)
Write constants to h file.
void generate()
Generator cpp and h files.
void write_cpp(FILE *f)
Write cpp file.
void write_create_message_method_cpp(FILE *f)
Write create_message() method to cpp file.
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_message_clone_method_cpp(FILE *f, std::string classname)
Write message clone method.
void write_h(FILE *f)
Write h file.
void write_message_clone_method_h(FILE *f, std::string is)
Write message clone method header.
std::list< std::pair< std::string, std::string > > RefList
Reference list.
void write_ctor_dtor_cpp(FILE *f, std::string classname, std::string super_class, std::string inclusion_prefix, std::vector< InterfaceField > fields, std::vector< InterfaceMessage > messages)
Write constructor and destructor to cpp file.
void write_ctor_dtor_h(FILE *f, std::string is, std::string classname)
Write constructor and destructor to h file.
void write_enum_maps_h(FILE *f)
Write enum maps to header.
void write_methods_h(FILE *f, std::string is, std::vector< InterfaceField > fields)
Write methods to h file.
void write_enum_constants_tostring_cpp(FILE *f)
Write enum constant tostring methods to cpp file.
void write_copy_value_method_cpp(FILE *f)
Write copy_value() method to CPP file.
void write_basemethods_cpp(FILE *f)
Write base methods.
void write_messages_h(FILE *f)
Write messages to h file.
~CppInterfaceGenerator()
Destructor.
void write_methods_cpp(FILE *f, std::string interface_classname, std::string classname, std::vector< InterfaceField > fields, std::string inclusion_prefix, bool write_data_changed)
Write methods to cpp file.
void write_enum_tostring_method_cpp(FILE *f)
Write enum_tostring() method to CPP file.
void write_header(FILE *f, std::string filename)
Write header to file.
void write_constants_cpp(FILE *f)
Write constants to cpp file.