clientdeclarationprinter.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>, Tatyana Borisova <tanusshhka@mail.ru>
  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 "clientdeclarationprinter.h"
  26. #include <google/protobuf/stubs/logging.h>
  27. #include <google/protobuf/stubs/common.h>
  28. #include <google/protobuf/io/printer.h>
  29. #include <google/protobuf/io/zero_copy_stream.h>
  30. #include <google/protobuf/descriptor.h>
  31. #include <unordered_set>
  32. #include "utils.h"
  33. #include "templates.h"
  34. using namespace ::QtProtobuf::generator;
  35. using namespace ::google::protobuf;
  36. using namespace ::google::protobuf::compiler;
  37. ClientDeclarationPrinter::ClientDeclarationPrinter(const ::google::protobuf::ServiceDescriptor *service, const std::shared_ptr<::google::protobuf::io::Printer> &printer) :
  38. ServiceDeclarationPrinterBase(service, printer)
  39. {
  40. mName += "Client";
  41. }
  42. void ClientDeclarationPrinter::printClientClass()
  43. {
  44. mPrinter->Print({{"classname", mName}, {"parent_class", "QAbstractGrpcClient"}}, Templates::ClassDefinitionTemplate);
  45. Indent();
  46. mPrinter->Print(Templates::QObjectMacro);
  47. mPrinter->Print("\n");
  48. Outdent();
  49. }
  50. void ClientDeclarationPrinter::printConstructor()
  51. {
  52. Indent();
  53. mPrinter->Print({{"classname", mName}}, Templates::QObjectConstructorTemplate);
  54. Outdent();
  55. }
  56. void ClientDeclarationPrinter::printClientIncludes()
  57. {
  58. std::unordered_set<std::string> includeSet;
  59. includeSet.insert("QAbstractGrpcClient");
  60. includeSet.insert("QGrpcCallReply");
  61. includeSet.insert("QGrpcStream");
  62. for (auto type : includeSet) {
  63. mPrinter->Print({{"include", type}}, Templates::ExternalIncludeTemplate);
  64. }
  65. }
  66. void ClientDeclarationPrinter::printClientMethodsDeclaration()
  67. {
  68. Indent();
  69. for (int i = 0; i < mDescriptor->method_count(); i++) {
  70. const MethodDescriptor *method = mDescriptor->method(i);
  71. std::map<std::string, std::string> parameters = common::produceMethodMap(method, mName);
  72. if (method->server_streaming()) {
  73. mPrinter->Print(parameters, Templates::ClientMethodServerStreamDeclarationTemplate);
  74. mPrinter->Print(parameters, Templates::ClientMethodServerStream2DeclarationTemplate);
  75. } else {
  76. mPrinter->Print(parameters, Templates::ClientMethodDeclarationSyncTemplate);
  77. mPrinter->Print(parameters, Templates::ClientMethodDeclarationAsyncTemplate);
  78. mPrinter->Print(parameters, Templates::ClientMethodDeclarationAsync2Template);
  79. if (GeneratorOptions::instance().hasQml()) {
  80. mPrinter->Print(parameters, Templates::ClientMethodDeclarationQmlTemplate);
  81. mPrinter->Print(parameters, Templates::ClientMethodDeclarationQml2Template);
  82. }
  83. }
  84. mPrinter->Print("\n");
  85. }
  86. Outdent();
  87. printPrivateBlock();
  88. Indent();
  89. for (int i = 0; i < mDescriptor->method_count(); i++) {
  90. const MethodDescriptor *method = mDescriptor->method(i);
  91. std::map<std::string, std::string> parameters = common::produceMethodMap(method, mName);
  92. if (method->server_streaming()) {
  93. if (GeneratorOptions::instance().hasQml()) {
  94. mPrinter->Print(parameters, Templates::ClientMethodServerStreamQmlDeclarationTemplate);
  95. }
  96. }
  97. }
  98. Outdent();
  99. mPrinter->Print("\n");
  100. }