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>
36 template<typename EventType>
37 class ControllerEventHandler
38 : public EventListener<EventType>,
39 private EventSupport<EventType>
45 ControllerEventHandler()
48 EventSupport<EventType>::AddListener(this);
51 virtual ~ControllerEventHandler()
53 EventSupport<EventType>::RemoveListener(this);
56 void PostEvent(const EventType &event)
58 Assert(m_touched && "Default context not inherited. Call Touch() to inherit one.");
59 EventSupport<EventType>::EmitEvent(event, EmitMode::Queued);
62 void PostTimedEvent(const EventType &event, double dueTime)
64 Assert(m_touched && "Default context not inherited. Call Touch() to inherit one.");
65 EventSupport<EventType>::EmitEvent(event, EmitMode::Deffered, dueTime);
68 void PostSyncEvent(const EventType &event)
70 Assert(m_touched && "Default context not inherited. Call Touch() to inherit one.");
72 // Check calling context
73 EventSupport<EventType>::EmitEvent(event, EmitMode::Blocking);
76 void SwitchToThread(Thread *thread)
78 Assert(m_touched && "Default context not inherited. Call Touch() to inherit one.");
79 EventSupport<EventType>::SwitchListenerToThread(this, thread);
85 EventSupport<EventType>::SwitchListenerToThread(this, Thread::GetCurrentThread());
89 template<typename EventTypeList>
91 : public Controller<typename EventTypeList::Tail>,
92 public ControllerEventHandler<typename EventTypeList::Head>
95 typedef typename EventTypeList::Head EventType;
102 virtual ~Controller()
106 virtual void SwitchToThread(Thread *thread)
108 ControllerEventHandler<EventType>::SwitchToThread(thread);
109 Controller<typename EventTypeList::Tail>::SwitchToThread(thread);
114 ControllerEventHandler<EventType>::Touch();
115 Controller<typename EventTypeList::Tail>::Touch();
120 class Controller<TypeListDecl<>::Type>
127 virtual ~Controller()
131 virtual void SwitchToThread(Thread *thread)
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)
149 #endif // DPL_CONTROLLER_H