baseprinter.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #pragma once
  26. #include <google/protobuf/io/printer.h>
  27. #include <google/protobuf/descriptor.h>
  28. #include <memory>
  29. #include "utils.h"
  30. #include "generatoroptions.h"
  31. #include "generatorcommon.h"
  32. namespace google { namespace protobuf {
  33. class FieldDescriptor;
  34. class Descriptor;
  35. class MethodDescriptor;
  36. }}
  37. namespace QtProtobuf {
  38. namespace generator {
  39. using PropertyMap = std::map<std::string, std::string>;
  40. /*!
  41. * \ingroup generator
  42. * \private
  43. * \brief The BasePrinter class is base of source code generation
  44. */
  45. class BasePrinter
  46. {
  47. public:
  48. BasePrinter(const std::shared_ptr<::google::protobuf::io::Printer> &printer);
  49. virtual ~BasePrinter() = default;
  50. void printPublicBlock();
  51. void printPrivateBlock();
  52. void printSignalsBlock();
  53. template<typename T>
  54. void printComments(T *descriptor) {
  55. if (!GeneratorOptions::instance().generateComments()) {
  56. return;
  57. }
  58. ::google::protobuf::SourceLocation loc;
  59. descriptor->GetSourceLocation(&loc);
  60. utils::trim(loc.leading_comments);
  61. if (loc.leading_comments.size() > 0) {
  62. auto firstEntry = loc.leading_comments.find('\n');
  63. bool isSingleLine = firstEntry == std::string::npos;
  64. if (loc.leading_comments[0] != '!' && loc.leading_comments[0] != '*' && loc.leading_comments[0] != ' ') {
  65. loc.leading_comments = " " + loc.leading_comments;
  66. if (!isSingleLine) {
  67. loc.leading_comments = "\n" + loc.leading_comments;
  68. }
  69. }
  70. mPrinter->Print("\n/*");
  71. if (isSingleLine) {
  72. mPrinter->Print(loc.leading_comments.c_str());
  73. } else {
  74. utils::replace(loc.leading_comments, "\n", "\n *");
  75. mPrinter->Print(loc.leading_comments.c_str());
  76. if (loc.leading_comments[loc.leading_comments.size() - 1] != '\n') {
  77. mPrinter->Print("\n");
  78. }
  79. }
  80. mPrinter->Print(" */");
  81. }
  82. }
  83. void Indent() {
  84. mPrinter->Indent();
  85. mPrinter->Indent();
  86. }
  87. void Outdent() {
  88. mPrinter->Outdent();
  89. mPrinter->Outdent();
  90. }
  91. protected:
  92. std::shared_ptr<::google::protobuf::io::Printer> mPrinter;
  93. std::vector<std::string> mNamespaces;
  94. };
  95. } //namespace generator
  96. } //namespace QtProtobuf