generatorbase.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "generatorbase.h"
  26. #include <google/protobuf/descriptor.h>
  27. #include <google/protobuf/stubs/logging.h>
  28. #include <google/protobuf/stubs/common.h>
  29. #include <google/protobuf/io/printer.h>
  30. #include <google/protobuf/io/zero_copy_stream.h>
  31. #include "utils.h"
  32. #include "templates.h"
  33. using namespace ::QtProtobuf::generator;
  34. using namespace ::google::protobuf;
  35. using namespace ::google::protobuf::compiler;
  36. GeneratorBase::GeneratorBase(Mode mode) : m_mode(mode)
  37. {
  38. }
  39. void GeneratorBase::iterateNonNestedFileds(const ::google::protobuf::FileDescriptor *file, std::function<void(const ::google::protobuf::Descriptor *)> callback) const
  40. {
  41. for (int i = 0; i < file->message_type_count(); i++) {
  42. const Descriptor *message = file->message_type(i);
  43. //Detect nested fields and filter maps fields
  44. int mapsFieldsCount = 0;
  45. for (int j = 0; j < message->nested_type_count(); j++) {
  46. for (int k = 0; k < message->field_count(); k++) {
  47. if (message->field(k)->is_map() && message->field(k)->message_type() == message->nested_type(j)) {
  48. ++mapsFieldsCount;
  49. }
  50. }
  51. }
  52. if (message->nested_type_count() > 0 && message->nested_type_count() > mapsFieldsCount) {
  53. std::cerr << file->name() << ":" << (message->index() + 1) << ": " << " Error: Meta object features not supported for nested classes in " << message->full_name() << std::endl;
  54. continue;
  55. }
  56. callback(message);
  57. }
  58. }
  59. bool GeneratorBase::GenerateAll(const std::vector<const FileDescriptor *> &files, const string &parameter, GeneratorContext *generatorContext, string *error) const
  60. {
  61. PackagesList packageList;
  62. for (auto file : files) {
  63. packageList[file->package()].push_back(file);
  64. }
  65. std::shared_ptr<io::ZeroCopyOutputStream> outfHeader(generatorContext->Open(Templates::GlobalDeclarationsFilename + Templates::ProtoFileSuffix + ".h"));
  66. std::shared_ptr<::google::protobuf::io::Printer> outfHeaderPrinter(new ::google::protobuf::io::Printer(outfHeader.get(), '$'));
  67. outfHeaderPrinter->Print(Templates::DisclaimerTemplate);
  68. outfHeaderPrinter->Print(Templates::PreambleTemplate);
  69. outfHeaderPrinter->Print(Templates::DefaultProtobufIncludesTemplate);
  70. bool addGlobalEnumsHeader = false;
  71. for (auto file : files) {
  72. if (m_mode == SingleMode) {
  73. if (file->message_type_count() > 0) {
  74. outfHeaderPrinter->Print({{"include", utils::extractFileName(file->name()) + Templates::ProtoFileSuffix}}, Templates::InternalIncludeTemplate);
  75. }
  76. } else {
  77. iterateNonNestedFileds(file, [&outfHeaderPrinter, file, &addGlobalEnumsHeader](const ::google::protobuf::Descriptor *message) {
  78. std::string messageName = message->name();
  79. utils::tolower(messageName);
  80. outfHeaderPrinter->Print({{"include", messageName}}, Templates::InternalIncludeTemplate);
  81. if (file->enum_type_count() > 0) {
  82. addGlobalEnumsHeader = true;
  83. }
  84. });
  85. }
  86. }
  87. if (addGlobalEnumsHeader) {
  88. outfHeaderPrinter->Print({{"include", "globalenums"}}, Templates::InternalIncludeTemplate);
  89. }
  90. for (auto package : packageList) {
  91. std::vector<std::string> namespaces;
  92. utils::split(package.first, namespaces, '.');
  93. for (auto ns : namespaces) {
  94. outfHeaderPrinter->Print({{"namespace", ns}}, Templates::NamespaceTemplate);
  95. }
  96. outfHeaderPrinter->Print("inline void qRegisterProtobufTypes() {\n");
  97. outfHeaderPrinter->Indent();
  98. outfHeaderPrinter->Indent();
  99. for (auto file : package.second) {
  100. iterateNonNestedFileds(file, [&outfHeaderPrinter](const ::google::protobuf::Descriptor *message) {
  101. outfHeaderPrinter->Print({{"classname", message->name()}}, "qRegisterProtobufType<$classname$>();\n");
  102. });
  103. if (file->enum_type_count() > 0) {
  104. outfHeaderPrinter->Print("GlobalEnums::registerTypes();\n");
  105. }
  106. }
  107. outfHeaderPrinter->Outdent();
  108. outfHeaderPrinter->Outdent();
  109. outfHeaderPrinter->Print("}\n");
  110. for (size_t i = 0; i < namespaces.size(); i++) {
  111. outfHeaderPrinter->Print("}\n");
  112. }
  113. }
  114. return CodeGenerator::GenerateAll(files, parameter, generatorContext, error);
  115. }