Boost logo

Boost Users :

Subject: [Boost-users] Boost python and python sets
From: Will Hall (boost_at_[hidden])
Date: 2015-07-06 14:47:25


Hi,

I'm struggling to find a way to support python sets with boost. The
Python API is well defined, so am unable to switch to Python list type.

The API needs to support Python sets for both input and output values. I
have output values (from C++ to Python) working as follows with to_python

namespace bp = boost::python;

template<class T> struct to_python;

template<class T>
struct to_python<std::set<T> >
{
     to_python() {
         bp::to_python_converter< std::set<T>, to_python< std::set<T> > >();
     }
     static PyObject* convert(const std::set<T>& set) {
         bp::list values;
         for(typename std::set<T>::const_iterator iter=set.begin();
iter!=set.end(); ++iter) {
             values.append(bp::object(*iter));
         }
         PyObject* result = PySet_New(values.ptr());
         return result;
     }
     static const PyTypeObject* get_pytype() {
         return &PySet_Type;
     }
};

BOOST_PYTHON_MODULE(helpers)
{
     to_python< std::set<int> >();

     bp:class_<MyClass>()
     // Define constructor with const std::set& arg
     // Define method that returns const std::set& arg
}

But I have no idea how to write an equivalent "from_python" to convert
Python set to std::set.

Any help would be very appreciated!


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