xref: /haiku/src/servers/net/SimpleMessageFilter.cpp (revision 67bce78b48ed6d01b5a8eef89f5694c372b7e0a1)
1 //-----------------------------------------------------------------------
2 //  This software is part of the OpenBeOS distribution and is covered
3 //  by the OpenBeOS license.
4 //
5 //  Copyright (c) 2004 Waldemar Kornewald, Waldemar.Kornewald@web.de
6 //-----------------------------------------------------------------------
7 
8 /*!	\class SimpleMessageFilter
9 	\brief This is a BMessageFilter that can filter multiple \a what values.
10 
11 	This class extends the BMessageFilter's ability of handling one \a what value
12 	to handling multiple \a what values.
13 */
14 
15 #include "SimpleMessageFilter.h"
16 #include <Message.h>
17 
18 
19 /*!	\brief Creates a copy of the \a what array.
20 
21 	\param what A pointer to an array of \a what values that should be filtered.
22 		The end-of-list indicator is an element valued 0 (zero).
23 	\param target The target for messages matching one of the \a what values.
24 		If \a target == NULL the messages will be discarded.
25 */
26 SimpleMessageFilter::SimpleMessageFilter(const uint32 *what, BHandler *target)
27 	: BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE),
28 	fTarget(target)
29 {
30 	if(!what) {
31 		fWhatArray = NULL;
32 		return;
33 	}
34 
35 	// include the trailing zero in the copy
36 	int32 count = 0;
37 	while(what[count++] != 0)
38 		;
39 	fWhatArray = new uint32[count];
40 	memcpy(fWhatArray, what, count * sizeof(uint32));
41 }
42 
43 
44 //! Frees the copied \a what array.
45 SimpleMessageFilter::~SimpleMessageFilter()
46 {
47 	delete fWhatArray;
48 }
49 
50 
51 //! Filters all messages that match the \a what array given in the constructor.
52 filter_result
53 SimpleMessageFilter::Filter(BMessage *message, BHandler **target)
54 {
55 	for(int32 index = 0; fWhatArray[index] != 0; index++)
56 		if(fWhatArray[index] == message->what) {
57 			if(!fTarget)
58 				return B_SKIP_MESSAGE;
59 
60 			*target = fTarget;
61 			break;
62 		}
63 
64 	return B_DISPATCH_MESSAGE;
65 }
66