26#include "dynamic_protobuf_manager.h"
29std::shared_ptr<dccl::DynamicProtobufManager> dccl::DynamicProtobufManager::inst_;
34const google::protobuf::Descriptor*
38 DCCL_LOCK_DYNAMIC_PROTOBUF_MANAGER_MUTEX
39 const google::protobuf::Descriptor* desc =
nullptr;
43 desc = get_instance()->user_descriptor_pool_->FindMessageTypeByName(protobuf_type_name);
49 desc = google::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName(
57 desc = get_instance()->user_descriptor_pool_->FindMessageTypeByName(protobuf_type_name);
63std::shared_ptr<google::protobuf::Message>
66 DCCL_LOCK_DYNAMIC_PROTOBUF_MANAGER_MUTEX
67 return new_protobuf_message<std::shared_ptr<google::protobuf::Message>>(desc);
70std::shared_ptr<google::protobuf::Message>
73 DCCL_LOCK_DYNAMIC_PROTOBUF_MANAGER_MUTEX
74 return new_protobuf_message<std::shared_ptr<google::protobuf::Message>>(protobuf_type_name);
78 std::shared_ptr<google::protobuf::DescriptorDatabase> database)
80 DCCL_LOCK_DYNAMIC_PROTOBUF_MANAGER_MUTEX
81 get_instance()->databases_.push_back(database);
82 get_instance()->update_databases();
87 DCCL_LOCK_DYNAMIC_PROTOBUF_MANAGER_MUTEX
89 if (!get_instance()->disk_source_tree_)
90 throw(std::runtime_error(
91 "Must called enable_compilation() before loading proto files directly"));
93 get_instance()->disk_source_tree_->MapPath(
"", path);
98 DCCL_LOCK_DYNAMIC_PROTOBUF_MANAGER_MUTEX
99 void* handle = dlopen(shared_lib_path.c_str(), RTLD_LAZY);
101 get_instance()->dl_handles_.push_back(handle);
110const google::protobuf::FileDescriptor*
113 DCCL_LOCK_DYNAMIC_PROTOBUF_MANAGER_MUTEX
115 if (!get_instance()->source_database_)
117 "Must called enable_compilation() before loading proto files directly"));
119 return get_instance()->user_descriptor_pool_->FindFileByName(protofile_absolute_path);
122const google::protobuf::FileDescriptor*
125 DCCL_LOCK_DYNAMIC_PROTOBUF_MANAGER_MUTEX
126 get_instance()->simple_database_->Add(proto);
128 const google::protobuf::FileDescriptor* return_desc =
129 get_instance()->user_descriptor_pool_->FindFileByName(proto.name());
137void dccl::DynamicProtobufManager::enable_disk_source_database()
139 if (disk_source_tree_)
142 disk_source_tree_.reset(
new google::protobuf::compiler::DiskSourceTree);
143 source_database_.reset(
144 new google::protobuf::compiler::SourceTreeDescriptorDatabase(disk_source_tree_.get()));
145 error_collector_.reset(
new DLogMultiFileErrorCollector);
147 source_database_->RecordErrorsTo(error_collector_.get());
148 disk_source_tree_->MapPath(
"/",
"/");
149 disk_source_tree_->MapPath(
"",
"");
150 add_database(source_database_);
154#if GOOGLE_PROTOBUF_VERSION >= 4022000
155void dccl::DynamicProtobufManager::DLogMultiFileErrorCollector::RecordError(
156 absl::string_view filename,
int line,
int column, absl::string_view message)
158void dccl::DynamicProtobufManager::DLogMultiFileErrorCollector::AddError(
159 const std::string& filename,
int line,
int column,
const std::string& message)
162 std::stringstream ss;
163 ss <<
"File: " << filename <<
" has error (line: " << line <<
", column: " << column
static GoogleProtobufMessagePointer new_protobuf_message(const std::string &protobuf_type_name, bool user_pool_first=false)
Create a new (empty) Google Protobuf message of a given type by name.
static void add_include_path(const std::string &path)
Add a path for searching for import messages when loading .proto files using load_from_proto_file()
static void * load_from_shared_lib(const std::string &shared_lib_path)
Load compiled .proto files from a UNIX shared library (i.e. *.so or *.dylib)
static const google::protobuf::Descriptor * find_descriptor(const std::string &protobuf_type_name, bool user_pool_first=false)
Finds the Google Protobuf Descriptor (essentially a meta-class for a given Message) from a given Mess...
static const google::protobuf::FileDescriptor * add_protobuf_file(const google::protobuf::FileDescriptorProto &proto)
Add a protobuf file defined in a google::protobuf::FileDescriptorProto.
static const google::protobuf::FileDescriptor * load_from_proto_file(const std::string &protofile_absolute_path)
Load a message from a .proto file on the disk. enable_compilation() must be called first.
static void add_database(std::shared_ptr< google::protobuf::DescriptorDatabase > database)
Add a Google Protobuf DescriptorDatabase to the set of databases searched for Message Descriptors.
Exception class for DCCL.