[C++-sig] Let pyhton deside which data it recieves...
Hendrik van Arragon
HarrievA at gmx.de
Tue Dec 11 19:14:59 CET 2012
Hi,
I've made a little class for serial communication, which i want to export from c++ to python. My problem is, that this class does not know what data it's receiving. The layer above the protocol knows how to handle the data. Here is an example:
class Packet{
public:
uint8_t id;
uint8_t data[256];
template<typename T>
void copy_to(T *pointer){
if(sizeof(T) > sizeof(data)){
cout << "size of object you want to copy data to is bigger then a Packtet. Do you know what you are doing?" << endl;
}
memcpy(pointer,&data,sizeof(T));
}
void copy_to_void(void *p, uint8_t size){
memcpy(p,&data,size);
}
};
struct example_data{
uint8_t ad_value;
int16_t acc_x;
int16_t acc_y;
};
I use pyplusplus to export everything. With the line
template void Packet::copy_to<example_data>(example_data*)
it works. But how do i do that, without knowing exemple_data at compile time... I know ctypes has some pointer and dataalignmentstuff, but my understanding of pyplusplus, boost::python and ctype's is not good enough.
Maybe it is possible to get a ctype-pointer and use the copy_to_void()-Funktion. But i don't know how????
Can somebody help me?
Regards, Hendrik
More information about the Cplusplus-sig
mailing list