bitfield.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #if !defined(INCLUDED_GENERIC_BITFIELD_H)
00023 #define INCLUDED_GENERIC_BITFIELD_H
00024
00027
00035 template<typename Enumeration>
00036 class BitFieldValue : public Enumeration {
00037 unsigned m_value;
00038 protected:
00039 explicit BitFieldValue(unsigned value) : m_value(value) {
00040 }
00041 public:
00042 BitFieldValue() : m_value(0) {
00043 }
00044 explicit BitFieldValue(typename Enumeration::Value value) : m_value(1 << value) {
00045 }
00046 unsigned get() const {
00047 return m_value;
00048 }
00049 };
00050
00051 template<typename Enumeration>
00052 class BitFieldValueUnsafe : public BitFieldValue<Enumeration> {
00053 public:
00054 explicit BitFieldValueUnsafe(unsigned value) : BitFieldValue<Enumeration>(value) {
00055 }
00056 };
00057
00058 template<typename Enumeration>
00059 inline bool operator==(BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other) {
00060 return self.get() == other.get();
00061 }
00062 template<typename Enumeration>
00063 inline bool operator!=(BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other) {
00064 return !operator==(self, other);
00065 }
00066
00067 template<typename Enumeration>
00068 inline BitFieldValue<Enumeration> operator|(BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other) {
00069 return BitFieldValueUnsafe<Enumeration>(self.get() | other.get());
00070 }
00071 template<typename Enumeration>
00072 inline BitFieldValue<Enumeration>& operator|=(BitFieldValue<Enumeration>& self, BitFieldValue<Enumeration> other) {
00073 return self = self | other;
00074 }
00075 template<typename Enumeration>
00076 inline BitFieldValue<Enumeration> operator&(BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other) {
00077 return BitFieldValueUnsafe<Enumeration>(self.get() & other.get());
00078 }
00079 template<typename Enumeration>
00080 inline BitFieldValue<Enumeration>& operator&=(BitFieldValue<Enumeration>& self, BitFieldValue<Enumeration> other) {
00081 return self = self & other;
00082 }
00083 template<typename Enumeration>
00084 inline BitFieldValue<Enumeration> operator~(BitFieldValue<Enumeration> self) {
00085 return BitFieldValueUnsafe<Enumeration>(~self.get());
00086 }
00087
00088
00089
00090 inline unsigned int bitfield_enable(unsigned int bitfield, unsigned int mask) {
00091 return bitfield | mask;
00092 }
00093 inline unsigned int bitfield_disable(unsigned int bitfield, unsigned int mask) {
00094 return bitfield & ~mask;
00095 }
00096 inline bool bitfield_enabled(unsigned int bitfield, unsigned int mask) {
00097 return (bitfield & mask) != 0;
00098 }
00099
00100 template<typename Enumeration>
00101 inline BitFieldValue<Enumeration> bitfield_enable(BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask) {
00102 return bitfield | mask;
00103 }
00104 template<typename Enumeration>
00105 inline BitFieldValue<Enumeration> bitfield_disable(BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask) {
00106 return bitfield & ~mask;
00107 }
00108 template<typename Enumeration>
00109 inline bool bitfield_enabled(BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask) {
00110 return (bitfield & mask).get() != 0;
00111 }
00112
00113 #endif