HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyResultConversions.h
Go to the documentation of this file.
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 
21 
22 template <typename T> struct Tf_PySequenceToListConverter;
23 template <typename T> struct Tf_PySequenceToSetConverter;
24 template <typename T> struct Tf_PyMapToDictionaryConverter;
25 template <typename T> struct Tf_PySequenceToTupleConverter;
26 template <typename First, typename Second> struct Tf_PyPairToTupleConverter;
27 
28 /// \class TfPySequenceToList
29 ///
30 /// A \c pxr_boost::python result converter generator which converts standard
31 /// library sequences to lists.
32 ///
33 /// The way to use this is as a return value policy for a function which
34 /// returns a sequence or a const reference to a sequence. For example this
35 /// function:
36 /// \code
37 /// vector<double> getDoubles() {
38 /// vector<double> ret;
39 /// ret.push_back(1.0);
40 /// ret.push_back(2.0);
41 /// ret.push_back(3.0);
42 /// return ret;
43 /// }
44 /// \endcode
45 ///
46 /// May be wrapped as:
47 /// \code
48 /// def("getDoubles", &getDoubles, return_value_policy<TfPySequenceToList>())
49 /// \endcode
51  template <typename T>
52  struct apply {
54  };
55 };
56 
57 /// \class TfPySequenceToSet
58 ///
59 /// A \c pxr_boost::python result converter generator which converts standard
60 /// library sequences to sets.
61 ///
62 /// The way to use this is as a return value policy for a function which
63 /// returns a sequence or a const reference to a sequence. For example this
64 /// function:
65 /// \code
66 /// unordered_set<double> getDoubles() {
67 /// unordered_set<double> ret;
68 /// ret.insert(1.0);
69 /// ret.insert(2.0);
70 /// ret.insert(3.0);
71 /// return ret;
72 /// }
73 /// \endcode
74 ///
75 /// May be wrapped as:
76 /// \code
77 /// def("getDoubles", &getDoubles, return_value_policy<TfPySequenceToSet>())
78 /// \endcode
80  template <typename T>
81  struct apply {
83  };
84 };
85 
86 /// \class TfPyMapToDictionary
87 ///
88 /// A \c pxr_boost::python result converter generator which converts standard
89 /// library maps to dictionaries.
91  template <typename T>
92  struct apply {
94  };
95 };
96 
97 /// \class TfPySequenceToTuple
98 ///
99 /// A \c pxr_boost::python result converter generator which converts standard
100 /// library sequences to tuples.
101 /// \see TfPySequenceToList.
103  template <typename T>
104  struct apply {
106  };
107 };
108 
109 /// A \c pxr_boost::python result converter generator which converts standard
110 /// library pairs to tuples.
112  template <typename T>
113  struct apply {
114  typedef Tf_PyPairToTupleConverter<typename T::first_type,
115  typename T::second_type> type;
116  };
117 };
118 
119 template <typename T>
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 
133 template <typename T>
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 
147 template <typename T>
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 
163 template <typename T>
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 
177 template <typename First, typename Second>
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 
194 
195 #endif // TF_RESULT_CONVERSIONS_H
pxr_boost::python::list TfPyCopySequenceToList(Seq const &seq)
Definition: pyUtils.h:267
Tf_PySequenceToSetConverter< T > type
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
**But if you need a result
Definition: thread.h:622
Tf_PyMapToDictionaryConverter< T > type
PyObject * operator()(T seq) const
std::remove_reference_t< T > SeqType
std::pair< First, Second > PairType
Tf_PySequenceToTupleConverter< T > type
PyObject * operator()(T seq) const
std::remove_reference_t< T > SeqType
Tf_PySequenceToListConverter< T > type
PyObject * operator()(T seq) const
std::remove_reference_t< T > SeqType
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
PyObject * operator()(T seq) const
Tf_PyPairToTupleConverter< typename T::first_type, typename T::second_type > type
auto ptr(T p) -> const void *
Definition: format.h:4331
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
pxr_boost::python::tuple TfPyCopySequenceToTuple(Seq const &seq)
Definition: pyUtils.h:297
pxr_boost::python::object TfPyCopySequenceToSet(Seq const &seq)
Definition: pyUtils.h:281
PyObject * operator()(PairType const &a) const
std::remove_reference_t< T > SeqType
pxr_boost::python::dict TfPyCopyMapToDictionary(Map const &map)
Creates a python dictionary from a std::map.
Definition: pyUtils.h:258