1 /* 2 * Copyright (c) 2024 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 HDI_CALLBACK_HANDLER_H 17 #define HDI_CALLBACK_HANDLER_H 18 19 #include <set> 20 21 #undef LOG_DOMAIN 22 #define LOG_DOMAIN 0xD001566 23 24 namespace OHOS { 25 namespace HDI { 26 namespace Wlan { 27 namespace Chip { 28 namespace V1_0 { 29 30 template <typename CallbackType> 31 class CallbackHandler { 32 public: CallbackHandler()33 CallbackHandler() {} 34 ~CallbackHandler() = default; 35 RemoveCallback(const sptr<CallbackType> & cb)36 bool RemoveCallback(const sptr<CallbackType>& cb) 37 { 38 if (cb == nullptr) { 39 return false; 40 } 41 if (cbSet_.find(cb) != cbSet_.end()) { 42 cbSet_.erase(cb); 43 } 44 return true; 45 } 46 AddCallback(const sptr<CallbackType> & cb)47 bool AddCallback(const sptr<CallbackType>& cb) 48 { 49 if (cb == nullptr) { 50 return false; 51 } 52 if (cbSet_.find(cb) != cbSet_.end()) { 53 return true; 54 } 55 cbSet_.insert(cb); 56 return true; 57 } 58 GetCallbacks()59 const std::set<sptr<CallbackType>>& GetCallbacks() 60 { 61 return cbSet_; 62 } 63 Invalidate()64 void Invalidate() 65 { 66 cbSet_.clear(); 67 } 68 69 private: 70 std::set<sptr<CallbackType>> cbSet_; 71 }; 72 73 } 74 } 75 } 76 } 77 } 78 #endif