1 /* 2 * Copyright (c) 2021 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 16 #ifndef OHOS_ABILITY_RUNTIME_APP_LOADER_H 17 #define OHOS_ABILITY_RUNTIME_APP_LOADER_H 18 19 #include "ohos_application.h" 20 21 #include <functional> 22 #include <string> 23 #include <unordered_map> 24 25 namespace OHOS { 26 namespace AppExecFwk { 27 using CreateApplication = std::function<OHOSApplication *(void)>; 28 29 class ApplicationLoader { 30 public: 31 /** 32 * @description: Gets the ApplicationLoader object to application register 33 * @param None 34 * @return ApplicationLoader 35 */ 36 static ApplicationLoader &GetInstance(); 37 38 /** 39 * @description: Ddefault deconstructor 40 * @param None 41 * @return None 42 */ 43 ~ApplicationLoader() = default; 44 45 /** 46 * @description: Gets the ApplicationLoader object to register application 47 * @param bundleName the bundle name of the application. 48 * @param createFunc constructor function of application class. 49 * @return None 50 */ 51 void RegisterApplication(const std::string &bundleName, const CreateApplication &createFunc); 52 53 /** 54 * @description: Gets the {@link OHOSApplication} object 55 * @return Return {@link OHOSApplication} object which is registered by developer. 56 */ 57 OHOSApplication *GetApplicationByName(); 58 59 private: 60 ApplicationLoader() = default; 61 ApplicationLoader(const ApplicationLoader &) = delete; 62 ApplicationLoader &operator=(const ApplicationLoader &) = delete; 63 ApplicationLoader(ApplicationLoader &&) = delete; 64 ApplicationLoader &operator=(ApplicationLoader &&) = delete; 65 66 std::unordered_map<std::string, CreateApplication> applications_; 67 }; 68 69 /** 70 * @brief Registers the class name of an {@link OHOSApplication} child class. 71 * 72 * After implementing your own {@link OHOSApplication} class, you should call this function so that the 73 * OHOSApplication management framework can create <b>OHOSApplication</b> instances when loading your 74 * <b>OHOSApplication</b> class. 75 * 76 * @param className Indicates the {@link OHOSApplication} class name to register. 77 */ 78 #define REGISTER_APPLICATION(bundleName, className) \ 79 __attribute__((constructor)) void REGISTER_APPLICATION##className() \ 80 { \ 81 ApplicationLoader::GetInstance().RegisterApplication( \ 82 #bundleName, []()->OHOSApplication * { return new (std::nothrow) className; }); \ 83 } 84 } // namespace AppExecFwk 85 } // namespace OHOS 86 #endif // OHOS_ABILITY_RUNTIME_APP_LOADER_H 87