2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined(INCLUDED_GENERIC_BITFIELD_H)
23 #define INCLUDED_GENERIC_BITFIELD_H
26 /// \brief Type safe bitfield.
28 /// \brief A bit-field value.
30 /// - Can be forward-declared when the definition of Enumeration is unknown.
31 /// - Can only be constructed from valid enumerated values.
32 /// - Can only be compared and combined with others of the same type.
34 /// \param Enumeration A type that contains an enum \c Value of the bits that can be set in this field.
35 template<typename Enumeration>
36 class BitFieldValue : public Enumeration
40 explicit BitFieldValue(unsigned value) : m_value(value)
44 BitFieldValue() : m_value(0)
47 explicit BitFieldValue(typename Enumeration::Value value) : m_value(1 << value)
56 template<typename Enumeration>
57 class BitFieldValueUnsafe : public BitFieldValue<Enumeration>
60 explicit BitFieldValueUnsafe(unsigned value) : BitFieldValue<Enumeration>(value)
65 template<typename Enumeration>
66 inline bool operator==(BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other)
68 return self.get() == other.get();
70 template<typename Enumeration>
71 inline bool operator!=(BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other)
73 return !operator==(self, other);
76 template<typename Enumeration>
77 inline BitFieldValue<Enumeration> operator|(BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other)
79 return BitFieldValueUnsafe<Enumeration>(self.get() | other.get());
81 template<typename Enumeration>
82 inline BitFieldValue<Enumeration>& operator|=(BitFieldValue<Enumeration>& self, BitFieldValue<Enumeration> other)
84 return self = self | other;
86 template<typename Enumeration>
87 inline BitFieldValue<Enumeration> operator&(BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other)
89 return BitFieldValueUnsafe<Enumeration>(self.get() & other.get());
91 template<typename Enumeration>
92 inline BitFieldValue<Enumeration>& operator&=(BitFieldValue<Enumeration>& self, BitFieldValue<Enumeration> other)
94 return self = self & other;
96 template<typename Enumeration>
97 inline BitFieldValue<Enumeration> operator~(BitFieldValue<Enumeration> self)
99 return BitFieldValueUnsafe<Enumeration>(~self.get());
104 inline unsigned int bitfield_enable(unsigned int bitfield, unsigned int mask)
106 return bitfield | mask;
108 inline unsigned int bitfield_disable(unsigned int bitfield, unsigned int mask)
110 return bitfield & ~mask;
112 inline bool bitfield_enabled(unsigned int bitfield, unsigned int mask)
114 return (bitfield & mask) != 0;
117 template<typename Enumeration>
118 inline BitFieldValue<Enumeration> bitfield_enable(BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask)
120 return bitfield | mask;
122 template<typename Enumeration>
123 inline BitFieldValue<Enumeration> bitfield_disable(BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask)
125 return bitfield & ~mask;
127 template<typename Enumeration>
128 inline bool bitfield_enabled(BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask)
130 return (bitfield & mask).get() != 0;