• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef MSG_HANDLER_H
16 #define MSG_HANDLER_H
17 
18 #include <map>
19 #include <functional>
20 #include "proto.h"
21 
22 namespace OHOS {
23 namespace MMI {
24 template<typename T>
25 class MsgHandler {
26 public:
Clear()27     void Clear()
28     {
29         callbacks_.clear();
30     }
31 
32 protected:
33     struct MsgCallback {
34         MmiMessageId id;
35         T fun;
36     };
37 
38 protected:
~MsgHandler()39     virtual ~MsgHandler() {};
GetMsgCallback(MmiMessageId id)40     T *GetMsgCallback(MmiMessageId id)
41     {
42         auto it = callbacks_.find(id);
43         if (it == callbacks_.end()) {
44             return nullptr;
45         }
46         return &it->second;
47     }
RegistrationEvent(MsgCallback & msg)48     bool RegistrationEvent(MsgCallback& msg)
49     {
50         auto it = callbacks_.find(msg.id);
51         if (it != callbacks_.end()) {
52             return false;
53         }
54         callbacks_[msg.id] = msg.fun;
55         return true;
56     }
57 
58 protected:
59     std::map<MmiMessageId, T> callbacks_;
60 };
61 } // namespace MMI
62 } // namespace OHOS
63 #endif // MSG_HANDLER_H