/******************************************************************************
* Copyright 2018 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#ifndef CYBER_CLASS_LOADER_UTILITY_CLASS_LOADER_UTILITY_H_
#define CYBER_CLASS_LOADER_UTILITY_CLASS_LOADER_UTILITY_H_
#include <cassert>
#include <cstdio>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
#include "../shared_library/shared_library.h"
#include "class_factory.h"
//#include "cyber/common/log.h"
/**
* class register implement
*/
namespace apollo {
namespace cyber {
namespace class_loader {
class ClassLoader;
namespace utility {
using SharedLibraryPtr = std::shared_ptr<SharedLibrary>;
using ClassClassFactoryMap =
std::map<std::string, utility::AbstractClassFactoryBase*>;
using BaseToClassFactoryMapMap = std::map<std::string, ClassClassFactoryMap>;
using LibPathSharedLibVector =
std::vector<std::pair<std::string, SharedLibraryPtr>>;
using ClassFactoryVector = std::vector<AbstractClassFactoryBase*>;
BaseToClassFactoryMapMap& GetClassFactoryMapMap();
std::recursive_mutex& GetClassFactoryMapMapMutex();
LibPathSharedLibVector& GetLibPathSharedLibVector();
std::recursive_mutex& GetLibPathSharedLibMutex();
ClassClassFactoryMap& GetClassFactoryMapByBaseClass(
const std::string& typeid_base_class_name);
std::string GetCurLoadingLibraryName();
void SetCurLoadingLibraryName(const std::string& library_name);
ClassLoader* GetCurActiveClassLoader();
void SetCurActiveClassLoader(ClassLoader* loader);
bool IsLibraryLoaded(const std::string& library_path, ClassLoader* loader);
bool IsLibraryLoadedByAnybody(const std::string& library_path);
bool LoadLibrary(const std::string& library_path, ClassLoader* loader);
void UnloadLibrary(const std::string& library_path, ClassLoader* loader);
template <typename Derived, typename Base>
void RegisterClass(const std::string& class_name,
const std::string& base_class_name);
template <typename Base>
Base* CreateClassObj(const std::string& class_name, ClassLoader* loader);
template <typename Base>
std::vector<std::string> GetValidClassNames(ClassLoader* loader);
template <typename Derived, typename Base>
void RegisterClass(const std::string& class_name,
const std::string& base_class_name) {
// AINFO << "registerclass:" << class_name << "," << base_class_name << ","
// << GetCurLoadingLibraryName();
utility::AbstractClassFactory<Base>* new_class_factory_obj =
new utility::ClassFactory<Derived, Base>(class_name, base_class_name);
new_class_factory_obj->AddOwnedClassLoader(GetCurActiveClassLoader());
new_class_factory_obj->SetRelativeLibraryPath(GetCurLoadingLibraryName());
GetClassFactoryMapMapMutex().lock();
ClassClassFactoryMap& factory_map =
GetClassFactoryMapByBaseClass(typeid(Base).name());
factory_map[class_name] = new_class_factory_obj;
GetClassFactoryMapMapMutex().unlock();
}
template <typename Base>
Base* CreateClassObj(const std::string& class_name, ClassLoader* loader) {
GetClassFactoryMapMapMutex().lock();
ClassClassFactoryMap& factoryMap =
GetClassFactoryMapByBaseClass(typeid(Base).name());
AbstractClassFactory<Base>* factory = nullptr;
if (factoryMap.find(class_name) != factoryMap.end()) {
factory = dynamic_cast<utility::AbstractClassFactory<Base>*>(
factoryMap[class_name]);
}
GetClassFactoryMapMapMutex().unlock();
Base* classobj = nullptr;
if (factory && factory->IsOwnedBy(loader)) {
classobj = factory->CreateObj();
}
return classobj;
}
template <typename Base>
std::vector<std::string> GetValidClassNames(ClassLoader* loader) {
std::lock_guard<std::recursive_mutex> lck(GetClassFactoryMapMapMutex());
ClassClassFactoryMap& factoryMap =
GetClassFactoryMapByBaseClass(typeid(Base).name());
std::vector<std::string> classes;
for (auto& class_factory : factoryMap) {
AbstractClassFactoryBase* factory = class_factory.second;
if (factory && factory->IsOwnedBy(loader)) {
classes.emplace_back(class_factory.first);
}
}
return classes;
}
} // namespace utility
} // namespace class_loader
} // namespace cyber
} // namespace apollo
#endif // CYBER_CLASS_LOADER_UTILITY_CLASS_LOADER_UTILITY_H_
没有合适的资源?快使用搜索试试~ 我知道了~
class-loader测试工程
共25个文件
h:8个
cc:7个
build:4个
需积分: 0 5 下载量 171 浏览量
2023-06-01
16:01:43
上传
评论
收藏 22KB ZIP 举报
温馨提示
class-loader测试工程
资源推荐
资源详情
资源评论
收起资源包目录
20230531.zip (25个子文件)
20230531
WORKSPACE 0B
src
plugin.cc 1KB
class_loader
utility
class_factory.h 3KB
class_loader_utility.cc 10KB
class_loader_utility.h 5KB
class_factory.cc 3KB
BUILD 397B
class_loader.h 3KB
class_loader_manager.h 4KB
class_loader.cc 2KB
shared_library
shared_library.h 4KB
shared_library.cc 3KB
BUILD 302B
exceptions.h 2KB
class_loader_manager.cc 3KB
BUILD 732B
class_loader_register_macro.h 2KB
BUILD 575B
main.cc 1KB
base.h 978B
.idea
workspace.xml 4KB
misc.xml 137B
20230531.iml 97B
modules.xml 268B
.gitignore 176B
共 25 条
- 1
资源评论
H-KING
- 粉丝: 3433
- 资源: 33
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功