libzypp  13.10.6
Bit.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_BIT_H
13 #define ZYPP_BIT_H
14 
15 #include <iosfwd>
16 #include <string>
17 
19 namespace zypp
20 {
21 
28  namespace bit
29  {
30 
31  namespace bit_detail
32  {
34  template<class _IntT, unsigned _size>
35  struct Gen1Bits
36  {
37  static const _IntT value = (Gen1Bits<_IntT,_size-1>::value << 1)+1;
38  };
40  template<class _IntT>
41  struct Gen1Bits<_IntT, 0>
42  {
43  static const _IntT value = 0;
44  };
45  }
46 
48  template<class _IntT>
49  struct MaxBits
50  {
51  typedef _IntT IntT;
52  static const unsigned value = (sizeof(IntT)*8);
53  };
54 
56  template<class _IntT>
57  inline std::string asString( _IntT val, char zero = '0', char one = '1' )
58  {
59  std::string s( MaxBits<_IntT>::value, zero );
60  for( unsigned i = MaxBits<_IntT>::value; i; )
61  {
62  --i;
63  if ( val & (_IntT)1 )
64  s[i] = one;
65  val = val >> 1;
66  };
67  return s;
68  }
69 
71  template<class _IntT, unsigned _begin, unsigned _size>
72  struct Mask
73  {
74  typedef _IntT IntT;
76  static const IntT inverted = ~value;
77  };
78 
80  template<class _IntT, unsigned _begin, unsigned _size>
81  struct Range
82  {
83  typedef _IntT IntT;
86 
87  static const unsigned begin = _begin;
88  static const unsigned size = _size;
89  static const unsigned end = _begin + _size;
90  };
95  template<class _IntT, unsigned _begin>
96  struct Range<_IntT, _begin, 0>
97  {};
98 
109  template<class _Range, typename _Range::IntT _value>
110  struct RangeValue
111  {
112  typedef _Range RangeT;
113  typedef typename _Range::IntT IntT;
114 
115  static const IntT value = _value << RangeT::begin;
116  };
117 
127  template<class _Range, unsigned _pos>
128  struct RangeBit
129  {
130  typedef _Range RangeT;
131  typedef typename _Range::IntT IntT;
132 
133  static const IntT value = IntT(1) << (RangeT::begin + _pos);
134  };
135 
137  //
138  // CLASS NAME : BitField
139  //
158  template<class _IntT>
159  class BitField : public Range<_IntT, 0, MaxBits<_IntT>::value>
160  {
161  public:
164  : _value( (_IntT)0 )
165  {}
167  BitField( const _IntT & value_r )
168  : _value( value_r )
169  {}
170 
171  public:
173  explicit operator bool() const
174  { return _value != (_IntT)0; }
175 
176  public:
178  template<class _Range>
179  _IntT value() const
180  {
181  return _value & _Range::Mask::value;
182  }
183  _IntT value() const
184  {
185  return _value;
186  }
187 
189  template<class _Range>
190  std::string asString() const
191  {
192  return bit::asString( _value & _Range::Mask::value, '_' );
193  }
194  std::string asString() const
195  {
196  return bit::asString( _value, '_' );
197  }
198 
200  template<class _Range>
201  BitField & assign( _IntT rhs )
202  {
203  _value = (_value & _Range::Mask::inverted)
204  | (rhs & _Range::Mask::value);
205  return *this;
206  }
207  BitField & assign( _IntT rhs )
208  {
209  _value = rhs;
210  return *this;
211  }
212 
214  template<class _Range>
215  bool isEqual( _IntT rhs ) const
216  {
217  return (_value & _Range::Mask::value)
218  == (rhs & _Range::Mask::value);
219  }
220  bool isEqual( _IntT rhs ) const
221  {
222  return _value == rhs;
223  }
224 
225  public:
226 
228  template<class _Range>
229  BitField & set( _IntT rhs, bool doset_r )
230  { return set( (rhs & _Range::Mask::value), doset_r ); }
231 
232  BitField & set( _IntT rhs, bool doset_r )
233  { return doset_r ? set( rhs ) : unset( rhs ); }
234 
236  template<class _Range>
237  BitField & set( _IntT rhs )
238  { return set( rhs & _Range::Mask::value ); }
239 
240  BitField & set( _IntT rhs )
241  { _value |= rhs; return *this; }
242 
244  template<class _Range>
245  BitField & unset( _IntT rhs )
246  { return unset( rhs & _Range::Mask::value ); }
247 
248  BitField & unset( _IntT rhs )
249  { _value &= ~rhs; return *this; }
250 
252  template<class _Range>
253  bool test( _IntT rhs )
254  { return test( rhs & _Range::Mask::value ); }
255 
256  bool test( _IntT rhs ) const
257  { return (_value & rhs) == rhs; }
258 
260  template<class _Range>
261  bool testAnyOf( _IntT rhs )
262  { return testAnyOf( rhs & _Range::Mask::value ); }
263 
264  bool testAnyOf( _IntT rhs ) const
265  { return (_value & rhs); }
266 
267  public:
268 
269  BitField & operator=( const BitField & rhs )
270  { _value = rhs._value; return *this; }
271 
272  BitField & operator&=( const BitField & rhs )
273  { _value &= rhs._value; return *this; }
274 
275  BitField & operator|=( const BitField & rhs )
276  { _value |= rhs._value; return *this; }
277 
278  BitField & operator^=( const BitField & rhs )
279  { _value ^= rhs._value; return *this; }
280 
281  BitField & operator<<=( unsigned num )
282  { _value <<= num; return *this; }
283 
284  BitField & operator>>=( unsigned num )
285  { _value >>= num; return *this; }
286 
288  { return ~_value; }
289 
290  private:
291  _IntT _value;
292  };
294 
296  template<class _IntT>
297  std::ostream & operator<<( std::ostream & str, const BitField<_IntT> & obj )
298  {
299  return str << obj.asString();
300  }
301 
303  template<class _IntT>
304  inline bool operator==( const BitField<_IntT> & lhs, const BitField<_IntT> & rhs )
305  { return lhs.value() == rhs.value(); }
306 
308  template<class _IntT>
309  inline bool operator!=( const BitField<_IntT> & lhs, const BitField<_IntT> & rhs )
310  { return ! (lhs == rhs); }
311 
312 
314  template<class _IntT>
315  inline BitField<_IntT> operator&( const BitField<_IntT> & lhs, const BitField<_IntT> & rhs )
316  { return BitField<_IntT>(lhs) &= rhs; }
317 
319  template<class _IntT>
320  inline BitField<_IntT> operator|( const BitField<_IntT> & lhs, const BitField<_IntT> & rhs )
321  { return BitField<_IntT>(lhs) |= rhs; }
322 
324  template<class _IntT>
325  inline BitField<_IntT> operator^( const BitField<_IntT> & lhs, const BitField<_IntT> & rhs )
326  { return BitField<_IntT>(lhs) ^= rhs; }
327 
329  template<class _IntT>
330  inline BitField<_IntT> operator<<( const BitField<_IntT> & lhs, unsigned num )
331  { return BitField<_IntT>(lhs) <<= num; }
332 
334  template<class _IntT>
335  inline BitField<_IntT> operator>>( const BitField<_IntT> & lhs, unsigned num )
336  { return BitField<_IntT>(lhs) >>= num; }
337 
339  } // namespace bit
342 } // namespace zypp
344 #endif // ZYPP_BIT_H
_IntT value() const
Return the value.
Definition: Bit.h:179
BitField()
Default ctor: zero.
Definition: Bit.h:163
BitField & operator=(const BitField &rhs)
Definition: Bit.h:269
BitField & operator<<=(unsigned num)
Definition: Bit.h:281
Number of bits available in _IntT.
Definition: Bit.h:49
_Range::IntT IntT
Definition: Bit.h:113
static const IntT value
Definition: Bit.h:115
_Range RangeT
Definition: Bit.h:130
std::string asString() const
Definition: Bit.h:194
static const unsigned end
Definition: Bit.h:89
bool isEqual(_IntT rhs) const
Definition: Bit.h:220
zypp::bit::MaxBits< IntT > MaxBits
Definition: Bit.h:84
BitField & unset(_IntT rhs)
Unset bits of rhs.
Definition: Bit.h:245
BitField & operator^=(const BitField &rhs)
Definition: Bit.h:278
BitField< _IntT > operator|(const BitField< _IntT > &lhs, const BitField< _IntT > &rhs)
Definition: Bit.h:320
Edition * _value
Definition: SysContent.cc:311
_IntT _value
Definition: Bit.h:291
A bitmaks of _size 1-bits starting at bit _begin.
Definition: Bit.h:72
bool operator==(const BitField< _IntT > &lhs, const BitField< _IntT > &rhs)
Definition: Bit.h:304
BitField< _IntT > operator^(const BitField< _IntT > &lhs, const BitField< _IntT > &rhs)
Definition: Bit.h:325
BitField & assign(_IntT rhs)
Assign Range in rhs to this.
Definition: Bit.h:201
static const IntT inverted
Definition: Bit.h:76
BitField & set(_IntT rhs, bool doset_r)
Set or unset bits of rhs.
Definition: Bit.h:229
BitField & operator>>=(unsigned num)
Definition: Bit.h:284
static const IntT value
Definition: Bit.h:133
_IntT IntT
Definition: Bit.h:83
A single 1-bit within a Range.
Definition: Bit.h:128
bool operator!=(const BitField< _IntT > &lhs, const BitField< _IntT > &rhs)
Definition: Bit.h:309
bool test(_IntT rhs) const
Definition: Bit.h:256
BitField & set(_IntT rhs, bool doset_r)
Definition: Bit.h:232
static const _IntT value
Definition: Bit.h:37
Generate constants with _size trailing &#39;1&#39;-bits.
Definition: Bit.h:35
_IntT IntT
Definition: Bit.h:74
std::string asString(_IntT val, char zero= '0', char one= '1')
For printing bits.
Definition: Bit.h:57
BitField & operator&=(const BitField &rhs)
Definition: Bit.h:272
BitField< _IntT > operator&(const BitField< _IntT > &lhs, const BitField< _IntT > &rhs)
Definition: Bit.h:315
BitField & operator|=(const BitField &rhs)
Definition: Bit.h:275
BitField(const _IntT &value_r)
Ctor taking an _IntT.
Definition: Bit.h:167
static const IntT value
Definition: Bit.h:75
BitField & unset(_IntT rhs)
Definition: Bit.h:248
static const unsigned begin
Definition: Bit.h:87
BitField operator~() const
Definition: Bit.h:287
An integral type used as BitField.
Definition: Bit.h:159
bool testAnyOf(_IntT rhs) const
Definition: Bit.h:264
BitField & set(_IntT rhs)
Set bits of rhs.
Definition: Bit.h:237
std::string asString() const
Value as bit string.
Definition: Bit.h:190
_IntT IntT
Definition: Bit.h:51
bool isEqual(_IntT rhs) const
Test for equal value within a Range.
Definition: Bit.h:215
BitField & assign(_IntT rhs)
Definition: Bit.h:207
bool testAnyOf(_IntT rhs)
Test whether at least one bit of rhs is set.
Definition: Bit.h:261
static const unsigned size
Definition: Bit.h:88
BitField< _IntT > operator>>(const BitField< _IntT > &lhs, unsigned num)
Definition: Bit.h:335
zypp::bit::Mask< IntT, _begin, _size > Mask
Definition: Bit.h:85
static const unsigned value
Definition: Bit.h:52
Range of bits starting at bit _begin with length _size.
Definition: Bit.h:81
_IntT value() const
Definition: Bit.h:183
BitField & set(_IntT rhs)
Definition: Bit.h:240
bool test(_IntT rhs)
Test whether all bits of rhs are set.
Definition: Bit.h:253
_Range::IntT IntT
Definition: Bit.h:131
A value with in a Range.
Definition: Bit.h:110