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 header 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 arguments to its listeners.
39 * The %Event class provides methods for notifying events with argument to its listeners.
40 * 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.
46 * using namespace Tizen::Base::Runtime;
48 * class MyEventArg : public IEventArg
55 * MyEventArg::MyEventArg(int t)
60 * class MyEventListener : public IEventListener
63 * void OnEventReceived(const IEventArg& arg);
67 * MyEventListener::OnEventReceived(const IEventArg& arg)
69 * const MyEventArg* myarg = dynamic_cast<const MyEventArg*> (&arg);
72 * class MyEvent : public Event
75 * virtual void FireImpl(IEventListener& listener, const IEventArg& arg);
79 * MyEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
81 * MyEventListener* plistener = dynamic_cast<MyEventListener*> (&listener);
82 * plistener->OnEventReceived(arg);
86 * MyApplication::Test Code(void)
88 * MyEventListener* mylistener = new MyEventListener();
89 * MyEvent* my = new MyEvent();
90 * MyEventArg* myarg = new MyEventArg(3);
91 * my->AddListener(*mylistener);
97 class _OSP_EXPORT_ Event
98 : virtual public Tizen::Base::Object
102 * This is the default constructor for this class.
109 * This is the destructor for this class.
113 virtual ~Event(void);
116 * Adds the listener object.
117 * The added listener can listen to events when they are fired.
121 * @return An error code
122 * @param[in] listener The listener to add
123 * @param[in] calledByCallerThread Set to @c true, to call the listener on the caller thread of this method, @n
124 * else @c false, to call the listener on the thread in which the event is fired.
125 * @exception E_SUCCESS The method is successful.
126 * @exception E_OBJ_ALREADY_EXIST The listener already exists.
127 * @exception E_INVALID_OPERATION The specified @c calledByCallerThread is @c true but the caller thread is a worker thread.
129 result AddListener(IEventListener& listener, bool calledByCallerThread = true);
132 * Removes the listener object.
133 * The removed listener cannot listen to events when they are fired.
137 * @return An error code
138 * @param[in] listener The listener to remove
139 * @exception E_SUCCESS The method is successful.
140 * @exception E_OBJ_NOT_FOUND The listener has not been found.
142 result RemoveListener(IEventListener& listener);
145 * Fires the event with an argument.
149 * @return An error code
150 * @param[in] arg The event argument
151 * @exception E_SUCCESS The method is successful.
152 * @exception E_INVALID_STATE This event has not been initialized.
154 * @remarks This takes the ownership of @c arg. @n
155 * So @c 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_