Boost logo

Boost Users :

Subject: Re: [Boost-users] c++ Bit Fields
From: Chris Gem (lennisorex_at_[hidden])
Date: 2013-04-08 08:24:11


Thank you Steve,

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

Still got the same error. However, after looking at add_property, I wrapped the struct with a class that implemented setters and getters for the bitfields, thusly:

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;

class FrameHdr :public frameHdr_t
{
   public:
      uint32_t getHdrSize() { return hdrSize; }
      void setHdrSize(uint32_t val) { hdrSize = val; }
};

Then I used add_property to expose the setters and getters as the name of the property in question:
BOOST_PYTHON_MODULE(api) {
    class_<FrameHdr>("FrameHdr")
        //.def_readwrite("hdrSize", &frameHdr_t::hdrSize)
        .add_property("hdrSize", &FrameHdr::getHdrSize, &FrameHdr::setHdrSize)
    ;

Without trying to sound like too much of a whiner, this seems like a lot of work. Is this what you had in mind?

Thanks for your time,
Chris

> Date: Sat, 6 Apr 2013 13:03:55 -0700
> From: watanabesj_at_[hidden]
> To: boost-users_at_[hidden]
> Subject: Re: [Boost-users] c++ Bit Fields
>
> AMDG
>
> On 04/05/2013 08:19 AM, Chris Gem wrote:
> > 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’
> >
>
> You can't take that address of a bitfield. You'll
> have to use add_property directly.
>
> In Christ,
> Steven Watanabe
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
                                               






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