Is there a portable alternative to C++ bitfields

前端 未结 5 1271
梦谈多话
梦谈多话 2021-02-05 06:53

There are many situations (especially in low-level programming), where the binary layout of the data is important. For example: hardware/driver manipulation, network protocols,

5条回答
  •  再見小時候
    2021-02-05 07:33

    I have written an implementation of bit fields in C++ as a library header file. An example I give in the documentation is that, instead of writing this:

    struct A
      {
        union
          {
            struct
              {
                unsigned x : 5;
                unsigned a0 : 2;
                unsigned a1 : 2;
                unsigned a2 : 2;
              }
            u;
            struct
              {
                unsigned x : 5;
                unsigned all_a : 6;
              }
            v;
          };
      };
    
    // …
    
    A x;
    x.v.all_a = 0x3f;
    x.u.a1 = 0;
    

    you can write:

    typedef Bitfield > Bf;
    
    struct A : private Bitfield_fmt
      {
        F<5> x;
        F<2> a[3];
      };
    
    typedef Bitfield_w_fmt Bwf;
    
    // …
    
    Bwf::Format::Define::T x;
    BITF(Bwf, x, a) = 0x3f;
    BITF(Bwf, x, a[1]) = 0;
    

    There's an alternative interface, under which the last two lines of the above would change to:

    #define BITF_U_X_BWF Bwf
    #define BITF_U_X_BASE x
    BITF(X, a) = 0x3f;
    BITF(X, a[1]) = 0;
    

    Using this implementation of bit fields, the traits template parameter gives the programmer a lot of flexibility. Memory is just processor memory by default, or it can be an abstraction, with the programmer providing functions to perform "memory" reads and writes. The abstracted memory is a sequence of elements of any unsigned integral type (chosen by the programmer). Fields can be laid out either from least-to-most or most-to-least significance. The layout of fields in memory can be the reverse of what they are in the format structure.

    The implementation is located at: https://github.com/wkaras/C-plus-plus-library-bit-fields

    (As you can see, I unfortunately was not able to fully avoid use of macros.)

提交回复
热议问题