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>
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); }
23  {
24  mask &= r.mask;
25  return *this;
26  }
28  {
29  mask |= r.mask;
30  return *this;
31  }
33  {
34  mask ^= r.mask;
35  return *this;
36  }
38  {
39  mask -= r.mask;
40  return *this;
41  }
43  {
44  mask += r.mask;
45  return *this;
46  }
48  {
49  mask <<= shift;
50  return *this;
51  }
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); }
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
106  {
107  return l.value() == r.value();
108  }
109  template <class Integer> inline
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
124  return result &= r;
125  }
126  template <class Integer> inline
130  return result |= r;
131  }
132  template <class Integer> inline
136  return result ^= r;
137  }
138  template <class Integer> inline
139  const GeneralMask<Integer> operator<<(GeneralMask<Integer> m, int shift) {
141  return result <<= shift;
142  }
143  template <class Integer> inline
146  return result >>= shift;
147  }
148 
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
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: