24 #ifndef PXR_BASE_TF_PY_CONTAINER_CONVERSIONS_H
25 #define PXR_BASE_TF_PY_CONTAINER_CONVERSIONS_H
47 #include <hboost/python/list.hpp>
48 #include <hboost/python/tuple.hpp>
49 #include <hboost/python/extract.hpp>
50 #include <hboost/python/to_python_converter.hpp>
60 template <
typename ContainerType>
65 hboost::python::list
result;
69 return hboost::python::incref(result.ptr());
74 template <
typename ContainerType>
79 PyObject*
result = PySet_New(
nullptr);
80 for (
const auto &elem : c) {
87 template <
typename ContainerType>
96 namespace TfPyContainerConversions {
98 template <
typename ContainerType>
103 hboost::python::list
result;
104 typedef typename ContainerType::const_iterator const_iter;
105 for(const_iter p=a.begin();p!=a.end();p++) {
108 return hboost::python::incref(hboost::python::tuple(result).
ptr());
112 template <
typename First,
typename Second>
114 static PyObject*
convert(std::pair<First, Second>
const&
a)
116 hboost::python::tuple
result =
117 hboost::python::make_tuple(a.first, a.second);
118 return hboost::python::incref(result.ptr());
126 template <
typename ContainerType>
127 static bool check_size(hboost::type<ContainerType>, std::size_t sz)
132 template <
typename ContainerType>
133 static void assert_size(hboost::type<ContainerType>, std::size_t sz) {}
135 template <
typename ContainerType>
143 template <
typename ContainerType>
144 static bool check_size(hboost::type<ContainerType>, std::size_t sz)
149 template <
typename ContainerType>
150 static void assert_size(hboost::type<ContainerType>, std::size_t sz)
152 if (!
check_size(hboost::type<ContainerType>(), sz)) {
153 PyErr_SetString(PyExc_RuntimeError,
154 "Insufficient elements for fixed-size array.");
155 hboost::python::throw_error_already_set();
159 template <
typename ContainerType>
163 PyErr_SetString(PyExc_RuntimeError,
164 "Too many elements for fixed-size array.");
165 hboost::python::throw_error_already_set();
169 template <
typename ContainerType,
typename ValueType>
179 template <
typename ContainerType>
185 template <
typename ContainerType,
typename ValueType>
200 template <
typename ContainerType>
201 static bool check_size(hboost::type<ContainerType>, std::size_t sz)
203 return ContainerType::max_size() >= sz;
209 template <
typename ContainerType,
typename ValueType>
218 template <
typename ContainerType,
typename ValueType>
225 template <
typename ContainerType,
typename ConversionPolicy>
232 hboost::python::converter::registry::push_back(
235 hboost::python::type_id<ContainerType>());
240 if (!( PyList_Check(obj_ptr)
241 || PyTuple_Check(obj_ptr)
242 || PySet_Check(obj_ptr)
243 || PyFrozenSet_Check(obj_ptr)
244 || PyIter_Check(obj_ptr)
245 || PyRange_Check(obj_ptr)
246 || ( !PyBytes_Check(obj_ptr)
247 && !PyUnicode_Check(obj_ptr)
248 && ( Py_TYPE(obj_ptr) == 0
249 || Py_TYPE(Py_TYPE(obj_ptr)) == 0
250 || Py_TYPE(Py_TYPE(obj_ptr))->tp_name == 0
252 Py_TYPE(Py_TYPE(obj_ptr))->tp_name,
253 "Boost.Python.class") != 0)
254 && PyObject_HasAttrString(obj_ptr,
"__len__")
255 && PyObject_HasAttrString(obj_ptr,
"__getitem__"))))
return 0;
256 hboost::python::handle<> obj_iter(
257 hboost::python::allow_null(PyObject_GetIter(obj_ptr)));
258 if (!obj_iter.get()) {
262 if (ConversionPolicy::check_convertibility_per_element()) {
263 Py_ssize_t obj_size = PyObject_Length(obj_ptr);
268 if (!ConversionPolicy::check_size(
269 hboost::type<ContainerType>(), obj_size))
return 0;
270 bool is_range = PyRange_Check(obj_ptr);
273 if (!is_range) assert(i == (std::size_t)obj_size);
282 hboost::python::handle<>& obj_iter,
287 hboost::python::handle<> py_elem_hdl(
288 hboost::python::allow_null(PyIter_Next(obj_iter.get())));
289 if (PyErr_Occurred()) {
293 if (!py_elem_hdl.get())
break;
295 hboost::python::extract<container_element_type>
296 elem_proxy(py_elem_obj);
297 if (!elem_proxy.check())
return false;
305 hboost::python::converter::rvalue_from_python_stage1_data*
data)
307 hboost::python::handle<> obj_iter(PyObject_GetIter(obj_ptr));
309 (hboost::python::converter::rvalue_from_python_storage<ContainerType>*)
310 data)->storage.bytes;
316 hboost::python::handle<> py_elem_hdl(
317 hboost::python::allow_null(PyIter_Next(obj_iter.get())));
318 if (PyErr_Occurred()) hboost::python::throw_error_already_set();
319 if (!py_elem_hdl.get())
break;
321 hboost::python::extract<container_element_type> elem_proxy(py_elem_obj);
322 ConversionPolicy::set_value(result, i, elem_proxy());
324 ConversionPolicy::assert_size(hboost::type<ContainerType>(), i);
328 template <
typename PairType>
335 hboost::python::converter::registry::push_back(
338 hboost::python::type_id<PairType>());
343 if (!PyTuple_Check(obj_ptr) || PyTuple_Size(obj_ptr) != 2) {
346 hboost::python::extract<first_type> e1(PyTuple_GetItem(obj_ptr, 0));
347 hboost::python::extract<second_type> e2(PyTuple_GetItem(obj_ptr, 1));
348 if (!e1.check() || !e2.check()) {
356 hboost::python::converter::rvalue_from_python_stage1_data*
data)
359 (hboost::python::converter::rvalue_from_python_storage<PairType>*)
360 data)->storage.bytes;
361 hboost::python::extract<first_type> e1(PyTuple_GetItem(obj_ptr, 0));
362 hboost::python::extract<second_type> e2(PyTuple_GetItem(obj_ptr, 1));
363 new (
storage) PairType(e1(), e2());
368 template <
typename ContainerType>
372 hboost::python::to_python_converter<
378 template <
typename ContainerType,
typename ConversionPolicy>
388 template <
typename ContainerType>
398 template <
typename ContainerType>
408 template <
typename ContainerType>
418 template <
typename ContainerType>
428 template <
typename ContainerType>
432 hboost::python::to_python_converter<
444 using namespace TfPyContainerConversions;
455 #endif // PXR_BASE_TF_PY_CONTAINER_CONVERSIONS_H
static bool check_convertibility_per_element()
static bool check_convertibility_per_element()
PairType::second_type second_type
tuple_mapping_variable_capacity()
static void set_value(ContainerType &a, std::size_t i, ValueType const &v)
getFileOption("OpenEXR:storage") storage
static void assert_size(hboost::type< ContainerType >, std::size_t sz)
tuple_mapping_fixed_capacity()
GLboolean GLboolean GLboolean GLboolean a
static void construct(PyObject *obj_ptr, hboost::python::converter::rvalue_from_python_stage1_data *data)
static PyObject * convert(ContainerType const &c)
static void set_value(ContainerType &a, std::size_t i, ValueType const &v)
ContainerType::value_type container_element_type
**But if you need a result
static void set_value(ContainerType &a, std::size_t i, ValueType const &v)
static void construct(PyObject *obj_ptr, hboost::python::converter::rvalue_from_python_stage1_data *data)
static PyObject * convert(ContainerType const &a)
tuple_mapping_fixed_size()
static bool check_size(hboost::type< ContainerType >, std::size_t sz)
void TfPyRegisterStlSequencesFromPython()
hboost::python::dict TfPyCopyMapToDictionary(Map const &map)
Creates a python dictionary from a std::map.
static bool check_size(hboost::type< ContainerType >, std::size_t sz)
static void assert_size(hboost::type< ContainerType >, std::size_t sz)
static void reserve(ContainerType &a, std::size_t sz)
static bool check_convertibility_per_element()
static bool check_size(hboost::type< ContainerType >, std::size_t sz)
static PyObject * convert(ContainerType const &c)
static void * convertible(PyObject *obj_ptr)
static bool all_elements_convertible(hboost::python::handle<> &obj_iter, bool is_range, std::size_t &i)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
#define PXR_NAMESPACE_CLOSE_SCOPE
static void set_value(ContainerType &a, std::size_t i, ValueType const &v)
#define TF_FOR_ALL(iter, c)
static void reserve(ContainerType &a, std::size_t sz)
static PyObject * convert(std::pair< First, Second > const &a)
static void * convertible(PyObject *obj_ptr)
static PyObject * convert(ContainerType const &c)
PairType::first_type first_type
static void reserve(ContainerType &a, std::size_t sz)