c++ - Create a propety with a call policy - boost::python -
i have following c++ classes expose python.
class plainolddata { ... }; class fancyclass { public: const plainolddata& getmypod() {return mypod;} private: plainolddata mypod; }; because want python classes pythonic, expose mypod property. however, when try following:
// expose boost::python boost_python_module(mymod) { class_<plainolddata>("plainolddata", init<>()); // fails class_<fancyclass>("fancyclass", init<>()) .add_property("mypod", &fancyclass::getmypod); } i following error: error c2027: use of undefined type 'boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<t>'
but, if try specify call policy, such as:
class_<fancyclass>("fancyclass", init<>()) .add_property("mypod", &fancyclass::getmypod, return_value_policy<copy_const_reference>()); i incredibly long error message.
is possible expose function property; doing wrong?
similar how python's property() passed python callable objects, boost::python::class_::add_property() function can accept python callable objects can created callpolicies, such returned boost::python::make_function().
for example, property in original code exposed like:
class_<fancyclass>("fancyclass", init<>()) .add_property("mypod", make_function(&fancyclass::getmypod, return_value_policy<copy_const_reference>())); here complete minimal example:
#include <boost/python.hpp> class egg {}; class spam { public: const egg& get_egg() { return egg_; } private: egg egg_; }; boost_python_module(example) { namespace python = boost::python; python::class_<egg>("egg"); python::class_<spam>("spam") .add_property("egg", python::make_function(&spam::get_egg, python::return_value_policy<python::copy_const_reference>())) ; } interactive usage:
>>> import example >>> spam = example.spam() >>> assert(spam.egg not spam.egg) # unique identities spam.egg # returns copy >>> egg1 = spam.egg >>> assert(egg1 not spam.egg) >>> egg2 = spam.egg >>> assert(egg1 not egg2)
Comments
Post a Comment