Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #pragma once
00012
00013 #include <vector>
00014 #include <boost/python.hpp>
00015
00016 using namespace boost::python;
00017
00018
00019
00020
00021
00022 template<class T>
00023 struct vector_to_python_converter
00024 {
00025
00026 static PyObject * convert(const std::vector<T> & vec)
00027 {
00028 list result;
00029
00030 for (auto e : vec)
00031 { result.append(e); }
00032
00033 return incref(result.ptr());
00034 }
00035 };
00036
00037
00038
00039
00040
00041
00042 #define def_readwrite_vector(NAME, FIELDREF) \
00043 add_property(NAME, make_getter(FIELDREF, return_value_policy<return_by_value>()), \
00044 make_setter(FIELDREF, return_value_policy<return_by_value>()))
00045
00046
00047
00048
00049
00050 template<typename T>
00051 struct vector_from_python_converter
00052 {
00053
00054 vector_from_python_converter()
00055 {
00056 converter::registry::push_back(&convertible, &construct, type_id<std::vector<T>>());
00057 }
00058
00059 static void * convertible(PyObject * obj_ptr)
00060 {
00061 if (PySequence_Check(obj_ptr))
00062 {
00063 for (Py_ssize_t i = 0; i < PySequence_Length(obj_ptr); i++)
00064 {
00065 if (!extract<T>(object(handle<>(PySequence_GetItem(obj_ptr, i)))).check())
00066 {
00067 return NULL;
00068 }
00069 }
00070
00071 return obj_ptr;
00072 }
00073
00074 else
00075 {
00076 return NULL;
00077 }
00078 }
00079
00080 static void construct(PyObject * obj_ptr, converter::rvalue_from_python_stage1_data * data)
00081 {
00082
00083 std::vector<T> vs;
00084
00085 for (Py_ssize_t i = 0; i < PySequence_Length(obj_ptr); i++)
00086 {
00087 PyObject * elt = PySequence_GetItem(obj_ptr, i);
00088 vs.push_back(extract<T>(object(handle<>(borrowed(elt)))));
00089 }
00090
00091 void * storage = ((converter::rvalue_from_python_storage<std::vector<T>> *)data)->storage.bytes;
00092
00093 new(storage) std::vector<T>(vs);
00094
00095 data->convertible = storage;
00096
00097 }
00098
00099 };
00100