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 "%s/** Internal data storage, do NOT modify! */\n" 133 "%stypedef struct __attribute__((packed)) {\n" 134 "%s int64_t timestamp_sec; /**< Interface Unix timestamp, seconds */\n" 135 "%s int64_t timestamp_usec; /**< Interface Unix timestamp, micro-seconds */\n", is.c_str(), is.c_str(), is.c_str(), is.c_str());
137 for (vector<InterfaceField>::iterator i = fields.begin(); i != fields.end(); ++i) {
138 fprintf(f,
"%s %s %s", is.c_str(), (*i).getStructType().c_str(), (*i).getName().c_str());
139 if ( (*i).getLength().length() > 0 ) {
140 fprintf(f,
"[%s]", (*i).getLength().c_str());
142 fprintf(f,
"; /**< %s */\n", (*i).getComment().c_str());
145 fprintf(f,
"%s} %s;\n\n", is.c_str(), name.c_str());
155 for (vector<InterfaceEnumConstant>::iterator i = enum_constants.begin(); i != enum_constants.end(); ++i) {
156 fprintf(f,
" interface_enum_map_t enum_map_%s;\n", i->get_name().c_str());
170 "\n/***************************************************************************\n" 171 " * %s - Fawkes BlackBoard Interface - %s\n" 174 " * Templated created: Thu Oct 12 10:49:19 2006\n" 175 " * Copyright %s %s\n" 177 " ****************************************************************************/\n\n" 178 "/* This program is free software; you can redistribute it and/or modify\n" 179 " * it under the terms of the GNU General Public License as published by\n" 180 " * the Free Software Foundation; either version 2 of the License, or\n" 181 " * (at your option) any later version. A runtime exception applies to\n" 182 " * this software (see LICENSE.GPL_WRE file mentioned below for details).\n" 184 " * This program is distributed in the hope that it will be useful,\n" 185 " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n" 186 " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" 187 " * GNU Library General Public License for more details.\n" 189 " * Read the full text in the LICENSE.GPL_WRE file in the doc directory.\n" 191 filename.c_str(), class_name.c_str(),
192 (creation_date.length() > 0 ) ?
" * Interface created: " :
"",
193 (creation_date.length() > 0 ) ? creation_date.c_str() :
"",
194 (creation_date.length() > 0 ) ?
"\n" :
"",
195 year.c_str(), (author.length() > 0) ? author.c_str() :
"AllemaniACs RoboCup Team" 206 fprintf(f,
"#ifndef %s\n", deflector.c_str());
207 fprintf(f,
"#define %s\n\n", deflector.c_str());
217 write_header(f, filename_cpp);
219 "#include <interfaces/%s>\n\n" 220 "#include <core/exceptions/software.h>\n\n" 222 "#include <string>\n" 223 "#include <cstring>\n" 224 "#include <cstdlib>\n\n" 225 "namespace fawkes {\n\n" 226 "/** @class %s <interfaces/%s>\n" 227 " * %s Fawkes BlackBoard Interface.\n" 229 " * @ingroup FawkesInterfaces\n" 231 filename_h.c_str(), class_name.c_str(), filename_h.c_str(),
232 class_name.c_str(), data_comment.c_str());
233 write_constants_cpp(f);
234 write_ctor_dtor_cpp(f, class_name,
"Interface",
"", data_fields, messages);
235 write_enum_constants_tostring_cpp(f);
236 write_methods_cpp(f, class_name, class_name, data_fields, pseudo_maps,
"");
237 write_basemethods_cpp(f);
238 write_messages_cpp(f);
240 write_management_funcs_cpp(f);
242 fprintf(f,
"\n} // end namespace fawkes\n");
253 "/// @cond INTERNALS\n" 254 "EXPORT_INTERFACE(%s)\n" 266 for ( vector<InterfaceConstant>::iterator i = constants.begin(); i != constants.end(); ++i) {
267 const char *type_suffix =
"";
268 if (i->getType() ==
"uint32_t") {
272 "/** %s constant */\n" 273 "const %s %s::%s = %s%s;\n",
274 (*i).getName().c_str(),
275 (*i).getType().c_str(),
276 class_name.c_str(), i->getName().c_str(),
277 i->getValue().c_str(), type_suffix);
289 for ( vector<InterfaceEnumConstant>::iterator i = enum_constants.begin(); i != enum_constants.end(); ++i) {
291 "/** Convert %s constant to string.\n" 292 " * @param value value to convert to string\n" 293 " * @return constant value as string.\n" 296 "%s::tostring_%s(%s value) const\n" 298 " switch (value) {\n",
299 i->get_name().c_str(), class_name.c_str(), i->get_name().c_str(),
300 i->get_name().c_str());
301 vector<InterfaceEnumConstant::EnumItem> items = i->get_items();
302 vector<InterfaceEnumConstant::EnumItem>::iterator j;
303 for (j = items.begin(); j != items.end(); ++j) {
304 fprintf(f,
" case %s: return \"%s\";\n",
305 j->name.c_str(), j->name.c_str());
308 " default: return \"UNKNOWN\";\n" 320 fprintf(f,
" /* constants */\n");
321 for ( vector<InterfaceConstant>::iterator i = constants.begin(); i != constants.end(); ++i) {
322 fprintf(f,
" static const %s %s;\n", (*i).getType().c_str(), (*i).getName().c_str());
326 for ( vector<InterfaceEnumConstant>::iterator i = enum_constants.begin(); i != enum_constants.end(); ++i) {
330 (*i).get_comment().c_str());
331 vector<InterfaceEnumConstant::EnumItem> items = i->get_items();
332 vector<InterfaceEnumConstant::EnumItem>::iterator j = items.begin();
333 while (j != items.end()) {
334 if (j->has_custom_value) {
335 fprintf(f,
" %s = %i /**< %s */", j->name.c_str(),
336 j->custom_value, j->comment.c_str());
338 fprintf(f,
" %s /**< %s */", j->name.c_str(), j->comment.c_str());
341 if ( j != items.end() ) {
347 fprintf(f,
" } %s;\n", (*i).get_name().c_str());
348 fprintf(f,
" const char * tostring_%s(%s value) const;\n\n",
349 i->get_name().c_str(), i->get_name().c_str());
360 fprintf(f,
" /* messages */\n");
361 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
362 fprintf(f,
" class %s : public Message\n" 363 " {\n", (*i).getName().c_str());
365 fprintf(f,
" private:\n");
366 write_struct(f, (*i).getName() +
"_data_t",
" ", (*i).getFields());
368 " %s_data_t *data;\n\n",
369 (*i).getName().c_str());
371 write_enum_maps_h(f);
373 fprintf(f,
" public:\n");
374 write_message_ctor_dtor_h(f,
" ", (*i).getName(), (*i).getFields());
375 write_methods_h(f,
" ", (*i).getFields());
376 write_message_clone_method_h(f,
" ");
377 fprintf(f,
" };\n\n");
379 fprintf(f,
" virtual bool message_valid(const Message *message) const;\n");
390 fprintf(f,
"/* =========== messages =========== */\n");
391 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
393 "/** @class %s::%s <interfaces/%s>\n" 394 " * %s Fawkes BlackBoard Interface Message.\n" 397 class_name.c_str(), (*i).getName().c_str(), filename_h.c_str(),
398 (*i).getName().c_str(), (*i).getComment().c_str());
400 write_message_ctor_dtor_cpp(f, (*i).getName(),
"Message", class_name +
"::",
402 write_methods_cpp(f, class_name, (*i).getName(), (*i).getFields(), class_name +
"::",
false);
403 write_message_clone_method_cpp(f, (class_name +
"::" + (*i).getName()).c_str());
406 "/** Check if message is valid and can be enqueued.\n" 407 " * @param message Message to check\n" 408 " * @return true if the message is valid, false otherwise.\n" 411 "%s::message_valid(const Message *message) const\n" 412 "{\n", class_name.c_str());
414 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
416 " const %s *m%u = dynamic_cast<const %s *>(message);\n" 417 " if ( m%u != NULL ) {\n" 420 (*i).getName().c_str(), n, (*i).getName().c_str(), n);
435 fprintf(f,
"/* =========== message create =========== */\n");
438 "%s::create_message(const char *type) const\n" 439 "{\n", class_name.c_str());
442 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
444 " %sif ( strncmp(\"%s\", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {\n" 445 " return new %s();\n",
446 first ?
"" :
"} else ", i->getName().c_str(), i->getName().c_str());
451 " throw UnknownTypeException(\"The given type '%%s' does not match any known \"\n" 452 " \"message type for this interface type.\", type);\n" 457 " throw UnknownTypeException(\"The given type '%%s' does not match any known \"\n" 458 " \"message type for this interface type.\", type);\n" 472 "/** Copy values from other interface.\n" 473 " * @param other other interface to copy values from\n" 476 "%s::copy_values(const Interface *other)\n" 478 " const %s *oi = dynamic_cast<const %s *>(other);\n" 479 " if (oi == NULL) {\n" 480 " throw TypeMismatchException(\"Can only copy values from interface of same type (%%s vs. %%s)\",\n" 481 " type(), other->type());\n" 483 " memcpy(data, oi->data, sizeof(%s_data_t));\n" 485 class_name.c_str(), class_name.c_str(), class_name.c_str(), class_name.c_str());
497 "%s::enum_tostring(const char *enumtype, int val) const\n" 498 "{\n", class_name.c_str());
499 for ( vector<InterfaceEnumConstant>::iterator i = enum_constants.begin(); i != enum_constants.end(); ++i) {
501 " if (strcmp(enumtype, \"%s\") == 0) {\n" 502 " return tostring_%s((%s)val);\n" 504 i->get_name().c_str(), i->get_name().c_str(), i->get_name().c_str());
507 " throw UnknownTypeException(\"Unknown enum type %%s\", enumtype);\n" 518 write_create_message_method_cpp(f);
519 write_copy_value_method_cpp(f);
520 write_enum_tostring_method_cpp(f);
531 std::string classname)
536 is.c_str(), classname.c_str(),
537 is.c_str(), classname.c_str());
549 std::string classname,
550 std::vector<InterfaceField> fields)
552 vector<InterfaceField>::iterator i;
554 if ( fields.size() > 0 ) {
556 fprintf(f,
"%s%s(", is.c_str(), classname.c_str());
559 while (i != fields.end()) {
560 fprintf(f,
"const %s ini_%s",
561 (*i).getAccessType().c_str(), (*i).getName().c_str());
563 if ( i != fields.end() ) {
571 write_ctor_dtor_h(f, is, classname);
572 fprintf(f,
"%s%s(const %s *m);\n", is.c_str(), classname.c_str(), classname.c_str());
584 fprintf(f,
"%svirtual Message * clone() const;\n", is.c_str());
596 "/** Clone this message.\n" 597 " * Produces a message of the same type as this message and copies the\n" 598 " * data to the new message.\n" 599 " * @return clone of this message\n" 602 "%s::clone() const\n" 604 " return new %s(this);\n" 605 "}\n", classname.c_str(), classname.c_str());
614 for (vector<InterfaceEnumConstant>::iterator i = enum_constants.begin(); i != enum_constants.end(); ++i) {
615 const std::vector<InterfaceEnumConstant::EnumItem> &enum_values = i->get_items();
617 std::vector<InterfaceEnumConstant::EnumItem>::const_iterator ef;
618 for (ef = enum_values.begin(); ef != enum_values.end(); ++ef) {
620 " enum_map_%s[(int)%s] = \"%s\";\n",
621 i->get_name().c_str(), ef->name.c_str(), ef->name.c_str());
634 std::vector<InterfaceField>::iterator i;
635 for (i = fields.begin(); i != fields.end(); ++i) {
636 const char *type =
"";
637 const char *dataptr =
"&";
638 std::string enumtype;
640 if ( i->getType() ==
"bool" ) {
642 }
else if ( i->getType() ==
"int8" ) {
644 }
else if ( i->getType() ==
"uint8" ) {
646 }
else if ( i->getType() ==
"int16" ) {
648 }
else if ( i->getType() ==
"uint16" ) {
650 }
else if ( i->getType() ==
"int32" ) {
652 }
else if ( i->getType() ==
"uint32" ) {
654 }
else if ( i->getType() ==
"int64" ) {
656 }
else if ( i->getType() ==
"uint64" ) {
658 }
else if ( i->getType() ==
"byte" ) {
660 }
else if ( i->getType() ==
"float" ) {
662 }
else if ( i->getType() ==
"double" ) {
664 }
else if ( i->getType() ==
"string" ) {
669 enumtype = i->getType();
672 fprintf(f,
" add_fieldinfo(IFT_%s, \"%s\", %u, %sdata->%s%s%s%s%s%s%s);\n",
673 type, i->getName().c_str(),
674 (i->getLengthValue() > 0) ? i->getLengthValue() : 1,
675 dataptr, i->getName().c_str(),
676 enumtype.empty() ?
"" :
", \"",
677 enumtype.empty() ?
"" : enumtype.c_str(),
678 enumtype.empty() ?
"" :
"\"",
679 enumtype.empty() ?
"" :
", ",
680 enumtype.empty() ?
"" :
"&enum_map_",
681 enumtype.empty() ?
"" : enumtype.c_str()
697 std::string classname, std::string super_class,
698 std::string inclusion_prefix,
699 std::vector<InterfaceField> fields,
700 std::vector<InterfaceMessage> messages)
703 "/** Constructor */\n" 704 "%s%s::%s() : %s()\n" 706 inclusion_prefix.c_str(), classname.c_str(),
707 classname.c_str(), super_class.c_str());
710 " data_size = sizeof(%s_data_t);\n" 711 " data_ptr = malloc(data_size);\n" 712 " data = (%s_data_t *)data_ptr;\n" 713 " data_ts = (interface_data_ts_t *)data_ptr;\n" 714 " memset(data_ptr, 0, data_size);\n",
715 classname.c_str(), classname.c_str());
717 write_enum_map_population(f);
718 write_add_fieldinfo_calls(f, fields);
720 for (vector<InterfaceMessage>::iterator i = messages.begin(); i != messages.end(); ++i) {
721 fprintf(f,
" add_messageinfo(\"%s\");\n", i->getName().c_str());
724 fprintf(f,
" unsigned char tmp_hash[] = {");
725 for (
size_t st = 0; st < hash_size-1; ++st) {
726 fprintf(f,
"%#02x, ", hash[st]);
728 fprintf(f,
"%#02x};\n", hash[hash_size-1]);
729 fprintf(f,
" set_hash(tmp_hash);\n");
733 "/** Destructor */\n" 738 inclusion_prefix.c_str(), classname.c_str(), classname.c_str()
752 std::string classname, std::string super_class,
753 std::string inclusion_prefix,
754 std::vector<InterfaceField> fields)
756 vector<InterfaceField>::iterator i;
758 if ( fields.size() > 0 ) {
760 "/** Constructor with initial values.\n");
762 for (i = fields.begin(); i != fields.end(); ++i) {
763 fprintf(f,
" * @param ini_%s initial value for %s\n",
764 (*i).getName().c_str(), (*i).getName().c_str());
770 inclusion_prefix.c_str(), classname.c_str(), classname.c_str());
773 while (i != fields.end()) {
774 fprintf(f,
"const %s ini_%s",
775 (*i).getAccessType().c_str(), (*i).getName().c_str());
777 if ( i != fields.end() ) {
782 fprintf(f,
") : %s(\"%s\")\n" 784 " data_size = sizeof(%s_data_t);\n" 785 " data_ptr = malloc(data_size);\n" 786 " memset(data_ptr, 0, data_size);\n" 787 " data = (%s_data_t *)data_ptr;\n" 788 " data_ts = (message_data_ts_t *)data_ptr;\n",
789 super_class.c_str(), classname.c_str(), classname.c_str(), classname.c_str());
791 for (i = fields.begin(); i != fields.end(); ++i) {
792 if ( (*i).getType() ==
"string" ) {
793 fprintf(f,
" strncpy(data->%s, ini_%s, %s);\n",
794 (*i).getName().c_str(), (*i).getName().c_str(),
795 (*i).getLength().c_str());
796 }
else if (i->getLengthValue() > 1) {
797 fprintf(f,
" memcpy(data->%s, ini_%s, sizeof(%s) * %s);\n",
798 i->getName().c_str(), i->getName().c_str(),
799 i->getPlainAccessType().c_str(), i->getLength().c_str());
803 fprintf(f,
" data->%s = ini_%s;\n",
804 (*i).getName().c_str(), (*i).getName().c_str());
808 write_enum_map_population(f);
809 write_add_fieldinfo_calls(f, fields);
815 "/** Constructor */\n" 816 "%s%s::%s() : %s(\"%s\")\n" 818 inclusion_prefix.c_str(), classname.c_str(),
819 classname.c_str(), super_class.c_str(), classname.c_str());
822 " data_size = sizeof(%s_data_t);\n" 823 " data_ptr = malloc(data_size);\n" 824 " memset(data_ptr, 0, data_size);\n" 825 " data = (%s_data_t *)data_ptr;\n" 826 " data_ts = (message_data_ts_t *)data_ptr;\n",
827 classname.c_str(), classname.c_str());
829 write_enum_map_population(f);
830 write_add_fieldinfo_calls(f, fields);
834 "/** Destructor */\n" 839 inclusion_prefix.c_str(), classname.c_str(), classname.c_str());
842 "/** Copy constructor.\n" 843 " * @param m message to copy from\n" 845 "%s%s::%s(const %s *m) : %s(\"%s\")\n" 847 inclusion_prefix.c_str(), classname.c_str(), classname.c_str(),
848 classname.c_str(), super_class.c_str(), classname.c_str());
851 " data_size = m->data_size;\n" 852 " data_ptr = malloc(data_size);\n" 853 " memcpy(data_ptr, m->data_ptr, data_size);\n" 854 " data = (%s_data_t *)data_ptr;\n" 855 " data_ts = (message_data_ts_t *)data_ptr;\n",
875 std::string classname,
876 std::vector<InterfaceField> fields,
877 std::string inclusion_prefix,
878 bool write_data_changed)
880 fprintf(f,
"/* Methods */\n");
881 for (vector<InterfaceField>::iterator i = fields.begin(); i != fields.end(); ++i) {
883 "/** Get %s value.\n" 885 " * @return %s value\n" 888 "%s%s::%s%s() const\n" 890 " return %sdata->%s;\n" 892 (*i).getName().c_str(),
893 (*i).getComment().c_str(),
894 (*i).getName().c_str(),
895 (*i).isEnumType() ? (interface_classname +
"::").c_str() :
"",
896 (*i).getAccessType().c_str(),
897 inclusion_prefix.c_str(), classname.c_str(), ( ((*i).getType() ==
"bool" ) ?
"is_" :
""), (*i).getName().c_str(),
898 (*i).isEnumType() ? (std::string(
"(") + interface_classname +
"::" +
899 i->getAccessType() +
")").c_str() :
"",
900 (*i).getName().c_str() );
902 if ( (i->getLengthValue() > 0) && (i->getType() !=
"string") ) {
904 "/** Get %s value at given index.\n" 906 " * @param index index of value\n" 907 " * @return %s value\n" 908 " * @exception Exception thrown if index is out of bounds\n" 911 "%s%s::%s%s(unsigned int index) const\n" 913 " if (index > %s) {\n" 914 " throw Exception(\"Index value %%u out of bounds (0..%s)\", index);\n" 916 " return %sdata->%s[index];\n" 918 (*i).getName().c_str(),
919 (*i).getComment().c_str(),
920 (*i).getName().c_str(),
921 (*i).isEnumType() ? (interface_classname +
"::").c_str() :
"",
922 (*i).getPlainAccessType().c_str(),
923 inclusion_prefix.c_str(), classname.c_str(),
924 ( ((*i).getType() ==
"bool" ) ?
"is_" :
""), (*i).getName().c_str(),
925 i->getLength().c_str(), i->getLength().c_str(),
926 (*i).isEnumType() ? (std::string(
"(") + interface_classname +
"::" +
927 i->getPlainAccessType() +
")").c_str() :
"",
928 (*i).getName().c_str() );
932 "/** Get maximum length of %s value.\n" 933 " * @return length of %s value, can be length of the array or number of \n" 934 " * maximum number of characters for a string\n" 937 "%s%s::maxlenof_%s() const\n" 941 i->getName().c_str(), i->getName().c_str(), inclusion_prefix.c_str(),
942 classname.c_str(), i->getName().c_str(),
943 i->getLengthValue() > 0 ? i->getLength().c_str() :
"1" );
946 "/** Set %s value.\n" 948 " * @param new_%s new %s value\n" 951 "%s%s::set_%s(const %s new_%s)\n" 953 (*i).getName().c_str(),
954 (*i).getComment().c_str(),
955 (*i).getName().c_str(), (*i).getName().c_str(),
956 inclusion_prefix.c_str(), classname.c_str(), (*i).getName().c_str(), (*i).getAccessType().c_str(), (*i).getName().c_str()
958 if ( (*i).getType() ==
"string" ) {
960 " strncpy(data->%s, new_%s, sizeof(data->%s));\n",
961 (*i).getName().c_str(), (*i).getName().c_str(), (*i).getName().c_str());
962 }
else if ( (*i).getLength() !=
"" ) {
964 " memcpy(data->%s, new_%s, sizeof(%s) * %s);\n",
965 (*i).getName().c_str(), (*i).getName().c_str(),
966 (*i).getPlainAccessType().c_str(), (*i).getLength().c_str());
969 " data->%s = new_%s;\n",
970 (*i).getName().c_str(), (*i).getName().c_str());
972 fprintf(f,
"%s}\n\n", write_data_changed ?
" data_changed = true;\n" :
"");
974 if ( ((*i).getType() !=
"string") && ((*i).getLengthValue() > 0) ) {
976 "/** Set %s value at given index.\n" 978 " * @param new_%s new %s value\n" 979 " * @param index index for of the value\n" 982 "%s%s::set_%s(unsigned int index, const %s new_%s)\n" 984 " if (index > %s) {\n" 985 " throw Exception(\"Index value %%u out of bounds (0..%s)\", index);\n" 987 " data->%s[index] = new_%s;\n" 990 (*i).getName().c_str(),
991 (*i).getComment().c_str(),
992 (*i).getName().c_str(), (*i).getName().c_str(),
993 inclusion_prefix.c_str(), classname.c_str(), (*i).getName().c_str(),
994 (*i).getPlainAccessType().c_str(), i->getName().c_str(),
995 i->getLength().c_str(), i->getLength().c_str(),
996 i->getName().c_str(), i->getName().c_str(),
997 write_data_changed ?
" data_changed = true;\n" :
"");
1013 std::string classname,
1014 std::vector<InterfaceField> fields,
1015 std::vector<InterfacePseudoMap> pseudo_maps,
1016 std::string inclusion_prefix)
1018 write_methods_cpp(f, interface_classname, classname, fields,
1019 inclusion_prefix,
true);
1021 for (vector<InterfacePseudoMap>::iterator i = pseudo_maps.begin(); i != pseudo_maps.end(); ++i) {
1023 "/** Get %s value.\n" 1025 " * @param key key of the value\n" 1026 " * @return %s value\n" 1029 "%s%s::%s(const %s key) const\n" 1031 (*i).getName().c_str(),
1032 (*i).getComment().c_str(),
1033 (*i).getName().c_str(),
1034 (*i).getType().c_str(),
1035 inclusion_prefix.c_str(), classname.c_str(), (*i).getName().c_str(),
1036 (*i).getKeyType().c_str() );
1039 InterfacePseudoMap::RefList::iterator paref;
1041 for (paref = reflist.begin(); paref != reflist.end(); ++paref) {
1042 fprintf(f,
" %sif (key == %s) {\n" 1043 " return data->%s;\n",
1044 first ?
"" :
"} else ",
1045 paref->second.c_str(), paref->first.c_str());
1048 fprintf(f,
" } else {\n" 1049 " throw Exception(\"Invalid key, cannot retrieve value\");\n" 1054 "/** Set %s value.\n" 1056 " * @param key key of the value\n" 1057 " * @param new_value new value\n" 1060 "%s%s::set_%s(const %s key, const %s new_value)\n" 1062 (*i).getName().c_str(),
1063 (*i).getComment().c_str(),
1064 inclusion_prefix.c_str(), classname.c_str(), (*i).getName().c_str(),
1065 (*i).getKeyType().c_str(), (*i).getType().c_str());
1068 for (paref = reflist.begin(); paref != reflist.end(); ++paref) {
1069 fprintf(f,
" %sif (key == %s) {\n" 1070 " data->%s = new_value;\n",
1071 first ?
"" :
"} else ",
1072 paref->second.c_str(), paref->first.c_str());
1090 std::vector<InterfaceField> fields)
1092 fprintf(f,
"%s/* Methods */\n", is.c_str());
1093 for (vector<InterfaceField>::iterator i = fields.begin(); i != fields.end(); ++i) {
1095 "%s%s %s%s() const;\n",
1096 is.c_str(), (*i).getAccessType().c_str(),
1097 ( ((*i).getType() ==
"bool" ) ?
"is_" :
""),
1098 (*i).getName().c_str());
1100 if ((i->getLengthValue() > 0) && (i->getType() !=
"string")) {
1102 "%s%s %s%s(unsigned int index) const;\n" 1103 "%svoid set_%s(unsigned int index, const %s new_%s);\n",
1104 is.c_str(), i->getPlainAccessType().c_str(),
1105 ( ((*i).getType() ==
"bool" ) ?
"is_" :
""),
1106 (*i).getName().c_str(),
1107 is.c_str(), (*i).getName().c_str(),
1108 i->getPlainAccessType().c_str(), i->getName().c_str());
1112 "%svoid set_%s(const %s new_%s);\n" 1113 "%ssize_t maxlenof_%s() const;\n",
1114 is.c_str(), (*i).getName().c_str(),
1115 i->getAccessType().c_str(), i->getName().c_str(),
1116 is.c_str(), i->getName().c_str()
1130 std::vector<InterfaceField> fields,
1131 std::vector<InterfacePseudoMap> pseudo_maps)
1133 write_methods_h(f, is, fields);
1135 for (vector<InterfacePseudoMap>::iterator i = pseudo_maps.begin(); i != pseudo_maps.end(); ++i) {
1137 "%s%s %s(%s key) const;\n" 1138 "%svoid set_%s(const %s key, const %s new_value);\n",
1139 is.c_str(), (*i).getType().c_str(),
1140 (*i).getName().c_str(), (*i).getKeyType().c_str(),
1141 is.c_str(), (*i).getName().c_str(),
1142 i->getKeyType().c_str(), i->getType().c_str());
1155 "%svirtual Message * create_message(const char *type) const;\n\n" 1156 "%svirtual void copy_values(const Interface *other);\n" 1157 "%svirtual const char * enum_tostring(const char *enumtype, int val) const;\n",
1158 is.c_str(), is.c_str(), is.c_str());
1167 write_header(f, filename_h);
1171 "#include <interface/interface.h>\n" 1172 "#include <interface/message.h>\n" 1173 "#include <interface/field_iterator.h>\n\n" 1174 "namespace fawkes {\n\n" 1175 "class %s : public Interface\n" 1177 " /// @cond INTERNALS\n" 1178 " INTERFACE_MGMT_FRIENDS(%s)\n" 1182 class_name.c_str());
1184 write_constants_h(f);
1186 fprintf(f,
" private:\n");
1188 write_struct(f, class_name +
"_data_t",
" ", data_fields);
1190 fprintf(f,
" %s_data_t *data;\n\n", class_name.c_str());
1192 write_enum_maps_h(f);
1194 fprintf(f,
" public:\n");
1196 write_messages_h(f);
1197 fprintf(f,
" private:\n");
1198 write_ctor_dtor_h(f,
" ", class_name);
1199 fprintf(f,
" public:\n");
1200 write_methods_h(f,
" ", data_fields, pseudo_maps);
1201 write_basemethods_h(f,
" ");
1202 fprintf(f,
"\n};\n\n} // end namespace fawkes\n\n#endif\n");
1211 char timestring[26];
1212 struct tm timestruct;
1213 time_t t = time(NULL);
1214 localtime_r(&t, ×truct);
1215 asctime_r(×truct, timestring);
1216 gendate = timestring;
1221 cpp = fopen(
string(dir + filename_cpp).c_str(),
"w");
1222 h = fopen(
string(dir + filename_h).c_str(),
"w");
1224 if ( cpp == NULL ) {
1225 printf(
"Cannot open cpp file %s%s\n", dir.c_str(), filename_cpp.c_str());
1228 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.