servicedeclarationprinterbase.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 "servicedeclarationprinterbase.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. ServiceDeclarationPrinterBase::ServiceDeclarationPrinterBase(const ::google::protobuf::ServiceDescriptor *service,
  38. const std::shared_ptr<::google::protobuf::io::Printer> &printer) :
  39. DescriptorPrinterBase<::google::protobuf::ServiceDescriptor>(service, printer)
  40. {}
  41. void ServiceDeclarationPrinterBase::printIncludes()
  42. {
  43. std::unordered_set<std::string> includeSet;
  44. for (int i = 0; i < mDescriptor->method_count(); i++) {
  45. const MethodDescriptor *method = mDescriptor->method(i);
  46. std::string inputTypeName = method->input_type()->name();
  47. std::string outputTypeName = method->output_type()->name();
  48. utils::tolower(inputTypeName);
  49. utils::tolower(outputTypeName);
  50. includeSet.insert(inputTypeName);
  51. includeSet.insert(outputTypeName);
  52. }
  53. for (auto type : includeSet) {
  54. mPrinter->Print({{"include", type}}, Templates::InternalIncludeTemplate);
  55. }
  56. }
  57. void ServiceDeclarationPrinterBase::printClassName()
  58. {
  59. mPrinter->Print({{"classname", mName}}, Templates::ClassDeclarationTemplate);
  60. }
  61. void ServiceDeclarationPrinterBase::printMethodsDeclaration(const char *methodTemplate, const char *methodAsyncTemplate, const char *methodAsync2Template)
  62. {
  63. Indent();
  64. for (int i = 0; i < mDescriptor->method_count(); i++) {
  65. const MethodDescriptor *method = mDescriptor->method(i);
  66. std::map<std::string, std::string> parameters = common::produceMethodMap(method, mName);
  67. mPrinter->Print(parameters, methodTemplate);
  68. mPrinter->Print(parameters, methodAsyncTemplate);
  69. mPrinter->Print(parameters, methodAsync2Template);
  70. }
  71. Outdent();
  72. }