generator.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "protobufsourcegenerator.h"
  30. #include "globalenumsgenerator.h"
  31. #include "globalenumssourcegenerator.h"
  32. #include "servergenerator.h"
  33. #include "clientgenerator.h"
  34. #include "clientsourcegenerator.h"
  35. #include "utils.h"
  36. #include <iostream>
  37. #include <google/protobuf/stubs/logging.h>
  38. #include <google/protobuf/stubs/common.h>
  39. #include <google/protobuf/io/printer.h>
  40. #include <google/protobuf/io/zero_copy_stream.h>
  41. #include <google/protobuf/descriptor.h>
  42. using namespace ::QtProtobuf::generator;
  43. using namespace ::google::protobuf;
  44. using namespace ::google::protobuf::compiler;
  45. bool QtGenerator::Generate(const FileDescriptor *file,
  46. const std::string &parameter,
  47. GeneratorContext *generatorContext,
  48. std::string *error) const
  49. {
  50. // the variable contains only the relative path to ouput dir which is unused in this method
  51. UNUSED(parameter);
  52. if (file->syntax() != FileDescriptor::SYNTAX_PROTO3) {
  53. *error = "Invalid proto used. This plugin only supports 'proto3' syntax";
  54. return false;
  55. }
  56. for (int i = 0; i < file->message_type_count(); i++) {
  57. const Descriptor *message = file->message_type(i);
  58. //Detect nested fields and filter maps fields
  59. int mapsFieldsCount = 0;
  60. for (int j = 0; j < message->nested_type_count(); j++) {
  61. for (int k = 0; k < message->field_count(); k++) {
  62. if (message->field(k)->is_map() && message->field(k)->message_type() == message->nested_type(j)) {
  63. ++mapsFieldsCount;
  64. }
  65. }
  66. }
  67. if (message->nested_type_count() > 0 && message->nested_type_count() > mapsFieldsCount) {
  68. std::cerr << file->name() << ":" << (message->index() + 1) << ": " << " Error: Meta object features not supported for nested classes in " << message->full_name() << std::endl;
  69. continue;
  70. }
  71. std::string baseFilename(message->name());
  72. utils::tolower(baseFilename);
  73. std::string filename = baseFilename + ".h";
  74. ProtobufClassGenerator classGen(message,
  75. std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(filename))));
  76. classGen.run();
  77. filename = baseFilename + ".cpp";
  78. ProtobufSourceGenerator classSourceGen(message,
  79. std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(filename))));
  80. classSourceGen.run();
  81. }
  82. for (int i = 0; i < file->service_count(); i++) {
  83. const ServiceDescriptor *service = file->service(i);
  84. std::string baseFilename(service->name());
  85. utils::tolower(baseFilename);
  86. std::string fullFilename = baseFilename + "server.h";
  87. ServerGenerator serverGen(service,
  88. std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(fullFilename))));
  89. serverGen.run();
  90. fullFilename = baseFilename + "client.h";
  91. ClientGenerator clientGen(service,
  92. std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(fullFilename))));
  93. clientGen.run();
  94. fullFilename = baseFilename + "client.cpp";
  95. ClientSourceGenerator clientSrcGen(service,
  96. std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(fullFilename))));
  97. clientSrcGen.run();
  98. }
  99. return true;
  100. }
  101. bool QtGenerator::GenerateAll(const std::vector<const FileDescriptor *> &files, const string &parameter, GeneratorContext *generatorContext, string *error) const
  102. {
  103. std::string globalEnumsFilename = "globalenums";
  104. PackagesList packageList;
  105. for (auto file : files) {
  106. packageList[file->package()].push_back(file);
  107. }
  108. GlobalEnumsGenerator enumGen(packageList,
  109. std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(globalEnumsFilename + ".h"))));
  110. enumGen.run();
  111. GlobalEnumsSourceGenerator enumSourceGen(packageList,
  112. std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(globalEnumsFilename + ".cpp"))));
  113. enumSourceGen.run();
  114. return CodeGenerator::GenerateAll(files, parameter, generatorContext, error);
  115. }