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