Tizen 2.0 Release
[framework/web/wrt-plugins-common.git] / src / 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 {
31
32 template<typename Sig>
33 class CallbackSupport
34 {
35 public:
36     typedef typename Sig::Signature SlotSignature;
37     typedef typename Sig::Type SlotType;
38     typedef std::string GroupType;
39     typedef std::vector<SlotType> SlotList;
40
41     void Connect(const GroupType& group, const SlotType& slot)
42     {
43         auto groupIt = m_slots.find(group);
44         if (m_slots.end() == groupIt)
45         {
46             groupIt = m_slots.insert(std::make_pair(group, SlotList())).first;
47         }
48         groupIt->second.push_back(slot);
49     }
50
51     void Disconnect(const GroupType& group)
52     {
53         m_slots.erase(group);
54     }
55
56     template<typename...Args>
57     void Invoke(const Args&... args)
58     {
59
60         FOREACH(groupIt, m_slots)
61         {
62             FOREACH(slotIt, groupIt->second)
63             {
64                 (*slotIt)(args...);
65             }
66         }
67     }
68
69     template<typename...Args>
70     void InvokeGroup(const GroupType& group, const Args&... args)
71     {
72         auto groupIt = m_slots.find(group);
73
74         if (m_slots.end() != groupIt)
75         {
76             FOREACH (slotIt, groupIt->second)
77             {
78                 (*slotIt)(args...);
79             }
80         }
81     }
82
83 private:
84     std::map<GroupType, SlotList> m_slots;
85 };
86
87 }
88 #endif