// Demonstration/tutorial file copyright Craig Finch 2007. // Released under the Boost Software License, which is available at // http://www.boost.org/LICENSE_1_0.txt #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { #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; class FrameHdr :public frameHdr_t { public: uint32_t getHdrSize() { return hdrSize; } void setHdrSize(uint32_t val) { hdrSize = val; } }; typedef struct frame_s { frameHdr_t frame_header; ///< frame header uint8_t data[DATA_SIZE]; } frame_t; class Frame : public frame_t { public: class FrameHdr frame_header; }; namespace bp = boost::python; class myClass { public: Frame frame; boost::python::object pycb; void register_callback(boost::python::object cb) { pycb = cb; } void boo() { this->frame.frame_header.hdrSize = 17; /// Call the equivelent Python code /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [.py] /// import api /// frame = api.frame_t() /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ boost::python::object api = bp::import("api"); boost::python::object frame = api.attr("Frame")(); frame.attr("frame_header").attr("hdrSize") = this->frame.frame_header.hdrSize; pycb(frame); } Frame crap() { Frame frame; frame.frame_header.hdrSize = 4; fprintf(stderr, "hdrSize:%d\n", frame.frame_header.hdrSize); return frame; } } ; } using namespace boost::python; BOOST_PYTHON_MODULE(api) { class_("FrameHdr") //.def_readwrite("hdrSize", &frameHdr_t::hdrSize) .add_property("hdrSize", &FrameHdr::getHdrSize, &FrameHdr::setHdrSize) ; class_("Frame") .def_readwrite("frame_header", &Frame::frame_header) ; class_("myClass") .def("register_callback", &myClass::register_callback) .def("boo", &myClass::boo) .def("crap", &myClass::crap) ; }