Program Listing for File flags.hpp

Return to documentation for file (/tmp/ws/src/ecl_core/ecl_utilities/include/ecl/utilities/flags.hpp)

/*****************************************************************************
 ** Ifdefs
 *****************************************************************************/

#ifndef ECL_UTILITIES_FLAGS_HPP_
#define ECL_UTILITIES_FLAGS_HPP_

/*****************************************************************************
 ** Includes
 *****************************************************************************/

/*****************************************************************************
 ** Namespaces
 *****************************************************************************/

namespace ecl
{

/*****************************************************************************
 ** Classes
 *****************************************************************************/

enum Bits
{
  Bit0 =  0x0000, Bit1 =  0x0001, Bit2 =  0x0002, Bit3 =  0x0004, Bit4 =  0x0008, Bit5 =  0x0010, Bit6 =  0x0020,
  Bit7 =  0x0040, Bit8 =  0x0080, Bit9 =  0x0100, Bit10 = 0x0200, Bit11 = 0x0400, Bit12 = 0x0800, Bit13 = 0x1000,
  Bit14 = 0x2000, Bit15 = 0x4000, Bit16 = 0x8000,
};
template<typename Enum>
  class Flags
  {
  public:
    /*********************
     ** C&D
     **********************/
    Flags() : value(0) {};
    Flags(const Flags<Enum> & other) : value(other.value) {};
    Flags(Enum flag) : value(flag) {};
    ~Flags() { }
    ;

    /*********************
     ** Utility
     **********************/
    void clear() { value = 0; }
    bool testFlag(Enum flag) const
    {
      if ((value & flag) != 0)
      {
        return true;
      }
      else
      {
        if ( ( flag == 0 ) && ( value == 0 ) ) {
          return true;
        } else {
          return false;
        }
      }
    }
    ;

    /*********************
     ** Type compatibility
     **********************/
    operator int() const
    {
      return value;
    }
    ;
    /******************************************
     ** Return new instance
     *******************************************/
    Flags<Enum> operator&(int mask) const
    {
      Flags<Enum> f;
      f.value = value & mask;
      return f;
    }
    ;
    Flags<Enum> operator&(Enum flag) const
    {
      Flags<Enum> f;
      f.value = value & flag;
      return f;
    }
    ;
    Flags<Enum> operator|(Flags<Enum> other) const
    {
      Flags<Enum> f;
      f.value = value | other.value;
      return f;
    }
    ;
    Flags<Enum> operator|(Enum flag) const
    {
      Flags<Enum> f;
      f.value = value | flag;
      return f;
    }
    ;
    Flags<Enum> operator^(Flags<Enum> other) const
    {
      Flags<Enum> f;
      f.value = value ^ other.value;
      return f;
    }
    ;
    Flags<Enum> operator^(Enum flag) const
    {
      Flags<Enum> f;
      f.value = value ^ flag;
      return f;
    }
    ;
    Flags<Enum> operator~() const
    {
      Flags<Enum> f;
      f.value = ~value;
      return f;
    }
    ;

    /******************************************
     ** Update current instance
     *******************************************/
    Flags<Enum> & operator=(const Flags<Enum> & other)
    {
      value = other.value;
      return *this;
    }
    ;
    Flags<Enum> & operator=(const Enum & flag)
    {
      value = static_cast<int>(flag);
      return *this;
    }
    ;
    Flags<Enum> & operator&=(int mask)
    {
      value &= mask;
      return *this;
    }
    ;
    Flags<Enum> & operator|=(Flags<Enum> other)
    {
      value |= other;
      return *this;
    }
    ;
    Flags<Enum> & operator|=(Enum flag)
    {
      value |= flag;
      return *this;
    }
    ;
    Flags<Enum> & operator^=(Flags<Enum> other)
    {
      value ^= other;
      return *this;
    }
    ;
    Flags<Enum> & operator^=(Enum flag)
    {
      value ^= flag;
      return *this;
    }
    ;

    /*********************
     ** Helper Methods
     **********************/
    friend inline Flags<Enum> operator|(Enum flag, Flags<Enum> flags)
    {
      return flags | flag;
    }
  private:
    int value;
  };

} // namespace ecl

#endif /* ECL_UTILITIES_FLAGS_HPP_ */