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