Apollo 10.0
自动驾驶开放平台
protobuf_factory.h
浏览该文件的文档.
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
34namespace apollo {
35namespace cyber {
36namespace message {
37
39using google::protobuf::Descriptor;
40using google::protobuf::DescriptorPool;
41using google::protobuf::DynamicMessageFactory;
42using google::protobuf::FileDescriptor;
43using google::protobuf::FileDescriptorProto;
44
45class 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:
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 // Get the storage location of proto.
86 void GetProtoPath(const std::string& type, std::string& location);
87
88 // Given a type name, constructs the default (prototype) Message of that type.
89 // Returns nullptr if no such message exists.
90 google::protobuf::Message* GenerateMessageByType(
91 const std::string& type) const;
92
93 // Find a descriptor by FileDescriptorProto. Returns nullptr if not found.
94 const Descriptor* FindMessageTypeByFile(
95 const FileDescriptorProto& file_desc_proto);
96
97 // Find a top-level message type by name. Returns nullptr if not found.
98 const Descriptor* FindMessageTypeByName(const std::string& type) const;
99
100 // Find a service definition by name. Returns nullptr if not found.
101 const google::protobuf::ServiceDescriptor* FindServiceByName(
102 const std::string& name) const;
103
104 void GetPythonDesc(const std::string& type, std::string* desc_str);
105
106 private:
107 bool RegisterMessage(const ProtoDesc& proto_desc);
108 google::protobuf::Message* GetMessageByGeneratedType(
109 const std::string& type) const;
110 static bool GetProtoDesc(const FileDescriptor* file_desc,
111 ProtoDesc* proto_desc);
112
113 std::mutex register_mutex_;
114 std::unique_ptr<DescriptorPool> pool_ = nullptr;
115 std::unique_ptr<DynamicMessageFactory> factory_ = nullptr;
116
118};
119
120} // namespace message
121} // namespace cyber
122} // namespace apollo
123
124#endif // CYBER_MESSAGE_PROTOBUF_FACTORY_H_
const google::protobuf::ServiceDescriptor * FindServiceByName(const std::string &name) const
bool RegisterMessage(const std::string &proto_desc_str)
const Descriptor * FindMessageTypeByName(const std::string &type) const
const Descriptor * FindMessageTypeByFile(const FileDescriptorProto &file_desc_proto)
bool RegisterPythonMessage(const std::string &proto_str)
void GetPythonDesc(const std::string &type, std::string *desc_str)
void GetProtoPath(const std::string &type, std::string &location)
static void GetDescriptorString(const google::protobuf::Message &message, std::string *desc_str)
google::protobuf::Message * GenerateMessageByType(const std::string &type) const
#define DECLARE_SINGLETON(classname)
Definition macros.h:52
class register implement
Definition arena_queue.h:37