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.