tizen 2.4 release
[framework/web/wrt-commons.git] / modules / event / include / dpl / event / controller.h
1 /*
2  * Copyright (c) 2011 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        controller.h
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of MVC controller
21  */
22 #ifndef DPL_CONTROLLER_H
23 #define DPL_CONTROLLER_H
24
25 #include <dpl/event/event_support.h>
26 #include <dpl/event/event_listener.h>
27 #include <dpl/type_list.h>
28 #include <dpl/thread.h>
29 #include <dpl/assert.h>
30
31 namespace DPL {
32 namespace Event {
33 template<typename EventType>
34 class ControllerEventHandler :
35     public EventListener<EventType>,
36     private EventSupport<EventType>
37 {
38   private:
39     bool m_touched;
40
41   public:
42     ControllerEventHandler() :
43         m_touched(false)
44     {
45         EventSupport<EventType>::AddListener(this);
46     }
47
48     virtual ~ControllerEventHandler()
49     {
50         EventSupport<EventType>::RemoveListener(this);
51     }
52
53     void PostEvent(const EventType &event)
54     {
55         Assert(
56             m_touched &&
57             "Default context not inherited. Call Touch() to inherit one.");
58         EventSupport<EventType>::EmitEvent(event, EmitMode::Queued);
59     }
60
61     void PostTimedEvent(const EventType &event, double dueTime)
62     {
63         Assert(
64             m_touched &&
65             "Default context not inherited. Call Touch() to inherit one.");
66         EventSupport<EventType>::EmitEvent(event, EmitMode::Deffered, dueTime);
67     }
68
69     void PostSyncEvent(const EventType &event)
70     {
71         Assert(
72             m_touched &&
73             "Default context not inherited. Call Touch() to inherit one.");
74
75         // Check calling context
76         EventSupport<EventType>::EmitEvent(event, EmitMode::Blocking);
77     }
78
79     void SwitchToThread(Thread *thread)
80     {
81         Assert(
82             m_touched &&
83             "Default context not inherited. Call Touch() to inherit one.");
84         EventSupport<EventType>::SwitchListenerToThread(this, thread);
85     }
86
87     void Touch()
88     {
89         m_touched = true;
90         EventSupport<EventType>::SwitchListenerToThread(
91             this,
92             Thread::
93                 GetCurrentThread());
94     }
95 };
96
97 template<typename EventTypeList>
98 class Controller :
99     public Controller<typename EventTypeList::Tail>,
100     public ControllerEventHandler<typename EventTypeList::Head>
101 {
102   public:
103     typedef typename EventTypeList::Head EventType;
104
105   public:
106     Controller()
107     {}
108
109     virtual ~Controller()
110     {}
111
112     virtual void SwitchToThread(Thread *thread)
113     {
114         ControllerEventHandler<EventType>::SwitchToThread(thread);
115         Controller<typename EventTypeList::Tail>::SwitchToThread(thread);
116     }
117
118     virtual void Touch()
119     {
120         ControllerEventHandler<EventType>::Touch();
121         Controller<typename EventTypeList::Tail>::Touch();
122     }
123 };
124
125 template<>
126 class Controller<TypeListDecl<>::Type>
127 {
128   public:
129     Controller()
130     {}
131
132     virtual ~Controller()
133     {}
134
135     virtual void SwitchToThread(Thread *thread)
136     {
137         (void)thread;
138     }
139
140     virtual void Touch()
141     {}
142 };
143 }
144 } // namespace DPL
145
146 // Utilities
147 #define CONTROLLER_POST_EVENT(Name, \
148                               EventArg) Name##Singleton::Instance().DPL::Event \
149         ::ControllerEventHandler< \
150         __typeof__ EventArg>::PostEvent(EventArg)
151 #define CONTROLLER_POST_TIMED_EVENT(Name, EventArg, \
152                                     DueTime) Name##Singleton::Instance().DPL:: \
153         Event::ControllerEventHandler< \
154         __typeof__ EventArg>::PostTimedEvent(EventArg, DueTime)
155 #define CONTROLLER_POST_SYNC_EVENT(Name, \
156                                    EventArg) Name##Singleton::Instance().DPL:: \
157         Event::ControllerEventHandler< \
158         __typeof__ EventArg>::PostSyncEvent(EventArg)
159
160 #endif // DPL_CONTROLLER_H