]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/generic/bitfield.h
Merge remote-tracking branch 'github/master'
[xonotic/netradiant.git] / libs / generic / bitfield.h
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #if !defined( INCLUDED_GENERIC_BITFIELD_H )
23 #define INCLUDED_GENERIC_BITFIELD_H
24
25 /// \file
26 /// \brief Type safe bitfield.
27
28 /// \brief A bit-field value.
29 ///
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.
33 ///
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
37 {
38 unsigned m_value;
39 protected:
40 explicit BitFieldValue( unsigned value ) : m_value( value ){
41 }
42 public:
43 BitFieldValue() : m_value( 0 ){
44 }
45 explicit BitFieldValue( typename Enumeration::Value value ) : m_value( 1 << value ){
46 }
47 unsigned get() const {
48         return m_value;
49 }
50 };
51
52 template<typename Enumeration>
53 class BitFieldValueUnsafe : public BitFieldValue<Enumeration>
54 {
55 public:
56 explicit BitFieldValueUnsafe( unsigned value ) : BitFieldValue<Enumeration>( value ){
57 }
58 };
59
60 template<typename Enumeration>
61 inline bool operator==( BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other ){
62         return self.get() == other.get();
63 }
64 template<typename Enumeration>
65 inline bool operator!=( BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other ){
66         return !operator==( self, other );
67 }
68
69 template<typename Enumeration>
70 inline BitFieldValue<Enumeration> operator|( BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other ){
71         return BitFieldValueUnsafe<Enumeration>( self.get() | other.get() );
72 }
73 template<typename Enumeration>
74 inline BitFieldValue<Enumeration>& operator|=( BitFieldValue<Enumeration>& self, BitFieldValue<Enumeration> other ){
75         return self = self | other;
76 }
77 template<typename Enumeration>
78 inline BitFieldValue<Enumeration> operator&( BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other ){
79         return BitFieldValueUnsafe<Enumeration>( self.get() & other.get() );
80 }
81 template<typename Enumeration>
82 inline BitFieldValue<Enumeration>& operator&=( BitFieldValue<Enumeration>& self, BitFieldValue<Enumeration> other ){
83         return self = self & other;
84 }
85 template<typename Enumeration>
86 inline BitFieldValue<Enumeration> operator~( BitFieldValue<Enumeration> self ){
87         return BitFieldValueUnsafe<Enumeration>( ~self.get() );
88 }
89
90
91
92 inline unsigned int bitfield_enable( unsigned int bitfield, unsigned int mask ){
93         return bitfield | mask;
94 }
95 inline unsigned int bitfield_disable( unsigned int bitfield, unsigned int mask ){
96         return bitfield & ~mask;
97 }
98 inline bool bitfield_enabled( unsigned int bitfield, unsigned int mask ){
99         return ( bitfield & mask ) != 0;
100 }
101
102 template<typename Enumeration>
103 inline BitFieldValue<Enumeration> bitfield_enable( BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask ){
104         return bitfield | mask;
105 }
106 template<typename Enumeration>
107 inline BitFieldValue<Enumeration> bitfield_disable( BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask ){
108         return bitfield & ~mask;
109 }
110 template<typename Enumeration>
111 inline bool bitfield_enabled( BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask ){
112         return ( bitfield & mask ).get() != 0;
113 }
114
115 #endif