]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/mediasource/extra/netradiant-src/libs/generic/bitfield.h
Rename the compiled fteqcc to fteqcc-win32 (as that's what it is)
[voretournament/voretournament.git] / misc / mediasource / extra / netradiant-src / 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   }
43 public:
44   BitFieldValue() : m_value(0)
45   {
46   }
47   explicit BitFieldValue(typename Enumeration::Value value) : m_value(1 << value)
48   {
49   }
50   unsigned get() const
51   {
52     return m_value;
53   }
54 };
55
56 template<typename Enumeration>
57 class BitFieldValueUnsafe : public BitFieldValue<Enumeration>
58 {
59 public:
60   explicit BitFieldValueUnsafe(unsigned value) : BitFieldValue<Enumeration>(value)
61   {
62   }
63 };
64
65 template<typename Enumeration>
66 inline bool operator==(BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other)
67 {
68   return self.get() == other.get();
69 }
70 template<typename Enumeration>
71 inline bool operator!=(BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other)
72 {
73   return !operator==(self, other);
74 }
75
76 template<typename Enumeration>
77 inline BitFieldValue<Enumeration> operator|(BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other)
78 {
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 {
84   return self = self | other;
85 }
86 template<typename Enumeration>
87 inline BitFieldValue<Enumeration> operator&(BitFieldValue<Enumeration> self, BitFieldValue<Enumeration> other)
88 {
89   return BitFieldValueUnsafe<Enumeration>(self.get() & other.get());
90 }
91 template<typename Enumeration>
92 inline BitFieldValue<Enumeration>& operator&=(BitFieldValue<Enumeration>& self, BitFieldValue<Enumeration> other)
93 {
94   return self = self & other;
95 }
96 template<typename Enumeration>
97 inline BitFieldValue<Enumeration> operator~(BitFieldValue<Enumeration> self)
98 {
99   return BitFieldValueUnsafe<Enumeration>(~self.get());
100 }
101
102
103
104 inline unsigned int bitfield_enable(unsigned int bitfield, unsigned int mask)
105 {
106   return bitfield | mask;
107 }
108 inline unsigned int bitfield_disable(unsigned int bitfield, unsigned int mask)
109 {
110   return bitfield & ~mask;
111 }
112 inline bool bitfield_enabled(unsigned int bitfield, unsigned int mask)
113 {
114   return (bitfield & mask) != 0;
115 }
116
117 template<typename Enumeration>
118 inline BitFieldValue<Enumeration> bitfield_enable(BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask)
119 {
120   return bitfield | mask;
121 }
122 template<typename Enumeration>
123 inline BitFieldValue<Enumeration> bitfield_disable(BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask)
124 {
125   return bitfield & ~mask;
126 }
127 template<typename Enumeration>
128 inline bool bitfield_enabled(BitFieldValue<Enumeration> bitfield, BitFieldValue<Enumeration> mask)
129 {
130   return (bitfield & mask).get() != 0;
131 }
132
133 #endif