1//------------------------------------------------------------------------------
2//	Copyright (c) 2001-2002, OpenBeOS
3//
4//	Permission is hereby granted, free of charge, to any person obtaining a
5//	copy of this software and associated documentation files (the "Software"),
6//	to deal in the Software without restriction, including without limitation
7//	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8//	and/or sell copies of the Software, and to permit persons to whom the
9//	Software is furnished to do so, subject to the following conditions:
10//
11//	The above copyright notice and this permission notice shall be included in
12//	all copies or substantial portions of the Software.
13//
14//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19//	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20//	DEALINGS IN THE SOFTWARE.
21//
22//	File Name:		EventMaskWatcher.cpp
23//	Author:			Ingo Weinhold (bonefish@users.sf.net)
24//	Description:	EventMaskWatcher is a Watcher extended by an event mask.
25//					EventMaskWatcherFilter filters EventMaskWatchers with
26//					respect to their event mask and a specific event.
27//------------------------------------------------------------------------------
28
29#include "EventMaskWatcher.h"
30
31// EventMaskWatcher
32
33/*!	\class EventMaskWatcher
34	\brief EventMaskWatcher is a Watcher extended by an event mask.
35
36	Each set bit in the watcher's event mask specifies an event the watcher
37	wants to get notified about.
38
39	This class is intended to be used together with EventMaskWatcherFilter.
40	Such a filter object is created with a certain event and its
41	EventMaskWatcherFilter::Filter() method only selects those
42	EventMaskWatchers with an event mask that contains the event.
43*/
44
45/*!	\var uint32 EventMaskWatcher::fEventMask
46	\brief The watcher's event mask.
47*/
48
49// constructor
50/*!	\brief Creates a new EventMaskWatcher with a given target and event mask.
51	\param target The watcher's message target.
52	\param eventMask the watcher's event mask.
53*/
54EventMaskWatcher::EventMaskWatcher(const BMessenger &target, uint32 eventMask)
55	: Watcher(target),
56	  fEventMask(eventMask)
57{
58}
59
60// EventMask
61/*!	\brief Returns the watcher's event mask.
62	\return The watcher's event mask.
63*/
64uint32
65EventMaskWatcher::EventMask() const
66{
67	return fEventMask;
68}
69
70
71// EventMaskWatcherFilter
72
73/*!	\class EventMaskWatcherFilter
74	\brief EventMaskWatcherFilter filters EventMaskWatchers with respect to
75		   their event mask and a specific event.
76
77	This class is intended to be used together with EventMaskWatcher.
78	A filter object is created with a certain event and its Filter() method
79	only selects those EventMaskWatchers with an event mask that contains
80	the event.
81*/
82
83/*!	\var uint32 EventMaskWatcherFilter::fEvent
84	\brief The filter's event.
85
86	Filter only returns \c true for watchers whose event mask contains the
87	event.
88*/
89
90// constructor
91/*!	\brief Creates a new EventMaskWatcherFilter with a specified event.
92
93	Actually \a event may specify more than one event. Usually each set bit
94	represents an event.
95
96	\param event The event.
97*/
98EventMaskWatcherFilter::EventMaskWatcherFilter(uint32 event)
99	: WatcherFilter(),
100	  fEvent(event)
101{
102}
103
104// Filter
105/*!	\brief Returns whether the watcher-message pair satisfies the predicate
106		   represented by this object.
107
108	Returns \c true, if the supplied watcher is an EventMaskWatcher and its
109	event mask contains this filter's event, or more precise the bitwise AND
110	on event mask and event is not 0.
111
112	\param watcher The watcher in question.
113	\param message The message in question.
114	\return \c true, if the watcher's event mask contains the filter's event.
115*/
116bool
117EventMaskWatcherFilter::Filter(Watcher *watcher, BMessage *message)
118{
119	EventMaskWatcher *emWatcher = dynamic_cast<EventMaskWatcher *>(watcher);
120	return (emWatcher && (emWatcher->EventMask() & fEvent));
121}
122
123