From 076fd9a707a2c5bc019bdf5eafeb9ca8271e28c4 Mon Sep 17 00:00:00 2001 From: Gabe Black Date: Tue, 20 Mar 2007 11:40:30 +0000 Subject: [PATCH] Fixed up some types and const placement, and added signed bitfields that sign extend themselves. --HG-- extra : convert_revision : 84bda8fc14f9a6f7dc7982c9aeb15bf688457706 --- src/base/bitfield.hh | 83 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 70 insertions(+), 13 deletions(-) diff --git a/src/base/bitfield.hh b/src/base/bitfield.hh index ff816286f..ca0c06770 100644 --- a/src/base/bitfield.hh +++ b/src/base/bitfield.hh @@ -182,13 +182,13 @@ namespace BitfieldBackend class Bitfield : public BitfieldBase { public: - operator const Type () + operator uint64_t () const { return this->getBits(first, last); } - const Type - operator=(const Type & _data) + uint64_t + operator=(const uint64_t _data) { this->setBits(first, last, _data); return _data; @@ -204,8 +204,8 @@ namespace BitfieldBackend class BitfieldRO : public Bitfield { private: - const Type - operator=(const Type & _data); + uint64_t + operator=(const uint64_t _data); }; //Similar to the above, but only allows writing. @@ -213,19 +213,70 @@ namespace BitfieldBackend class BitfieldWO : public Bitfield { private: - operator const Type (); + operator uint64_t () const; public: - const Type operator=(const Type & _data) + using Bitfield::operator=; + }; + }; + + //This class contains all the "regular" bitfield classes. It is inherited + //by all BitUnions which give them access to those types. + template + class SignedBitfieldTypes + { + protected: + //This class implements ordinary bitfields, that is a span of bits + //who's msb is "first", and who's lsb is "last". + template + class SignedBitfield : public BitfieldBase + { + public: + operator int64_t () const + { + return sext(this->getBits(first, last)); + } + + int64_t + operator=(const int64_t _data) { - *((Bitfield *)this) = _data; + this->setBits(first, last, _data); + return _data; + } + }; + + //A class which specializes the above so that it can only be read + //from. This is accomplished explicitly making sure the assignment + //operator is blocked. The conversion operator is carried through + //inheritance. This will unfortunately need to be copied into each + //bitfield type due to limitations with how templates work + template + class SignedBitfieldRO : public SignedBitfield + { + private: + int64_t + operator=(const int64_t _data); + }; + + //Similar to the above, but only allows writing. + template + class SignedBitfieldWO : public SignedBitfield + { + private: + operator int64_t () const; + + public: + int64_t operator=(const int64_t _data) + { + *((SignedBitfield *)this) = _data; return _data; } }; }; template - class BitfieldTypes : public RegularBitfieldTypes + class BitfieldTypes : public RegularBitfieldTypes, + public SignedBitfieldTypes {}; //When a BitUnion is set up, an underlying class is created which holds @@ -238,25 +289,26 @@ namespace BitfieldBackend class BitUnionOperators : public Base { public: - operator const Type () + operator Type () const { return Base::__data; } - const Type + Type operator=(const Type & _data) { Base::__data = _data; + return _data; } bool - operator<(const Base & base) + operator<(const Base & base) const { return Base::__data < base.__data; } bool - operator==(const Base & base) + operator==(const Base & base) const { return Base::__data == base.__data; } @@ -333,6 +385,11 @@ namespace BitfieldBackend #define SubBitUnion(name, first, last) \ __SubBitUnion(Bitfield, first, last, name) +//Regular bitfields +//These define macros for read/write regular bitfield based subbitfields. +#define SignedSubBitUnion(name, first, last) \ + __SubBitUnion(SignedBitfield, first, last, name) + //Use this to define an arbitrary type overlayed with bitfields. #define BitUnion(type, name) __BitUnion(type, name) -- 2.30.2