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 FOUNDATION_APPEXECFWK_OHOS_APPLICATION_LOADER_H 17 #define FOUNDATION_APPEXECFWK_OHOS_APPLICATION_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 * @param bundleName the bundle name of the application. 56 * @return Return {@link OHOSApplication} object which is registered by developer. 57 */ 58 OHOSApplication *GetApplicationByName(const std::string &bundleName = "OHOSApplication"); 59 60 private: 61 ApplicationLoader() = default; 62 ApplicationLoader(const ApplicationLoader &) = delete; 63 ApplicationLoader &operator=(const ApplicationLoader &) = delete; 64 ApplicationLoader(ApplicationLoader &&) = delete; 65 ApplicationLoader &operator=(ApplicationLoader &&) = delete; 66 67 std::unordered_map<std::string, CreateApplication> applications_; 68 }; 69 70 /** 71 * @brief Registers the class name of an {@link OHOSApplication} child class. 72 * 73 * After implementing your own {@link OHOSApplication} class, you should call this function so that the 74 * OHOSApplication management framework can create <b>OHOSApplication</b> instances when loading your 75 * <b>OHOSApplication</b> class. 76 * 77 * @param className Indicates the {@link OHOSApplication} class name to register. 78 */ 79 #define REGISTER_APPLICATION(bundleName, className) \ 80 __attribute__((constructor)) void REGISTER_APPLICATION##className() \ 81 { \ 82 ApplicationLoader::GetInstance().RegisterApplication( \ 83 #bundleName, []()->OHOSApplication * { return new (std::nothrow) className; }); \ 84 } 85 86 } // namespace AppExecFwk 87 } // namespace OHOS 88 #endif // FOUNDATION_APPEXECFWK_OHOS_APPLICATION_LOADER_H 89