generatorcommon.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 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. #pragma once
  26. #include <vector>
  27. #include <map>
  28. #include <string>
  29. #include <functional>
  30. #include "utils.h"
  31. #include "templates.h"
  32. #include <google/protobuf/descriptor.h>
  33. namespace QtProtobuf {
  34. namespace generator {
  35. using TypeMap = std::map<std::string, std::string>;
  36. using PropertyMap = std::map<std::string, std::string>;
  37. using MethodMap = std::map<std::string, std::string>;
  38. struct common {
  39. enum EnumVisibility {
  40. GLOBAL_ENUM,
  41. LOCAL_ENUM,
  42. NEIGHBOR_ENUM
  43. };
  44. template <typename T>
  45. static std::vector<std::string> getNamespaces(const T *type) {
  46. if (type == nullptr) {
  47. return {};
  48. }
  49. std::vector<std::string> namespacesList = utils::split(type->full_name(), '.');
  50. if (!namespacesList.empty()) {
  51. namespacesList.pop_back(); //Remove name
  52. }
  53. return namespacesList;
  54. }
  55. static std::vector<std::string> getNestedNamespaces(const ::google::protobuf::Descriptor *type) {
  56. auto namespaces = getNamespaces(type);
  57. auto package = utils::split(type->file()->package(), '.');
  58. for (size_t i = package.size(); i < namespaces.size(); i++) {
  59. namespaces[i] += Templates::QtProtobufNestedNamespace;
  60. }
  61. return namespaces;
  62. }
  63. static std::string getNamespacesString(const std::vector<std::string> &namespacesList, const std::string &separator);
  64. static std::string getScopeNamespacesString(std::string original, const std::string &scope);
  65. static TypeMap produceQtTypeMap(const ::google::protobuf::Descriptor *type, const ::google::protobuf::Descriptor *scope);
  66. static TypeMap produceMessageTypeMap(const ::google::protobuf::Descriptor *type, const ::google::protobuf::Descriptor *scope);
  67. static TypeMap produceEnumTypeMap(const ::google::protobuf::EnumDescriptor *type, const ::google::protobuf::Descriptor *scope);
  68. static TypeMap produceSimpleTypeMap(::google::protobuf::FieldDescriptor::Type type);
  69. static TypeMap produceTypeMap(const ::google::protobuf::FieldDescriptor *field, const ::google::protobuf::Descriptor *scope);
  70. static PropertyMap producePropertyMap(const ::google::protobuf::FieldDescriptor *field, const ::google::protobuf::Descriptor *scope);
  71. static std::string qualifiedName(const std::string &name);
  72. static bool isLocalEnum(const ::google::protobuf::EnumDescriptor *type, const google::protobuf::Descriptor *scope);
  73. static EnumVisibility enumVisibility(const ::google::protobuf::EnumDescriptor *type, const ::google::protobuf::Descriptor *scope);
  74. static bool hasQmlAlias(const ::google::protobuf::FieldDescriptor *field);
  75. static bool isQtType(const ::google::protobuf::FieldDescriptor *field);
  76. static bool isPureMessage(const ::google::protobuf::FieldDescriptor *field);
  77. using InterateMessageLogic = std::function<void(const ::google::protobuf::FieldDescriptor *, PropertyMap &)>;
  78. static void iterateMessageFields(const ::google::protobuf::Descriptor *message, InterateMessageLogic callback) {
  79. for (int i = 0; i < message->field_count(); i++) {
  80. const ::google::protobuf::FieldDescriptor *field = message->field(i);
  81. auto propertyMap = common::producePropertyMap(field, message);
  82. callback(field, propertyMap);
  83. }
  84. }
  85. static MethodMap produceMethodMap(const ::google::protobuf::MethodDescriptor *method, const std::string &scope); //TODO: scope should be ServiceDescriptor
  86. static void iterateMessages(const ::google::protobuf::FileDescriptor *file, std::function<void(const ::google::protobuf::Descriptor *)> callback);
  87. static void iterateNestedMessages(const ::google::protobuf::Descriptor *message, std::function<void(const ::google::protobuf::Descriptor *)> callback);
  88. static bool hasNestedMessages(const ::google::protobuf::Descriptor *message);
  89. static bool isNested(const ::google::protobuf::Descriptor *message);
  90. static bool isNestedOf(const ::google::protobuf::Descriptor *message, const ::google::protobuf::Descriptor *containing) {
  91. return containing == message->containing_type();
  92. }
  93. static const ::google::protobuf::Descriptor *findHighestMessage(const ::google::protobuf::Descriptor *message);
  94. };
  95. }
  96. }