descriptorprinterbase.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "baseprinter.h"
  27. #include "utils.h"
  28. #include "templates.h"
  29. #include "generatoroptions.h"
  30. #include "generatorcommon.h"
  31. namespace QtProtobuf {
  32. namespace generator {
  33. using PropertyMap = std::map<std::string, std::string>;
  34. /*!
  35. * \ingroup generator
  36. * \private
  37. * \brief The DescriptorPrinterBase class is base of source code generation
  38. */
  39. template<typename T>
  40. class DescriptorPrinterBase : public BasePrinter
  41. {
  42. public:
  43. DescriptorPrinterBase(const T* descriptor, const std::shared_ptr<::google::protobuf::io::Printer> &printer) : BasePrinter(printer)
  44. , mDescriptor(descriptor)
  45. , mName(utils::upperCaseName(descriptor->name()))// TODO: migrate to typemaps for the client and service generators
  46. {}
  47. virtual ~DescriptorPrinterBase() = default;
  48. public:
  49. void encloseClass() {
  50. mPrinter->Print(Templates::SemicolonBlockEnclosureTemplate);
  51. mPrinter->Print("\n");
  52. }
  53. void printNamespaces() {
  54. auto namespaces = common::getNamespaces(mDescriptor);
  55. if (!GeneratorOptions::instance().extraNamespace().empty()) {
  56. namespaces.insert(namespaces.begin(), GeneratorOptions::instance().extraNamespace());
  57. }
  58. mPrinter->Print("\n");
  59. for (auto ns : namespaces) {
  60. mPrinter->Print({{"namespace", ns}}, Templates::NamespaceTemplate);
  61. }
  62. }
  63. void encloseNamespaces() {
  64. auto namespacesSize = common::getNamespaces(mDescriptor).size();
  65. if (!GeneratorOptions::instance().extraNamespace().empty()) {
  66. namespacesSize++;
  67. }
  68. mPrinter->Print("\n");
  69. for (size_t i = 0; i < namespacesSize; ++i) {
  70. mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  71. }
  72. mPrinter->Print("\n");
  73. }
  74. protected:
  75. const T* mDescriptor;
  76. std::string mName;// TODO: migrate to typemaps for the client and service generators
  77. TypeMap mTypeMap;
  78. };
  79. } //namespace generator
  80. } //namespace QtProtobuf