generator.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "generator.h"
  26. #include "templates.h"
  27. #include "classgeneratorbase.h"
  28. #include "protobufclassgenerator.h"
  29. #include "globalenumsgenerator.h"
  30. #include "servergenerator.h"
  31. #include "utils.h"
  32. #include <google/protobuf/stubs/logging.h>
  33. #include <google/protobuf/stubs/common.h>
  34. #include <google/protobuf/io/printer.h>
  35. #include <google/protobuf/io/zero_copy_stream.h>
  36. #include <google/protobuf/descriptor.h>
  37. #include <algorithm>
  38. #include <unordered_map>
  39. #include <vector>
  40. #include <sstream>
  41. #include <string>
  42. #include <list>
  43. using namespace ::qtprotobuf::generator;
  44. using namespace ::google::protobuf;
  45. using namespace ::google::protobuf::compiler;
  46. class QtSourcesGenerator : public ClassGeneratorBase
  47. {
  48. const Descriptor* mMessage;
  49. public:
  50. QtSourcesGenerator(const Descriptor *message, std::unique_ptr<io::ZeroCopyOutputStream> out) :
  51. ClassGeneratorBase(message->full_name(), std::move(out))
  52. , mMessage(message) {}
  53. void run() {
  54. printClassHeaderInclude();
  55. printNamespaces();
  56. printFieldsOrdering();
  57. encloseNamespaces();
  58. }
  59. void printClassHeaderInclude() {
  60. std::string includeFileName = mClassName;
  61. utils::tolower(includeFileName);
  62. mPrinter.Print({{"type_lower", includeFileName}}, InternalIncludeTemplate);
  63. }
  64. void printFieldsOrdering() {
  65. mPrinter.Print({{"type", mClassName}}, FieldsOrderingContainerTemplate);
  66. Indent();
  67. for (int i = 0; i < mMessage->field_count(); i++) {
  68. const FieldDescriptor* field = mMessage->field(i);
  69. if (i != 0) {
  70. mPrinter.Print("\n,");
  71. }
  72. //property_number is incremented by 1 because user properties stating from 1.
  73. //Property with index 0 is "objectName"
  74. mPrinter.Print({{"field_number", std::to_string(field->number())},
  75. {"property_number", std::to_string(i + 1)}}, FieldOrderTemplate);
  76. }
  77. Outdent();
  78. mPrinter.Print(SemicolonBlockEnclosureTemplate);
  79. }
  80. };
  81. bool QtGenerator::Generate(const FileDescriptor *file,
  82. const std::string &/*parameter*/,
  83. GeneratorContext *generatorContext,
  84. std::string *error) const
  85. {
  86. if (file->syntax() != FileDescriptor::SYNTAX_PROTO3) {
  87. *error = "Invalid proto used. This plugin only supports 'proto3' syntax";
  88. return false;
  89. }
  90. std::set<std::string> extractedModels;
  91. for(int i = 0; i < file->message_type_count(); i++) {
  92. const Descriptor *message = file->message_type(i);
  93. std::string baseFilename(message->name());
  94. utils::tolower(baseFilename);
  95. std::string filename = baseFilename + ".h";
  96. ProtobufClassGenerator classGen(message,
  97. std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(filename))));
  98. classGen.run();
  99. std::set<std::string> models = classGen.extractModels();
  100. extractedModels.insert(std::begin(models), std::end(models));
  101. std::string sourceFileName = baseFilename + ".cpp";
  102. QtSourcesGenerator classSourceGen(message,
  103. std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(sourceFileName))));
  104. classSourceGen.run();
  105. }
  106. for(int i = 0; i < file->service_count(); i++) {
  107. const ServiceDescriptor* service = file->service(i);
  108. std::string baseFilename(service->name());
  109. utils::tolower(baseFilename);
  110. std::string headeFilename = baseFilename + ".h";
  111. ServerGenerator serverGen(service,
  112. std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(headeFilename))));
  113. serverGen.run();
  114. }
  115. return true;
  116. }
  117. bool QtGenerator::GenerateAll(const std::vector<const FileDescriptor *> &files, const string &parameter, GeneratorContext *generatorContext, string *error) const
  118. {
  119. std::string globalEnumsFilename = "globalenums.h";
  120. PackagesList packageList;
  121. for (auto file : files) {
  122. packageList[file->package()].push_back(file);
  123. }
  124. GlobalEnumsGenerator enumGen(packageList, std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(globalEnumsFilename))));
  125. enumGen.run();
  126. return CodeGenerator::GenerateAll(files, parameter, generatorContext, error);
  127. }