Apollo  6.0
Open source self driving car software
protobuf_factory.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #ifndef CYBER_MESSAGE_PROTOBUF_FACTORY_H_
18 #define CYBER_MESSAGE_PROTOBUF_FACTORY_H_
19 
20 #include <iostream>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 
25 #include "google/protobuf/compiler/parser.h"
26 #include "google/protobuf/descriptor.h"
27 #include "google/protobuf/dynamic_message.h"
28 #include "google/protobuf/io/tokenizer.h"
29 
30 #include "cyber/proto/proto_desc.pb.h"
31 
32 #include "cyber/common/macros.h"
33 
34 namespace apollo {
35 namespace cyber {
36 namespace message {
37 
38 using apollo::cyber::proto::ProtoDesc;
39 using google::protobuf::Descriptor;
40 using google::protobuf::DescriptorPool;
41 using google::protobuf::DynamicMessageFactory;
42 using google::protobuf::FileDescriptor;
43 using google::protobuf::FileDescriptorProto;
44 
45 class ErrorCollector : public google::protobuf::DescriptorPool::ErrorCollector {
46  using ErrorLocation =
47  google::protobuf::DescriptorPool::ErrorCollector::ErrorLocation;
48  void AddError(const std::string& filename, const std::string& element_name,
49  const google::protobuf::Message* descriptor,
50  ErrorLocation location, const std::string& message) override;
51 
52  void AddWarning(const std::string& filename, const std::string& element_name,
53  const google::protobuf::Message* descriptor,
54  ErrorLocation location, const std::string& message) override;
55 };
56 
58  public:
59  ~ProtobufFactory();
60 
61  // Recursively register FileDescriptorProto and all its dependencies to
62  // factory.
63  bool RegisterMessage(const std::string& proto_desc_str);
64  bool RegisterPythonMessage(const std::string& proto_str);
65 
66  // Convert the serialized FileDescriptorProto to real descriptors and place
67  // them in factory.
68  // It is an error if a FileDescriptorProto contains references to types or
69  // other files that are not found in the Factory.
70  bool RegisterMessage(const google::protobuf::Message& message);
71  bool RegisterMessage(const Descriptor& desc);
72  bool RegisterMessage(const FileDescriptorProto& file_desc_proto);
73 
74  // Serialize all descriptors of the given message to string.
75  static void GetDescriptorString(const google::protobuf::Message& message,
76  std::string* desc_str);
77 
78  // Serialize all descriptors of the descriptor to string.
79  static void GetDescriptorString(const Descriptor* desc,
80  std::string* desc_str);
81 
82  // Get Serialized descriptors of messages with the given type.
83  void GetDescriptorString(const std::string& type, std::string* desc_str);
84 
85  // Given a type name, constructs the default (prototype) Message of that type.
86  // Returns nullptr if no such message exists.
87  google::protobuf::Message* GenerateMessageByType(
88  const std::string& type) const;
89 
90  // Find a top-level message type by name. Returns nullptr if not found.
91  const Descriptor* FindMessageTypeByName(const std::string& type) const;
92 
93  // Find a service definition by name. Returns nullptr if not found.
94  const google::protobuf::ServiceDescriptor* FindServiceByName(
95  const std::string& name) const;
96 
97  void GetPythonDesc(const std::string& type, std::string* desc_str);
98 
99  private:
100  bool RegisterMessage(const ProtoDesc& proto_desc);
101  google::protobuf::Message* GetMessageByGeneratedType(
102  const std::string& type) const;
103  static bool GetProtoDesc(const FileDescriptor* file_desc,
104  ProtoDesc* proto_desc);
105 
106  std::mutex register_mutex_;
107  std::unique_ptr<DescriptorPool> pool_ = nullptr;
108  std::unique_ptr<DynamicMessageFactory> factory_ = nullptr;
109 
111 };
112 
113 } // namespace message
114 } // namespace cyber
115 } // namespace apollo
116 
117 #endif // CYBER_MESSAGE_PROTOBUF_FACTORY_H_
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
bool RegisterMessage(const MessageT &message)
Definition: protobuf_traits.h:68
#define DECLARE_SINGLETON(classname)
Definition: macros.h:52
void GetDescriptorString(const std::string &type, std::string *desc_str)
Definition: message_traits.h:237
Definition: protobuf_factory.h:57
Definition: protobuf_factory.h:45