singlefilegenerator.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 "singlefilegenerator.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 "generatoroptions.h"
  37. #include <iostream>
  38. #include <set>
  39. #include <google/protobuf/stubs/logging.h>
  40. #include <google/protobuf/stubs/common.h>
  41. #include <google/protobuf/io/printer.h>
  42. #include <google/protobuf/io/zero_copy_stream.h>
  43. #include <google/protobuf/descriptor.h>
  44. using namespace ::QtProtobuf::generator;
  45. using namespace ::google::protobuf;
  46. using namespace ::google::protobuf::compiler;
  47. SingleFileGenerator::SingleFileGenerator() : GeneratorBase(GeneratorBase::SingleMode)
  48. {}
  49. bool SingleFileGenerator::Generate(const FileDescriptor *file,
  50. const std::string &parameter,
  51. GeneratorContext *generatorContext,
  52. std::string *error) const
  53. {
  54. if (file->syntax() != FileDescriptor::SYNTAX_PROTO3) {
  55. *error = "Invalid proto used. This plugin only supports 'proto3' syntax";
  56. return false;
  57. }
  58. return GenerateMessages(file, parameter, generatorContext, error)
  59. && GenerateServices(file, parameter, generatorContext, error);
  60. }
  61. bool SingleFileGenerator::GenerateMessages(const ::google::protobuf::FileDescriptor *file,
  62. const std::string &,
  63. ::google::protobuf::compiler::GeneratorContext *generatorContext,
  64. std::string *) const {
  65. if (file->message_type_count() <= 0) {
  66. return true;
  67. }
  68. std::string outFileBasename = utils::extractFileName(file->name());
  69. std::set<std::string> internalIncludes;
  70. std::set<std::string> externalIncludes;
  71. std::shared_ptr<io::ZeroCopyOutputStream> outHeader(generatorContext->Open(outFileBasename + Templates::ProtoFileSuffix + ".h"));
  72. std::shared_ptr<io::ZeroCopyOutputStream> outSource(generatorContext->Open(outFileBasename + Templates::ProtoFileSuffix + ".cpp"));
  73. std::shared_ptr<::google::protobuf::io::Printer> outHeaderPrinter(new ::google::protobuf::io::Printer(outHeader.get(), '$'));
  74. std::shared_ptr<::google::protobuf::io::Printer> outSourcePrinter(new ::google::protobuf::io::Printer(outSource.get(), '$'));
  75. outHeaderPrinter->Print(Templates::DisclaimerTemplate);
  76. outHeaderPrinter->Print(Templates::PreambleTemplate);
  77. outHeaderPrinter->Print(Templates::DefaultProtobufIncludesTemplate);
  78. if (GeneratorOptions::instance().hasQml()) {
  79. outHeaderPrinter->Print(Templates::QmlProtobufIncludesTemplate);
  80. }
  81. outSourcePrinter->Print(Templates::DisclaimerTemplate);
  82. outSourcePrinter->Print({{"include", outFileBasename + Templates::ProtoFileSuffix}}, Templates::InternalIncludeTemplate);
  83. externalIncludes.insert("QByteArray");
  84. externalIncludes.insert("QString");
  85. for (int i = 0; i < file->dependency_count(); i++) {
  86. internalIncludes.insert(utils::extractFileName(file->dependency(i)->name()) + Templates::ProtoFileSuffix);
  87. }
  88. for(auto include : externalIncludes) {
  89. outHeaderPrinter->Print({{"include", include}}, Templates::ExternalIncludeTemplate);
  90. }
  91. for(auto include : internalIncludes) {
  92. outHeaderPrinter->Print({{"include", include}}, Templates::InternalIncludeTemplate);
  93. }
  94. outSourcePrinter->Print({{"namespace", "QtProtobuf"}}, Templates::UsingNamespaceTemplate);
  95. PackagesList packageList;
  96. packageList[file->package()].push_back(file);
  97. GlobalEnumsGenerator enumGen(packageList,
  98. outHeaderPrinter);
  99. enumGen.run();
  100. GlobalEnumsSourceGenerator enumSourceGen(packageList,
  101. outSourcePrinter);
  102. enumSourceGen.run();
  103. std::vector<std::string> namespaces;
  104. std::string namespacesColonDelimited;
  105. utils::split(file->package(), namespaces, '.');
  106. assert(namespaces.size() > 0);
  107. for (size_t i = 0; i < namespaces.size(); i++) {
  108. if (i > 0) {
  109. namespacesColonDelimited = namespacesColonDelimited.append("::");
  110. }
  111. namespacesColonDelimited = namespacesColonDelimited.append(namespaces[i]);
  112. outHeaderPrinter->Print({{"namespace", namespaces[i]}}, Templates::NamespaceTemplate);
  113. }
  114. iterateNonNestedFileds(file, [&outHeaderPrinter](const ::google::protobuf::Descriptor *message){
  115. std::string qualifiedClassName = utils::upperCaseName(message->name());
  116. outHeaderPrinter->Print({{"classname", qualifiedClassName}}, Templates::ProtoClassDeclarationTemplate);
  117. outHeaderPrinter->Print({{"classname", qualifiedClassName}}, Templates::ComplexListTypeUsingTemplate);
  118. });
  119. for (size_t i = 0; i < namespaces.size(); i++) {
  120. outHeaderPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  121. }
  122. iterateNonNestedFileds(file, [&outHeaderPrinter, &outSourcePrinter](const ::google::protobuf::Descriptor *message){
  123. ProtobufClassGenerator classGen(message,
  124. outHeaderPrinter);
  125. classGen.printNamespaces();
  126. classGen.printClassDeclaration();
  127. classGen.printProperties();
  128. classGen.printPrivate();
  129. classGen.printClassMembers();
  130. classGen.printPublic();
  131. classGen.printDestructor();
  132. classGen.encloseClass();
  133. classGen.encloseNamespaces();
  134. classGen.printMetaTypeDeclaration();
  135. classGen.printMapsMetaTypesDeclaration();
  136. classGen.printLocalEnumsMetaTypesDeclaration();
  137. ProtobufSourceGenerator srcGen(message,
  138. outSourcePrinter);
  139. srcGen.printNamespaces();
  140. srcGen.printFieldsOrdering();
  141. srcGen.printRegisterBody();
  142. srcGen.printDestructor();
  143. srcGen.printConstructor();
  144. srcGen.printCopyFunctionality();
  145. srcGen.printMoveSemantic();
  146. srcGen.printComparisonOperators();
  147. srcGen.printGetters();
  148. srcGen.encloseNamespaces();
  149. });
  150. return true;
  151. }
  152. bool SingleFileGenerator::GenerateServices(const ::google::protobuf::FileDescriptor *file,
  153. const std::string &,
  154. ::google::protobuf::compiler::GeneratorContext *generatorContext,
  155. std::string *) const
  156. {
  157. if (file->service_count() <= 0) {
  158. return true;
  159. }
  160. std::string outFileBasename = utils::extractFileName(file->name());
  161. std::set<std::string> internalIncludes;
  162. std::set<std::string> externalIncludes;
  163. std::shared_ptr<io::ZeroCopyOutputStream> outHeader(generatorContext->Open(outFileBasename + Templates::GrpcFileSuffix + Templates::ProtoFileSuffix + ".h"));
  164. std::shared_ptr<io::ZeroCopyOutputStream> outSource(generatorContext->Open(outFileBasename + Templates::GrpcFileSuffix + Templates::ProtoFileSuffix + ".cpp"));
  165. std::shared_ptr<::google::protobuf::io::Printer> outHeaderPrinter(new ::google::protobuf::io::Printer(outHeader.get(), '$'));
  166. std::shared_ptr<::google::protobuf::io::Printer> outSourcePrinter(new ::google::protobuf::io::Printer(outSource.get(), '$'));
  167. outHeaderPrinter->Print(Templates::DisclaimerTemplate);
  168. outHeaderPrinter->Print(Templates::PreambleTemplate);
  169. outHeaderPrinter->Print(Templates::DefaultProtobufIncludesTemplate);
  170. if (GeneratorOptions::instance().hasQml()) {
  171. outHeaderPrinter->Print(Templates::QmlProtobufIncludesTemplate);
  172. }
  173. outSourcePrinter->Print(Templates::DisclaimerTemplate);
  174. outSourcePrinter->Print({{"include", outFileBasename + Templates::GrpcFileSuffix + Templates::ProtoFileSuffix}}, Templates::InternalIncludeTemplate);
  175. for (int i = 0; i < file->service_count(); i++) {
  176. const ServiceDescriptor *service = file->service(i);
  177. for (int i = 0; i < service->method_count(); i++) {
  178. const MethodDescriptor *method = service->method(i);
  179. if (method->input_type()->file() != service->file()) {
  180. internalIncludes.insert(utils::extractFileName(method->input_type()->file()->name()) + Templates::ProtoFileSuffix);
  181. }
  182. if (method->output_type()->file() != service->file()) {
  183. internalIncludes.insert(utils::extractFileName(method->output_type()->file()->name()) + Templates::ProtoFileSuffix);
  184. }
  185. }
  186. }
  187. externalIncludes.insert("QAbstractGrpcClient");
  188. externalIncludes.insert("QGrpcAsyncReply");
  189. if (file->message_type_count() > 0) {
  190. internalIncludes.insert(outFileBasename + Templates::ProtoFileSuffix);
  191. }
  192. for(auto include : externalIncludes) {
  193. outHeaderPrinter->Print({{"include", include}}, Templates::ExternalIncludeTemplate);
  194. }
  195. for(auto include : internalIncludes) {
  196. outHeaderPrinter->Print({{"include", include}}, Templates::InternalIncludeTemplate);
  197. }
  198. outSourcePrinter->Print({{"namespace", "QtProtobuf"}}, Templates::UsingNamespaceTemplate);
  199. for (int i = 0; i < file->service_count(); i++) {
  200. const ServiceDescriptor *service = file->service(i);
  201. // Serverside descriptor is not in use
  202. // std::string baseFilename(service->name());
  203. // utils::tolower(baseFilename);
  204. // std::string fullFilename = baseFilename + "server.h";
  205. // ServerGenerator serverGen(service,
  206. // std::shared_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(fullFilename)));
  207. // serverGen.run();
  208. ClientGenerator clientGen(service,
  209. outHeaderPrinter);
  210. clientGen.printNamespaces();
  211. clientGen.printClientClass();
  212. clientGen.printPublic();
  213. clientGen.printConstructor();
  214. clientGen.printClientMethodsDeclaration();
  215. clientGen.encloseClass();
  216. clientGen.encloseNamespaces();
  217. ClientSourceGenerator clientSrcGen(service,
  218. outSourcePrinter);
  219. clientSrcGen.printNamespaces();
  220. clientSrcGen.printConstructor();
  221. clientSrcGen.printMethods();
  222. clientSrcGen.encloseNamespaces();
  223. }
  224. return true;
  225. }