messagedefinitionprinter.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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", mTypeMap["classname"] + 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", mTypeMap["classname"]}}, 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)},
  92. {"json_name", field->json_name()}}, Templates::FieldOrderTemplate);
  93. }
  94. Outdent();
  95. mPrinter->Print(Templates::SemicolonBlockEnclosureTemplate);
  96. mPrinter->Print("\n");
  97. }
  98. void MessageDefinitionPrinter::printConstructors() {
  99. for (int i = 0; i <= mDescriptor->field_count(); i++) {
  100. mPrinter->Print(mTypeMap, Templates::ProtoConstructorDefinitionBeginTemplate);
  101. printConstructor(i);
  102. mPrinter->Print(mTypeMap, Templates::ProtoConstructorDefinitionEndTemplate);
  103. printInitializationList(i);
  104. mPrinter->Print(Templates::ConstructorContentTemplate);
  105. }
  106. if (mDescriptor->full_name() == std::string("google.protobuf.Timestamp")) {
  107. mPrinter->Print("Timestamp::Timestamp(const QDateTime &datetime, QObject *parent) : QObject(parent)\n"
  108. ", m_seconds(datetime.toMSecsSinceEpoch() / 1000)\n"
  109. ", m_nanos((datetime.toMSecsSinceEpoch() % 1000) * 1000)\n"
  110. "{}\n"
  111. "Timestamp::operator QDateTime() const\n"
  112. "{\n"
  113. " return QDateTime::fromMSecsSinceEpoch(m_seconds * 1000 + m_nanos / 1000);\n"
  114. "}\n");
  115. }
  116. }
  117. void MessageDefinitionPrinter::printConstructor(int fieldCount)
  118. {
  119. for (int i = 0; i < fieldCount; i++) {
  120. const FieldDescriptor *field = mDescriptor->field(i);
  121. const char *parameterTemplate = Templates::ConstructorParameterTemplate;
  122. FieldDescriptor::Type fieldType = field->type();
  123. if (field->is_repeated() && !field->is_map()) {
  124. parameterTemplate = Templates::ConstructorRepeatedParameterTemplate;
  125. } else if (fieldType == FieldDescriptor::TYPE_BYTES
  126. || fieldType == FieldDescriptor::TYPE_STRING
  127. || fieldType == FieldDescriptor::TYPE_MESSAGE
  128. || field->is_map()) {
  129. parameterTemplate = Templates::ConstructorMessageParameterTemplate;
  130. }
  131. mPrinter->Print(common::producePropertyMap(field, mDescriptor), parameterTemplate);
  132. }
  133. }
  134. void MessageDefinitionPrinter::printInitializationList(int fieldCount)
  135. {
  136. for (int i = 0; i < mDescriptor->field_count(); i++) {
  137. const FieldDescriptor *field = mDescriptor->field(i);
  138. auto propertyMap = common::producePropertyMap(field, mDescriptor);
  139. propertyMap["initializer"] = "";
  140. if (!field->is_repeated() && !field->is_map()) {
  141. switch (field->type()) {
  142. case FieldDescriptor::TYPE_DOUBLE:
  143. case FieldDescriptor::TYPE_FLOAT:
  144. propertyMap["initializer"] = "0.0";
  145. break;
  146. case FieldDescriptor::TYPE_FIXED32:
  147. case FieldDescriptor::TYPE_FIXED64:
  148. case FieldDescriptor::TYPE_INT32:
  149. case FieldDescriptor::TYPE_INT64:
  150. case FieldDescriptor::TYPE_SINT32:
  151. case FieldDescriptor::TYPE_SINT64:
  152. case FieldDescriptor::TYPE_UINT32:
  153. case FieldDescriptor::TYPE_UINT64:
  154. propertyMap["initializer"] = "0";
  155. break;
  156. case FieldDescriptor::TYPE_BOOL:
  157. propertyMap["initializer"] = "false";
  158. break;
  159. case FieldDescriptor::TYPE_ENUM:
  160. propertyMap["initializer"] = propertyMap["scope_type"] + "::" + field->enum_type()->value(0)->name();
  161. break;
  162. default:
  163. break;
  164. }
  165. }
  166. if (common::isPureMessage(field)) {
  167. if (i < fieldCount) {
  168. mPrinter->Print(propertyMap, Templates::MessagePropertyInitializerTemplate);
  169. } else {
  170. mPrinter->Print(propertyMap, Templates::MessagePropertyDefaultInitializerTemplate);
  171. }
  172. } else {
  173. if (i < fieldCount) {
  174. mPrinter->Print(propertyMap, Templates::PropertyInitializerTemplate);
  175. } else {
  176. if (!propertyMap["initializer"].empty()) {
  177. mPrinter->Print(propertyMap, Templates::PropertyDefaultInitializerTemplate);
  178. }
  179. }
  180. }
  181. }
  182. }
  183. void MessageDefinitionPrinter::printCopyFunctionality()
  184. {
  185. assert(mDescriptor != nullptr);
  186. const char *constructorTemplate = Templates::CopyConstructorDefinitionTemplate;
  187. const char *assignmentOperatorTemplate = Templates::AssignmentOperatorDefinitionTemplate;
  188. if (mDescriptor->field_count() <= 0) {
  189. constructorTemplate = Templates::EmptyCopyConstructorDefinitionTemplate;
  190. assignmentOperatorTemplate = Templates::EmptyAssignmentOperatorDefinitionTemplate;
  191. }
  192. mPrinter->Print(mTypeMap,
  193. constructorTemplate);
  194. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  195. if (common::isPureMessage(field)) {
  196. mPrinter->Print(propertyMap, Templates::MessagePropertyDefaultInitializerTemplate);
  197. }
  198. });
  199. mPrinter->Print("\n{\n");
  200. Indent();
  201. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  202. if (common::isPureMessage(field)) {
  203. mPrinter->Print(propertyMap, Templates::CopyComplexFieldTemplate);
  204. } else {
  205. mPrinter->Print(propertyMap, Templates::CopyFieldTemplate);
  206. }
  207. });
  208. Outdent();
  209. mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  210. mPrinter->Print(mTypeMap, assignmentOperatorTemplate);
  211. Indent();
  212. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  213. if (common::isPureMessage(field)) {
  214. mPrinter->Print(propertyMap, Templates::AssignComplexFieldTemplate);
  215. } else {
  216. mPrinter->Print(propertyMap, Templates::CopyFieldTemplate);
  217. }
  218. });
  219. mPrinter->Print(Templates::AssignmentOperatorReturnTemplate);
  220. Outdent();
  221. mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  222. }
  223. void MessageDefinitionPrinter::printMoveSemantic()
  224. {
  225. assert(mDescriptor != nullptr);
  226. const char *constructorTemplate = Templates::MoveConstructorDefinitionTemplate;
  227. const char *assignmentOperatorTemplate = Templates::MoveAssignmentOperatorDefinitionTemplate;
  228. if (mDescriptor->field_count() <= 0) {
  229. constructorTemplate = Templates::EmptyMoveConstructorDefinitionTemplate;
  230. assignmentOperatorTemplate = Templates::EmptyMoveAssignmentOperatorDefinitionTemplate;
  231. }
  232. mPrinter->Print(mTypeMap,
  233. constructorTemplate);
  234. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  235. if (common::isPureMessage(field)) {
  236. mPrinter->Print(propertyMap, Templates::MessagePropertyDefaultInitializerTemplate);
  237. }
  238. });
  239. mPrinter->Print("\n{\n");
  240. Indent();
  241. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  242. if (field->type() == FieldDescriptor::TYPE_MESSAGE
  243. || field->type() == FieldDescriptor::TYPE_STRING
  244. || field->type() == FieldDescriptor::TYPE_BYTES
  245. || field->is_repeated()) {
  246. if (common::isPureMessage(field)) {
  247. mPrinter->Print(propertyMap, Templates::MoveMessageFieldTemplate);
  248. } else {
  249. mPrinter->Print(propertyMap, Templates::MoveComplexFieldConstructorTemplate);
  250. }
  251. } else {
  252. if (field->type() != FieldDescriptor::TYPE_ENUM) {
  253. mPrinter->Print(propertyMap, Templates::MoveFieldTemplate);
  254. }
  255. else {
  256. mPrinter->Print(propertyMap, Templates::EnumMoveFieldTemplate);
  257. }
  258. }
  259. });
  260. Outdent();
  261. mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  262. mPrinter->Print(mTypeMap, assignmentOperatorTemplate);
  263. Indent();
  264. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  265. if (field->type() == FieldDescriptor::TYPE_MESSAGE
  266. || field->type() == FieldDescriptor::TYPE_STRING
  267. || field->type() == FieldDescriptor::TYPE_BYTES
  268. || field->is_repeated()) {
  269. if (common::isPureMessage(field)) {
  270. mPrinter->Print(propertyMap, Templates::MoveAssignMessageFieldTemplate);
  271. } else {
  272. mPrinter->Print(propertyMap, Templates::MoveComplexFieldTemplate);
  273. }
  274. } else {
  275. if (field->type() != FieldDescriptor::TYPE_ENUM) {
  276. mPrinter->Print(propertyMap, Templates::MoveFieldTemplate);
  277. }
  278. else {
  279. mPrinter->Print(propertyMap, Templates::EnumMoveFieldTemplate);
  280. }
  281. }
  282. });
  283. mPrinter->Print(Templates::AssignmentOperatorReturnTemplate);
  284. Outdent();
  285. mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  286. }
  287. void MessageDefinitionPrinter::printComparisonOperators()
  288. {
  289. assert(mDescriptor != nullptr);
  290. if (mDescriptor->field_count() <= 0) {
  291. mPrinter->Print(mTypeMap, Templates::EmptyEqualOperatorDefinitionTemplate);
  292. mPrinter->Print(mTypeMap, Templates::NotEqualOperatorDefinitionTemplate);
  293. return;
  294. }
  295. mPrinter->Print(mTypeMap, Templates::EqualOperatorDefinitionTemplate);
  296. bool isFirst = true;
  297. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, PropertyMap &propertyMap) {
  298. if (!isFirst) {
  299. mPrinter->Print("\n&& ");
  300. } else {
  301. Indent();
  302. Indent();
  303. isFirst = false;
  304. }
  305. if (common::isPureMessage(field)) {
  306. mPrinter->Print(propertyMap, Templates::EqualOperatorMessagePropertyTemplate);
  307. } else if (field->type() == FieldDescriptor::TYPE_MESSAGE && field->is_repeated()) {
  308. mPrinter->Print(propertyMap, Templates::EqualOperatorRepeatedPropertyTemplate);
  309. } else {
  310. mPrinter->Print(propertyMap, Templates::EqualOperatorPropertyTemplate);
  311. }
  312. });
  313. //Only if at least one field "copied"
  314. if (!isFirst) {
  315. Outdent();
  316. Outdent();
  317. }
  318. mPrinter->Print(";\n");
  319. mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  320. mPrinter->Print(mTypeMap, Templates::NotEqualOperatorDefinitionTemplate);
  321. }
  322. void MessageDefinitionPrinter::printGetters()
  323. {
  324. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, PropertyMap &propertyMap) {
  325. if (common::isPureMessage(field)) {
  326. mPrinter->Print(propertyMap, Templates::GetterPrivateMessageDefinitionTemplate);
  327. mPrinter->Print(propertyMap, Templates::GetterMessageDefinitionTemplate);
  328. }
  329. if (field->is_repeated()) {
  330. if (field->type() == FieldDescriptor::TYPE_MESSAGE && !field->is_map() && !common::isQtType(field)
  331. && GeneratorOptions::instance().hasQml()) {
  332. mPrinter->Print(propertyMap, Templates::GetterQmlListDefinitionTemplate);
  333. }
  334. }
  335. });
  336. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, PropertyMap &propertyMap) {
  337. switch (field->type()) {
  338. case FieldDescriptor::TYPE_MESSAGE:
  339. if (!field->is_map() && !field->is_repeated() && !common::isQtType(field)) {
  340. mPrinter->Print(propertyMap, Templates::SetterPrivateTemplateDefinitionMessageType);
  341. mPrinter->Print(propertyMap, Templates::SetterTemplateDefinitionMessageType);
  342. } else {
  343. mPrinter->Print(propertyMap, Templates::SetterTemplateDefinitionComplexType);
  344. }
  345. break;
  346. case FieldDescriptor::FieldDescriptor::TYPE_STRING:
  347. case FieldDescriptor::FieldDescriptor::TYPE_BYTES:
  348. mPrinter->Print(propertyMap, Templates::SetterTemplateDefinitionComplexType);
  349. break;
  350. default:
  351. break;
  352. }
  353. });
  354. }
  355. void MessageDefinitionPrinter::printDestructor()
  356. {
  357. mPrinter->Print(mTypeMap, Templates::RegistrarTemplate);
  358. mPrinter->Print(mTypeMap, "$classname$::~$classname$()\n"
  359. "{}\n\n");
  360. }