Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / zypp / Bit.h
index fc48997..a08a2b1 100644 (file)
@@ -15,8 +15,6 @@
 #include <iosfwd>
 #include <string>
 
-#include "zypp/base/SafeBool.h"
-
 ///////////////////////////////////////////////////////////////////
 namespace zypp
 { /////////////////////////////////////////////////////////////////
@@ -48,7 +46,7 @@ namespace zypp
 
     /** Number of bits available in \a _IntT. */
     template<class _IntT>
-      struct MaxBitsT
+      struct MaxBits
       {
         typedef _IntT IntT;
         static const unsigned value = (sizeof(IntT)*8);
@@ -58,8 +56,8 @@ namespace zypp
     template<class _IntT>
       inline std::string asString( _IntT val, char zero = '0', char one = '1' )
       {
-        std::string s( MaxBitsT<_IntT>::value, zero );
-        for( unsigned i = MaxBitsT<_IntT>::value; i; )
+        std::string s( MaxBits<_IntT>::value, zero );
+        for( unsigned i = MaxBits<_IntT>::value; i; )
           {
             --i;
             if ( val & (_IntT)1 )
@@ -71,7 +69,7 @@ namespace zypp
 
     /** A bitmaks of \a _size 1-bits starting at bit \a _begin. */
     template<class _IntT, unsigned _begin, unsigned _size>
-      struct MaskT
+      struct Mask
       {
         typedef _IntT IntT;
         static const IntT value    = bit_detail::Gen1Bits<IntT,_size>::value << _begin;
@@ -83,8 +81,8 @@ namespace zypp
       struct Range
       {
         typedef _IntT IntT;
-        typedef MaxBitsT<IntT>          MaxBits;
-        typedef MaskT<IntT,_begin,_size> Mask;
+        typedef zypp::bit::MaxBits<IntT>           MaxBits;
+        typedef zypp::bit::Mask<IntT,_begin,_size> Mask;
 
         static const unsigned begin  = _begin;
         static const unsigned size   = _size;
@@ -158,11 +156,8 @@ namespace zypp
      * \endcode
     */
     template<class _IntT>
-      class BitField  : public Range<_IntT, 0, MaxBitsT<_IntT>::value>
-                      , private base::SafeBool<BitField<_IntT> >
+      class BitField  : public Range<_IntT, 0, MaxBits<_IntT>::value>
       {
-        typedef typename base::SafeBool<BitField<_IntT> >::bool_type bool_type;
-
       public:
         /** Default ctor: zero. */
         BitField()
@@ -175,7 +170,8 @@ namespace zypp
 
       public:
         /** Validate in a boolean context. */
-        using base::SafeBool<BitField<_IntT> >::operator bool_type;
+        explicit operator bool() const
+        { return _value != (_IntT)0; }
 
       public:
         /** Return the value. */
@@ -225,6 +221,49 @@ namespace zypp
         {
           return _value == rhs;
         }
+
+       public:
+
+         /** Set or unset bits of \a rhs. */
+        template<class _Range>
+            BitField & set( _IntT rhs, bool doset_r )
+            { return set( (rhs & _Range::Mask::value), doset_r ); }
+
+        BitField & set( _IntT rhs, bool doset_r )
+        { return doset_r ? set( rhs ) : unset( rhs ); }
+
+        /** Set bits of \a rhs. */
+        template<class _Range>
+            BitField & set( _IntT rhs )
+            { return set( rhs & _Range::Mask::value ); }
+
+        BitField & set( _IntT rhs )
+        { _value |= rhs; return *this; }
+
+        /** Unset bits of \a rhs. */
+        template<class _Range>
+            BitField & unset( _IntT rhs )
+            { return unset( rhs & _Range::Mask::value ); }
+
+        BitField & unset( _IntT rhs )
+        { _value &= ~rhs; return *this; }
+
+        /** Test whether \b all bits of \a rhs are set. */
+        template<class _Range>
+            bool test( _IntT rhs )
+            { return test( rhs & _Range::Mask::value ); }
+
+        bool test( _IntT rhs ) const
+        { return (_value & rhs) == rhs; }
+
+        /** Test whether \b at \b least \b one bit of \a rhs is set. */
+        template<class _Range>
+            bool testAnyOf( _IntT rhs )
+            { return testAnyOf( rhs & _Range::Mask::value ); }
+
+        bool testAnyOf( _IntT rhs ) const
+        { return (_value & rhs); }
+
       public:
 
         BitField & operator=( const BitField & rhs )
@@ -249,12 +288,6 @@ namespace zypp
         { return ~_value; }
 
       private:
-        friend base::SafeBool<BitField<_IntT> >::operator bool_type() const;
-        /** \ref base::SafeBool test. */
-        bool boolTest() const
-        { return _value; }
-
-      private:
         _IntT _value;
       };
     ///////////////////////////////////////////////////////////////////