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 ){
43 BitFieldValue() : m_value( 0 ){
45 explicit BitFieldValue( typename Enumeration::Value value ) : m_value( 1 << value ){
47 unsigned get() const {
52 template<typename Enumeration>
53 class BitFieldValueUnsafe : public BitFieldValue<Enumeration>
56 explicit BitFieldValueUnsafe( unsigned value ) : BitFieldValue<Enumeration>( value ){
60 template<typename Enumeration>
61 inline bool operator==( BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other ){
62 return self.get() == other.get();
64 template<typename Enumeration>
65 inline bool operator!=( BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other ){
66 return !operator==( self, other );
69 template<typename Enumeration>
70 inline BitFieldValue<Enumeration> operator|( BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other ){
71 return BitFieldValueUnsafe<Enumeration>( self.get() | other.get() );
73 template<typename Enumeration>
74 inline BitFieldValue<Enumeration>& operator|=( BitFieldValue<Enumeration>& self, BitFieldValue<Enumeration> other ){
75 return self = self | other;
77 template<typename Enumeration>
78 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 ){
83 return self = self & other;
85 template<typename Enumeration>
86 inline BitFieldValue<Enumeration> operator~( BitFieldValue<Enumeration> self ){
87 return BitFieldValueUnsafe<Enumeration>( ~self.get() );
92 inline unsigned int bitfield_enable( unsigned int bitfield, unsigned int mask ){
93 return bitfield | mask;
95 inline unsigned int bitfield_disable( unsigned int bitfield, unsigned int mask ){
96 return bitfield & ~mask;
98 inline bool bitfield_enabled( unsigned int bitfield, unsigned int mask ){
99 return ( bitfield & mask ) != 0;
102 template<typename Enumeration>
103 inline BitFieldValue<Enumeration> bitfield_enable( BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask ){
104 return bitfield | mask;
106 template<typename Enumeration>
107 inline BitFieldValue<Enumeration> bitfield_disable( BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask ){
108 return bitfield & ~mask;
110 template<typename Enumeration>
111 inline bool bitfield_enabled( BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask ){
112 return ( bitfield & mask ).get() != 0;