2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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 * @file FBaseRt_Event.h
19 * @brief This is the header file for the %_Event class.
21 * This file contains the declarations of the %_Event class.
23 #ifndef _FBASE_RT_INTERNAL_EVENT_H_
24 #define _FBASE_RT_INTERNAL_EVENT_H_
27 #include <FOspConfig.h>
28 #include <FBaseResult.h>
29 #include <FBaseObject.h>
30 #include <FBaseColLinkedListT.h>
31 #include <FBaseRtIEventListener.h>
32 #include "FBaseRtIEventArg.h"
33 #include "FBase_HandleT.h"
34 #include "FBase_ObjectManagerT.h"
36 namespace Tizen { namespace Base { namespace Runtime
43 * @brief This class provides methods for delivering an event with an argument synchronously and asynchronously.
45 * This class provides methods for asynchronous operation.
46 * @see IEventListener, IEventArg
48 class _OSP_EXPORT_ _Event
49 : public Tizen::Base::Object
53 * This is the default constructor for this class.
60 * This is the destructor for this class.
64 virtual ~_Event(void);
67 * Adds the listener object.
68 * The added listener can listen to events when they are fired.
72 * @return An error code
73 * @param[in] listener Listener to add
74 * @param[in] calledByCallerThread true, to call the listener on the caller thread of this method
75 * false, to call the listener on the thread in which the event is created.
76 * @exception E_SUCCESS This method is successful.
77 * @exception E_OBJ_ALREADY_EXIST The listener already exists.
78 * @exception E_INVALID_OPERATION calledByCallerThread is set to true but the caller thread is a worker thread.
80 result AddListener(const IEventListener& listener, bool calledByCallerThread = false);
83 * Removes a listener object.
84 * Removed listener cannot listen to events when they are fired.
87 * @return An error code
88 * @param[in] listener Listener to remove
89 * @exception E_SUCCESS This method was successful.
90 * @exception E_OBJ_NOT_FOUND The listener was not found.
93 result RemoveListener(const IEventListener& listener);
96 * Fires the event with an argument synchronously.
102 * MyClass::SendModeChangeEvent(int mode)
104 * // An event argument should be created on a heap.
105 * MyEventArg* pArg = new MyEventArg(MY_EVENT_TYPE_MODE_CHANGED, mode);
107 * // Fires a MyEvent with asynchronous mode.
108 * // _Event takes the ownership of the pArg and will delete it after it is delivered to listeners.
109 * __pMyEvent->Fire(*pArg);
113 * @return An error code
114 * @param[in] arg The event argument.
115 * @exception E_SUCCESS This method was successful.
116 * @exception E_INVALID_STATE This event has not been initialized.
118 * @remark This takes the ownership of @c arg. So arg should be created on a heap and should not be deleted.
120 result Fire(IEventArg& arg);
123 * Fires the event with an argument asynchronously.
124 * This post the event in the event queue of the thread that the event belongs to. And the listener will
125 * be called on that thread context.
131 * MyClass::SendModeChangeEvent(int mode)
133 * // An event argument should be created on a heap.
134 * MyEventArg* pArg = new MyEventArg(MY_EVENT_TYPE_MODE_CHANGED, mode);
136 * // Fires a MyEvent with asynchronous mode.
137 * // _Event takes the ownership of the pArg and will delete it after it is delivered to listeners.
138 * __pMyEvent->Fire(*pArg);
142 * @return An error code
143 * @param[in] arg The event argument.
144 * @exception E_SUCCESS This method was successful.
145 * @exception E_INVALID_STATE This event has not been initialized.
146 * @exception E_INVALID_OPERATION This event does not support asynchronous mode.
148 * @remark This takes the ownership of @c arg. So arg should be created on a heap and should not be deleted.
150 result FireAsync(IEventArg& arg);
153 * Fires the event with an argument asynchronously.
157 * @return The number of listeners in the current event instance.
159 int GetListenerCount(void);
162 * Initializes the event
168 * MyEvent::Construct(void)
170 * _Event::Initialize(); // You should always invoke this method at your event construction phase
174 * @return An error code
175 * @exception E_SUCCESS This method was successful.
176 * @exception E_INVALID_STATE The event was already initialized.
178 * @remark A derived class from _Event should call this method
180 result Initialize(void);
183 * Implement to call the corresponding event listener's method.
189 * MyEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
191 * const MyEventArg* pMyEventArg = dynamic_cast<const MyEventArg*>(&arg);
192 * IMyEventListener* pMyEventListener = dynamic_cast<IMyEventListener*>(&listener);
194 * if (pMyEventArg->GetType() == MY_EVENT_TYPE_LEVEL_CHANGED)
196 * pMyEventListener->OnLevelChanged(pMyEventArg->GetValue());
198 * else (pMyEventArg->GetType() == MY_EVENT_TYPE_MODE_CHANGED)
200 * pMyEventListener->OnModeChanged(pMyEventArg->GetValue());
209 * @param[in] listener The listener instance which is currently processing
210 * @param[in] arg The event argument that is fired
212 * @remark A derived class must override this method.
214 virtual void FireImpl(IEventListener& listener, const IEventArg& arg);
217 _Event(const _Event& rhs);
219 _Event& operator =(const _Event& rhs);
221 result Fire(std::tr1::shared_ptr< IEventArg > arg);
223 result ProcessListeners(std::tr1::shared_ptr< IEventArg > arg);
225 const _HandleT< _Event > GetHandle(void) const;
227 _HandleT< _Event > GetHandle(void);
237 const IEventListener* pListener;
238 _HandleT< IEventListener > listener;
239 _HandleT< _EventManager > eventManager;
241 bool operator ==(const _ListenerInfo& rhs) const;
242 bool operator !=(const _ListenerInfo& rhs) const;
244 _ListenerInfo& operator =(const _ListenerInfo& rhs);
263 Tizen::Base::Collection::LinkedListT< _ListenerInfo > __listeners;
265 _RefCount* __pRefCount;
267 _HandleT< _EventManager > __eventManager;
268 _HandleT< _Event > __handle;
270 friend class _EventManager;
272 }}} // Tizen::Base::Runtime
273 #endif // _FBASE_RT_INTERNAL_EVENT_H_