2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of MVC controller
22 #ifndef DPL_CONTROLLER_H
23 #define DPL_CONTROLLER_H
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>
33 template<typename EventType>
34 class ControllerEventHandler :
35 public EventListener<EventType>,
36 private EventSupport<EventType>
42 ControllerEventHandler() :
45 EventSupport<EventType>::AddListener(this);
48 virtual ~ControllerEventHandler()
50 EventSupport<EventType>::RemoveListener(this);
53 void PostEvent(const EventType &event)
57 "Default context not inherited. Call Touch() to inherit one.");
58 EventSupport<EventType>::EmitEvent(event, EmitMode::Queued);
61 void PostTimedEvent(const EventType &event, double dueTime)
65 "Default context not inherited. Call Touch() to inherit one.");
66 EventSupport<EventType>::EmitEvent(event, EmitMode::Deffered, dueTime);
69 void PostSyncEvent(const EventType &event)
73 "Default context not inherited. Call Touch() to inherit one.");
75 // Check calling context
76 EventSupport<EventType>::EmitEvent(event, EmitMode::Blocking);
79 void SwitchToThread(Thread *thread)
83 "Default context not inherited. Call Touch() to inherit one.");
84 EventSupport<EventType>::SwitchListenerToThread(this, thread);
90 EventSupport<EventType>::SwitchListenerToThread(
97 template<typename EventTypeList>
99 public Controller<typename EventTypeList::Tail>,
100 public ControllerEventHandler<typename EventTypeList::Head>
103 typedef typename EventTypeList::Head EventType;
109 virtual ~Controller()
112 virtual void SwitchToThread(Thread *thread)
114 ControllerEventHandler<EventType>::SwitchToThread(thread);
115 Controller<typename EventTypeList::Tail>::SwitchToThread(thread);
120 ControllerEventHandler<EventType>::Touch();
121 Controller<typename EventTypeList::Tail>::Touch();
126 class Controller<TypeListDecl<>::Type>
132 virtual ~Controller()
135 virtual void SwitchToThread(Thread *thread)
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)
160 #endif // DPL_CONTROLLER_H