Apollo 10.0
自动驾驶开放平台
class_loader.h
浏览该文件的文档.
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
28namespace apollo {
29namespace cyber {
30namespace class_loader {
31
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
63template <typename Base>
64std::vector<std::string> ClassLoader::GetValidClassNames() {
65 return (utility::GetValidClassNames<Base>(this));
66}
67
68template <typename Base>
69bool 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
75template <typename Base>
76std::shared_ptr<Base> ClassLoader::CreateClassObj(
77 const std::string& class_name) {
78 if (!IsLibraryLoaded()) {
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
97template <typename Base>
98void ClassLoader::OnClassObjDeleter(Base* obj) {
99 if (nullptr == obj) {
100 return;
101 }
102
103 delete obj;
104 std::lock_guard<std::mutex> lck(classobj_ref_count_mutex_);
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
for library load,createclass object
std::vector< std::string > GetValidClassNames()
bool IsClassValid(const std::string &class_name)
const std::string GetLibraryPath() const
std::shared_ptr< Base > CreateClassObj(const std::string &class_name)
#define AWARN
Definition log.h:43
class register implement
Definition arena_queue.h:37