898d4837203de45d5067450e253a58d475134a1a
[platform/core/security/key-manager.git] / src / manager / main / communication-manager.h
1 /*
2  *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 /*
17  * @file       communication-manager.h
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #pragma once
23
24 #include <functional>
25 #include <list>
26 #include <noncopyable.h>
27
28 namespace CKM {
29
30 /*
31  * class responsible for keeping a list of listeners for given M type of message and notifying them
32  */
33 template <typename M>
34 class MessageManager
35 {
36 public:
37     NONCOPYABLE(MessageManager);
38
39     // Listener is an object callable with const M& as argument
40     template <typename L>
41     void Register(L&& listener)
42     {
43         m_listeners.push_back(std::move(listener));
44     }
45
46     // Sends message of type M to all registered listeners
47     void SendMessage(const M& msg)
48     {
49         for(auto it : m_listeners)
50             it(msg);
51     }
52 protected:
53     MessageManager() {}
54     // No one is going to destroy this class directly (only via inherited class). Hence no 'virtual'
55     ~MessageManager() {}
56
57 private:
58     std::list<std::function<void(const M&)>> m_listeners;
59 };
60
61 // generic template declaration
62 template <typename... Args>
63 struct CommunicationManager;
64
65 /*
66  * Class that combines MessageManagers of all requested Message types into a single object. Examples
67  * can be found in tests (test_msg-manager.cpp)
68  */
69 template <typename First, typename... Args>
70 struct CommunicationManager<First, Args...> :
71     public MessageManager<First>, public CommunicationManager<Args...>
72 {
73 public:
74     CommunicationManager() {}
75     NONCOPYABLE(CommunicationManager);
76
77     // M - message type, L - listener to register
78     template <typename M, typename L>
79     void Register(L&& listener)
80     {
81         MessageManager<M>::Register(std::move(listener));
82     }
83
84     // M message type
85     template <typename M>
86     void SendMessage(const M& msg)
87     {
88         MessageManager<M>::SendMessage(msg);
89     }
90 };
91
92 // stop condition for recursive inheritance
93 template <>
94 struct CommunicationManager<> {
95 };
96
97 } /* namespace CKM */