@@ -159,13 +159,53 @@ template <class L, class R> struct IF<true, L, R> { typedef L type; };
159
159
#define FI FORCE_INLINE
160
160
161
161
// Define types based on largest bit width stored value required
162
- #define bits_t (W ) typename IF<((W)> 16 ), uint32_t , typename IF<((W)> 8 ), uint16_t , uint8_t >::type>::type
162
+ #define bits_t (W ) typename IF<((W)>32 ), uint64_t , typename IF<((W)> 16 ), uint32_t , typename IF<((W)>8 ), uint16_t , uint8_t >::type >::type>::type
163
163
#define uvalue_t (V ) typename IF<((V)>65535 ), uint32_t , typename IF<((V)>255 ), uint16_t , uint8_t >::type>::type
164
164
#define value_t (V ) typename IF<((V)>32767 ), int32_t , typename IF<((V)>127 ), int16_t , int8_t >::type>::type
165
165
166
- // General Flags for some number of states
166
+ // Define a template for a bit field of N bits, using the smallest type that can hold N bits
167
+ template <size_t N, bool UseArray = (N > 64 )>
168
+ struct Flags ;
169
+
170
+ // Flag bits for <= 64 states
171
+ template <size_t N>
172
+ struct Flags <N, false > {
173
+ typedef bits_t (N) flagbits_t;
174
+ flagbits_t b;
175
+
176
+ class BitProxy {
177
+ public:
178
+ BitProxy (flagbits_t & data, int bit) : data_(data), bit_(bit) {}
179
+
180
+ BitProxy& operator =(const bool value) {
181
+ if (value)
182
+ data_ |= _BV (bit_);
183
+ else
184
+ data_ &= ~_BV (bit_);
185
+ return *this ;
186
+ }
187
+
188
+ operator bool () const { return TEST (data_, bit_); }
189
+
190
+ private:
191
+ flagbits_t & data_;
192
+ uint8_t bit_;
193
+ };
194
+
195
+ FI void reset () { b = 0 ; }
196
+ FI void set (const int n, const bool onoff) { onoff ? set (n) : clear (n); }
197
+ FI void set (const int n) { b |= _BV (n); }
198
+ FI void clear (const int n) { b &= ~_BV (n); }
199
+ FI bool test (const int n) const { return TEST (b, n); }
200
+ FI BitProxy operator [](const int n) { return BitProxy (b, n); }
201
+ FI bool operator [](const int n) const { return test (n); }
202
+ FI int size () const { return sizeof (b); }
203
+ FI operator bool () const { return b != 0 ; }
204
+ };
205
+
206
+ // Flag bits for more than 64 states
167
207
template <size_t N>
168
- struct Flags {
208
+ struct Flags <N, true > {
169
209
uint8_t bitmask[(N+7 )>>3 ];
170
210
// Proxy class for handling bit assignment
171
211
class BitProxy {
0 commit comments