All pastes #489485 Raw Edit

spam

public cpp v1 · immutable
·published 2007-05-15 15:41 UTC
rendered paste body
#include <assert.h>template <class E, class S = int>struct Flags{	typedef E EnumType;	typedef S StorageType;	StorageType value;	Flags() : value(0)	{	}	Flags(EnumType T)	{		value = 1 << (int)T;	}	Flags<E,S> &operator|=(Flags<E,S> X)	{		value |= X.value;		return *this;	}	Flags<E,S> &operator&=(Flags<E,S> X)	{		value &= X.value;		return *this;	}	operator bool()	{		return value != 0;	}	Flags<E> operator~()	{		Flags<E> T;		T.value = ~value;		return T;	}};template <class E>Flags<E> operator&(Flags<E> A, Flags<E> B){	Flags<E> R;	R.value = A.value & B.value;	return R;}template <class E>bool operator&(Flags<E> A, E B){      return (A.value & (1 << B)) != 0; }template <class E>bool operator==(Flags<E> A, Flags<E> B){      return A.value == B.value; }template <class E>typename Flags<E> operator|(Flags<E> A, E B){      A.value |= 1 << B;      return A;}template <class E>typename Flags<E> operator|(E A, E B){      return Flags<E>(A) | B; }enum Enum{      One, Two, Three, Four, Five};void main(){      Flags<Enum> flags(Two | Three);      assert(flags & Three);      assert(flags & Two);      assert(flags & (One | Two));      assert(flags & (One | Three));      assert(flags & (One | Two | Three));      assert(!(flags & One));      assert(!(flags == (Two | One)));      assert(flags == (Two | Three));	  flags = One | Five;	  assert(flags & One);	  assert(flags & Five);	  assert(!(flags & (Two | Three | Four)));	  flags = Two | Three;	  flags &= Two;	  assert(flags == Two);	  flags |= Four;	  assert(flags & Two);	  assert(flags & Four);	  //flags &= ~Two;		// compile-time error.	  //assert(flags == Four);}