Apollo  6.0
Open source self driving car software
class_loader_manager.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_CLASS_LOADER_CLASS_LOADER_MANAGER_H_
18 #define CYBER_CLASS_LOADER_CLASS_LOADER_MANAGER_H_
19 
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include <vector>
25 
27 
28 namespace apollo {
29 namespace cyber {
30 namespace class_loader {
31 
33  public:
35  virtual ~ClassLoaderManager();
36 
37  bool LoadLibrary(const std::string& library_path);
38  void UnloadAllLibrary();
39  bool IsLibraryValid(const std::string& library_path);
40  template <typename Base>
41  std::shared_ptr<Base> CreateClassObj(const std::string& class_name);
42  template <typename Base>
43  std::shared_ptr<Base> CreateClassObj(const std::string& class_name,
44  const std::string& library_path);
45  template <typename Base>
46  bool IsClassValid(const std::string& class_name);
47  template <typename Base>
48  std::vector<std::string> GetValidClassNames();
49 
50  private:
51  ClassLoader* GetClassLoaderByLibPath(const std::string& library_path);
52  std::vector<ClassLoader*> GetAllValidClassLoaders();
53  std::vector<std::string> GetAllValidLibPath();
54  int UnloadLibrary(const std::string& library_path);
55 
56  private:
57  std::mutex libpath_loader_map_mutex_;
58  std::map<std::string, ClassLoader*> libpath_loader_map_;
59 };
60 
61 template <typename Base>
62 std::shared_ptr<Base> ClassLoaderManager::CreateClassObj(
63  const std::string& class_name) {
64  std::vector<ClassLoader*> class_loaders = GetAllValidClassLoaders();
65  for (auto class_loader : class_loaders) {
66  if (class_loader->IsClassValid<Base>(class_name)) {
67  return (class_loader->CreateClassObj<Base>(class_name));
68  }
69  }
70  AERROR << "Invalid class name: " << class_name;
71  return std::shared_ptr<Base>();
72 }
73 
74 template <typename Base>
75 std::shared_ptr<Base> ClassLoaderManager::CreateClassObj(
76  const std::string& class_name, const std::string& library_path) {
77  ClassLoader* loader = GetClassLoaderByLibPath(library_path);
78  if (loader) {
79  return (loader->CreateClassObj<Base>(class_name));
80  }
81  AERROR << "Could not create classobj, there is no ClassLoader in: "
82  << class_name;
83  return std::shared_ptr<Base>();
84 }
85 
86 template <typename Base>
87 bool ClassLoaderManager::IsClassValid(const std::string& class_name) {
88  std::vector<std::string> valid_classes = GetValidClassNames<Base>();
89  return (valid_classes.end() !=
90  std::find(valid_classes.begin(), valid_classes.end(), class_name));
91 }
92 
93 template <typename Base>
94 std::vector<std::string> ClassLoaderManager::GetValidClassNames() {
95  std::vector<std::string> valid_classes;
96  for (auto class_loader : GetAllValidClassLoaders()) {
97  std::vector<std::string> class_loaders =
98  class_loader->GetValidClassNames<Base>();
99  valid_classes.insert(valid_classes.end(), class_loaders.begin(),
100  class_loaders.end());
101  }
102  return valid_classes;
103 }
104 
105 } // namespace class_loader
106 } // namespace cyber
107 } // namespace apollo
108 
109 #endif // CYBER_CLASS_LOADER_CLASS_LOADER_MANAGER_H_
Definition: base.h:20
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: class_loader_manager.h:32
bool LoadLibrary(const std::string &library_path)
std::shared_ptr< Base > CreateClassObj(const std::string &class_name)
Definition: class_loader.h:76
bool IsClassValid(const std::string &class_name)
Definition: class_loader_manager.h:87
std::vector< std::string > GetValidClassNames()
Definition: class_loader_manager.h:94
#define AERROR
Definition: log.h:44
bool IsLibraryValid(const std::string &library_path)
std::shared_ptr< Base > CreateClassObj(const std::string &class_name)
Definition: class_loader_manager.h:62
Definition: class_loader.h:35