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 16 #ifndef OHOS_ABILITY_ABILITY_TRANSACTION_CALLBACK_INFO_H 17 #define OHOS_ABILITY_ABILITY_TRANSACTION_CALLBACK_INFO_H 18 19 #include <memory> 20 #include <stack> 21 22 namespace OHOS { 23 namespace AppExecFwk { 24 template<typename T = void> 25 class AbilityTransactionCallbackInfo { 26 public: 27 using CallbackFunc = std::function<void(T&)>; 28 29 AbilityTransactionCallbackInfo() = default; 30 ~AbilityTransactionCallbackInfo() = default; 31 Create()32 static AbilityTransactionCallbackInfo *Create() 33 { 34 return new(std::nothrow) AbilityTransactionCallbackInfo(); 35 } 36 Destroy(AbilityTransactionCallbackInfo * callbackInfo)37 static void Destroy(AbilityTransactionCallbackInfo *callbackInfo) 38 { 39 delete callbackInfo; 40 } 41 Push(const CallbackFunc & callback)42 void Push(const CallbackFunc &callback) 43 { 44 callbackStack_.push(callback); 45 } 46 Call(T & callbackResult)47 void Call(T &callbackResult) 48 { 49 while (!callbackStack_.empty()) { 50 CallbackFunc &callbackFunc = callbackStack_.top(); 51 callbackFunc(callbackResult); 52 callbackStack_.pop(); 53 } 54 } 55 IsEmpty()56 bool IsEmpty() const 57 { 58 return callbackStack_.empty(); 59 } 60 61 private: 62 std::stack<CallbackFunc> callbackStack_ {}; 63 }; 64 65 template<> 66 class AbilityTransactionCallbackInfo<void> { 67 public: 68 using CallbackFunc = std::function<void()>; 69 70 AbilityTransactionCallbackInfo() = default; 71 ~AbilityTransactionCallbackInfo() = default; 72 Create()73 static AbilityTransactionCallbackInfo *Create() 74 { 75 return new(std::nothrow) AbilityTransactionCallbackInfo(); 76 } 77 Destroy(AbilityTransactionCallbackInfo * callbackInfo)78 static void Destroy(AbilityTransactionCallbackInfo *callbackInfo) 79 { 80 delete callbackInfo; 81 } 82 Push(const CallbackFunc & callback)83 void Push(const CallbackFunc &callback) 84 { 85 callbackStack_.push(callback); 86 } 87 Call()88 void Call() 89 { 90 while (!callbackStack_.empty()) { 91 CallbackFunc &callbackFunc = callbackStack_.top(); 92 callbackFunc(); 93 callbackStack_.pop(); 94 } 95 } 96 IsEmpty()97 bool IsEmpty() const 98 { 99 return callbackStack_.empty(); 100 } 101 102 private: 103 std::stack<CallbackFunc> callbackStack_ {}; 104 }; 105 } // namespace AppExecFwk 106 } // namespace OHOS 107 #endif // OHOS_ABILITY_ABILITY_TRANSACTION_CALLBACK_INFO_H 108