#ifndef _utl_Bit_h_ #define _utl_Bit_h_ /** \author Hans Dembinski \author Lukas Nellen \author Darko Veberic \date 27 Jan 2014 \version $Id$ */ #include namespace utl { namespace Bit { template class Array { public: Array(T& target) : fTarget(target) { } class Bit { public: Bit(T& target, T mask) : fTarget(target), fMask(mask) { } operator bool() const { return fTarget & fMask; } bool operator~() const { return !bool(*this); } Bit& operator=(const bool value) { if (value) fTarget |= fMask; else fTarget &= ~fMask; return *this; } Bit& Flip() { return *this = ~(*this); } private: T& fTarget; T fMask; }; Bit operator[](unsigned int position) { return Bit(fTarget, T(1) << position); } Bit At(unsigned int position) { if (position >= 8*sizeof(T)) throw OutOfBoundException("Running out of bits."); return (*this)[position]; } template Array& Mask(const M mask, const bool value) { Bit(fTarget, mask) = value; return *this; } template T Get(const M mask) { return fTarget & T(mask); } private: T& fTarget; }; } // helper template inline Bit::Array AsBitArray(T& target) { return Bit::Array(target); } } #endif