Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / base / runtime / FBaseRt_EventManager.h
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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 /**
18  * @file        FBaseRt_EventManager.h
19  * @brief       This is the header file for the _EventManager class.
20  *
21  * This file contains the declarations of _EventManager.
22  */
23
24 #ifndef _FBASE_RT_INTERNAL_EVENT_MANAGER_H_
25 #define _FBASE_RT_INTERNAL_EVENT_MANAGER_H_
26
27 #include <tr1/memory>
28 #include <glib.h>
29
30 #include <FOspConfig.h>
31 #include <FBaseResult.h>
32 #include <FBaseObject.h>
33 #include <FBaseColLinkedListT.h>
34 #include <FBaseColArrayListT.h>
35
36 #include "FBase_HandleT.h"
37
38 namespace Tizen { namespace Base { namespace Runtime
39 {
40
41 class Mutex;
42 class _Event;
43 class IEventArg;
44 class IEventListener;
45
46 /**
47 * @class   _EventManager
48 * @brief   This class provides methods for managingasynchronous event.
49 * @since 2.0
50 *
51 * This class provides methods for managing asynchronous event.
52 */
53 class _EventManager
54         : public Tizen::Base::Object
55 {
56 public:
57         /**
58          *      This is the default constructor for this class.
59          *      value.
60          *
61          *      @since 2.0
62          */
63         _EventManager(void);
64
65         /**
66          * This is the destructor for this class
67          * @since 2.0
68          */
69         virtual ~_EventManager(void);
70
71         /**
72          * Initializes this instanc of %_EventManager.
73          *
74          * @since 2.0
75          *
76          * @return An error code
77          * @param[in] pGmainContext     A GMainLoopContext
78          * @exception E_SUCCESS         The method is successful.
79          * @exception E_SYSTEM          A system error has occurred.
80          */
81         result Construct(GMainContext* pGmainContext);
82
83         /**
84          * Registers an event.
85          *
86          * @since 2.0
87          * @return An error code
88          * @param[in] event                                     The handle of an event to register
89          * @exception E_SUCCESS                         The method is successful.
90          * @exception E_OBJ_ALREADY_EXIST       The event has already been registered.
91          * @exception E_SYSTEM                          A system error has occurred.
92          */
93         result RegisterEvent(const _HandleT< _Event >& event);
94
95         /**
96          * Unregisters an event.
97          *
98          * @since 2.0
99          *
100          * @return An error code
101          * @param[in] event                     The handle of an event to unregister
102          * @exception E_SUCCESS                         The method is successful.
103          * @exception E_OBJ_NOT_FOUND           The event has not been registered.
104          */
105         result UnregisterEvent(const _HandleT< _Event >& event);
106
107         /**
108          * Returns the %_EventManager of current thread.
109          *
110          * @since 2.0
111          *
112          * @return The pointer to _EventManager
113          *
114          * @remark null will be returned if this is called by a worker thread.
115          */
116         static _EventManager* GetCurrentEventManager(void);
117
118         /**
119          * Sends an event asynchronously.
120          *
121          * @since 2.0
122          *
123          * @return An error code
124          * @param[in] event The event to send
125          * @param[in] arg       The event arg
126          * @exception E_SUCCESS The method is successful.
127          * @remark This takes ownership of the arg. Therefore the caller must not delete the arg instance.
128          */
129         result FireEventAsync(const _HandleT< _Event >& event, std::tr1::shared_ptr< IEventArg > arg);
130
131         /**
132          * Calls a listener added to an event asynchronously.
133          *
134          * @since 2.0
135          *
136          * @return An error code
137          * @param[in] event                     The event
138          * @param[in] arg                       The event arg
139          * @param[in] listener          The listener to call
140          * @exception E_SUCCESS         The method is successful.
141          * @remark This takes ownership of the arg. Therefore the caller must not delete the arg instance.
142          */
143         result CallListenerAsync(const _HandleT< _Event >& event, std::tr1::shared_ptr< IEventArg > arg, const _HandleT< IEventListener >& listener);
144
145         /**
146          * Clears all pending event messages of an event.
147          *
148          * @since 2.0
149          * @param[in] event The event to clear
150          */
151         void ClearAllPendingEventMessages(const _HandleT< _Event >& event);
152
153         /**
154          * Returns the handle
155          *
156          * @since 2.0
157          * @param[in] event The event to clear
158          */
159         _HandleT< _EventManager > GetHandle(void) const;
160
161         /**
162          * Gets _EventManager using given handle
163          *
164          * @since 2.0
165          *
166          * @param[in] handle The handle of _EventManager
167          */
168         static _EventManager* GetEventManagerByHandle(const _HandleT< _EventManager >& handle);
169
170         /**
171          * Gets object manager for _EventManager
172          *
173          * @since 2.0
174          */
175         static _ObjectManagerT< _EventManager >& GetEventManagerObjectManager(void);
176
177         /**
178          * Gets object manager for _Event
179          *
180          * @since 2.0
181          */
182         static _ObjectManagerT< _Event >& GetEventObjectManager(void);
183
184         /**
185          * Gets object manager for IEventListener
186          *
187          * @since 2.0
188          */
189         static _ObjectManagerT< IEventListener >& GetEventListenerObjectManager(void);
190
191 private:
192         _EventManager(const _EventManager& rhs);
193         _EventManager& operator =(const _EventManager& rhs);
194
195         static gboolean OnEventReceived(GIOChannel* pChannel, GIOCondition condition, gpointer data);
196
197 private:
198         enum _MessageType
199         {
200                 MESSAGE_TYPE_FIRE_EVENT,
201                 MESSAGE_TYPE_CALL_LISTENER
202         };
203
204         struct _EventMessage
205         {
206                 _EventMessage(void)
207                         : id(0)
208                         , type(MESSAGE_TYPE_FIRE_EVENT)
209                 {
210                 }
211
212                 int id;
213                 _MessageType type;
214                 _HandleT< _Event > event;
215                 std::tr1::shared_ptr< IEventArg > arg;
216                 _HandleT< IEventListener > listener;
217
218                 bool operator ==(const _EventMessage& rhs) const;
219                 bool operator !=(const _EventMessage& rhs) const;
220
221                 _EventMessage& operator =(const _EventMessage& rhs);
222         };
223
224 private:
225         Mutex* __pMutex;
226         GSource* __pSource;
227         GIOChannel* __pChannel;
228         GMainContext* __pGmainContext;
229         _HandleT< _EventManager > __handle;
230         Tizen::Base::Collection::LinkedListT< _HandleT< _Event > > __events;
231         Tizen::Base::Collection::ArrayListT< _EventMessage > __messages1;
232         Tizen::Base::Collection::ArrayListT< _EventMessage > __messages2;
233         Tizen::Base::Collection::ArrayListT< _EventMessage >* __pActive;
234         Tizen::Base::Collection::ArrayListT< _EventMessage >* __pReady;
235 };
236
237 } } } // Tizen::Base::Runtime
238
239 #endif // _FBASE_RT_INTERNAL_EVENT_MANAGER_H_