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