1 /* 2 * Copyright (C) 2021–2022 Beijing OSWare Technology Co., Ltd 3 * This file contains confidential and proprietary information of 4 * OSWare Technology Co., Ltd 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 #ifndef DISPLAY_MODULE_LOADER_H 19 #define DISPLAY_MODULE_LOADER_H 20 21 #include <dlfcn.h> 22 #include <memory> 23 #include <string> 24 #include <display_common.h> 25 namespace OHOS { 26 namespace HDI { 27 namespace DISPLAY { 28 class DisplayModuleLoader { 29 public: Create(const std::string pathName)30 static std::unique_ptr<DisplayModuleLoader> Create(const std::string pathName) 31 { 32 void *handle = dlopen(pathName.c_str(), RTLD_NOW | RTLD_NOLOAD); 33 if (handle == nullptr) { 34 handle = dlopen(pathName.c_str(), RTLD_NOW); 35 if (handle == nullptr) { 36 return nullptr; 37 } 38 } 39 return std::make_unique<DisplayModuleLoader>(handle); 40 } 41 DisplayModuleLoader(void * handle)42 explicit DisplayModuleLoader(void *handle) : handle_(handle) {} 43 GetSymbol(const std::string symbol)44 void *GetSymbol(const std::string symbol) 45 { 46 return dlsym(handle_, symbol.c_str()); 47 } 48 ~DisplayModuleLoader()49 ~DisplayModuleLoader() 50 { 51 if (handle_ != nullptr) { 52 dlclose(handle_); 53 handle_ = nullptr; 54 } 55 } 56 57 private: 58 void *handle_; 59 }; 60 } // namespace DISPLAY 61 } // namespace HDI 62 } // namespace OHOS 63 #endif // DISPLAY_MODULE_LOADER_H