简体   繁体   中英

Can templates be used to access Bitfield members by name in C++?

In Can templates be used to access struct variables by name? there is an example of a template function allowing to set arbitrary members of a given struct. Simplified example:

#include <iostream>
#include <ostream>
#include <string>


template < typename T, typename M, typename V >
void set(T *obj, M mem, V val)
{
    obj->*mem = val;
}


struct Obj1
{
    int a;
    double b;
};

struct Obj2
{
    std::string foo;
    float bar;
};


int main()
{
    Obj1 o1;
    o1.a=10;
    o1.b=10;
    Obj2 o2;
    o2.foo="foobarbaz";
    o2.bar=3.2f;

    std::cout<<"o1.a="<<o1.a<<", o1.b="<<o1.b<<std::endl;
    set(&o1, &Obj1::a, 30);
    std::cout<<"o1.a="<<o1.a<<", o1.b="<<o1.b<<std::endl;


    std::cout<<"o2.foo="<<o2.foo<<", o2.bar="<<o2.bar<<std::endl;
    set(&o2, &Obj2::foo, "example text");
    std::cout<<"o2.foo="<<o2.foo<<", o2.bar="<<o2.bar<<std::endl;

    return 0;
}

This works as expected. However, it will not work for bitfields because no pointer to bitfield members can be taken. So, my question is, is there a way to write something like this for a bitfield? Say, I want to access the members of:

struct Obj3
{
    unsigned char first:3;
    unsigned char second:5;
};

Regards,

mox

edit: If I expand the code to

#include <iostream>
#include <ostream>
#include <string>


template < typename T, typename M, typename V >
void set(T *obj, M mem, V val)
{
    obj->*mem = val;
}


struct Obj1
{
    int a;
    double b;
};

struct Obj2
{
    std::string foo;
    float bar;
};

struct Obj3
{
    unsigned char first:3;
    unsigned char second:5;
};



int main()
{
    Obj1 o1;
    o1.a=10;
    o1.b=10;
    Obj2 o2;
    o2.foo="foobarbaz";
    o2.bar=3.2f;

    Obj3 o3;
    o3.first=1;
    o3.second=1;

    std::cout<<"o1.a="<<o1.a<<", o1.b="<<o1.b<<std::endl;
    set(&o1, &Obj1::a, 30);
    std::cout<<"o1.a="<<o1.a<<", o1.b="<<o1.b<<std::endl;


    std::cout<<"o2.foo="<<o2.foo<<", o2.bar="<<o2.bar<<std::endl;
    set(&o2, &Obj2::foo, "example text");
    std::cout<<"o2.foo="<<o2.foo<<", o2.bar="<<o2.bar<<std::endl;

    std::cout<<"o3.first="<<o3.first<<", o1.second="<<o3.second<<std::endl;
    set(&o3, &Obj3::second, 2);
    std::cout<<"o3.first="<<o3.first<<", o1.second="<<o3.second<<std::endl;
    return 0;
}

I get the following error message:

$ g++ main.cpp && ./a.out
main.cpp: In function ‘int main()’:
main.cpp:56:18: error: invalid pointer to bit-field ‘Obj3::second’
  set(&o3, &Obj3::second, 2);

Edit: (My use_case) I write sort of a driver for communication with a read-only i2c-device. The usual, old C way of doing this would require #define-ing the register addresses, the offsets of each bit within the registers and then saving a bunch of uint16_t my_register_whatever values. Access would be done by bitwise operations. I wanted to try a more C++-like approach:

struct __attribute__((__packed__))
{
    unsigned char address : 7;
    unsigned char DAC : 4;
    unsigned char ADC : 1;
    unsigned char MIC : 3;
    unsigned char LINE : 1;
} power_down_control;

This would add type-safety and a much cleaner, more readable syntax. Now, since my I²C-device will not allow reading and there can be errors when doing an I²C-transfer, I want to copy the register struct, change one or more values, send it to the device and if the transfer went all right, overwrite the register struct with the changed copy.

First, change your set to be:

template<class M>
struct member_type;
template<class T, class X>
struct member_type<T::*X> {
  using type=X;
};
template<class M>
using member_t = typename member_type<M>::type;

template<class T, class M>
struct set_t {
  T* t;
  M* m;
  template<class V>
  auto operator()(V&& v)const
  -> decltype(
    (std::declval<T* const&>(t)->*std::declval<M* const&>())
    =std::declval<V>()
  ) {
    return (t->*m) = std::forward<V>(v);
  }
  member_t<M*>& operator()(member_t<M*>&& v)const{
    return (t->*m) = std::move(v);
  }
};
template<class T, class M>
set_t<T,M> set(T* t, M* m) { return {t, m}; }

and use becomes:

auto setter = set(&o2, &Obj2::foo);
setter("example text");

or

set(&o2, &Obj2::foo)("example text");

on one line.

The idea is that we split the creating of the setting object from the setting.

Then we can create specific setting objects for a given field.

#define SETTER(M) \
  [&](auto* t){
    return [t,&](auto&& v) {
      return (t->M) = decltype(v)(v);
    };
  }

used like:

auto setter = SETTER(second)(&o3);
setter(2);

or

SETTER(second)(&o3)(2);

in short.


As an aside, the fancy gymnastics involving member_t above allow set({construct, the, value, here}) syntax. I didn't do the same for the SETTER macro, but I probably could with more work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM