libzypp 17.37.10

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{
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 };
39
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 using IntT = TInt;
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 using IntT = TInt;
75 static const IntT value = bit_detail::Gen1Bits<IntT,_size>::value << _begin;
76 static const IntT inverted = ~value;
77 };
78
80 template<class TInt, unsigned _begin, unsigned _size>
81 struct Range
82 {
83 using IntT = TInt;
84 using MaxBits = zypp::bit::MaxBits<IntT>;
85 using Mask = zypp::bit::Mask<IntT, _begin, _size>;
86
87 static const unsigned begin = _begin;
88 static const unsigned size = _size;
89 static const unsigned end = _begin + _size;
90 };
91
95 template<class TInt, unsigned _begin>
96 struct Range<TInt, _begin, 0>
97 {};
98
109 template<class TRange, typename TRange::IntT _value>
110 struct RangeValue
111 {
112 using RangeT = TRange;
113 using IntT = typename TRange::IntT;
114
115 static const IntT value = _value << RangeT::begin;
116 };
117
127 template<class TRange, unsigned _pos>
128 struct RangeBit
129 {
130 using RangeT = TRange;
131 using IntT = typename TRange::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:
163 BitField()
164 : _value( (TInt)0 )
165 {}
166
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
287 BitField operator~() const
288 { return ~_value; }
289
290 private:
291 TInt _value;
292 };
293
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
340
342} // namespace zypp
344#endif // ZYPP_BIT_H
bool
zypp::bit::BitField
An integral type used as BitField.
Definition Bit.h:160
zypp::bit::BitField::operator|=
BitField & operator|=(const BitField &rhs)
Definition Bit.h:275
zypp::bit::BitField::operator>>=
BitField & operator>>=(unsigned num)
Definition Bit.h:284
zypp::bit::BitField::testAnyOf
bool testAnyOf(TInt rhs)
Test whether at least one bit of rhs is set.
Definition Bit.h:261
zypp::bit::BitField::test
bool test(TInt rhs)
Test whether all bits of rhs are set.
Definition Bit.h:253
zypp::bit::BitField::operator==
bool operator==(const BitField< TInt > &lhs, const BitField< TInt > &rhs)
Definition Bit.h:304
zypp::bit::BitField::operator&
BitField< TInt > operator&(const BitField< TInt > &lhs, const BitField< TInt > &rhs)
Definition Bit.h:315
zypp::bit::BitField::assign
BitField & assign(TInt rhs)
Definition Bit.h:207
zypp::bit::BitField::BitField
BitField(const TInt &value_r)
Ctor taking an TInt.
Definition Bit.h:167
zypp::bit::BitField::set
BitField & set(TInt rhs, bool doset_r)
Set or unset bits of rhs.
Definition Bit.h:229
zypp::bit::BitField::assign
BitField & assign(TInt rhs)
Assign Range in rhs to this.
Definition Bit.h:201
zypp::bit::BitField::operator<<=
BitField & operator<<=(unsigned num)
Definition Bit.h:281
zypp::bit::BitField::operator~
BitField operator~() const
Definition Bit.h:287
zypp::bit::BitField::BitField
BitField()
Default ctor: zero.
Definition Bit.h:163
zypp::bit::BitField::operator<<
std::ostream & operator<<(std::ostream &str, const BitField< TInt > &obj)
Stream output.
Definition Bit.h:297
zypp::bit::BitField::operator!=
bool operator!=(const BitField< TInt > &lhs, const BitField< TInt > &rhs)
Definition Bit.h:309
zypp::bit::BitField::operator|
BitField< TInt > operator|(const BitField< TInt > &lhs, const BitField< TInt > &rhs)
Definition Bit.h:320
zypp::bit::BitField::value
TInt value() const
Return the value.
Definition Bit.h:179
zypp::bit::BitField::asString
std::string asString() const
Definition Bit.h:194
zypp::bit::BitField::unset
BitField & unset(TInt rhs)
Definition Bit.h:248
zypp::bit::BitField::operator^=
BitField & operator^=(const BitField &rhs)
Definition Bit.h:278
zypp::bit::BitField::operator^
BitField< TInt > operator^(const BitField< TInt > &lhs, const BitField< TInt > &rhs)
Definition Bit.h:325
zypp::bit::BitField::testAnyOf
bool testAnyOf(TInt rhs) const
Definition Bit.h:264
zypp::bit::BitField::set
BitField & set(TInt rhs)
Definition Bit.h:240
zypp::bit::BitField::operator&=
BitField & operator&=(const BitField &rhs)
Definition Bit.h:272
zypp::bit::BitField::operator=
BitField & operator=(const BitField &rhs)
Definition Bit.h:269
zypp::bit::BitField::set
BitField & set(TInt rhs)
Set bits of rhs.
Definition Bit.h:237
zypp::bit::BitField< uint64_t >::_value
uint64_t _value
Definition Bit.h:291
zypp::bit::BitField::operator>>
BitField< TInt > operator>>(const BitField< TInt > &lhs, unsigned num)
Definition Bit.h:335
zypp::bit::BitField::isEqual
bool isEqual(TInt rhs) const
Definition Bit.h:220
zypp::bit::BitField::set
BitField & set(TInt rhs, bool doset_r)
Definition Bit.h:232
zypp::bit::BitField::isEqual
bool isEqual(TInt rhs) const
Test for equal value within a Range.
Definition Bit.h:215
zypp::bit::BitField::asString
std::string asString() const
Value as bit string.
Definition Bit.h:190
zypp::bit::BitField::test
bool test(TInt rhs) const
Definition Bit.h:256
zypp::bit::BitField::value
TInt value() const
Definition Bit.h:183
zypp::bit::BitField::unset
BitField & unset(TInt rhs)
Unset bits of rhs.
Definition Bit.h:245
str
String related utilities and Regular expression matching.
zypp::bit::bit_detail
Definition Bit.h:32
zypp::bit
Definition Bit.h:29
zypp::bit::asString
std::string asString(TInt val, char zero='0', char one='1')
For printing bits.
Definition Bit.h:57
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zypp::bit::Mask
A bitmaks of _size 1-bits starting at bit _begin.
Definition Bit.h:73
zypp::bit::Mask< IntT, _begin, _size >::value
static const IntT value
Definition Bit.h:75
zypp::bit::Mask< IntT, _begin, _size >::inverted
static const IntT inverted
Definition Bit.h:76
zypp::bit::Mask::IntT
TInt IntT
Definition Bit.h:74
zypp::bit::MaxBits
Number of bits available in TInt.
Definition Bit.h:50
zypp::bit::MaxBits::IntT
TInt IntT
Definition Bit.h:51
zypp::bit::MaxBits< IntT >::value
static const unsigned value
Definition Bit.h:52
zypp::bit::RangeBit
A single 1-bit within a Range.
Definition Bit.h:129
zypp::bit::RangeBit::IntT
typename TRange::IntT IntT
Definition Bit.h:131
zypp::bit::RangeBit::value
static const IntT value
Definition Bit.h:133
zypp::bit::RangeBit::RangeT
TRange RangeT
Definition Bit.h:130
zypp::bit::RangeValue
A value with in a Range.
Definition Bit.h:111
zypp::bit::RangeValue::value
static const IntT value
Definition Bit.h:115
zypp::bit::RangeValue::RangeT
TRange RangeT
Definition Bit.h:112
zypp::bit::RangeValue::IntT
typename TRange::IntT IntT
Definition Bit.h:113
zypp::bit::Range
Range of bits starting at bit _begin with length _size.
Definition Bit.h:82
zypp::bit::Range< ModeBitsType, 0, 1 >::end
static const unsigned end
Definition Bit.h:89
zypp::bit::Range< ModeBitsType, 0, 1 >::begin
static const unsigned begin
Definition Bit.h:87
zypp::bit::Range< ModeBitsType, 0, 1 >::size
static const unsigned size
Definition Bit.h:88
zypp::bit::Range::MaxBits
zypp::bit::MaxBits< IntT > MaxBits
Definition Bit.h:84
zypp::bit::Range::Mask
zypp::bit::Mask< IntT, _begin, _size > Mask
Definition Bit.h:85
zypp::bit::Range::IntT
TInt IntT
Definition Bit.h:83
zypp::bit::bit_detail::Gen1Bits< TInt, 0 >::value
static const TInt value
Definition Bit.h:43
zypp::bit::bit_detail::Gen1Bits
Generate constants with _size trailing '1'-bits.
Definition Bit.h:36
zypp::bit::bit_detail::Gen1Bits::value
static const TInt value
Definition Bit.h:37