Tizen 2.0 Release
[framework/web/wrt-commons.git] / modules / event / src / thread_event_dispatcher.cpp
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        thread_event_dispatcher.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of thread event dispatcher
21  */
22 #include <stddef.h>
23 #include <dpl/event/thread_event_dispatcher.h>
24 #include <dpl/log/log.h>
25 #include <dpl/assert.h>
26
27 namespace DPL
28 {
29 namespace Event
30 {
31
32 ThreadEventDispatcher::ThreadEventDispatcher()
33     : m_thread(NULL)
34 {
35 }
36
37 ThreadEventDispatcher::~ThreadEventDispatcher()
38 {
39 }
40
41 void ThreadEventDispatcher::SetThread(Thread *thread)
42 {
43     m_thread = thread;
44 }
45
46 void ThreadEventDispatcher::StaticEventDelete(void *event, void *userParam)
47 {
48     AbstractEventCall *abstractEventCall = static_cast<AbstractEventCall *>(event);
49     ThreadEventDispatcher *This = static_cast<ThreadEventDispatcher *>(userParam);
50
51     LogPedantic("Received static event delete from thread");
52
53     Assert(abstractEventCall != NULL);
54     Assert(This != NULL);
55
56     This->EventDelete(abstractEventCall);
57 }
58
59 void ThreadEventDispatcher::StaticEventDispatch(void *event, void *userParam)
60 {
61     AbstractEventCall *abstractEventCall = static_cast<AbstractEventCall *>(event);
62     ThreadEventDispatcher *This = static_cast<ThreadEventDispatcher *>(userParam);
63
64     LogPedantic("Received static event dispatch from thread");
65
66     Assert(abstractEventCall != NULL);
67     Assert(This != NULL);
68
69     This->EventDispatch(abstractEventCall);
70 }
71
72 void ThreadEventDispatcher::EventDelete(AbstractEventCall *abstractEventCall)
73 {
74     LogPedantic("Deleting event");
75     delete abstractEventCall;
76 }
77
78 void ThreadEventDispatcher::EventDispatch(AbstractEventCall *abstractEventCall)
79 {
80     LogPedantic("Dispatching event to event support");
81     abstractEventCall->Call();
82 }
83
84 void ThreadEventDispatcher::AddEventCall(AbstractEventCall *abstractEventCall)
85 {
86     // Thread must be set prior to call
87     Assert(m_thread != NULL);
88
89     LogPedantic("Adding event to thread event loop");
90
91     // Call abstract event call in dedicated thread
92     m_thread->PushEvent(abstractEventCall, &StaticEventDispatch, &StaticEventDelete, this);
93 }
94
95 void ThreadEventDispatcher::AddTimedEventCall(AbstractEventCall *abstractEventCall, double dueTime)
96 {
97     // Thread must be set prior to call
98     Assert(m_thread != NULL);
99
100     LogPedantic("Adding timed event to thread event loop");
101
102     // Call abstract event call in dedicated thread
103     m_thread->PushTimedEvent(abstractEventCall, dueTime, &StaticEventDispatch, &StaticEventDelete, this);
104 }
105
106 }
107 } // namespace DPL