bitfield.hh (4262:e851cdcf279b) bitfield.hh (4274:638f735c9bc7)
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;

--- 168 unchanged lines hidden (view full) ---

177 {
178 protected:
179 //This class implements ordinary bitfields, that is a span of bits
180 //who's msb is "first", and who's lsb is "last".
181 template<int first, int last=first>
182 class Bitfield : public BitfieldBase<Type>
183 {
184 public:
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;

--- 168 unchanged lines hidden (view full) ---

177 {
178 protected:
179 //This class implements ordinary bitfields, that is a span of bits
180 //who's msb is "first", and who's lsb is "last".
181 template<int first, int last=first>
182 class Bitfield : public BitfieldBase<Type>
183 {
184 public:
185 operator const Type ()
185 operator uint64_t () const
186 {
187 return this->getBits(first, last);
188 }
189
186 {
187 return this->getBits(first, last);
188 }
189
190 const Type
191 operator=(const Type & _data)
190 uint64_t
191 operator=(const uint64_t _data)
192 {
193 this->setBits(first, last, _data);
194 return _data;
195 }
196 };
197
198 //A class which specializes the above so that it can only be read
199 //from. This is accomplished explicitly making sure the assignment
200 //operator is blocked. The conversion operator is carried through
201 //inheritance. This will unfortunately need to be copied into each
202 //bitfield type due to limitations with how templates work
203 template<int first, int last=first>
204 class BitfieldRO : public Bitfield<first, last>
205 {
206 private:
192 {
193 this->setBits(first, last, _data);
194 return _data;
195 }
196 };
197
198 //A class which specializes the above so that it can only be read
199 //from. This is accomplished explicitly making sure the assignment
200 //operator is blocked. The conversion operator is carried through
201 //inheritance. This will unfortunately need to be copied into each
202 //bitfield type due to limitations with how templates work
203 template<int first, int last=first>
204 class BitfieldRO : public Bitfield<first, last>
205 {
206 private:
207 const Type
208 operator=(const Type & _data);
207 uint64_t
208 operator=(const uint64_t _data);
209 };
210
211 //Similar to the above, but only allows writing.
212 template<int first, int last=first>
213 class BitfieldWO : public Bitfield<first, last>
214 {
215 private:
209 };
210
211 //Similar to the above, but only allows writing.
212 template<int first, int last=first>
213 class BitfieldWO : public Bitfield<first, last>
214 {
215 private:
216 operator const Type ();
216 operator uint64_t () const;
217
218 public:
217
218 public:
219 const Type operator=(const Type & _data)
219 using Bitfield<first, last>::operator=;
220 };
221 };
222
223 //This class contains all the "regular" bitfield classes. It is inherited
224 //by all BitUnions which give them access to those types.
225 template<class Type>
226 class SignedBitfieldTypes
227 {
228 protected:
229 //This class implements ordinary bitfields, that is a span of bits
230 //who's msb is "first", and who's lsb is "last".
231 template<int first, int last=first>
232 class SignedBitfield : public BitfieldBase<Type>
233 {
234 public:
235 operator int64_t () const
220 {
236 {
221 *((Bitfield<first, last> *)this) = _data;
237 return sext<first - last + 1>(this->getBits(first, last));
238 }
239
240 int64_t
241 operator=(const int64_t _data)
242 {
243 this->setBits(first, last, _data);
222 return _data;
223 }
224 };
244 return _data;
245 }
246 };
247
248 //A class which specializes the above so that it can only be read
249 //from. This is accomplished explicitly making sure the assignment
250 //operator is blocked. The conversion operator is carried through
251 //inheritance. This will unfortunately need to be copied into each
252 //bitfield type due to limitations with how templates work
253 template<int first, int last=first>
254 class SignedBitfieldRO : public SignedBitfield<first, last>
255 {
256 private:
257 int64_t
258 operator=(const int64_t _data);
259 };
260
261 //Similar to the above, but only allows writing.
262 template<int first, int last=first>
263 class SignedBitfieldWO : public SignedBitfield<first, last>
264 {
265 private:
266 operator int64_t () const;
267
268 public:
269 int64_t operator=(const int64_t _data)
270 {
271 *((SignedBitfield<first, last> *)this) = _data;
272 return _data;
273 }
274 };
225 };
226
227 template<class Type>
275 };
276
277 template<class Type>
228 class BitfieldTypes : public RegularBitfieldTypes
278 class BitfieldTypes : public RegularBitfieldTypes<Type>,
279 public SignedBitfieldTypes<Type>
229 {};
230
231 //When a BitUnion is set up, an underlying class is created which holds
232 //the actual union. This class then inherits from it, and provids the
233 //implementations for various operators. Setting things up this way
234 //prevents having to redefine these functions in every different BitUnion
235 //type. More operators could be implemented in the future, as the need
236 //arises.
237 template <class Type, class Base>
238 class BitUnionOperators : public Base
239 {
240 public:
280 {};
281
282 //When a BitUnion is set up, an underlying class is created which holds
283 //the actual union. This class then inherits from it, and provids the
284 //implementations for various operators. Setting things up this way
285 //prevents having to redefine these functions in every different BitUnion
286 //type. More operators could be implemented in the future, as the need
287 //arises.
288 template <class Type, class Base>
289 class BitUnionOperators : public Base
290 {
291 public:
241 operator const Type ()
292 operator Type () const
242 {
243 return Base::__data;
244 }
245
293 {
294 return Base::__data;
295 }
296
246 const Type
297 Type
247 operator=(const Type & _data)
248 {
249 Base::__data = _data;
298 operator=(const Type & _data)
299 {
300 Base::__data = _data;
301 return _data;
250 }
251
252 bool
302 }
303
304 bool
253 operator<(const Base & base)
305 operator<(const Base & base) const
254 {
255 return Base::__data < base.__data;
256 }
257
258 bool
306 {
307 return Base::__data < base.__data;
308 }
309
310 bool
259 operator==(const Base & base)
311 operator==(const Base & base) const
260 {
261 return Base::__data == base.__data;
262 }
263 };
264}
265
266//This macro is a backend for other macros that specialize it slightly.
267//First, it creates/extends a namespace "BitfieldUnderlyingClasses" and

--- 60 unchanged lines hidden (view full) ---

328 { __data = _data; } \
329 } name;
330
331//Regular bitfields
332//These define macros for read/write regular bitfield based subbitfields.
333#define SubBitUnion(name, first, last) \
334 __SubBitUnion(Bitfield, first, last, name)
335
312 {
313 return Base::__data == base.__data;
314 }
315 };
316}
317
318//This macro is a backend for other macros that specialize it slightly.
319//First, it creates/extends a namespace "BitfieldUnderlyingClasses" and

--- 60 unchanged lines hidden (view full) ---

380 { __data = _data; } \
381 } name;
382
383//Regular bitfields
384//These define macros for read/write regular bitfield based subbitfields.
385#define SubBitUnion(name, first, last) \
386 __SubBitUnion(Bitfield, first, last, name)
387
388//Regular bitfields
389//These define macros for read/write regular bitfield based subbitfields.
390#define SignedSubBitUnion(name, first, last) \
391 __SubBitUnion(SignedBitfield, first, last, name)
392
336//Use this to define an arbitrary type overlayed with bitfields.
337#define BitUnion(type, name) __BitUnion(type, name)
338
339//Use this to define conveniently sized values overlayed with bitfields.
340#define BitUnion64(name) __BitUnion(uint64_t, name)
341#define BitUnion32(name) __BitUnion(uint32_t, name)
342#define BitUnion16(name) __BitUnion(uint16_t, name)
343#define BitUnion8(name) __BitUnion(uint8_t, name)
344
345#endif // __BASE_BITFIELD_HH__
393//Use this to define an arbitrary type overlayed with bitfields.
394#define BitUnion(type, name) __BitUnion(type, name)
395
396//Use this to define conveniently sized values overlayed with bitfields.
397#define BitUnion64(name) __BitUnion(uint64_t, name)
398#define BitUnion32(name) __BitUnion(uint32_t, name)
399#define BitUnion16(name) __BitUnion(uint16_t, name)
400#define BitUnion8(name) __BitUnion(uint8_t, name)
401
402#endif // __BASE_BITFIELD_HH__