Tizen 2.1 base
[platform/framework/web/wrt-plugins-common.git] / src / plugins-api-support / SignalsSupport.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    IPluginRegistry.h
18  * @author  Grzegorz Krawczyk (g.krawczyk@samgsung.com)
19  * @version
20  * @brief
21  */
22
23 #ifndef _WRT_PLUGINS_COMMON_PLUGIN_API_SUPPORT_SIGNALS_SUPPORT_H_
24 #define _WRT_PLUGINS_COMMON_PLUGIN_API_SUPPORT_SIGNALS_SUPPORT_H_
25
26 #include <tuple>
27 #include <string>
28 #include "CallbackSupport.h"
29 #include "tuple.h"
30 #include "PluginSignals.h"
31 #include "Plugin.h"
32
33 namespace WrtPluginsApi
34 {
35
36 class SignalsSupport
37 {
38 public:
39     virtual ~SignalsSupport() {}
40
41     template<typename T>
42     void Connect(const std::string& libraryName, const typename T::Type& slot)
43     {
44         Tuple::get_by_type<CallbackSupport<T>>(m_slots).Connect(libraryName,
45                                                                 slot);
46     }
47
48     void Disconnect(const std::string& libraryName)
49     {
50         DisconnectGroup(m_slots, libraryName);
51     }
52
53     virtual void AddPlugin(const std::string& libraryName, Plugin& plugin) = 0;
54
55 protected:
56     template<typename T, typename ...Args>
57     void Invoke(const Args&... args)
58     {
59         Tuple::get_by_type<CallbackSupport<T>>(m_slots).Invoke(args...);
60     }
61
62     template<typename T, typename ...Args>
63     void InvokeGroup(const std::string& libraryName, const Args&... args)
64     {
65         Tuple::get_by_type<CallbackSupport<T>>(m_slots).InvokeGroup(libraryName,
66                                                                     args...);
67     }
68
69 private:
70     template<int N, typename ...Args>
71     void DisconnectSlot(std::tuple<Args...>& slots,
72                         const std::string& libraryName,
73                         typename std::enable_if<(N >= 0)>::type* = NULL)
74     {
75         std::get<N>(slots).Disconnect(libraryName);
76         DisconnectSlot<N-1>(slots, libraryName);
77     }
78
79     template<int N, typename ...Args>
80     void DisconnectSlot(std::tuple<Args...>& slots,
81                         const std::string& libraryName,
82                         typename std::enable_if<(N == -1)>::type* = NULL)
83     {
84     }
85
86     template<typename ...Args>
87     void DisconnectGroup(std::tuple<Args...>& slots,
88                          const std::string& libraryName)
89     {
90         DisconnectSlot<sizeof...(Args)-1>(slots, libraryName);
91     }
92
93     std::tuple<CallbackSupport<OnWidgetStart>,
94                CallbackSupport<OnWidgetStop>,
95                CallbackSupport<OnFrameLoad>,
96                CallbackSupport<OnFrameUnload>> m_slots;
97 };
98
99 }
100
101 #endif