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