singlefilegenerator.cpp 12 KB

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