File: BitwiseEnum.h

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (197 lines) | stat: -rwxr-xr-x 3,692 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef BITWISEENUM_H
#define BITWISEENUM_H

/**
 * This template is loosely based on MiLi's bitwise_enums.h written by Daniel Gutson (FuDePAN).
 * Their implementation is licensed under GPLv3, so we had to rewrite it. Still all kudos should go to them.
 *
 * URL of the MiLi project: http://code.google.com/p/mili/
 **/


/**
 * @brief BitwiseEnum<class Enum>
 *
 * example usage:
 *  enum Colors {red, green, blue};
 *  typedef BitwiseEnum<Colors> Color;
 *  enum Vehicles {car, bus, train};
 *  void foo(Color c);
 *  Color c = Colors::red | Colors::green;
 *
 *  foo(c);                            // valid
 *  foo(Colors::red);                  // valid
 *  foo(Colors::red | Colors::green);  // valid
 *  foo(Vehicles::car);                // not valid
 *  foo(1);                            // not valid
 **/
template <class Enum>
class BitwiseEnum
{
private:
	int v;

public:
	BitwiseEnum(const Enum& _v) : v(_v) {}
	BitwiseEnum() : v(0) {}

	//! fixes http://code.google.com/p/mili/issues/detail?id=40
	operator int() const { return v; }


	// We use c++ default ones
	/*BitwiseEnum<Enum>& operator = (const Enum& v2)
	{
		return v = v2;
	}

	BitwiseEnum<Enum>& operator = (const BitwiseEnum<Enum>& be2)
	{
		return v = be2.v;
	}*/


	//===================================
	// BitwiseEnum<Enum> operators

	// Compound assignment operators
	BitwiseEnum<Enum>& operator |= (const BitwiseEnum<Enum>& be2)
	{
		v |= be2.v;
		return *this;
	}

	BitwiseEnum<Enum>& operator &= (const BitwiseEnum<Enum>& be2)
	{
		v &= be2.v;
		return *this;
	}

	BitwiseEnum<Enum>& operator ^= (const BitwiseEnum<Enum>& be2)
	{
		v ^= be2.v;
		return *this;
	}


	// Bitwise operators
	BitwiseEnum<Enum> operator | (const BitwiseEnum<Enum>& be2) const
	{
		BitwiseEnum<Enum> be(*this);
		be.v |= be2.v;
		return be;
	}

	BitwiseEnum<Enum> operator & (const BitwiseEnum<Enum>& be2) const
	{
		BitwiseEnum<Enum> be(*this);
		be.v &= be2.v;
		return be;
	}

	BitwiseEnum<Enum> operator ^ (const BitwiseEnum<Enum>& be2) const
	{
		BitwiseEnum<Enum> be(*this);
		be.v ^= be2.v;
		return be;
	}


	// Comparison operators
	bool operator == (const BitwiseEnum<Enum>& be2) const
	{
		return v == be2.v;
	}

	bool operator != (const BitwiseEnum<Enum>& be2) const
	{
		return v != be2.v;
	}


	//===================================
	// Enum operators

	// Compound assignment operators
	BitwiseEnum<Enum>& operator |= (const Enum& v2)
	{
		v |= v2;
		return *this;
	}

	BitwiseEnum<Enum>& operator &= (const Enum& v2)
	{
		v &= v2;
		return *this;
	}

	BitwiseEnum<Enum>& operator ^= (const Enum& v2)
	{
		v ^= v2;
		return *this;
	}


	// Bitwise operators
	BitwiseEnum<Enum> operator | (const Enum& v) const
	{
		BitwiseEnum<Enum> be(*this);
		be.v |= v;
		return be;
	}

	BitwiseEnum<Enum> operator & (const Enum& v) const
	{
		BitwiseEnum<Enum> be(*this);
		be.v &= v;
		return be;
	}

	BitwiseEnum<Enum> operator ^ (const Enum& v) const
	{
		BitwiseEnum<Enum> be(*this);
		be.v ^= v;
		return be;
	}


	// Comparison operators
	bool operator == (const Enum& v2) const
	{
		return v == v2;
	}

	bool operator != (const Enum& v2) const
	{
		return v != v2;
	}
};


template <class Enum>
inline BitwiseEnum<Enum> operator | (Enum a, Enum b)
{
	return BitwiseEnum<Enum>(a) | BitwiseEnum<Enum>(b);
}



/* WE DO NOT NEED THESE (I ASSUME)

template <class Enum>
inline BitwiseEnum<Enum> operator & (Enum a, Enum b)
{
	return BitwiseEnum<Enum>(a) | BitwiseEnum<Enum>(b);
}

template <class Enum>
inline BitwiseEnum<Enum> operator ^ (Enum a, Enum b)
{
	return BitwiseEnum<Enum>(a) | BitwiseEnum<Enum>(b);
}

*/

#endif // BITWISEENUM_H