• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 OBSERVER_HANDLER_H
17 #define OBSERVER_HANDLER_H
18 
19 #include <unordered_map>
20 #include "event_handler.h"
21 #include "telephony_log_wrapper.h"
22 
23 namespace OHOS {
24 namespace Telephony {
25 class ObserverHandler {
26 public:
27     ObserverHandler();
28 
29     virtual ~ObserverHandler();
30 
31     void RegObserver(int32_t what, const std::shared_ptr<AppExecFwk::EventHandler> handler);
32 
33     void RegUniqueObserver(int32_t what, const std::shared_ptr<AppExecFwk::EventHandler> handler);
34 
35     void Remove(int32_t what, const std::shared_ptr<AppExecFwk::EventHandler> handler);
36 
37     void RemoveAll();
38 
39     void NotifyObserver(int32_t what);
40 
NotifyObserver(int32_t what,int64_t param)41     void NotifyObserver(int32_t what, int64_t param)
42     {
43         auto iter = observerHandlerMap_.find(what);
44         if (iter == observerHandlerMap_.end()) {
45             TELEPHONY_LOGE("ObserverHandler NotifyObserver %{public}d not register", what);
46             return;
47         }
48 
49         for (auto handlers : iter->second) {
50             handlers->SendEvent(what, param, 0);
51         }
52     }
53 
54     template<typename T>
NotifyObserver(int32_t what,std::shared_ptr<T> object)55     void NotifyObserver(int32_t what, std::shared_ptr<T> object)
56     {
57         auto iter = observerHandlerMap_.find(what);
58         if (iter == observerHandlerMap_.end()) {
59             TELEPHONY_LOGE("ObserverHandler NotifyObserver %{public}d not register", what);
60             return;
61         }
62         for (auto handlers : iter->second) {
63             handlers->SendEvent(what, object);
64         }
65     }
66 
67 private:
68     std::unordered_map<int32_t, std::list<std::shared_ptr<AppExecFwk::EventHandler>>> observerHandlerMap_;
69 };
70 } // namespace Telephony
71 } // namespace OHOS
72 #endif // OBSERVER_HANDLER_H
73