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 FBaseRtEvent.h
19 * @brief This is the header file for the %Event class.
21 * This file contains the declarations of the %Event class.
24 #ifndef _FBASE_RT_EVENT_H_
25 #define _FBASE_RT_EVENT_H_
27 #include <FBaseObject.h>
29 namespace Tizen { namespace Base { namespace Runtime
36 * @brief This class provides methods for notifying events with argument to its listeners.
39 * The %Event class provides methods for notifying events with argument to its listeners.
40 * Because bounded to either default thread or event-driven thread, it cannot be created on worker thread.
41 * It supports two types of listeners; one is called on the thread where the event is fired, and another is called on the thread where the listener was registered.
47 * using namespace Tizen::Base::Runtime;
49 * class MyEventArg : public IEventArg
56 * MyEventArg::MyEventArg(int t)
61 * class MyEventListener : public IEventListener
64 * void OnEventReceived(const IEventArg& arg);
68 * MyEventListener::OnEventReceived(const IEventArg& arg)
70 * const MyEventArg* myarg = dynamic_cast<const MyEventArg*> (&arg);
73 * class MyEvent : public Event
76 * virtual void FireImpl(IEventListener& listener, const IEventArg& arg);
80 * MyEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
82 * MyEventListener* plistener = dynamic_cast<MyEventListener*> (&listener);
83 * plistener->OnEventReceived(arg);
87 * MyApplication::Test Code(void)
89 * MyEventListener* mylistener = new MyEventListener();
90 * MyEvent* my = new MyEvent();
91 * MyEventArg* myarg = new MyEventArg(3);
92 * my->AddListener(*mylistener);
98 class _OSP_EXPORT_ Event
99 : virtual public Tizen::Base::Object
103 * This is the default constructor for this class.
110 * This is the destructor for this class.
114 virtual ~Event(void);
117 * Adds the listener object.
118 * The added listener can listen to events when they are fired.
122 * @return An error code
123 * @param[in] listener Listener to add
124 * @param[in] calledByCallerThread true, to call the listener on the caller thread of this method
125 * false, to call the listener on the thread in which the event is fired.
126 * @exception E_SUCCESS This method is successful.
127 * @exception E_OBJ_ALREADY_EXIST The listener already exists.
128 * @exception E_INVALID_OPERATION calledByCallerThread is set to true but the caller thread is a worker thread.
130 result AddListener(IEventListener& listener, bool calledByCallerThread = true);
133 * Removes the listener object.
134 * The removed listener cannot listen to events when they are fired.
138 * @return An error code
139 * @param[in] listener Listener to remove
140 * @exception E_SUCCESS This method is successful.
141 * @exception E_OBJ_NOT_FOUND The listener is not found.
143 result RemoveListener(IEventListener& listener);
146 * Fires the event with an argument.
150 * @return An error code
151 * @param[in] arg The event argument.
152 * @exception E_SUCCESS This method is successful.
153 * @exception E_INVALID_STATE This event has not been initialized.
155 * @remarks This takes the ownership of @c arg. So arg should be created on a heap and should not be deleted by caller.
157 result Fire(IEventArg& arg);
161 * Implement to call the corresponding event listener's method.
165 * @param[in] listener The listener instance which is currently processing
166 * @param[in] arg The event argument that is fired
168 * @remarks A derived class must override this method.
170 virtual void FireImpl(IEventListener& listener, const IEventArg& arg) = 0;
173 friend class _EventImpl;
174 class _EventImpl* __pEventImpl;
178 } } } // Tizen::Base::Runtime
180 #endif // _FBASE_RT_EVENT_H_