CommunicationManager returns the number of called listeners
[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     // Returns the number of listeners called
48     size_t SendMessage(const M& msg) const
49     {
50         size_t num = 0;
51         for(auto& it : m_listeners) {
52             it(msg);
53             num++;
54         }
55         return num;
56     }
57 protected:
58     MessageManager() {}
59     // No one is going to destroy this class directly (only via inherited class). Hence no 'virtual'
60     ~MessageManager() {}
61
62 private:
63     std::list<std::function<void(const M&)>> m_listeners;
64 };
65
66 // generic template declaration
67 template <typename... Args>
68 struct CommunicationManager;
69
70 /*
71  * Class that combines MessageManagers of all requested Message types into a single object. Examples
72  * can be found in tests (test_msg-manager.cpp)
73  */
74 template <typename First, typename... Args>
75 struct CommunicationManager<First, Args...> :
76     public MessageManager<First>, public CommunicationManager<Args...>
77 {
78 public:
79     CommunicationManager() {}
80     NONCOPYABLE(CommunicationManager);
81
82     // M - message type, L - listener to register
83     template <typename M, typename L>
84     void Register(L&& listener)
85     {
86         MessageManager<M>::Register(std::move(listener));
87     }
88
89     // M message type
90     // Sending a message calls an unknown listener callback on the receiving side. It may throw.
91     template <typename M>
92     size_t SendMessage(const M& msg) const
93     {
94         return MessageManager<M>::SendMessage(msg);
95     }
96 };
97
98 // stop condition for recursive inheritance
99 template <>
100 struct CommunicationManager<> {
101 };
102
103 } /* namespace CKM */