classgeneratorbase.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of qtprotobuf project https://git.semlanik.org/semlanik/qtprotobuf
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "classgeneratorbase.h"
  26. #include "templates.h"
  27. #include "utils.h"
  28. #include <google/protobuf/descriptor.h>
  29. #include <google/protobuf/io/zero_copy_stream.h>
  30. #include <set>
  31. using namespace qtprotobuf;
  32. using namespace ::google::protobuf;
  33. using namespace ::google::protobuf::io;
  34. using namespace ::google::protobuf::compiler;
  35. ClassGeneratorBase::ClassGeneratorBase(std::string mClassName, std::unique_ptr<::google::protobuf::io::ZeroCopyOutputStream> out) : mOutput(std::move(out))
  36. , mPrinter(mOutput.get(), '$')
  37. , mClassName(std::move(mClassName))
  38. , mNamespaceCount(0)
  39. {
  40. }
  41. bool ClassGeneratorBase::producePropertyMap(const FieldDescriptor *field, PropertyMap &propertyMap)
  42. {
  43. std::string typeName = getTypeName(field);
  44. if (typeName.size() <= 0) {
  45. std::cerr << "Type "
  46. << field->type_name()
  47. << " is not supported by Qt Generator"
  48. " please look at https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html"
  49. " for details" << std::endl;
  50. return false;
  51. }
  52. std::string typeNameLower(typeName);
  53. std::transform(std::begin(typeName), std::end(typeName), std::begin(typeNameLower), ::tolower);
  54. std::string capProperty = field->camelcase_name();
  55. capProperty[0] = ::toupper(capProperty[0]);
  56. propertyMap = {{"type", typeName},
  57. {"type_lower", typeNameLower},
  58. {"property_name", field->camelcase_name()},
  59. {"property_name_cap", capProperty}};
  60. return true;
  61. }
  62. void ClassGeneratorBase::printPreamble()
  63. {
  64. mPrinter.Print(PreambleTemplate);
  65. }
  66. void ClassGeneratorBase::printIncludes(const Descriptor *message)
  67. {
  68. PropertyMap properties;
  69. std::set<std::string> existingIncludes;
  70. std::string newinclude;
  71. const char* includeTemplate;
  72. for (int i = 0; i < message->field_count(); i++) {
  73. const FieldDescriptor* field = message->field(i);
  74. if (producePropertyMap(field, properties)) {
  75. if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
  76. newinclude = properties["type_lower"];
  77. includeTemplate = InternalIncludeTemplate;
  78. } else if (field->type() == FieldDescriptor::TYPE_STRING) {
  79. includeTemplate = ExternalIncludeTemplate;
  80. } else {
  81. continue;
  82. }
  83. if (!field->is_repeated()) {
  84. if (existingIncludes.find(newinclude) == std::end(existingIncludes)) {
  85. mPrinter.Print(properties, includeTemplate);
  86. existingIncludes.insert(newinclude);
  87. }
  88. }
  89. }
  90. }
  91. }
  92. void ClassGeneratorBase::printNamespaces(const std::string &package)
  93. {
  94. std::vector<std::string> namespaces;
  95. utils::split(package, namespaces, '.');
  96. mNamespaceCount = namespaces.size();
  97. for (auto ns: namespaces) {
  98. mPrinter.Print({{"namespace", ns}}, NamespaceTemplate);
  99. }
  100. }
  101. void ClassGeneratorBase::printClass()
  102. {
  103. mPrinter.Print({{"classname", mClassName}}, StartTemplate);
  104. }
  105. void ClassGeneratorBase::printProperties(const Descriptor *message)
  106. {
  107. //private section
  108. for (int i = 0; i < message->field_count(); i++) {
  109. printField(message->field(i), PropertyTemplate);
  110. }
  111. for (int i = 0; i < message->field_count(); i++) {
  112. printField(message->field(i), MemberTemplate);
  113. }
  114. printQEnums(message);
  115. //public section
  116. printPublic();
  117. printConstructor();
  118. printCopyFunctionality(message);
  119. printEqualOperator(message);
  120. for (int i = 0; i < message->field_count(); i++) {
  121. printField(message->field(i), GetterTemplate);
  122. }
  123. for (int i = 0; i < message->field_count(); i++) {
  124. auto field = message->field(i);
  125. if (field->type() == FieldDescriptor::TYPE_MESSAGE
  126. || field->type() == FieldDescriptor::TYPE_STRING) {
  127. printField(field, SetterTemplateComplexType);
  128. } else {
  129. printField(field, SetterTemplateSimpleType);
  130. }
  131. }
  132. mPrinter.Print("\nsignals:\n");
  133. for (int i = 0; i < message->field_count(); i++) {
  134. printField(message->field(i), SignalTemplate);
  135. }
  136. }
  137. void ClassGeneratorBase::printField(const FieldDescriptor *field, const char *fieldTemplate)
  138. {
  139. std::map<std::string, std::string> propertyMap;
  140. if (producePropertyMap(field, propertyMap)) {
  141. mPrinter.Print(propertyMap, fieldTemplate);
  142. }
  143. }
  144. void ClassGeneratorBase::enclose()
  145. {
  146. mPrinter.Print("};\n");
  147. while (mNamespaceCount > 0) {
  148. mPrinter.Print(SimpleBlockEnclosureTemplate);
  149. --mNamespaceCount;
  150. }
  151. }
  152. std::string ClassGeneratorBase::getTypeName(const FieldDescriptor *field)
  153. {
  154. std::string typeName;
  155. if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
  156. typeName = field->message_type()->name();
  157. if (field->is_repeated()) {
  158. typeName = typeName.append("Model");
  159. }
  160. } else if (field->type() == FieldDescriptor::TYPE_ENUM) {
  161. if (field->is_repeated()) {
  162. typeName = std::string("QVariantList");
  163. } else {
  164. typeName = field->enum_type()->name();
  165. }
  166. } else {
  167. auto it = TypeReflection.find(field->type());
  168. if (it != std::end(TypeReflection)) {
  169. if (field->is_repeated()) {
  170. typeName = std::string("QVariantList");
  171. } else {
  172. typeName = it->second;
  173. }
  174. }
  175. }
  176. return typeName;
  177. }
  178. void ClassGeneratorBase::printCopyFunctionality(const ::google::protobuf::Descriptor *message)
  179. {
  180. mPrinter.Print({{"classname", mClassName}},
  181. " $classname$(const $classname$ &other) : QObject(other.parent()) {\n");
  182. for (int i = 0; i < message->field_count(); i++) {
  183. printField(message->field(i), CopyClassFunctionalityTemplate);
  184. }
  185. mPrinter.Print(" }\n\n");
  186. mPrinter.Print({{"classname", mClassName}},
  187. " $classname$ &operator =(const $classname$ &other) {\n");
  188. for (int i = 0; i < message->field_count(); i++) {
  189. printField(message->field(i), CopyClassFunctionalityTemplate);
  190. }
  191. mPrinter.Print(" }\n\n");
  192. }
  193. void ClassGeneratorBase::printConstructor()
  194. {
  195. mPrinter.Print({{"classname", mClassName}},
  196. " $classname$(QObject *parent = nullptr) : QObject(parent)\n");
  197. //FIXME: Explicit default values are not allowed in proto3 seems
  198. //this function is useless
  199. // for (int i = 0; i < mMessage->field_count(); i++) {
  200. // const FieldDescriptor* field = mMessage->field(i);
  201. // std::string defaultValue;
  202. // if (field->has_default_value()) {
  203. // switch (field->type()) {
  204. // case FieldDescriptor::TYPE_DOUBLE:
  205. // defaultValue = std::to_string(field->default_value_double());
  206. // break;
  207. // case FieldDescriptor::TYPE_FLOAT:
  208. // defaultValue = std::to_string(field->default_value_float());
  209. // break;
  210. // case FieldDescriptor::TYPE_INT32:
  211. // case FieldDescriptor::TYPE_UINT32:
  212. // case FieldDescriptor::TYPE_SFIXED32:
  213. // case FieldDescriptor::TYPE_SINT32:
  214. // defaultValue = std::to_string(field->default_value_int32());
  215. // break;
  216. // case FieldDescriptor::TYPE_BOOL:
  217. // defaultValue = field->default_value_bool() ? "true" : "false";
  218. // break;
  219. // case FieldDescriptor::TYPE_STRING:
  220. // defaultValue = field->default_value_string();
  221. // break;
  222. // case FieldDescriptor::TYPE_ENUM:
  223. // defaultValue = field->default_value_enum()->name();
  224. // break;
  225. // default:
  226. // std::cerr << "Default value substitution"
  227. // " is not supported for type"
  228. // << field->type_name() << std::endl;
  229. // break;
  230. // }
  231. // if (defaultValue.size() > 0) {
  232. // mPrinter.Print({{"property_name", field->camelcase_name()},
  233. // {"default_value", defaultValue}},
  234. // " , $m_property_name$($default_value$)\n");
  235. // }
  236. // }
  237. // }
  238. mPrinter.Print(" {}\n\n");
  239. }
  240. void ClassGeneratorBase::printPublic()
  241. {
  242. mPrinter.Print("\npublic:\n");
  243. }
  244. void ClassGeneratorBase::printEqualOperator(const Descriptor *message)
  245. {
  246. bool isFirst = true;
  247. PropertyMap properties;
  248. mPrinter.Print({{"type", message->name()}}, EqualOperatorTemplate);
  249. for (int i = 1; i < message->field_count(); i++) {
  250. const FieldDescriptor* field = message->field(i);
  251. if (producePropertyMap(field, properties)) {
  252. if (!isFirst) {
  253. mPrinter.Print("\n && ");
  254. }
  255. isFirst = false;
  256. mPrinter.Print(properties, EqualOperatorPropertyTemplate);
  257. }
  258. }
  259. mPrinter.Print(";\n ");
  260. mPrinter.Print(SimpleBlockEnclosureTemplate);
  261. }