globalenumsgenerator.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  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 "globalenumsgenerator.h"
  26. #include <google/protobuf/io/zero_copy_stream.h>
  27. using namespace ::QtProtobuf::generator;
  28. using namespace ::google::protobuf;
  29. using namespace ::google::protobuf::io;
  30. using namespace ::google::protobuf::compiler;
  31. GlobalEnumsGenerator::GlobalEnumsGenerator(const PackagesList &packageList, const std::shared_ptr<io::ZeroCopyOutputStream> &out) :
  32. ClassGeneratorBase(Templates::GlobalEnumClassNameTemplate, out)
  33. , mPackageList(packageList) {}
  34. GlobalEnumsGenerator::GlobalEnumsGenerator(const PackagesList &packageList, const std::shared_ptr<::google::protobuf::io::Printer> &printer) :
  35. ClassGeneratorBase(Templates::GlobalEnumClassNameTemplate, printer)
  36. , mPackageList(packageList) {}
  37. void GlobalEnumsGenerator::startEnum(const std::vector<std::string>& namespaces) {
  38. printNamespaces(namespaces);
  39. printEnumClass();
  40. printPublic();
  41. Indent();
  42. mPrinter->Print({{"classname", mClassName}}, Templates::ManualRegistrationDeclaration);
  43. Outdent();
  44. printPrivate();
  45. printConstructor();
  46. }
  47. void GlobalEnumsGenerator::printConstructor()
  48. {
  49. Indent();
  50. mPrinter->Print({{"classname", mClassName}}, Templates::ConstructorHeaderTemplate);
  51. mPrinter->Print({{"classname", mClassName}}, Templates::DeletedCopyConstructorTemplate);
  52. mPrinter->Print({{"classname", mClassName}}, Templates::DeletedMoveConstructorTemplate);
  53. Outdent();
  54. }
  55. void GlobalEnumsGenerator::run() {
  56. std::vector<std::string> namespaces;
  57. for (auto package : mPackageList) {
  58. if (!hasGlobalEnum(package.second)) {
  59. continue;
  60. }
  61. utils::split(package.first, namespaces, '.');
  62. startEnum(namespaces);
  63. for (auto file : package.second) {
  64. run(file);
  65. }
  66. encloseEnum(namespaces);
  67. for (auto file : package.second) {
  68. printMetatype(file, namespaces);
  69. }
  70. }
  71. }
  72. void GlobalEnumsGenerator::run(const FileDescriptor *file) {
  73. printQEnums(file);
  74. }
  75. void GlobalEnumsGenerator::printMetatype(const google::protobuf::FileDescriptor *file,
  76. const std::vector<std::string>& namespaces) {
  77. std::string fullNamespace;
  78. for (auto name : namespaces) {
  79. if (fullNamespace.empty()) {
  80. fullNamespace.append(name);
  81. continue;
  82. }
  83. fullNamespace.append("::").append(name);
  84. }
  85. fullNamespace.append("::").append(Templates::GlobalEnumClassNameTemplate);
  86. for (int i = 0; i < file->enum_type_count(); i++) {
  87. const auto enumDescr = file->enum_type(i);
  88. mPrinter->Print({{"classname", enumDescr->name()}, {"namespaces", fullNamespace}},
  89. Templates::DeclareMetaTypeListTemplate);
  90. }
  91. }
  92. void GlobalEnumsGenerator::encloseEnum(const std::vector<std::string>& namespaces) {
  93. encloseClass();
  94. encloseNamespaces(static_cast<int>(namespaces.size()));
  95. }
  96. void GlobalEnumsGenerator::printEnumClass() {
  97. mPrinter->Print({{"classname", mClassName}}, Templates::NonProtoClassDefinitionTemplate);
  98. }