protobufsourcegenerator.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "protobufsourcegenerator.h"
  26. #include <google/protobuf/descriptor.h>
  27. #include <google/protobuf/io/zero_copy_stream.h>
  28. using namespace QtProtobuf::generator;
  29. using namespace ::google::protobuf;
  30. using namespace ::google::protobuf::io;
  31. using namespace ::google::protobuf::compiler;
  32. ProtobufSourceGenerator::ProtobufSourceGenerator(const google::protobuf::Descriptor *message,
  33. std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out) :
  34. ClassSourceGeneratorBase(message->full_name(), std::move(out))
  35. , mMessage(message)
  36. {
  37. }
  38. void ProtobufSourceGenerator::printRegisterBody()
  39. {
  40. const std::map<std::string, std::string> registrationProperties = {{"classname", mClassName},
  41. {"namespaces", mNamespacesColonDelimited},
  42. {"package", mMessage->file()->package()}
  43. };
  44. mPrinter.Print(registrationProperties,
  45. Templates::ComplexTypeRegistrationTemplate);
  46. Indent();
  47. mPrinter.Print(registrationProperties, Templates::RegisterQmlListPropertyMetaTypeTemplate);
  48. mPrinter.Print(registrationProperties, Templates::QmlRegisterTypeTemplate);
  49. for (int i = 0; i < mMessage->field_count(); i++) {
  50. const FieldDescriptor *field = mMessage->field(i);
  51. if (field->type() == FieldDescriptor::TYPE_ENUM
  52. && isLocalMessageEnum(mMessage, field)) {
  53. mPrinter.Print({{"type", mClassName + "::" + field->enum_type()->name() + Templates::ListSuffix},
  54. {"namespaces", mNamespacesColonDelimited}},
  55. Templates::RegisterMetaTypeTemplateNoNamespace);
  56. mPrinter.Print({{"type", mClassName + "::" + field->enum_type()->name() + Templates::ListSuffix},
  57. {"namespaces", mNamespacesColonDelimited}},
  58. Templates::RegisterMetaTypeTemplate);
  59. mPrinter.Print({{"type", mClassName + "::" + field->enum_type()->name()}},
  60. Templates::RegisterEnumSerializersTemplate);
  61. } else if (field->is_map()) {
  62. mPrinter.Print({{"type", field->message_type()->name()},
  63. {"namespaces", mClassName}},
  64. Templates::RegisterMetaTypeTemplate);
  65. mPrinter.Print({{"type", field->message_type()->name()},
  66. {"namespaces", mNamespacesColonDelimited + "::" + mClassName}},
  67. Templates::RegisterMetaTypeTemplate);
  68. mPrinter.Print({{"classname", mClassName},
  69. {"type", field->message_type()->name()},
  70. {"key_type", getTypeName(field->message_type()->field(0), mMessage)},
  71. {"value_type", getTypeName(field->message_type()->field(1), mMessage)}},
  72. Templates::MapSerializationRegisterTemplate);
  73. }
  74. }
  75. mPrinter.Print({{"classname", mClassName}}, Templates::RegisterSerializersTemplate);
  76. Outdent();
  77. mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
  78. printRegistrationHelperInvokation();
  79. mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
  80. }
  81. void ProtobufSourceGenerator::printFieldsOrdering() {
  82. mPrinter.Print({{"type", mClassName}}, Templates::FieldsOrderingContainerTemplate);
  83. Indent();
  84. for (int i = 0; i < mMessage->field_count(); i++) {
  85. const FieldDescriptor *field = mMessage->field(i);
  86. if (i != 0) {
  87. mPrinter.Print("\n,");
  88. }
  89. //property_number is incremented by 1 because user properties stating from 1.
  90. //Property with index 0 is "objectName"
  91. mPrinter.Print({{"field_number", std::to_string(field->number())},
  92. {"property_number", std::to_string(i + 1)}}, Templates::FieldOrderTemplate);
  93. }
  94. Outdent();
  95. mPrinter.Print(Templates::SemicolonBlockEnclosureTemplate);
  96. mPrinter.Print("\n");
  97. }
  98. void ProtobufSourceGenerator::printRegistrationHelperInvokation()
  99. {
  100. mPrinter.Print(Templates::RegistratorTemplate);
  101. }
  102. void ProtobufSourceGenerator::printConstructor()
  103. {
  104. std::string parameterList;
  105. for (int i = 0; i < mMessage->field_count(); i++) {
  106. const FieldDescriptor *field = mMessage->field(i);
  107. std::string fieldTypeName = getTypeName(field, mMessage);
  108. std::string fieldName = field->name();
  109. fieldName[0] = static_cast<char>(::tolower(fieldName[0]));
  110. if (field->is_repeated() || field->is_map()) {
  111. parameterList += "const " + fieldTypeName + " &" + fieldName;
  112. } else {
  113. switch (field->type()) {
  114. case FieldDescriptor::TYPE_DOUBLE:
  115. case FieldDescriptor::TYPE_FLOAT:
  116. parameterList += fieldTypeName + " " + fieldName;
  117. break;
  118. case FieldDescriptor::TYPE_FIXED32:
  119. case FieldDescriptor::TYPE_FIXED64:
  120. case FieldDescriptor::TYPE_INT32:
  121. case FieldDescriptor::TYPE_INT64:
  122. case FieldDescriptor::TYPE_SINT32:
  123. case FieldDescriptor::TYPE_SINT64:
  124. case FieldDescriptor::TYPE_UINT32:
  125. case FieldDescriptor::TYPE_UINT64:
  126. parameterList += fieldTypeName + " " + fieldName;
  127. break;
  128. case FieldDescriptor::TYPE_BOOL:
  129. parameterList += fieldTypeName + " " + fieldName;
  130. break;
  131. case FieldDescriptor::TYPE_BYTES:
  132. case FieldDescriptor::TYPE_STRING:
  133. case FieldDescriptor::TYPE_MESSAGE:
  134. parameterList += "const " + fieldTypeName + " &" + fieldName;
  135. break;
  136. default:
  137. parameterList += fieldTypeName + " " + fieldName;
  138. break;
  139. }
  140. }
  141. parameterList += ", ";
  142. }
  143. mPrinter.Print({{"classname", mClassName},
  144. {"parameter_list", parameterList}}, Templates::ProtoConstructorDefinitionTemplate);
  145. for (int i = 0; i < mMessage->field_count(); i++) {
  146. const FieldDescriptor *field = mMessage->field(i);
  147. std::string fieldName = field->name();
  148. fieldName[0] = static_cast<char>(::tolower(fieldName[0]));
  149. mPrinter.Print({{"property_name", fieldName}}, Templates::PropertyInitializerTemplate);
  150. }
  151. mPrinter.Print(Templates::ConstructorContentTemplate);
  152. }