1 /* 2 * Copyright (c) 2020 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 /** 17 * @addtogroup AbilityKit 18 * @{ 19 * 20 * @brief Provides ability-related functions, including ability lifecycle callbacks and functions for connecting to or 21 * disconnecting from Particle Abilities. 22 * 23 * Abilities are classified into Feature Abilities and Particle Abilities. Feature Abilities support the Page template, 24 * and Particle Abilities support the Service template. An ability using the Page template is called a Page ability for 25 * short and that using the Service template is called a Service ability. 26 * 27 * @since 1.0 28 * @version 1.0 29 */ 30 31 /** 32 * @file ability_loader.h 33 * 34 * @brief Declares functions for registering the class names of {@link Ability} and {@link AbilitySlice} with the 35 * ability management framework. 36 * 37 * After creating your own {@link Ability} and {@link AbilitySlice} child classes, you should register their class 38 * names with the ability management framework so that the application can start <b>Ability</b> instances created in 39 * the background. 40 * 41 * @since 1.0 42 * @version 1.0 43 */ 44 45 #ifndef OHOS_ABILITY_LOADER_H 46 #define OHOS_ABILITY_LOADER_H 47 48 #include <functional> 49 #include <string> 50 #include <unordered_map> 51 52 #include "ability.h" 53 #ifdef ABILITY_WINDOW_SUPPORT 54 #include "ability_slice.h" 55 #endif 56 57 namespace OHOS { 58 using CreateAblity = std::function<Ability *(void)>; 59 #ifdef ABILITY_WINDOW_SUPPORT 60 using CreateSlice = std::function<AbilitySlice *(void)>; 61 #endif 62 63 /** 64 * @brief Declares functions for registering the class names of {@link Ability} and {@link AbilitySlice} with the 65 * ability management framework. 66 * 67 * After creating your own {@link Ability} and {@link AbilitySlice} child classes, you should register their class 68 * names with the ability management framework so that the application can start <b>Ability</b> instances created in 69 * the background. 70 * 71 * @since 1.0 72 * @version 1.0 73 */ 74 class AbilityLoader { 75 public: GetInstance()76 static AbilityLoader &GetInstance() 77 { 78 static AbilityLoader abilityLoader; 79 return abilityLoader; 80 } 81 82 ~AbilityLoader() = default; 83 84 void RegisterAbility(const std::string &abilityName, const CreateAblity &createFunc); 85 Ability *GetAbilityByName(const std::string &abilityName); 86 87 #ifdef ABILITY_WINDOW_SUPPORT 88 void RegisterAbilitySlice(const std::string &sliceName, const CreateSlice &createFunc); 89 AbilitySlice *GetAbilitySliceByName(const std::string &sliceName); 90 #endif 91 92 private: 93 AbilityLoader() = default; 94 AbilityLoader(const AbilityLoader&) = delete; 95 AbilityLoader &operator=(const AbilityLoader &) = delete; 96 AbilityLoader(AbilityLoader &&) = delete; 97 AbilityLoader &operator=(AbilityLoader &&) = delete; 98 99 std::unordered_map<std::string, CreateAblity> abilities_; 100 #ifdef ABILITY_WINDOW_SUPPORT 101 std::unordered_map<std::string, CreateSlice> slices_; 102 #endif 103 }; 104 105 /** 106 * @brief Registers the class name of an {@link Ability} child class. 107 * 108 * After implementing your own {@link Ability} class, you should call this function so that the ability management 109 * framework can create <b>Ability</b> instances when loading your <b>Ability</b> class. 110 * 111 * @param className Indicates the {@link Ability} class name to register. 112 */ 113 #define REGISTER_AA(className) \ 114 __attribute__((constructor)) void RegisterAA##className() { \ 115 AbilityLoader::GetInstance().RegisterAbility(#className, []()->Ability* { \ 116 return new className; \ 117 }); \ 118 } 119 120 /** 121 * @brief Registers the class name of an {@link AbilitySlice} child class. 122 * 123 * After implementing your own {@link AbilitySlice} class, you should call this function so that the ability 124 * management framework can create <b>AbilitySlice</b> instances when loading your <b>AbilitySlice</b> class. 125 * 126 * @param className Indicates the {@link AbilitySlice} class name to register. 127 */ 128 #ifdef ABILITY_WINDOW_SUPPORT 129 #define REGISTER_AS(className) \ 130 __attribute__((constructor)) void RegisterAS##className() { \ 131 AbilityLoader::GetInstance().RegisterAbilitySlice(#className, []()->AbilitySlice* { \ 132 return new className; \ 133 }); \ 134 } 135 #endif 136 } // namespace OHOS 137 #endif // OHOS_ABILITY_LOADER_H