1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
18 ///////////////////////////////////////////////////////////////////
20 { /////////////////////////////////////////////////////////////////
21 ///////////////////////////////////////////////////////////////////
23 * \todo Use boost::mpl library to assert constraints
24 * at compiletime! There various like (_IntT is an integral type)
25 * (begin+size < maxbits) or ( field dependent
26 * constants must be within the range defined by size ).
29 { /////////////////////////////////////////////////////////////////
33 /** Generate constants with \a _size trailing '1'-bits */
34 template<class _IntT, unsigned _size>
37 static const _IntT value = (Gen1Bits<_IntT,_size-1>::value << 1)+1;
39 /** Specialization for \a _length 0 */
41 struct Gen1Bits<_IntT, 0>
43 static const _IntT value = 0;
47 /** Number of bits available in \a _IntT. */
52 static const unsigned value = (sizeof(IntT)*8);
55 /** For printing bits. */
57 inline std::string asString( _IntT val, char zero = '0', char one = '1' )
59 std::string s( MaxBits<_IntT>::value, zero );
60 for( unsigned i = MaxBits<_IntT>::value; i; )
70 /** A bitmaks of \a _size 1-bits starting at bit \a _begin. */
71 template<class _IntT, unsigned _begin, unsigned _size>
75 static const IntT value = bit_detail::Gen1Bits<IntT,_size>::value << _begin;
76 static const IntT inverted = ~value;
79 /** Range of bits starting at bit \a _begin with length \a _size. */
80 template<class _IntT, unsigned _begin, unsigned _size>
84 typedef zypp::bit::MaxBits<IntT> MaxBits;
85 typedef zypp::bit::Mask<IntT,_begin,_size> Mask;
87 static const unsigned begin = _begin;
88 static const unsigned size = _size;
89 static const unsigned end = _begin + _size;
91 /** Range specialisation for (illegal) zero \a _size.
92 * Force error at compiletime. Currently because types
93 * and values are undefined
95 template<class _IntT, unsigned _begin>
96 struct Range<_IntT, _begin, 0>
99 /** A value with in a Range.
101 * typedef Range<char,2,3> SubField; // bits 2,3,4 in a char field
102 * SubField::Mask::value; // 00011100
103 * RangeValue<SubField,0>::value; // 00000000
104 * RangeValue<SubField,1>::value; // 00000100
105 * RangeValue<SubField,2>::value; // 00001000
106 * RangeValue<SubField,3>::value; // 00001100
109 template<class _Range, typename _Range::IntT _value>
112 typedef _Range RangeT;
113 typedef typename _Range::IntT IntT;
115 static const IntT value = _value << RangeT::begin;
118 /** A single 1-bit within a Range.
120 * typedef Range<char,2,3> SubField; // bits 2,3,4 in a char field
121 * SubField::Mask::value; // 00011100
122 * RangeBit<SubField,0>::value; // 00000100
123 * RangeBit<SubField,1>::value; // 00001000
124 * RangeBit<SubField,2>::value; // 00010000
127 template<class _Range, unsigned _pos>
130 typedef _Range RangeT;
131 typedef typename _Range::IntT IntT;
133 static const IntT value = IntT(1) << (RangeT::begin + _pos);
136 ///////////////////////////////////////////////////////////////////
138 // CLASS NAME : BitField
140 /** An integral type used as BitField.
142 * Most methods exist as templated and nontemplated
143 * version. The nontemplated operates on the complete
144 * BitField, while the tamplated ones are restricted
145 * to the given Range.
147 * BitField<char> bf; // 00000000
148 * typedef Range<char,2,3> SubField; // bits 2,3,4 in a char field
150 * bf<SubField>.assign( -1 ); // assign SubField in -1
151 * // to SubField in bf.
153 * bf.assign( -1 ); // assign -1 to bf
155 * bf<SubField>.assign( 0 ); // 11100011
158 template<class _IntT>
159 class BitField : public Range<_IntT, 0, MaxBits<_IntT>::value>
162 /** Default ctor: zero. */
166 /** Ctor taking an \a _IntT. */
167 BitField( const _IntT & value_r )
172 /** Validate in a boolean context. */
173 explicit operator bool() const
174 { return _value != (_IntT)0; }
177 /** Return the value. */
178 template<class _Range>
181 return _value & _Range::Mask::value;
188 /** Value as bit string. */
189 template<class _Range>
190 std::string asString() const
192 return bit::asString( _value & _Range::Mask::value, '_' );
194 std::string asString() const
196 return bit::asString( _value, '_' );
199 /** Assign Range in \a rhs to \c this. */
200 template<class _Range>
201 BitField & assign( _IntT rhs )
203 _value = (_value & _Range::Mask::inverted)
204 | (rhs & _Range::Mask::value);
207 BitField & assign( _IntT rhs )
213 /** Test for equal value within a Range. */
214 template<class _Range>
215 bool isEqual( _IntT rhs ) const
217 return (_value & _Range::Mask::value)
218 == (rhs & _Range::Mask::value);
220 bool isEqual( _IntT rhs ) const
222 return _value == rhs;
227 /** Set or unset bits of \a rhs. */
228 template<class _Range>
229 BitField & set( _IntT rhs, bool doset_r )
230 { return set( (rhs & _Range::Mask::value), doset_r ); }
232 BitField & set( _IntT rhs, bool doset_r )
233 { return doset_r ? set( rhs ) : unset( rhs ); }
235 /** Set bits of \a rhs. */
236 template<class _Range>
237 BitField & set( _IntT rhs )
238 { return set( rhs & _Range::Mask::value ); }
240 BitField & set( _IntT rhs )
241 { _value |= rhs; return *this; }
243 /** Unset bits of \a rhs. */
244 template<class _Range>
245 BitField & unset( _IntT rhs )
246 { return unset( rhs & _Range::Mask::value ); }
248 BitField & unset( _IntT rhs )
249 { _value &= ~rhs; return *this; }
251 /** Test whether \b all bits of \a rhs are set. */
252 template<class _Range>
253 bool test( _IntT rhs )
254 { return test( rhs & _Range::Mask::value ); }
256 bool test( _IntT rhs ) const
257 { return (_value & rhs) == rhs; }
259 /** Test whether \b at \b least \b one bit of \a rhs is set. */
260 template<class _Range>
261 bool testAnyOf( _IntT rhs )
262 { return testAnyOf( rhs & _Range::Mask::value ); }
264 bool testAnyOf( _IntT rhs ) const
265 { return (_value & rhs); }
269 BitField & operator=( const BitField & rhs )
270 { _value = rhs._value; return *this; }
272 BitField & operator&=( const BitField & rhs )
273 { _value &= rhs._value; return *this; }
275 BitField & operator|=( const BitField & rhs )
276 { _value |= rhs._value; return *this; }
278 BitField & operator^=( const BitField & rhs )
279 { _value ^= rhs._value; return *this; }
281 BitField & operator<<=( unsigned num )
282 { _value <<= num; return *this; }
284 BitField & operator>>=( unsigned num )
285 { _value >>= num; return *this; }
287 BitField operator~() const
293 ///////////////////////////////////////////////////////////////////
295 /** \relates BitField Stream output */
296 template<class _IntT>
297 std::ostream & operator<<( std::ostream & str, const BitField<_IntT> & obj )
299 return str << obj.asString();
302 /** \relates BitField */
303 template<class _IntT>
304 inline bool operator==( const BitField<_IntT> & lhs, const BitField<_IntT> & rhs )
305 { return lhs.value() == rhs.value(); }
307 /** \relates BitField */
308 template<class _IntT>
309 inline bool operator!=( const BitField<_IntT> & lhs, const BitField<_IntT> & rhs )
310 { return ! (lhs == rhs); }
313 /** \relates BitField */
314 template<class _IntT>
315 inline BitField<_IntT> operator&( const BitField<_IntT> & lhs, const BitField<_IntT> & rhs )
316 { return BitField<_IntT>(lhs) &= rhs; }
318 /** \relates BitField */
319 template<class _IntT>
320 inline BitField<_IntT> operator|( const BitField<_IntT> & lhs, const BitField<_IntT> & rhs )
321 { return BitField<_IntT>(lhs) |= rhs; }
323 /** \relates BitField */
324 template<class _IntT>
325 inline BitField<_IntT> operator^( const BitField<_IntT> & lhs, const BitField<_IntT> & rhs )
326 { return BitField<_IntT>(lhs) ^= rhs; }
328 /** \relates BitField */
329 template<class _IntT>
330 inline BitField<_IntT> operator<<( const BitField<_IntT> & lhs, unsigned num )
331 { return BitField<_IntT>(lhs) <<= num; }
333 /** \relates BitField */
334 template<class _IntT>
335 inline BitField<_IntT> operator>>( const BitField<_IntT> & lhs, unsigned num )
336 { return BitField<_IntT>(lhs) >>= num; }
338 /////////////////////////////////////////////////////////////////
340 ///////////////////////////////////////////////////////////////////
341 /////////////////////////////////////////////////////////////////
343 ///////////////////////////////////////////////////////////////////