Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules_mobile / 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 namespace Event {
29 ThreadEventDispatcher::ThreadEventDispatcher() :
30     m_thread(NULL)
31 {}
32
33 ThreadEventDispatcher::~ThreadEventDispatcher()
34 {}
35
36 void ThreadEventDispatcher::SetThread(Thread *thread)
37 {
38     m_thread = thread;
39 }
40
41 void ThreadEventDispatcher::StaticEventDelete(void *event, void *userParam)
42 {
43     AbstractEventCall *abstractEventCall =
44         static_cast<AbstractEventCall *>(event);
45     ThreadEventDispatcher *This =
46         static_cast<ThreadEventDispatcher *>(userParam);
47
48     LogPedantic("Received static event delete from thread");
49
50     Assert(abstractEventCall != NULL);
51     Assert(This != NULL);
52
53     This->EventDelete(abstractEventCall);
54 }
55
56 void ThreadEventDispatcher::StaticEventDispatch(void *event, void *userParam)
57 {
58     AbstractEventCall *abstractEventCall =
59         static_cast<AbstractEventCall *>(event);
60     ThreadEventDispatcher *This =
61         static_cast<ThreadEventDispatcher *>(userParam);
62
63     LogPedantic("Received static event dispatch from thread");
64
65     Assert(abstractEventCall != NULL);
66     Assert(This != NULL);
67
68     This->EventDispatch(abstractEventCall);
69 }
70
71 void ThreadEventDispatcher::EventDelete(AbstractEventCall *abstractEventCall)
72 {
73     LogPedantic("Deleting event");
74     delete abstractEventCall;
75 }
76
77 void ThreadEventDispatcher::EventDispatch(AbstractEventCall *abstractEventCall)
78 {
79     LogPedantic("Dispatching event to event support");
80     abstractEventCall->Call();
81 }
82
83 void ThreadEventDispatcher::AddEventCall(AbstractEventCall *abstractEventCall)
84 {
85     // Thread must be set prior to call
86     Assert(m_thread != NULL);
87
88     LogPedantic("Adding event to thread event loop");
89
90     // Call abstract event call in dedicated thread
91     m_thread->PushEvent(abstractEventCall,
92                         &StaticEventDispatch,
93                         &StaticEventDelete,
94                         this);
95 }
96
97 void ThreadEventDispatcher::AddTimedEventCall(
98     AbstractEventCall *abstractEventCall,
99     double dueTime)
100 {
101     // Thread must be set prior to call
102     Assert(m_thread != NULL);
103
104     LogPedantic("Adding timed event to thread event loop");
105
106     // Call abstract event call in dedicated thread
107     m_thread->PushTimedEvent(abstractEventCall,
108                              dueTime,
109                              &StaticEventDispatch,
110                              &StaticEventDelete,
111                              this);
112 }
113 }
114 } // namespace DPL