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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_TF_PY_RESULT_CONVERSIONS_H
25 #define PXR_BASE_TF_PY_RESULT_CONVERSIONS_H
26 
27 #include "pxr/pxr.h"
28 
29 #include "pxr/base/tf/pyUtils.h"
30 
31 #include <hboost/python/tuple.hpp>
32 #include <hboost/python/list.hpp>
33 #include <hboost/python/dict.hpp>
34 
35 #include <type_traits>
36 
38 
39 template <typename T> struct Tf_PySequenceToListConverter;
40 template <typename T> struct Tf_PySequenceToSetConverter;
41 template <typename T> struct Tf_PyMapToDictionaryConverter;
42 template <typename T> struct Tf_PySequenceToTupleConverter;
43 template <typename First, typename Second> struct Tf_PyPairToTupleConverter;
44 
45 /// \class TfPySequenceToList
46 ///
47 /// A \c hboost::python result converter generator which converts standard
48 /// library sequences to lists.
49 ///
50 /// The way to use this is as a return value policy for a function which
51 /// returns a sequence or a const reference to a sequence. For example this
52 /// function:
53 /// \code
54 /// vector<double> getDoubles() {
55 /// vector<double> ret;
56 /// ret.push_back(1.0);
57 /// ret.push_back(2.0);
58 /// ret.push_back(3.0);
59 /// return ret;
60 /// }
61 /// \endcode
62 ///
63 /// May be wrapped as:
64 /// \code
65 /// def("getDoubles", &getDoubles, return_value_policy<TfPySequenceToList>())
66 /// \endcode
68  template <typename T>
69  struct apply {
71  };
72 };
73 
74 /// \class TfPySequenceToSet
75 ///
76 /// A \c hboost::python result converter generator which converts standard
77 /// library sequences to sets.
78 ///
79 /// The way to use this is as a return value policy for a function which
80 /// returns a sequence or a const reference to a sequence. For example this
81 /// function:
82 /// \code
83 /// unordered_set<double> getDoubles() {
84 /// unordered_set<double> ret;
85 /// ret.insert(1.0);
86 /// ret.insert(2.0);
87 /// ret.insert(3.0);
88 /// return ret;
89 /// }
90 /// \endcode
91 ///
92 /// May be wrapped as:
93 /// \code
94 /// def("getDoubles", &getDoubles, return_value_policy<TfPySequenceToSet>())
95 /// \endcode
97  template <typename T>
98  struct apply {
100  };
101 };
102 
103 /// \class TfPyMapToDictionary
104 ///
105 /// A \c hboost::python result converter generator which converts standard
106 /// library maps to dictionaries.
108  template <typename T>
109  struct apply {
111  };
112 };
113 
114 /// \class TfPySequenceToTuple
115 ///
116 /// A \c hboost::python result converter generator which converts standard
117 /// library sequences to tuples.
118 /// \see TfPySequenceToList.
120  template <typename T>
121  struct apply {
123  };
124 };
125 
126 /// A \c hboost::python result converter generator which converts standard
127 /// library pairs to tuples.
129  template <typename T>
130  struct apply {
131  typedef Tf_PyPairToTupleConverter<typename T::first_type,
132  typename T::second_type> type;
133  };
134 };
135 
136 template <typename T>
138  typedef std::remove_reference_t<T> SeqType;
139  bool convertible() const {
140  return true;
141  }
142  PyObject *operator()(T seq) const {
143  return hboost::python::incref(TfPyCopySequenceToList(seq).ptr());
144  }
145  PyTypeObject *get_pytype() {
146  return &PyList_Type;
147  }
148 };
149 
150 template <typename T>
152  typedef std::remove_reference_t<T> SeqType;
153  bool convertible() const {
154  return true;
155  }
156  PyObject *operator()(T seq) const {
157  return hboost::python::incref(TfPyCopySequenceToSet(seq).ptr());
158  }
159  PyTypeObject *get_pytype() {
160  return &PySet_Type;
161  }
162 };
163 
164 template <typename T>
166  typedef std::remove_reference_t<T> SeqType;
167  // TODO: convertible() should be made more robust by checking that the
168  // value_type of the container is pair<const key_type, data_type>
169  bool convertible() const {
170  return true;
171  }
172  PyObject *operator()(T seq) const {
173  return hboost::python::incref(TfPyCopyMapToDictionary(seq).ptr());
174  }
175  PyTypeObject *get_pytype() {
176  return &PyDict_Type;
177  }
178 };
179 
180 template <typename T>
182  typedef std::remove_reference_t<T> SeqType;
183  bool convertible() const {
184  return true;
185  }
186  PyObject *operator()(T seq) const {
187  return hboost::python::incref(TfPyCopySequenceToTuple(seq).ptr());
188  }
189  PyTypeObject *get_pytype() {
190  return &PyTuple_Type;
191  }
192 };
193 
194 template <typename First, typename Second>
196  typedef std::pair<First, Second> PairType;
197  bool convertible() const {
198  return true;
199  }
200  PyObject *operator()(PairType const& a) const {
201  hboost::python::tuple result =
202  hboost::python::make_tuple(a.first, a.second);
203  return hboost::python::incref(result.ptr());
204  }
205  PyTypeObject *get_pytype() {
206  return &PyTuple_Type;
207  }
208 };
209 
211 
212 #endif // TF_RESULT_CONVERSIONS_H
Tf_PySequenceToSetConverter< T > type
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
**But if you need a result
Definition: thread.h:613
hboost::python::tuple TfPyCopySequenceToTuple(Seq const &seq)
Definition: pyUtils.h:314
Tf_PyMapToDictionaryConverter< T > type
PyObject * operator()(T seq) const
std::remove_reference_t< T > SeqType
hboost::python::object TfPyCopySequenceToSet(Seq const &seq)
Definition: pyUtils.h:298
std::pair< First, Second > PairType
Tf_PySequenceToTupleConverter< T > type
hboost::python::dict TfPyCopyMapToDictionary(Map const &map)
Creates a python dictionary from a std::map.
Definition: pyUtils.h:275
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:1441
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:2448
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
PyObject * operator()(PairType const &a) const
std::remove_reference_t< T > SeqType
hboost::python::list TfPyCopySequenceToList(Seq const &seq)
Definition: pyUtils.h:284