Boost logo

Boost Users :

Subject: [Boost-users] c++ Bit Fields
From: Chris Gem (lennisorex_at_[hidden])
Date: 2013-04-05 11:19:40


Please forgive me if this question has been answered but I've searched for days now and cannot find it.

How do I expose a c++ struct with a bit field to python using boost::python?

When I try I get this error.

error: invalid pointer to bit-field ‘frameHdr_s::hdrSize’

Here is the struct:

#define DATA_SIZE 0x100

typedef struct frameHdr_s
{
#define USE_BIT_FIELDS
#ifdef USE_BIT_FIELDS
   // Longword 0
   uint32_t hdrSize : 16; ///< Size of the header
   uint32_t revMin : 8; ///< Frame Minor Version
   uint32_t revMaj : 8; ///< Frame Major Version
#else
   // Longword 0
   uint32_t hdrSize; ///< Size of the header
   uint32_t revMin ; ///< Frame Minor Version
   uint32_t revMaj ; ///< Frame Major Version
#endif
} frameHdr_t;

typedef struct frame_t
{
   frameHdr_t frame_header; ///< frame header
   uint8_t data[DATA_SIZE]; ///< Byte array of data
} frame_t;

Here is boost code to expose it:

using namespace boost::python;

BOOST_PYTHON_MODULE(api) {
    class_<frameHdr_t>("frameHdr_t")
        .def_readwrite("hdrSize", &frameHdr_t::hdrSize)
    ;

    class_<frame_t>("frame_t")
        .def_readwrite("frame_header", &frame_t::frame_header)
    ;

    class_<myClass>("myClass")
        .def("register_callback", &myClass::register_callback)
        .def("boo", &myClass::boo)
        .def("crap", &myClass::crap)
    ;
}

I know that with ctypes I can create the same struct as in c++:
from ctypes import *
class frameHdr_t(Structure):
    _pack_ = 1
    _fields_=[
        # Longword 0
        ("hdrSize",c_uint32, 16),
        ("revMin",c_uint32, 8),
        ("revMaj",c_uint32, 8),
    ]

DATA_SIZE = 0x100
class frame_t(Structure):
    _pack_ = 1
    _fields_=[
        ("frame_header",frameHdr_t),
        ("data",c_uint8 * DATA_SIZE),
    ]

But how do I translate from the Python version of frame_t to the c++ version of frame_t?

In summary,
* How do I tell boost to expose a bitfield?
* If that is not possible, how do I translate from Python frame_t to C++ frame_t using boot? (I don't want to wrap in C because I need to preserve the C++ness of the interface.)

The code is attached.

Thank you for your time,
Chris
                                               






Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net