singlefilegenerator.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 "enumsgenerator.h"
  31. #include "enumssourcegenerator.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 && file->enum_type_count() <= 0) {
  66. return true;
  67. }
  68. std::string outFileBasename = generateBaseName(file);
  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(generateBaseName(file->dependency(i)) + 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. if (GeneratorOptions::instance().hasQml()) {
  95. outSourcePrinter->Print({{"include", "QQmlEngine"}}, Templates::ExternalIncludeTemplate);
  96. }
  97. outSourcePrinter->Print({{"namespace", "QtProtobuf"}}, Templates::UsingNamespaceTemplate);
  98. PackagesList packageList;
  99. packageList[file->package()].push_back(file);
  100. for(int i = 0; i < file->enum_type_count(); i++) {
  101. EnumsGenerator enumGen2(file->enum_type(i),
  102. outHeaderPrinter);
  103. enumGen2.run();
  104. }
  105. for(int i = 0; i < file->enum_type_count(); i++) {
  106. auto enumType = file->enum_type(i);
  107. EnumsSourceGenerator enumSourceGen2(enumType,
  108. outSourcePrinter);
  109. enumSourceGen2.run();
  110. }
  111. std::vector<std::string> namespaces;
  112. std::string namespacesColonDelimited;
  113. utils::split(file->package(), namespaces, '.');
  114. assert(namespaces.size() > 0);
  115. for (size_t i = 0; i < namespaces.size(); i++) {
  116. if (i > 0) {
  117. namespacesColonDelimited = namespacesColonDelimited.append("::");
  118. }
  119. namespacesColonDelimited = namespacesColonDelimited.append(namespaces[i]);
  120. outHeaderPrinter->Print({{"namespace", namespaces[i]}}, Templates::NamespaceTemplate);
  121. }
  122. iterateNonNestedFileds(file, [&outHeaderPrinter](const ::google::protobuf::Descriptor *message){
  123. std::string qualifiedClassName = utils::upperCaseName(message->name());
  124. outHeaderPrinter->Print({{"classname", qualifiedClassName}}, Templates::ProtoClassDeclarationTemplate);
  125. outHeaderPrinter->Print({{"classname", qualifiedClassName}}, Templates::ComplexListTypeUsingTemplate);
  126. });
  127. for (size_t i = 0; i < namespaces.size(); i++) {
  128. outHeaderPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  129. }
  130. iterateNonNestedFileds(file, [&outHeaderPrinter, &outSourcePrinter](const ::google::protobuf::Descriptor *message){
  131. ProtobufClassGenerator classGen(message,
  132. outHeaderPrinter);
  133. classGen.printNamespaces();
  134. classGen.printComments(message);
  135. classGen.printClassDeclaration();
  136. classGen.printProperties();
  137. classGen.printPrivate();
  138. classGen.printClassMembers();
  139. classGen.printPublic();
  140. classGen.printDestructor();
  141. classGen.encloseClass();
  142. classGen.encloseNamespaces();
  143. classGen.printMetaTypeDeclaration();
  144. classGen.printMapsMetaTypesDeclaration();
  145. classGen.printLocalEnumsMetaTypesDeclaration();
  146. ProtobufSourceGenerator srcGen(message,
  147. outSourcePrinter);
  148. srcGen.printNamespaces();
  149. srcGen.printFieldsOrdering();
  150. srcGen.printRegisterBody();
  151. srcGen.printDestructor();
  152. srcGen.printConstructor();
  153. srcGen.printCopyFunctionality();
  154. srcGen.printMoveSemantic();
  155. srcGen.printComparisonOperators();
  156. srcGen.printGetters();
  157. srcGen.encloseNamespaces();
  158. });
  159. return true;
  160. }
  161. bool SingleFileGenerator::GenerateServices(const ::google::protobuf::FileDescriptor *file,
  162. const std::string &,
  163. ::google::protobuf::compiler::GeneratorContext *generatorContext,
  164. std::string *) const
  165. {
  166. if (file->service_count() <= 0) {
  167. return true;
  168. }
  169. std::string outFileBasename = generateBaseName(file);
  170. std::set<std::string> internalIncludes;
  171. std::set<std::string> externalIncludes;
  172. std::shared_ptr<io::ZeroCopyOutputStream> outHeader(generatorContext->Open(outFileBasename + Templates::GrpcFileSuffix + Templates::ProtoFileSuffix + ".h"));
  173. std::shared_ptr<io::ZeroCopyOutputStream> outSource(generatorContext->Open(outFileBasename + Templates::GrpcFileSuffix + Templates::ProtoFileSuffix + ".cpp"));
  174. std::shared_ptr<::google::protobuf::io::Printer> outHeaderPrinter(new ::google::protobuf::io::Printer(outHeader.get(), '$'));
  175. std::shared_ptr<::google::protobuf::io::Printer> outSourcePrinter(new ::google::protobuf::io::Printer(outSource.get(), '$'));
  176. outHeaderPrinter->Print(Templates::DisclaimerTemplate);
  177. outHeaderPrinter->Print(Templates::PreambleTemplate);
  178. outHeaderPrinter->Print(Templates::DefaultProtobufIncludesTemplate);
  179. if (GeneratorOptions::instance().hasQml()) {
  180. outHeaderPrinter->Print(Templates::QmlProtobufIncludesTemplate);
  181. }
  182. outSourcePrinter->Print(Templates::DisclaimerTemplate);
  183. outSourcePrinter->Print({{"include", outFileBasename + Templates::GrpcFileSuffix + Templates::ProtoFileSuffix}}, Templates::InternalIncludeTemplate);
  184. for (int i = 0; i < file->service_count(); i++) {
  185. const ServiceDescriptor *service = file->service(i);
  186. for (int i = 0; i < service->method_count(); i++) {
  187. const MethodDescriptor *method = service->method(i);
  188. if (method->input_type()->file() != service->file()) {
  189. internalIncludes.insert(utils::extractFileName(method->input_type()->file()->name()) + Templates::ProtoFileSuffix);
  190. }
  191. if (method->output_type()->file() != service->file()) {
  192. internalIncludes.insert(utils::extractFileName(method->output_type()->file()->name()) + Templates::ProtoFileSuffix);
  193. }
  194. }
  195. }
  196. externalIncludes.insert("QAbstractGrpcClient");
  197. externalIncludes.insert("QGrpcAsyncReply");
  198. externalIncludes.insert("QGrpcSubscription");
  199. if (file->message_type_count() > 0) {
  200. internalIncludes.insert(outFileBasename + Templates::ProtoFileSuffix);
  201. }
  202. for(auto include : externalIncludes) {
  203. outHeaderPrinter->Print({{"include", include}}, Templates::ExternalIncludeTemplate);
  204. }
  205. for(auto include : internalIncludes) {
  206. outHeaderPrinter->Print({{"include", include}}, Templates::InternalIncludeTemplate);
  207. }
  208. outSourcePrinter->Print({{"namespace", "QtProtobuf"}}, Templates::UsingNamespaceTemplate);
  209. for (int i = 0; i < file->service_count(); i++) {
  210. const ServiceDescriptor *service = file->service(i);
  211. // Serverside descriptor is not in use
  212. // std::string baseFilename(service->name());
  213. // utils::tolower(baseFilename);
  214. // std::string fullFilename = baseFilename + "server.h";
  215. // ServerGenerator serverGen(service,
  216. // std::shared_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(fullFilename)));
  217. // serverGen.run();
  218. ClientGenerator clientGen(service,
  219. outHeaderPrinter);
  220. clientGen.printNamespaces();
  221. clientGen.printClientClass();
  222. clientGen.printPublic();
  223. clientGen.printConstructor();
  224. clientGen.printClientMethodsDeclaration();
  225. clientGen.encloseClass();
  226. clientGen.encloseNamespaces();
  227. ClientSourceGenerator clientSrcGen(service,
  228. outSourcePrinter);
  229. clientSrcGen.printNamespaces();
  230. clientSrcGen.printConstructor();
  231. clientSrcGen.printMethods();
  232. clientSrcGen.encloseNamespaces();
  233. }
  234. return true;
  235. }