35namespace plugin_manager {
49 std::string* library_path);
57 bool LoadPlugin(
const std::string& plugin_description_file_path);
72 template <
typename Base>
73 std::shared_ptr<Base>
CreateInstance(
const std::string& derived_class);
95 template <
typename Base>
104 template <
typename Base>
106 const std::string& conf_name);
120 template <
typename Base>
128 template <
typename Base>
135 template <
typename Base>
140 std::map<std::string, std::shared_ptr<PluginDescription>>
141 plugin_description_map_;
142 std::map<std::string, bool> plugin_loaded_map_;
143 std::map<std::pair<std::string, std::string>, std::string>
144 plugin_class_plugin_name_map_;
149template <
typename Base>
151 const std::string& derived_class) {
152 AINFO <<
"creating plugin instance of " << derived_class;
153 if (!CheckAndLoadPluginLibrary<Base>(derived_class)) {
154 AERROR <<
"plugin of class " << derived_class <<
" have not been loaded";
160template <
typename Base>
162 const std::string& class_name) {
163 if (!CheckAndLoadPluginLibrary<Base>(class_name)) {
164 AERROR <<
"plugin of class " << class_name <<
" have not been loaded";
167 std::string library_path =
169 if (library_path ==
"") {
170 AWARN <<
"plugin of class " << class_name <<
" not found";
173 for (
auto it = plugin_description_map_.begin();
174 it != plugin_description_map_.end(); ++it) {
175 if (it->second->actual_library_path_ == library_path) {
177 std::string relative_prefix =
"share/";
178 std::string relative_plugin_home_path =
180 if (relative_plugin_home_path.rfind(relative_prefix, 0) == 0) {
181 relative_plugin_home_path =
182 relative_plugin_home_path.substr(relative_prefix.size());
184 return relative_plugin_home_path;
191template <
typename Base>
193 const std::string& conf_name) {
194 std::string plugin_home_path = GetPluginClassHomePath<Base>(class_name);
197 AWARN <<
"plugin of class " << class_name <<
" load from absolute path, "
198 <<
"conf path will be relative to it's description file";
201 std::string relative_conf_path = plugin_home_path +
"/" + conf_name;
202 std::string actual_conf_path;
204 relative_conf_path,
"APOLLO_CONF_PATH", &actual_conf_path)) {
205 return actual_conf_path;
207 return plugin_home_path +
"/" + conf_name;
210template <
typename Base>
213 std::string base_class_name =
214 abi::__cxa_demangle(
typeid(
Base).name(), 0, 0, &status);
215 if (plugin_class_plugin_name_map_.find({class_name, base_class_name}) ==
216 plugin_class_plugin_name_map_.end()) {
220 std::string plugin_name =
221 plugin_class_plugin_name_map_[{class_name, base_class_name}];
222 if (plugin_loaded_map_.find(plugin_name) == plugin_loaded_map_.end()) {
227 return plugin_loaded_map_[plugin_name];
230template <
typename Base>
232 if (IsLibraryLoaded<Base>(class_name)) {
236 std::string base_class_name =
237 abi::__cxa_demangle(
typeid(
Base).name(), 0, 0, &status);
238 if (plugin_class_plugin_name_map_.find({class_name, base_class_name}) ==
239 plugin_class_plugin_name_map_.end()) {
241 AWARN <<
"plugin of class " << class_name <<
" not found, "
242 <<
"please check if it's registered";
245 std::string plugin_name =
246 plugin_class_plugin_name_map_[{class_name, base_class_name}];
247 if (plugin_description_map_.find(plugin_name) ==
248 plugin_description_map_.end()) {
250 AWARN <<
"plugin description of class " << class_name <<
" not found, "
251 <<
"please check if it's loaded";
254 auto plugin_description = plugin_description_map_[plugin_name];
255 return LoadLibrary(plugin_description->actual_library_path_);
258template <
typename Base>
261 std::string base_class_name =
262 abi::__cxa_demangle(
typeid(
Base).name(), 0, 0, &status);
263 std::vector<std::string> derived_class_name;
264 for (
const auto& iter : plugin_class_plugin_name_map_) {
265 if (iter.first.second == base_class_name) {
266 derived_class_name.push_back(iter.first.first);
269 return derived_class_name;
272#define CYBER_PLUGIN_MANAGER_REGISTER_PLUGIN(name, base) \
273 CLASS_LOADER_REGISTER_CLASS(name, base)
std::shared_ptr< Base > CreateClassObj(const std::string &class_name)
std::string GetClassValidLibrary(const std::string &class_name)
get pathof library that class belongs to
std::string GetPluginConfPath(const std::string &class_name, const std::string &conf_name)
get plugin configuration file location
static PluginManager * Instance()
get singleton instance of PluginManager
bool CheckAndLoadPluginLibrary(const std::string &class_name)
check if library of plugin is loaded, and load it if not
std::string GetPluginClassHomePath(const std::string &class_name)
@bried get plugin description file location that class belongs to
bool LoadInstalledPlugins()
load plugins from installed path
std::shared_ptr< Base > CreateInstance(const std::string &derived_class)
create plugin instance of derived class based on Base
bool LoadPlugin(const std::string &plugin_description_file_path)
load plugin clases from file
bool LoadLibrary(const std::string &library_path)
load library of plugin
bool IsLibraryLoaded(const std::string &class_name)
check if library of plugin is loaded
bool FindPluginIndexAndLoad(const std::string &plugin_index_path)
find plugin index file and load plugins
std::vector< std::string > GetDerivedClassNameByBaseClass()
Get all derived class name by base class name
bool ProcessPluginDescriptionFile(const std::string &file_path, std::string *library_path)
parse plugin description file and load the library TODO(liangjinping): parse description to struct
std::string GetDirName(const std::string &path)
get directory name of path
bool GetFilePathWithEnv(const std::string &path, const std::string &env_var, std::string *file_path)
get file path, judgement priority:
bool PathIsAbsolute(const std::string &path)