Apollo  6.0
Open source self driving car software
class_loader.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 #ifndef CYBER_CLASS_LOADER_CLASS_LOADER_H_
17 #define CYBER_CLASS_LOADER_CLASS_LOADER_H_
18 
19 #include <algorithm>
20 #include <functional>
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 
35 class ClassLoader {
36  public:
37  explicit ClassLoader(const std::string& library_path);
38  virtual ~ClassLoader();
39 
40  bool IsLibraryLoaded();
41  bool LoadLibrary();
42  int UnloadLibrary();
43  const std::string GetLibraryPath() const;
44  template <typename Base>
45  std::vector<std::string> GetValidClassNames();
46  template <typename Base>
47  std::shared_ptr<Base> CreateClassObj(const std::string& class_name);
48  template <typename Base>
49  bool IsClassValid(const std::string& class_name);
50 
51  private:
52  template <typename Base>
53  void OnClassObjDeleter(Base* obj);
54 
55  private:
56  std::string library_path_;
57  int loadlib_ref_count_;
58  std::mutex loadlib_ref_count_mutex_;
59  int classobj_ref_count_;
60  std::mutex classobj_ref_count_mutex_;
61 };
62 
63 template <typename Base>
64 std::vector<std::string> ClassLoader::GetValidClassNames() {
65  return (utility::GetValidClassNames<Base>(this));
66 }
67 
68 template <typename Base>
69 bool ClassLoader::IsClassValid(const std::string& class_name) {
70  std::vector<std::string> valid_classes = GetValidClassNames<Base>();
71  return (std::find(valid_classes.begin(), valid_classes.end(), class_name) !=
72  valid_classes.end());
73 }
74 
75 template <typename Base>
76 std::shared_ptr<Base> ClassLoader::CreateClassObj(
77  const std::string& class_name) {
78  if (!IsLibraryLoaded()) {
79  LoadLibrary();
80  }
81 
82  Base* class_object = utility::CreateClassObj<Base>(class_name, this);
83  if (class_object == nullptr) {
84  AWARN << "CreateClassObj failed, ensure class has been registered. "
85  << "classname: " << class_name << ",lib: " << GetLibraryPath();
86  return std::shared_ptr<Base>();
87  }
88 
89  std::lock_guard<std::mutex> lck(classobj_ref_count_mutex_);
90  classobj_ref_count_ = classobj_ref_count_ + 1;
91  std::shared_ptr<Base> classObjSharePtr(
92  class_object, std::bind(&ClassLoader::OnClassObjDeleter<Base>, this,
93  std::placeholders::_1));
94  return classObjSharePtr;
95 }
96 
97 template <typename Base>
98 void ClassLoader::OnClassObjDeleter(Base* obj) {
99  if (nullptr == obj) {
100  return;
101  }
102 
103  std::lock_guard<std::mutex> lck(classobj_ref_count_mutex_);
104  delete obj;
105  --classobj_ref_count_;
106 }
107 
108 } // namespace class_loader
109 } // namespace cyber
110 } // namespace apollo
111 #endif // CYBER_CLASS_LOADER_CLASS_LOADER_H_
Definition: base.h:20
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
std::shared_ptr< Base > CreateClassObj(const std::string &class_name)
Definition: class_loader.h:76
ClassLoader(const std::string &library_path)
std::vector< std::string > GetValidClassNames()
Definition: class_loader.h:64
const std::string GetLibraryPath() const
#define AWARN
Definition: log.h:43
bool IsClassValid(const std::string &class_name)
Definition: class_loader.h:69
Definition: class_loader.h:35