26 #include "dynamic_protobuf_manager.h"
27 #include "exception.h"
30 std::shared_ptr<dccl::DynamicProtobufManager> dccl::DynamicProtobufManager::inst_;
35 const google::protobuf::Descriptor*
39 DCCL_LOCK_DYNAMIC_PROTOBUF_MANAGER_MUTEX
40 const google::protobuf::Descriptor* desc =
nullptr;
44 desc = get_instance()->user_descriptor_pool_->FindMessageTypeByName(protobuf_type_name);
50 desc = google::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName(
58 desc = get_instance()->user_descriptor_pool_->FindMessageTypeByName(protobuf_type_name);
64 std::shared_ptr<google::protobuf::Message>
67 DCCL_LOCK_DYNAMIC_PROTOBUF_MANAGER_MUTEX
68 return new_protobuf_message<std::shared_ptr<google::protobuf::Message>>(desc);
71 std::shared_ptr<google::protobuf::Message>
74 DCCL_LOCK_DYNAMIC_PROTOBUF_MANAGER_MUTEX
75 return new_protobuf_message<std::shared_ptr<google::protobuf::Message>>(protobuf_type_name);
79 std::shared_ptr<google::protobuf::DescriptorDatabase> database)
81 DCCL_LOCK_DYNAMIC_PROTOBUF_MANAGER_MUTEX
82 get_instance()->databases_.push_back(database);
83 get_instance()->update_databases();
88 DCCL_LOCK_DYNAMIC_PROTOBUF_MANAGER_MUTEX
90 if (!get_instance()->disk_source_tree_)
91 throw(std::runtime_error(
92 "Must called enable_compilation() before loading proto files directly"));
94 get_instance()->disk_source_tree_->MapPath(
"", path);
99 DCCL_LOCK_DYNAMIC_PROTOBUF_MANAGER_MUTEX
100 void* handle = dlopen(shared_lib_path.c_str(), RTLD_LAZY);
102 get_instance()->dl_handles_.push_back(handle);
111 const google::protobuf::FileDescriptor*
114 DCCL_LOCK_DYNAMIC_PROTOBUF_MANAGER_MUTEX
116 if (!get_instance()->source_database_)
118 "Must called enable_compilation() before loading proto files directly"));
120 return get_instance()->user_descriptor_pool_->FindFileByName(protofile_absolute_path);
123 const google::protobuf::FileDescriptor*
126 DCCL_LOCK_DYNAMIC_PROTOBUF_MANAGER_MUTEX
127 get_instance()->simple_database_->Add(proto);
129 const google::protobuf::FileDescriptor* return_desc =
130 get_instance()->user_descriptor_pool_->FindFileByName(proto.name());
138 void dccl::DynamicProtobufManager::enable_disk_source_database()
140 if (disk_source_tree_)
143 disk_source_tree_.reset(
new google::protobuf::compiler::DiskSourceTree);
144 source_database_.reset(
145 new google::protobuf::compiler::SourceTreeDescriptorDatabase(disk_source_tree_.get()));
146 error_collector_.reset(
new DLogMultiFileErrorCollector);
148 source_database_->RecordErrorsTo(error_collector_.get());
149 disk_source_tree_->MapPath(
"/",
"/");
150 disk_source_tree_->MapPath(
"",
"");
151 add_database(source_database_);
155 void dccl::DynamicProtobufManager::DLogMultiFileErrorCollector::AddError(
156 const std::string& filename,
int line,
int column,
const std::string& message)
158 std::stringstream ss;
159 ss <<
"File: " << filename <<
" has error (line: " << line <<
", column: " << column