messagedefinitionprinter.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>, Tatyana Borisova <tanusshhka@mail.ru>
  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 "messagedefinitionprinter.h"
  26. #include <google/protobuf/descriptor.h>
  27. #include "generatoroptions.h"
  28. using namespace QtProtobuf::generator;
  29. using namespace ::google::protobuf;
  30. MessageDefinitionPrinter::MessageDefinitionPrinter(const Descriptor *message, const std::shared_ptr<::google::protobuf::io::Printer> &printer) :
  31. DescriptorPrinterBase<Descriptor>(message, printer)
  32. {
  33. mTypeMap = common::produceMessageTypeMap(message, nullptr);
  34. }
  35. void MessageDefinitionPrinter::printClassDefinitionPrivate()
  36. {
  37. if (common::hasNestedMessages(mDescriptor)) {
  38. mPrinter->Print({{"namespace", mName + Templates::QtProtobufNestedNamespace}}, Templates::NamespaceTemplate);
  39. common::iterateNestedMessages(mDescriptor, [this](const Descriptor *nestedMessage) {
  40. MessageDefinitionPrinter nestedPrinter(nestedMessage, mPrinter);
  41. nestedPrinter.printClassDefinitionPrivate();
  42. });
  43. mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  44. }
  45. printDestructor();
  46. printFieldsOrdering();
  47. printRegisterBody();
  48. printConstructors();
  49. printCopyFunctionality();
  50. printMoveSemantic();
  51. printComparisonOperators();
  52. printGetters();
  53. }
  54. void MessageDefinitionPrinter::printClassDefinition()
  55. {
  56. printNamespaces();
  57. printClassDefinitionPrivate();
  58. encloseNamespaces();
  59. }
  60. void MessageDefinitionPrinter::printRegisterBody()
  61. {
  62. mPrinter->Print(mTypeMap,
  63. Templates::ManualRegistrationComplexTypeDefinition);
  64. Indent();
  65. if (GeneratorOptions::instance().hasQml()) {
  66. mPrinter->Print(mTypeMap, Templates::RegisterQmlListPropertyMetaTypeTemplate);
  67. mPrinter->Print(mTypeMap, Templates::QmlRegisterTypeTemplate);
  68. }
  69. common::iterateMessageFields(mDescriptor, [this](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  70. if (field->type() == FieldDescriptor::TYPE_ENUM
  71. && common::isLocalEnum(field->enum_type(), mDescriptor)) {
  72. mPrinter->Print(propertyMap, Templates::RegisterLocalEnumTemplate);
  73. } else if (field->is_map()) {
  74. mPrinter->Print(propertyMap, Templates::RegisterMapTemplate);
  75. }
  76. });
  77. Outdent();
  78. mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  79. }
  80. void MessageDefinitionPrinter::printFieldsOrdering() {
  81. mPrinter->Print({{"type", mName}}, Templates::FieldsOrderingContainerTemplate);
  82. Indent();
  83. for (int i = 0; i < mDescriptor->field_count(); i++) {
  84. const FieldDescriptor *field = mDescriptor->field(i);
  85. if (i != 0) {
  86. mPrinter->Print("\n,");
  87. }
  88. //property_number is incremented by 1 because user properties stating from 1.
  89. //Property with index 0 is "objectName"
  90. mPrinter->Print({{"field_number", std::to_string(field->number())},
  91. {"property_number", std::to_string(i + 1)}}, Templates::FieldOrderTemplate);
  92. }
  93. Outdent();
  94. mPrinter->Print(Templates::SemicolonBlockEnclosureTemplate);
  95. mPrinter->Print("\n");
  96. }
  97. void MessageDefinitionPrinter::printConstructors() {
  98. for (int i = 0; i <= mDescriptor->field_count(); i++) {
  99. mPrinter->Print(mTypeMap, Templates::ProtoConstructorDefinitionBeginTemplate);
  100. printConstructor(i);
  101. mPrinter->Print(mTypeMap, Templates::ProtoConstructorDefinitionEndTemplate);
  102. printInitializationList(i);
  103. mPrinter->Print(Templates::ConstructorContentTemplate);
  104. }
  105. if (mDescriptor->full_name() == std::string("google.protobuf.Timestamp")) {
  106. mPrinter->Print("Timestamp::Timestamp(const QDateTime &datetime, QObject *parent) : QObject(parent)\n"
  107. ", m_seconds(datetime.toMSecsSinceEpoch() / 1000)\n"
  108. ", m_nanos((datetime.toMSecsSinceEpoch() % 1000) * 1000)\n"
  109. "{}\n"
  110. "Timestamp::operator QDateTime() const\n"
  111. "{\n"
  112. " return QDateTime::fromMSecsSinceEpoch(m_seconds * 1000 + m_nanos / 1000);\n"
  113. "}\n");
  114. }
  115. }
  116. void MessageDefinitionPrinter::printConstructor(int fieldCount)
  117. {
  118. for (int i = 0; i < fieldCount; i++) {
  119. const FieldDescriptor *field = mDescriptor->field(i);
  120. const char *parameterTemplate = Templates::ConstructorParameterTemplate;
  121. FieldDescriptor::Type fieldType = field->type();
  122. if (field->is_repeated() && !field->is_map()) {
  123. parameterTemplate = Templates::ConstructorRepeatedParameterTemplate;
  124. } else if (fieldType == FieldDescriptor::TYPE_BYTES
  125. || fieldType == FieldDescriptor::TYPE_STRING
  126. || fieldType == FieldDescriptor::TYPE_MESSAGE
  127. || field->is_map()) {
  128. parameterTemplate = Templates::ConstructorMessageParameterTemplate;
  129. }
  130. mPrinter->Print(common::producePropertyMap(field, mDescriptor), parameterTemplate);
  131. }
  132. }
  133. void MessageDefinitionPrinter::printInitializationList(int fieldCount)
  134. {
  135. for (int i = 0; i < mDescriptor->field_count(); i++) {
  136. const FieldDescriptor *field = mDescriptor->field(i);
  137. auto propertyMap = common::producePropertyMap(field, mDescriptor);
  138. propertyMap["initializer"] = "";
  139. if (!field->is_repeated() && !field->is_map()) {
  140. switch (field->type()) {
  141. case FieldDescriptor::TYPE_DOUBLE:
  142. case FieldDescriptor::TYPE_FLOAT:
  143. propertyMap["initializer"] = "0.0";
  144. break;
  145. case FieldDescriptor::TYPE_FIXED32:
  146. case FieldDescriptor::TYPE_FIXED64:
  147. case FieldDescriptor::TYPE_INT32:
  148. case FieldDescriptor::TYPE_INT64:
  149. case FieldDescriptor::TYPE_SINT32:
  150. case FieldDescriptor::TYPE_SINT64:
  151. case FieldDescriptor::TYPE_UINT32:
  152. case FieldDescriptor::TYPE_UINT64:
  153. propertyMap["initializer"] = "0";
  154. break;
  155. case FieldDescriptor::TYPE_BOOL:
  156. propertyMap["initializer"] = "false";
  157. break;
  158. case FieldDescriptor::TYPE_ENUM:
  159. propertyMap["initializer"] = propertyMap["scope_type"] + "::" + field->enum_type()->value(0)->name();
  160. break;
  161. default:
  162. break;
  163. }
  164. }
  165. if (common::isPureMessage(field)) {
  166. if (i < fieldCount) {
  167. mPrinter->Print(propertyMap, Templates::MessagePropertyInitializerTemplate);
  168. } else {
  169. mPrinter->Print(propertyMap, Templates::MessagePropertyDefaultInitializerTemplate);
  170. }
  171. } else {
  172. if (i < fieldCount) {
  173. mPrinter->Print(propertyMap, Templates::PropertyInitializerTemplate);
  174. } else {
  175. if (!propertyMap["initializer"].empty()) {
  176. mPrinter->Print(propertyMap, Templates::PropertyDefaultInitializerTemplate);
  177. }
  178. }
  179. }
  180. }
  181. }
  182. void MessageDefinitionPrinter::printCopyFunctionality()
  183. {
  184. assert(mDescriptor != nullptr);
  185. const char *constructorTemplate = Templates::CopyConstructorDefinitionTemplate;
  186. const char *assignmentOperatorTemplate = Templates::AssignmentOperatorDefinitionTemplate;
  187. if (mDescriptor->field_count() <= 0) {
  188. constructorTemplate = Templates::EmptyCopyConstructorDefinitionTemplate;
  189. assignmentOperatorTemplate = Templates::EmptyAssignmentOperatorDefinitionTemplate;
  190. }
  191. mPrinter->Print({{"classname", mName}},
  192. constructorTemplate);
  193. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  194. if (common::isPureMessage(field)) {
  195. mPrinter->Print(propertyMap, Templates::MessagePropertyDefaultInitializerTemplate);
  196. }
  197. });
  198. mPrinter->Print("\n{\n");
  199. Indent();
  200. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  201. if (common::isPureMessage(field)) {
  202. mPrinter->Print(propertyMap, Templates::CopyComplexFieldTemplate);
  203. } else {
  204. mPrinter->Print(propertyMap, Templates::CopyFieldTemplate);
  205. }
  206. });
  207. Outdent();
  208. mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  209. mPrinter->Print({{"classname", mName}}, assignmentOperatorTemplate);
  210. Indent();
  211. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  212. if (common::isPureMessage(field)) {
  213. mPrinter->Print(propertyMap, Templates::CopyComplexFieldTemplate);
  214. } else {
  215. mPrinter->Print(propertyMap, Templates::CopyFieldTemplate);
  216. }
  217. });
  218. mPrinter->Print(Templates::AssignmentOperatorReturnTemplate);
  219. Outdent();
  220. mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  221. }
  222. void MessageDefinitionPrinter::printMoveSemantic()
  223. {
  224. assert(mDescriptor != nullptr);
  225. const char *constructorTemplate = Templates::MoveConstructorDefinitionTemplate;
  226. const char *assignmentOperatorTemplate = Templates::MoveAssignmentOperatorDefinitionTemplate;
  227. if (mDescriptor->field_count() <= 0) {
  228. constructorTemplate = Templates::EmptyMoveConstructorDefinitionTemplate;
  229. assignmentOperatorTemplate = Templates::EmptyMoveAssignmentOperatorDefinitionTemplate;
  230. }
  231. mPrinter->Print({{"classname", mName}},
  232. constructorTemplate);
  233. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  234. if (common::isPureMessage(field)) {
  235. mPrinter->Print(propertyMap, Templates::MessagePropertyDefaultInitializerTemplate);
  236. }
  237. });
  238. mPrinter->Print("\n{\n");
  239. Indent();
  240. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  241. if (field->type() == FieldDescriptor::TYPE_MESSAGE
  242. || field->type() == FieldDescriptor::TYPE_STRING
  243. || field->type() == FieldDescriptor::TYPE_BYTES
  244. || field->is_repeated()) {
  245. if (common::isPureMessage(field)) {
  246. mPrinter->Print(propertyMap, Templates::MoveMessageFieldTemplate);
  247. } else {
  248. mPrinter->Print(propertyMap, Templates::MoveComplexFieldConstructorTemplate);
  249. }
  250. } else {
  251. if (field->type() != FieldDescriptor::TYPE_ENUM) {
  252. mPrinter->Print(propertyMap, Templates::MoveFieldTemplate);
  253. }
  254. else {
  255. mPrinter->Print(propertyMap, Templates::EnumMoveFieldTemplate);
  256. }
  257. }
  258. });
  259. Outdent();
  260. mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  261. mPrinter->Print({{"classname", mName}}, assignmentOperatorTemplate);
  262. Indent();
  263. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  264. if (field->type() == FieldDescriptor::TYPE_MESSAGE
  265. || field->type() == FieldDescriptor::TYPE_STRING
  266. || field->type() == FieldDescriptor::TYPE_BYTES
  267. || field->is_repeated()) {
  268. if (common::isPureMessage(field)) {
  269. mPrinter->Print(propertyMap, Templates::MoveMessageFieldTemplate);
  270. } else {
  271. mPrinter->Print(propertyMap, Templates::MoveComplexFieldTemplate);
  272. }
  273. } else {
  274. if (field->type() != FieldDescriptor::TYPE_ENUM) {
  275. mPrinter->Print(propertyMap, Templates::MoveFieldTemplate);
  276. }
  277. else {
  278. mPrinter->Print(propertyMap, Templates::EnumMoveFieldTemplate);
  279. }
  280. }
  281. });
  282. mPrinter->Print(Templates::AssignmentOperatorReturnTemplate);
  283. Outdent();
  284. mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  285. }
  286. void MessageDefinitionPrinter::printComparisonOperators()
  287. {
  288. assert(mDescriptor != nullptr);
  289. if (mDescriptor->field_count() <= 0) {
  290. mPrinter->Print({{"classname", mName}}, Templates::EmptyEqualOperatorDefinitionTemplate);
  291. mPrinter->Print({{"classname", mName}}, Templates::NotEqualOperatorDefinitionTemplate);
  292. return;
  293. }
  294. mPrinter->Print({{"classname", mName}}, Templates::EqualOperatorDefinitionTemplate);
  295. bool isFirst = true;
  296. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, PropertyMap &propertyMap) {
  297. if (!isFirst) {
  298. mPrinter->Print("\n&& ");
  299. } else {
  300. Indent();
  301. Indent();
  302. isFirst = false;
  303. }
  304. if (common::isPureMessage(field)) {
  305. mPrinter->Print(propertyMap, Templates::EqualOperatorMessagePropertyTemplate);
  306. } else {
  307. mPrinter->Print(propertyMap, Templates::EqualOperatorPropertyTemplate);
  308. }
  309. });
  310. //Only if at least one field "copied"
  311. if (!isFirst) {
  312. Outdent();
  313. Outdent();
  314. }
  315. mPrinter->Print(";\n");
  316. mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  317. mPrinter->Print({{"classname", mName}}, Templates::NotEqualOperatorDefinitionTemplate);
  318. }
  319. void MessageDefinitionPrinter::printGetters()
  320. {
  321. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, PropertyMap &propertyMap) {
  322. if (common::isPureMessage(field)) {
  323. mPrinter->Print(propertyMap, Templates::GetterPrivateMessageDefinitionTemplate);
  324. mPrinter->Print(propertyMap, Templates::GetterMessageDefinitionTemplate);
  325. }
  326. if (field->is_repeated()) {
  327. if (field->type() == FieldDescriptor::TYPE_MESSAGE && !field->is_map() && !common::isQtType(field)
  328. && GeneratorOptions::instance().hasQml()) {
  329. mPrinter->Print(propertyMap, Templates::GetterQmlListDefinitionTemplate);
  330. }
  331. }
  332. });
  333. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, PropertyMap &propertyMap) {
  334. switch (field->type()) {
  335. case FieldDescriptor::TYPE_MESSAGE:
  336. if (!field->is_map() && !field->is_repeated() && !common::isQtType(field)) {
  337. mPrinter->Print(propertyMap, Templates::SetterPrivateTemplateDefinitionMessageType);
  338. mPrinter->Print(propertyMap, Templates::SetterTemplateDefinitionMessageType);
  339. } else {
  340. mPrinter->Print(propertyMap, Templates::SetterTemplateDefinitionComplexType);
  341. }
  342. break;
  343. case FieldDescriptor::FieldDescriptor::TYPE_STRING:
  344. case FieldDescriptor::FieldDescriptor::TYPE_BYTES:
  345. mPrinter->Print(propertyMap, Templates::SetterTemplateDefinitionComplexType);
  346. break;
  347. default:
  348. break;
  349. }
  350. });
  351. }
  352. void MessageDefinitionPrinter::printDestructor()
  353. {
  354. mPrinter->Print({{"classname", mName}}, Templates::RegistrarTemplate);
  355. mPrinter->Print({{"classname", mName}}, "$classname$::~$classname$()\n"
  356. "{}\n\n");
  357. }