mask.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
mask.h
Go to the documentation of this file.
1 #ifndef OSL_MISC_MASK_H
2 #define OSL_MISC_MASK_H
3 #include "osl/misc/bitOp.h"
4 #include "osl/misc/cstdint.h"
5 #include "osl/config.h"
6 #include <cassert>
7 #include <iosfwd>
8
9 namespace osl
10 {
11 namespace misc
12 {
13 template <class Integer>
14 class GeneralMask
15 {
16 Integer mask;
17 private:
18 GeneralMask(Integer value) : mask(value) {}
19 public:
20 GeneralMask() : mask(0) {}
21 static const GeneralMask makeDirect(Integer value) { return GeneralMask(value); }
22 GeneralMask& operator&=(const GeneralMask& r)
23 {
24 mask &= r.mask;
25 return *this;
26 }
27 GeneralMask& operator|=(const GeneralMask& r)
28 {
29 mask |= r.mask;
30 return *this;
31 }
32 GeneralMask& operator^=(const GeneralMask& r)
33 {
34 mask ^= r.mask;
35 return *this;
36 }
37 GeneralMask& operator-=(const GeneralMask& r)
38 {
39 mask -= r.mask;
40 return *this;
41 }
42 GeneralMask& operator+=(const GeneralMask& r)
43 {
44 mask += r.mask;
45 return *this;
46 }
47 GeneralMask& operator<<=(int shift)
48 {
49 mask <<= shift;
50 return *this;
51 }
52 GeneralMask& operator>>=(int shift)
53 {
54 mask >>= shift;
55 return *this;
56 }
57 const GeneralMask operator~() const { return GeneralMask(~mask); }
58
59 int bsf() const { return BitOp::bsf(mask); }
60 int bsr() const { return BitOp::bsr(mask); }
67 int takeOneBit() { return BitOp::takeOneBit(mask); }
68
74 bool hasMultipleBit() const { return BitOp::hasMultipleBit(mask); }
80 int countBit2() const {
81 assert(mask);
82 return (mask & (mask-1)) ? 2 : 1;
83 }
88 int
89 #ifdef __GNUC__
90 __attribute__ ((pure))
91 #endif
92 countBit() const { return BitOp::countBit(mask); }
98 GeneralMask lowestBit() const { return BitOp::lowestBit(mask); }
99 bool none() const { return mask == 0; }
100 bool any() const { return ! none(); }
101 Integer value() const { return mask; }
102 };
103
104 template <class Integer> inline
105 bool operator==(const GeneralMask<Integer>& l, const GeneralMask<Integer>& r)
106 {
107 return l.value() == r.value();
108 }
109 template <class Integer> inline
110 bool operator!=(const GeneralMask<Integer>& l, const GeneralMask<Integer>& r)
111 {
112 return ! (l == r);
113 }
114 template <class Integer> inline
115 bool operator<(const GeneralMask<Integer>& l, const GeneralMask<Integer>& r)
116 {
117 return l.value() < r.value();
118 }
119
120 template <class Integer> inline
121 const GeneralMask<Integer> operator&(GeneralMask<Integer> l,
122 GeneralMask<Integer> r) {
123 GeneralMask<Integer> result = l;
124 return result &= r;
125 }
126 template <class Integer> inline
127 const GeneralMask<Integer> operator|(GeneralMask<Integer> l,
128 GeneralMask<Integer> r) {
129 GeneralMask<Integer> result = l;
130 return result |= r;
131 }
132 template <class Integer> inline
133 const GeneralMask<Integer> operator^(GeneralMask<Integer> l,
134 GeneralMask<Integer> r) {
135 GeneralMask<Integer> result = l;
136 return result ^= r;
137 }
138 template <class Integer> inline
139 const GeneralMask<Integer> operator<<(GeneralMask<Integer> m, int shift) {
140 GeneralMask<Integer> result = m;
141 return result <<= shift;
142 }
143 template <class Integer> inline
144 const GeneralMask<Integer> operator>>(GeneralMask<Integer> m, int shift) {
145 GeneralMask<Integer> result = m;
146 return result >>= shift;
147 }
148
149 typedef GeneralMask<unsigned long long> Mask64;
150 typedef GeneralMask<unsigned int> Mask32;
151
152
153 #if OSL_WORDSIZE == 64
154 typedef unsigned long long mask_int_t;
155 #elif OSL_WORDSIZE == 32
156 typedef unsigned int mask_int_t;
157 #endif
158 typedef GeneralMask<mask_int_t> mask_t;
159
160 std::ostream& operator<<(std::ostream&, const mask_t&);
161 } // namespace misc
162 using misc::mask_int_t;
163 using misc::mask_t;
164 } // namespace osl
165
166 #endif
167 // ;;; Local Variables:
168 // ;;; mode:c++
169 // ;;; c-basic-offset:2
170 // ;;; End: