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