1c5c75c08ec319781e9966ccd5a31dec307b4103
[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 {
33 namespace Event
34 {
35
36 template<typename EventType>
37 class ControllerEventHandler
38     : public EventListener<EventType>,
39       private EventSupport<EventType>
40 {
41 private:
42     bool m_touched;
43
44 public:
45     ControllerEventHandler()
46         : m_touched(false)
47     {
48         EventSupport<EventType>::AddListener(this);
49     }
50
51     virtual ~ControllerEventHandler()
52     {
53         EventSupport<EventType>::RemoveListener(this);
54     }
55
56     void PostEvent(const EventType &event)
57     {
58         Assert(m_touched && "Default context not inherited. Call Touch() to inherit one.");
59         EventSupport<EventType>::EmitEvent(event, EmitMode::Queued);
60     }
61
62     void PostTimedEvent(const EventType &event, double dueTime)
63     {
64         Assert(m_touched && "Default context not inherited. Call Touch() to inherit one.");
65         EventSupport<EventType>::EmitEvent(event, EmitMode::Deffered, dueTime);
66     }
67
68     void PostSyncEvent(const EventType &event)
69     {
70         Assert(m_touched && "Default context not inherited. Call Touch() to inherit one.");
71
72         // Check calling context
73         EventSupport<EventType>::EmitEvent(event, EmitMode::Blocking);
74     }
75
76     void SwitchToThread(Thread *thread)
77     {
78         Assert(m_touched && "Default context not inherited. Call Touch() to inherit one.");
79         EventSupport<EventType>::SwitchListenerToThread(this, thread);
80     }
81
82     void Touch()
83     {
84         m_touched = true;
85         EventSupport<EventType>::SwitchListenerToThread(this, Thread::GetCurrentThread());
86     }
87 };
88
89 template<typename EventTypeList>
90 class Controller
91     : public Controller<typename EventTypeList::Tail>,
92       public ControllerEventHandler<typename EventTypeList::Head>
93 {
94 public:
95     typedef typename EventTypeList::Head EventType;
96
97 public:
98     Controller()
99     {
100     }
101
102     virtual ~Controller()
103     {
104     }
105
106     virtual void SwitchToThread(Thread *thread)
107     {
108         ControllerEventHandler<EventType>::SwitchToThread(thread);
109         Controller<typename EventTypeList::Tail>::SwitchToThread(thread);
110     }
111
112     virtual void Touch()
113     {
114         ControllerEventHandler<EventType>::Touch();
115         Controller<typename EventTypeList::Tail>::Touch();
116     }
117 };
118
119 template<>
120 class Controller<TypeListDecl<>::Type>
121 {
122 public:
123     Controller()
124     {
125     }
126
127     virtual ~Controller()
128     {
129     }
130
131     virtual void SwitchToThread(Thread *thread)
132     {
133         (void)thread;
134     }
135
136     virtual void Touch()
137     {
138     }
139 };
140
141 }
142 } // namespace DPL
143
144 // Utilities
145 #define CONTROLLER_POST_EVENT(Name, EventArg) Name##Singleton::Instance().DPL::Event::ControllerEventHandler<__typeof__ EventArg>::PostEvent(EventArg)
146 #define CONTROLLER_POST_TIMED_EVENT(Name, EventArg, DueTime) Name##Singleton::Instance().DPL::Event::ControllerEventHandler<__typeof__ EventArg>::PostTimedEvent(EventArg, DueTime)
147 #define CONTROLLER_POST_SYNC_EVENT(Name, EventArg) Name##Singleton::Instance().DPL::Event::ControllerEventHandler<__typeof__ EventArg>::PostSyncEvent(EventArg)
148
149 #endif // DPL_CONTROLLER_H