Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / plugins-api-support / CallbackSupport.h
1 /*
2  * Copyright (c) 2012 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   CallbackSupport.h
18  * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  */
20
21 #ifndef _WRT_PLUGINS_COMMON_PLUGIN_API_SUPPORT_CALLBACK_SUPPORT_H_
22 #define _WRT_PLUGINS_COMMON_PLUGIN_API_SUPPORT_CALLBACK_SUPPORT_H_
23
24 #include <map>
25 #include <vector>
26 #include <string>
27 #include <dpl/foreach.h>
28
29 namespace WrtPluginsApi {
30 template<typename Sig>
31 class CallbackSupport
32 {
33   public:
34     typedef typename Sig::Signature SlotSignature;
35     typedef typename Sig::Type SlotType;
36     typedef std::string GroupType;
37     typedef std::vector<SlotType> SlotList;
38
39     void Connect(const GroupType& group, const SlotType& slot)
40     {
41         auto groupIt = m_slots.find(group);
42         if (m_slots.end() == groupIt) {
43             groupIt = m_slots.insert(std::make_pair(group, SlotList())).first;
44         }
45         groupIt->second.push_back(slot);
46     }
47
48     void Disconnect(const GroupType& group)
49     {
50         m_slots.erase(group);
51     }
52
53     template<typename ... Args>
54     void Invoke(const Args& ... args)
55     {
56         FOREACH(groupIt, m_slots)
57         {
58             FOREACH(slotIt, groupIt->second)
59             {
60                 (*slotIt)(args ...);
61             }
62         }
63     }
64
65     template<typename ... Args>
66     void InvokeGroup(const GroupType& group, const Args& ... args)
67     {
68         auto groupIt = m_slots.find(group);
69
70         if (m_slots.end() != groupIt) {
71             FOREACH(slotIt, groupIt->second)
72             {
73                 (*slotIt)(args ...);
74             }
75         }
76     }
77
78   private:
79     std::map<GroupType, SlotList> m_slots;
80 };
81 }
82 #endif