2 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 * @file communication-manager.h
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
26 #include <noncopyable.h>
31 * class responsible for keeping a list of listeners for given M type of message and notifying them
37 NONCOPYABLE(MessageManager);
39 // Listener is an object callable with const M& as argument
41 void Register(L&& listener)
43 m_listeners.push_back(std::move(listener));
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
51 for(auto& it : m_listeners) {
59 // No one is going to destroy this class directly (only via inherited class). Hence no 'virtual'
63 std::list<std::function<void(const M&)>> m_listeners;
66 // generic template declaration
67 template <typename... Args>
68 struct CommunicationManager;
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)
74 template <typename First, typename... Args>
75 struct CommunicationManager<First, Args...> :
76 public MessageManager<First>, public CommunicationManager<Args...>
79 CommunicationManager() {}
80 NONCOPYABLE(CommunicationManager);
82 // M - message type, L - listener to register
83 template <typename M, typename L>
84 void Register(L&& listener)
86 MessageManager<M>::Register(std::move(listener));
90 // Sending a message calls an unknown listener callback on the receiving side. It may throw.
92 size_t SendMessage(const M& msg) const
94 return MessageManager<M>::SendMessage(msg);
98 // stop condition for recursive inheritance
100 struct CommunicationManager<> {
103 } /* namespace CKM */