All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
pyResultConversions.h
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_PY_RESULT_CONVERSIONS_H
8#define PXR_BASE_TF_PY_RESULT_CONVERSIONS_H
9
10#include "pxr/pxr.h"
11
12#include "pxr/base/tf/pyUtils.h"
13
14#include "pxr/external/boost/python/tuple.hpp"
15#include "pxr/external/boost/python/list.hpp"
16#include "pxr/external/boost/python/dict.hpp"
17
18#include <type_traits>
19
20PXR_NAMESPACE_OPEN_SCOPE
21
22template <typename T> struct Tf_PySequenceToListConverter;
23template <typename T> struct Tf_PySequenceToSetConverter;
24template <typename T> struct Tf_PyMapToDictionaryConverter;
25template <typename T> struct Tf_PySequenceToTupleConverter;
26template <typename First, typename Second> struct Tf_PyPairToTupleConverter;
27
51 template <typename T>
52 struct apply {
53 typedef Tf_PySequenceToListConverter<T> type;
54 };
55};
56
80 template <typename T>
81 struct apply {
82 typedef Tf_PySequenceToSetConverter<T> type;
83 };
84};
85
91 template <typename T>
92 struct apply {
93 typedef Tf_PyMapToDictionaryConverter<T> type;
94 };
95};
96
103 template <typename T>
104 struct apply {
105 typedef Tf_PySequenceToTupleConverter<T> type;
106 };
107};
108
112 template <typename T>
113 struct apply {
114 typedef Tf_PyPairToTupleConverter<typename T::first_type,
115 typename T::second_type> type;
116 };
117};
118
119template <typename T>
120struct Tf_PySequenceToListConverter {
121 typedef std::remove_reference_t<T> SeqType;
122 bool convertible() const {
123 return true;
124 }
125 PyObject *operator()(T seq) const {
126 return pxr_boost::python::incref(TfPyCopySequenceToList(seq).ptr());
127 }
128 PyTypeObject *get_pytype() {
129 return &PyList_Type;
130 }
131};
132
133template <typename T>
134struct Tf_PySequenceToSetConverter {
135 typedef std::remove_reference_t<T> SeqType;
136 bool convertible() const {
137 return true;
138 }
139 PyObject *operator()(T seq) const {
140 return pxr_boost::python::incref(TfPyCopySequenceToSet(seq).ptr());
141 }
142 PyTypeObject *get_pytype() {
143 return &PySet_Type;
144 }
145};
146
147template <typename T>
148struct Tf_PyMapToDictionaryConverter {
149 typedef std::remove_reference_t<T> SeqType;
150 // TODO: convertible() should be made more robust by checking that the
151 // value_type of the container is pair<const key_type, data_type>
152 bool convertible() const {
153 return true;
154 }
155 PyObject *operator()(T seq) const {
156 return pxr_boost::python::incref(TfPyCopyMapToDictionary(seq).ptr());
157 }
158 PyTypeObject *get_pytype() {
159 return &PyDict_Type;
160 }
161};
162
163template <typename T>
164struct Tf_PySequenceToTupleConverter {
165 typedef std::remove_reference_t<T> SeqType;
166 bool convertible() const {
167 return true;
168 }
169 PyObject *operator()(T seq) const {
170 return pxr_boost::python::incref(TfPyCopySequenceToTuple(seq).ptr());
171 }
172 PyTypeObject *get_pytype() {
173 return &PyTuple_Type;
174 }
175};
176
177template <typename First, typename Second>
178struct Tf_PyPairToTupleConverter {
179 typedef std::pair<First, Second> PairType;
180 bool convertible() const {
181 return true;
182 }
183 PyObject *operator()(PairType const& a) const {
184 pxr_boost::python::tuple result =
185 pxr_boost::python::make_tuple(a.first, a.second);
186 return pxr_boost::python::incref(result.ptr());
187 }
188 PyTypeObject *get_pytype() {
189 return &PyTuple_Type;
190 }
191};
192
193PXR_NAMESPACE_CLOSE_SCOPE
194
195#endif // TF_RESULT_CONVERSIONS_H
Miscellaneous Utilities for dealing with script.
pxr_boost::python::object TfPyCopySequenceToSet(Seq const &seq)
Create a python set from an iterable sequence.
Definition: pyUtils.h:281
pxr_boost::python::dict TfPyCopyMapToDictionary(Map const &map)
Creates a python dictionary from a std::map.
Definition: pyUtils.h:258
A pxr_boost::python result converter generator which converts standard library maps to dictionaries.
A pxr_boost::python result converter generator which converts standard library pairs to tuples.
A pxr_boost::python result converter generator which converts standard library sequences to lists.
A pxr_boost::python result converter generator which converts standard library sequences to sets.
A pxr_boost::python result converter generator which converts standard library sequences to tuples.