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