Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_mobile / 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 class SignalsSupport
35 {
36   public:
37     virtual ~SignalsSupport() {}
38
39     template<typename T>
40     void Connect(const std::string& libraryName, const typename T::Type& slot)
41     {
42         Tuple::get_by_type<CallbackSupport<T> >(m_slots).Connect(libraryName,
43                                                                  slot);
44     }
45
46     void Disconnect(const std::string& libraryName)
47     {
48         DisconnectGroup(m_slots, libraryName);
49     }
50
51     virtual void AddPlugin(const std::string& libraryName, Plugin& plugin) = 0;
52
53   protected:
54     template<typename T, typename ... Args>
55     void Invoke(const Args& ... args)
56     {
57         Tuple::get_by_type<CallbackSupport<T> >(m_slots).Invoke(args ...);
58     }
59
60     template<typename T, typename ... Args>
61     void InvokeGroup(const std::string& libraryName, const Args& ... args)
62     {
63         Tuple::get_by_type<CallbackSupport<T> >(m_slots).InvokeGroup(
64             libraryName,
65             args ...);
66     }
67
68   private:
69     template<int N, typename ... Args>
70     void DisconnectSlot(std::tuple<Args ...>& slots,
71                         const std::string& libraryName,
72                         typename std::enable_if<(N >= 0)>::type* = NULL)
73     {
74         std::get<N>(slots).Disconnect(libraryName);
75         DisconnectSlot<N - 1>(slots, libraryName);
76     }
77
78     template<int N, typename ... Args>
79     void DisconnectSlot(std::tuple<Args ...>& /*slots*/,
80                         const std::string& /*libraryName*/,
81                         typename std::enable_if<(N == -1)>::type* = NULL)
82     {}
83
84     template<typename ... Args>
85     void DisconnectGroup(std::tuple<Args ...>& slots,
86                          const std::string& libraryName)
87     {
88         DisconnectSlot<sizeof ... (Args)-1>(slots, libraryName);
89     }
90
91     std::tuple<CallbackSupport<OnWidgetStart>,
92                CallbackSupport<OnWidgetStop>,
93                CallbackSupport<OnFrameLoad>,
94                CallbackSupport<OnFrameUnload> > m_slots;
95 };
96 }
97
98 #endif