xref: /haiku/src/add-ons/input_server/filters/shortcut_catcher/BitFieldTesters.h (revision 958d3f437574cefaf8831b29430b2cfa9e508052)
1 /*
2  * Copyright 1999-2009 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Jeremy Friesner
7  */
8 #ifndef _BIT_FIELD_TESTERS_H
9 #define _BIT_FIELD_TESTERS_H
10 
11 
12 #include <Archivable.h>
13 #include <List.h>
14 #include <Message.h>
15 
16 
17 // This file contains various BitTester classes, each of which defines a
18 // sequence of bit testing logics to do on a uint32.
19 
20 // The abstract base class. Defines the interface.
21 class _EXPORT BitFieldTester;
22 class BitFieldTester : public BArchivable {
23 public:
24 								BitFieldTester();
25 								BitFieldTester(BMessage* from);
26 
27 	virtual bool				IsMatching(uint32 field) = 0;
28 	virtual status_t			Archive(BMessage* into,
29 									bool deep = true) const;
30 };
31 
32 
33 // This version always returns the value specified in the constructor.
34 class _EXPORT ConstantFieldTester;
35 class ConstantFieldTester : public BitFieldTester {
36 public:
37 								ConstantFieldTester(bool result);
38 								ConstantFieldTester(BMessage* from);
39 
40 	virtual	status_t			Archive(BMessage* into,
41 									bool deep = true) const;
42 	static	BArchivable*		Instantiate(BMessage* from);
43 	virtual	bool				IsMatching(uint32 field);
44 
45 private:
46 			bool				fResult;
47 };
48 
49 
50 // This version matches if all requiredBits are found in the field,
51 // and no forbiddenBits are found.
52 class _EXPORT HasBitsFieldTester;
53 class HasBitsFieldTester : public BitFieldTester {
54 public:
55 								HasBitsFieldTester(uint32 requiredBits,
56 									uint32 forbiddenBits = 0);
57 								HasBitsFieldTester(BMessage* from);
58 
59 	virtual	status_t			Archive(BMessage* into,
60 									bool deep = true) const;
61 	static	BArchivable*		Instantiate(BMessage* from);
62 	virtual	bool				IsMatching(uint32 field);
63 
64 private:
65 			uint32				fRequiredBits;
66 			uint32				fForbiddenBits;
67 };
68 
69 
70 // This one negates the tester it holds.
71 class _EXPORT NotFieldTester;
72 class NotFieldTester : public BitFieldTester {
73 public:
74 	// (slave) should be allocated with new, becomes property of this object.
75 								NotFieldTester(BitFieldTester* slave);
76 								NotFieldTester(BMessage* from);
77 	virtual						~NotFieldTester();
78 
79 	virtual	status_t			Archive(BMessage* into,
80 									bool deep = true) const;
81 	static	BArchivable*		Instantiate(BMessage* from);
82 	virtual	bool				IsMatching(uint32 field);
83 
84 private:
85 			BitFieldTester*		fSlave;
86 };
87 
88 
89 // The most interesting class: This one returns true if at least (minNum) of
90 // its slaves return true. It can be used for OR (i.e. minNum==1), AND
91 // (i.e. minNum==numberofchildren), or anything in between!
92 class _EXPORT MinMatchFieldTester;
93 class MinMatchFieldTester : public BitFieldTester {
94 public:
95 								MinMatchFieldTester(int32 minNum,
96 									bool deleteSlaves = true);
97 									MinMatchFieldTester(BMessage* from);
98 	virtual						~MinMatchFieldTester();
99 
100 	// (slave) should be allocated with new, becomes property of this object.
101 			void				AddSlave(const BitFieldTester* slave);
102 
103 	virtual	status_t			Archive(BMessage* into, bool deep = true) const;
104 	static	BArchivable*		Instantiate(BMessage* from);
105 	virtual	bool				IsMatching(uint32 field);
106 
107 private:
108 			BList				fSlaves;
109 			int32				fMinNum;
110 
111 	// true if we should delete all our slaves when we are deleted.
112 			bool				fDeleteSlaves;
113 };
114 
115 
116 #endif	// _BIT_FIELD_TESTERS_H
117