Changeset b96d7c1


Ignore:
Timestamp:
May 10, 2019, 2:45:41 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
7f3f63c
Parents:
360b2e13
Message:

Made bitfield a template rather than an macro

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Bitfield.hpp

    r360b2e13 rb96d7c1  
    2222/// does not allow it. Requires type to have `unsigned val` field
    2323/// @param BFType  Name of containing type
    24 #define MakeBitfield( BFType )                                         \
    25         constexpr BFType() : val( 0 ) {}                                   \
    26         constexpr BFType( unsigned int v ) : val( v ) {}                   \
    27         bool operator[]( unsigned int i ) const { return val & (1 << i); } \
    28         bool any() const { return val != 0; }                              \
    29         void reset() { val = 0; }                                          \
    30         int ffs() { return ::ffs( val ) - 1; }                             \
    31         BFType operator&=( BFType other ) {                                \
    32                 val &= other.val; return *this;                                \
    33         }                                                                  \
    34         BFType operator&( BFType other ) const {                           \
    35                 BFType q = other;                                              \
    36                 q &= *this;                                                    \
    37                 return q;                                                      \
    38         }                                                                  \
    39         BFType operator|=( BFType other ) {                                \
    40                 val |= other.val; return *this;                                \
    41         }                                                                  \
    42         BFType operator|( BFType other ) const {                           \
    43                 BFType q = other;                                              \
    44                 q |= *this;                                                    \
    45                 return q;                                                      \
    46         }                                                                  \
    47         BFType operator-=( BFType other ) {                                \
    48                 val &= ~other.val; return *this;                               \
     24template<typename T>
     25struct bitfield : public T {
     26        static_assert(sizeof(T) == sizeof(unsigned int), "Type has incorrect size");
     27        using T::val;
     28        using val_t = decltype(val);
     29
     30        constexpr bitfield() : T( 0 ) {}
     31        constexpr bitfield( val_t v ) : T( v ) {}
     32
     33        bool operator[]( val_t i ) const { return val & (1 << i); }
     34        bool any() const { return val != 0; }
     35        void reset() { val = 0; }
     36        int ffs() { return ::ffs( val ) - 1; }
     37
     38        bitfield operator&=( bitfield other ) {
     39                val &= other.val; return *this;
    4940        }
     41        bitfield operator&( bitfield other ) const {
     42                bitfield q = other;
     43                q &= *this;
     44                return q;
     45        }
     46        bitfield operator|=( bitfield other ) {
     47                val |= other.val; return *this;
     48        }
     49        bitfield operator|( bitfield other ) const {
     50                bitfield q = other;
     51                q |= *this;
     52                return q;
     53        }
     54        bitfield operator-=( bitfield other ) {
     55                val &= ~other.val; return *this;
     56        }
     57};
    5058
    5159/// Adds default printing operator to a bitfield type.
    5260/// Include in definition to add print function, requires other bitfield operators.
    5361/// @param N  Number of bits in bitfield
    54 #define MakeBitfieldPrint( N )                                         \
    55         static const char* Names[];                                        \
    56         void print( std::ostream & os ) const {                            \
    57                 if ( (*this).any() ) {                                         \
    58                         for ( unsigned int i = 0; i < N; i += 1 ) {                \
    59                                 if ( (*this)[i] ) {                                    \
    60                                         os << Names[i] << ' ';                             \
    61                                 }                                                      \
    62                         }                                                          \
    63                 }                                                              \
     62#define MakeBitfieldPrint( N ) \
     63        static const char* Names[]; \
     64 \
     65        void print( std::ostream & os ) const { \
     66                if ( (*this).any() ) { \
     67                        for ( unsigned int i = 0; i < N; i += 1 ) { \
     68                                if ( (*this)[i] ) { \
     69                                        os << Names[i] << ' '; \
     70                                } \
     71                        } \
     72                } \
    6473        }
    6574
Note: See TracChangeset for help on using the changeset viewer.