All pastes #489417 Raw Edit

spam

public cpp v1 · immutable
·published 2007-05-15 15:08 UTC
rendered paste body
#include <assert.h>template <class E, class S = int>struct Flags{      typedef E EnumType;      typedef S StorageType;      StorageType value;      struct Bitset      {            StorageType value;            Bitset()            {                  value = 0;            }            Bitset(EnumType T)            {                  value = 1 << (int)T;            }      };            Flags() : value(0)      {      }      Flags(Bitset A) : value(A.value)      {      }};template <class E>bool operator&(Flags<E> flags, typename Flags<E>::Bitset bits){      return (flags.value & bits.value) != 0; }template <class E>bool operator==(Flags<E> flags, typename Flags<E>::Bitset bits){      return flags.value == bits.value; }template <class E>typename Flags<E>::Bitset operator|(typename Flags<E>::Bitset B, E A){      B.value |= 1 << A;      return B; }template <class E>typename Flags<E>::Bitset operator|(E A, E B){      return typename Flags<E>::Bitset(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)));}