classgeneratorbase.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <memory>
  28. #include "templates.h"
  29. namespace google { namespace protobuf {
  30. class FieldDescriptor;
  31. class Descriptor;
  32. namespace io {
  33. class ZeroCopyOutputStream;
  34. }}}
  35. namespace qtprotobuf {
  36. namespace generator {
  37. using PropertyMap = std::map<std::string, std::string>;
  38. class ClassGeneratorBase
  39. {
  40. public:
  41. ClassGeneratorBase(std::string className, std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out);
  42. virtual ~ClassGeneratorBase() = default;
  43. virtual void run() = 0;
  44. protected:
  45. std::unique_ptr<::google::protobuf::io::ZeroCopyOutputStream> mOutput;
  46. ::google::protobuf::io::Printer mPrinter;
  47. std::string mClassName;
  48. std::vector<std::string> mNamespaces;
  49. void printPreamble();
  50. void printNamespaces();
  51. void printNamespaces(const std::vector<std::string> &namespaces);
  52. void printClassDeclaration();
  53. void printConstructor();
  54. void printPublic();
  55. void encloseClass();
  56. void printMetaTypeDeclaration();
  57. void encloseNamespaces();
  58. void encloseNamespaces(int count);
  59. template<typename T>
  60. void printQEnums(const T *message) {
  61. if (message->enum_type_count() <= 0) {
  62. return;
  63. }
  64. printPublic();
  65. Indent();
  66. for (int i = 0; i < message->enum_type_count(); i++) {
  67. const auto enumDescr = message->enum_type(i);
  68. mPrinter.Print({{"enum", enumDescr->name()}}, EnumDefinitionTemplate);
  69. Indent();
  70. for (int j = 0; j < enumDescr->value_count(); j++) {
  71. const auto valueDescr = enumDescr->value(j);
  72. mPrinter.Print({{"enumvalue", valueDescr->name()},
  73. {"value", std::to_string(valueDescr->number())}}, EnumFieldTemplate);
  74. }
  75. Outdent();
  76. mPrinter.Print(SemicolonBlockEnclosureTemplate);
  77. mPrinter.Print({{"type", enumDescr->name().c_str()}}, QEnumTemplate);
  78. }
  79. Outdent();
  80. }
  81. void Indent() {
  82. mPrinter.Indent();
  83. mPrinter.Indent();
  84. }
  85. void Outdent() {
  86. mPrinter.Outdent();
  87. mPrinter.Outdent();
  88. }
  89. };
  90. } //namespace generator
  91. } //namespace qtprotobuf