xref: /haiku/src/servers/registrar/Event.cpp (revision 268f99dd7dc4bd7474a8bd2742d3f1ec1de6752a)
144c3726bSIngo Weinhold //------------------------------------------------------------------------------
2*2ca13760SColdfirex //	Copyright (c) 2001-2002, Haiku
344c3726bSIngo Weinhold //
444c3726bSIngo Weinhold //	Permission is hereby granted, free of charge, to any person obtaining a
544c3726bSIngo Weinhold //	copy of this software and associated documentation files (the "Software"),
644c3726bSIngo Weinhold //	to deal in the Software without restriction, including without limitation
744c3726bSIngo Weinhold //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
844c3726bSIngo Weinhold //	and/or sell copies of the Software, and to permit persons to whom the
944c3726bSIngo Weinhold //	Software is furnished to do so, subject to the following conditions:
1044c3726bSIngo Weinhold //
1144c3726bSIngo Weinhold //	The above copyright notice and this permission notice shall be included in
1244c3726bSIngo Weinhold //	all copies or substantial portions of the Software.
1344c3726bSIngo Weinhold //
1444c3726bSIngo Weinhold //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1544c3726bSIngo Weinhold //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1644c3726bSIngo Weinhold //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1744c3726bSIngo Weinhold //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1844c3726bSIngo Weinhold //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1944c3726bSIngo Weinhold //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2044c3726bSIngo Weinhold //	DEALINGS IN THE SOFTWARE.
2144c3726bSIngo Weinhold //
2244c3726bSIngo Weinhold //	File Name:		Event.cpp
2344c3726bSIngo Weinhold //	Author:			Ingo Weinhold (bonefish@users.sf.net)
2444c3726bSIngo Weinhold //					YellowBites (http://www.yellowbites.com)
2544c3726bSIngo Weinhold //	Description:	Base class for events as handled by EventQueue.
2644c3726bSIngo Weinhold //------------------------------------------------------------------------------
2744c3726bSIngo Weinhold 
2844c3726bSIngo Weinhold #include "Event.h"
2944c3726bSIngo Weinhold 
30c2b2c7d9SIngo Weinhold /*!	\class Event
31c2b2c7d9SIngo Weinhold 	\brief Base class for events as handled by EventQueue.
32c2b2c7d9SIngo Weinhold 
33c2b2c7d9SIngo Weinhold 	Event features methods to set and get the event time and the "auto delete"
34c2b2c7d9SIngo Weinhold 	flag, and a Do() method invoked, when the event is executed.
35c2b2c7d9SIngo Weinhold 
36c2b2c7d9SIngo Weinhold 	If the "auto delete" flag is set to \c true, the event is deleted by the
37c2b2c7d9SIngo Weinhold 	event queue after it has been executed. The same happens, if Do() returns
38c2b2c7d9SIngo Weinhold 	\c true.
39c2b2c7d9SIngo Weinhold */
40c2b2c7d9SIngo Weinhold 
41c2b2c7d9SIngo Weinhold /*!	\var bigtime_t Event::fTime
42c2b2c7d9SIngo Weinhold 	\brief The event time.
43c2b2c7d9SIngo Weinhold */
44c2b2c7d9SIngo Weinhold 
45c2b2c7d9SIngo Weinhold /*!	\var bool Event::fAutoDelete
46c2b2c7d9SIngo Weinhold 	\brief The "auto delete" flag.
47c2b2c7d9SIngo Weinhold */
48c2b2c7d9SIngo Weinhold 
4944c3726bSIngo Weinhold // constructor
50c2b2c7d9SIngo Weinhold /*!	\brief Creates a new event.
51c2b2c7d9SIngo Weinhold 
52c2b2c7d9SIngo Weinhold 	The event time is initialized to 0. That is, it should be set before
53c2b2c7d9SIngo Weinhold 	pushing the event into an event queue.
54c2b2c7d9SIngo Weinhold 
55c2b2c7d9SIngo Weinhold 	\param autoDelete Specifies whether the object shall automatically be
56c2b2c7d9SIngo Weinhold 		   deleted by the event queue after being executed.
57c2b2c7d9SIngo Weinhold */
Event(bool autoDelete)5844c3726bSIngo Weinhold Event::Event(bool autoDelete)
5944c3726bSIngo Weinhold 	: fTime(0),
6044c3726bSIngo Weinhold 	  fAutoDelete(autoDelete)
6144c3726bSIngo Weinhold {
6244c3726bSIngo Weinhold }
6344c3726bSIngo Weinhold 
6444c3726bSIngo Weinhold // constructor
65c2b2c7d9SIngo Weinhold /*!	\brief Creates a new event.
66c2b2c7d9SIngo Weinhold 	\param time Time when the event shall be executed.
67c2b2c7d9SIngo Weinhold 	\param autoDelete Specifies whether the object shall automatically be
68c2b2c7d9SIngo Weinhold 		   deleted by the event queue after being executed.
69c2b2c7d9SIngo Weinhold */
Event(bigtime_t time,bool autoDelete)7044c3726bSIngo Weinhold Event::Event(bigtime_t time, bool autoDelete)
7144c3726bSIngo Weinhold 	: fTime(time),
7244c3726bSIngo Weinhold 	  fAutoDelete(autoDelete)
7344c3726bSIngo Weinhold {
7444c3726bSIngo Weinhold }
7544c3726bSIngo Weinhold 
7644c3726bSIngo Weinhold // destructor
77c2b2c7d9SIngo Weinhold /*!	\brief Frees all resources associated with the object.
78c2b2c7d9SIngo Weinhold 
79c2b2c7d9SIngo Weinhold 	Does nothing.
80c2b2c7d9SIngo Weinhold */
~Event()8144c3726bSIngo Weinhold Event::~Event()
8244c3726bSIngo Weinhold {
8344c3726bSIngo Weinhold }
8444c3726bSIngo Weinhold 
8544c3726bSIngo Weinhold // SetTime
86c2b2c7d9SIngo Weinhold /*!	\brief Sets a new event time.
87c2b2c7d9SIngo Weinhold 
88c2b2c7d9SIngo Weinhold 	\note You must not call this method, when the event is in an event queue.
89c2b2c7d9SIngo Weinhold 		  Use EventQueue::ModifyEvent() instead.
90c2b2c7d9SIngo Weinhold 
91c2b2c7d9SIngo Weinhold 	\param time The new event time.
92c2b2c7d9SIngo Weinhold */
9344c3726bSIngo Weinhold void
SetTime(bigtime_t time)9444c3726bSIngo Weinhold Event::SetTime(bigtime_t time)
9544c3726bSIngo Weinhold {
9644c3726bSIngo Weinhold 	fTime = time;
9744c3726bSIngo Weinhold }
9844c3726bSIngo Weinhold 
9944c3726bSIngo Weinhold // Time
100c2b2c7d9SIngo Weinhold /*!	\brief Returns the time of the event.
101c2b2c7d9SIngo Weinhold 	\return Returns the time of the event.
102c2b2c7d9SIngo Weinhold */
10344c3726bSIngo Weinhold bigtime_t
Time() const10444c3726bSIngo Weinhold Event::Time() const
10544c3726bSIngo Weinhold {
10644c3726bSIngo Weinhold 	return fTime;
10744c3726bSIngo Weinhold }
10844c3726bSIngo Weinhold 
10944c3726bSIngo Weinhold // SetAutoDelete
110c2b2c7d9SIngo Weinhold /*!	\brief Sets whether the event shall be deleted after execution.
111c2b2c7d9SIngo Weinhold 	\param autoDelete Specifies whether the object shall automatically be
112c2b2c7d9SIngo Weinhold 		   deleted by the event queue after being executed.
113c2b2c7d9SIngo Weinhold */
11444c3726bSIngo Weinhold void
SetAutoDelete(bool autoDelete)11544c3726bSIngo Weinhold Event::SetAutoDelete(bool autoDelete)
11644c3726bSIngo Weinhold {
11744c3726bSIngo Weinhold 	fAutoDelete = autoDelete;
11844c3726bSIngo Weinhold }
11944c3726bSIngo Weinhold 
12044c3726bSIngo Weinhold // IsAutoDelete
121c2b2c7d9SIngo Weinhold /*!	\brief Returns whether the event shall be deleted after execution.
122c2b2c7d9SIngo Weinhold 	\return Returns whether the object shall automatically be
123c2b2c7d9SIngo Weinhold 			deleted by the event queue after being executed.
124c2b2c7d9SIngo Weinhold */
12544c3726bSIngo Weinhold bool
IsAutoDelete() const12644c3726bSIngo Weinhold Event::IsAutoDelete() const
12744c3726bSIngo Weinhold {
12844c3726bSIngo Weinhold 	return fAutoDelete;
12944c3726bSIngo Weinhold }
13044c3726bSIngo Weinhold 
13144c3726bSIngo Weinhold // Do
132c2b2c7d9SIngo Weinhold /*!	\brief Hook method invoked when the event time has arrived.
133c2b2c7d9SIngo Weinhold 
134c2b2c7d9SIngo Weinhold 	To be overridden by derived classes. As the method is executed in the
135c2b2c7d9SIngo Weinhold 	event queue's timer thread, the execution of the method should take
136c2b2c7d9SIngo Weinhold 	as little time as possible to keep the event queue precise.
137c2b2c7d9SIngo Weinhold 
138c2b2c7d9SIngo Weinhold 	The return value of this method indicates whether the event queue shall
139c2b2c7d9SIngo Weinhold 	delete the object. This does not override the IsAutoDelete() value. If
140c2b2c7d9SIngo Weinhold 	IsAutoDelete() is \c true, then the object is deleted regardless of this
141c2b2c7d9SIngo Weinhold 	method's return value, but if IsAutoDelete() is \c false, this method's
142c2b2c7d9SIngo Weinhold 	return value is taken into consideration. To be precise the logical OR
143c2b2c7d9SIngo Weinhold 	of IsAutoDelete() and the return value of Do() specifies whether the
144c2b2c7d9SIngo Weinhold 	object shall be deleted. The reason for this handling is that there are
145c2b2c7d9SIngo Weinhold 	usally two kind of events: "one-shot" events and those that are reused
146c2b2c7d9SIngo Weinhold 	periodically or from time to time. The first kind can simply be
147c2b2c7d9SIngo Weinhold 	constructed with "auto delete" set to \c true and doesn't need to care
148c2b2c7d9SIngo Weinhold 	about Do()'s return value. The second kind shall usually not be deleted,
149c2b2c7d9SIngo Weinhold 	but during the execution of Do() it might turn out, that it the would be
150c2b2c7d9SIngo Weinhold 	a good idea to let it be deleted by the event queue.
151c2b2c7d9SIngo Weinhold 	BTW, the event may as well delete itself in Do(); that is not a very
152c2b2c7d9SIngo Weinhold 	nice practice though.
153c2b2c7d9SIngo Weinhold 
154c2b2c7d9SIngo Weinhold 	\note IsAutoDelete() is checked by the event queue before Do() is invoked.
155c2b2c7d9SIngo Weinhold 		  Thus changing it in Do() won't have any effect.
156c2b2c7d9SIngo Weinhold 
157c2b2c7d9SIngo Weinhold 	If it is not deleted, the event can re-push itself into the queue
158c2b2c7d9SIngo Weinhold 	within Do().
159c2b2c7d9SIngo Weinhold 
160c2b2c7d9SIngo Weinhold 	\note The event queue is not locked when this method is invoked and it
161c2b2c7d9SIngo Weinhold 		  doesn't contain the event anymore.
162c2b2c7d9SIngo Weinhold 
163c2b2c7d9SIngo Weinhold 	\param queue The event queue executing the event.
164c2b2c7d9SIngo Weinhold 	\return \c true, if the event shall be deleted by the event queue,
165c2b2c7d9SIngo Weinhold 			\c false, if IsAutoDelete() shall be checked for this decision.
166c2b2c7d9SIngo Weinhold */
16744c3726bSIngo Weinhold bool
Do(EventQueue * queue)168c2b2c7d9SIngo Weinhold Event::Do(EventQueue *queue)
16944c3726bSIngo Weinhold {
17044c3726bSIngo Weinhold 	return fAutoDelete;
17144c3726bSIngo Weinhold }
17244c3726bSIngo Weinhold 
173