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 CreateAbility = 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: 76 static AbilityLoader &GetInstance(); 77 78 ~AbilityLoader() = default; 79 80 void RegisterAbility(const std::string &abilityName, const CreateAbility &createFunc); 81 Ability *GetAbilityByName(const std::string &abilityName); 82 83 #ifdef ABILITY_WINDOW_SUPPORT 84 void RegisterAbilitySlice(const std::string &sliceName, const CreateSlice &createFunc); 85 AbilitySlice *GetAbilitySliceByName(const std::string &sliceName); 86 #endif 87 88 private: 89 AbilityLoader() = default; 90 AbilityLoader(const AbilityLoader&) = delete; 91 AbilityLoader &operator=(const AbilityLoader &) = delete; 92 AbilityLoader(AbilityLoader &&) = delete; 93 AbilityLoader &operator=(AbilityLoader &&) = delete; 94 95 std::unordered_map<std::string, CreateAbility> abilities_; 96 #ifdef ABILITY_WINDOW_SUPPORT 97 std::unordered_map<std::string, CreateSlice> slices_; 98 #endif 99 }; 100 101 /** 102 * @brief Registers the class name of an {@link Ability} child class. 103 * 104 * After implementing your own {@link Ability} class, you should call this function so that the ability management 105 * framework can create <b>Ability</b> instances when loading your <b>Ability</b> class. 106 * 107 * @param className Indicates the {@link Ability} class name to register. 108 */ 109 #define REGISTER_AA(className) \ 110 __attribute__((constructor)) void RegisterAA##className() { \ 111 AbilityLoader::GetInstance().RegisterAbility(#className, []()->Ability* { \ 112 return new className; \ 113 }); \ 114 } 115 116 /** 117 * @brief Registers the class name of an {@link AbilitySlice} child class. 118 * 119 * After implementing your own {@link AbilitySlice} class, you should call this function so that the ability 120 * management framework can create <b>AbilitySlice</b> instances when loading your <b>AbilitySlice</b> class. 121 * 122 * @param className Indicates the {@link AbilitySlice} class name to register. 123 */ 124 #ifdef ABILITY_WINDOW_SUPPORT 125 #define REGISTER_AS(className) \ 126 __attribute__((constructor)) void RegisterAS##className() { \ 127 AbilityLoader::GetInstance().RegisterAbilitySlice(#className, []()->AbilitySlice* { \ 128 return new className; \ 129 }); \ 130 } 131 #endif 132 } // namespace OHOS 133 #endif // OHOS_ABILITY_LOADER_H 134