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 <hboost/type_traits/add_reference.hpp>
36 #include <hboost/type_traits/remove_reference.hpp>
37 
38 #include <type_traits>
39 
41 
42 template <typename T> struct Tf_PySequenceToListConverter;
43 template <typename T> struct Tf_PySequenceToSetConverter;
44 template <typename T> struct Tf_PyMapToDictionaryConverter;
45 template <typename T> struct Tf_PySequenceToTupleConverter;
46 template <typename First, typename Second> struct Tf_PyPairToTupleConverter;
47 
48 /// \class TfPySequenceToList
49 ///
50 /// A \c hboost::python result converter generator which converts standard
51 /// library sequences to lists.
52 ///
53 /// The way to use this is as a return value policy for a function which
54 /// returns a sequence or a const reference to a sequence. For example this
55 /// function:
56 /// \code
57 /// vector<double> getDoubles() {
58 /// vector<double> ret;
59 /// ret.push_back(1.0);
60 /// ret.push_back(2.0);
61 /// ret.push_back(3.0);
62 /// return ret;
63 /// }
64 /// \endcode
65 ///
66 /// May be wrapped as:
67 /// \code
68 /// def("getDoubles", &getDoubles, return_value_policy<TfPySequenceToList>())
69 /// \endcode
71  template <typename T>
72  struct apply {
74  };
75 };
76 
77 /// \class TfPySequenceToSet
78 ///
79 /// A \c hboost::python result converter generator which converts standard
80 /// library sequences to sets.
81 ///
82 /// The way to use this is as a return value policy for a function which
83 /// returns a sequence or a const reference to a sequence. For example this
84 /// function:
85 /// \code
86 /// unordered_set<double> getDoubles() {
87 /// unordered_set<double> ret;
88 /// ret.insert(1.0);
89 /// ret.insert(2.0);
90 /// ret.insert(3.0);
91 /// return ret;
92 /// }
93 /// \endcode
94 ///
95 /// May be wrapped as:
96 /// \code
97 /// def("getDoubles", &getDoubles, return_value_policy<TfPySequenceToSet>())
98 /// \endcode
100  template <typename T>
101  struct apply {
103  };
104 };
105 
106 /// \class TfPyMapToDictionary
107 ///
108 /// A \c hboost::python result converter generator which converts standard
109 /// library maps to dictionaries.
111  template <typename T>
112  struct apply {
114  };
115 };
116 
117 /// \class TfPySequenceToTuple
118 ///
119 /// A \c hboost::python result converter generator which converts standard
120 /// library sequences to tuples.
121 /// \see TfPySequenceToList.
123  template <typename T>
124  struct apply {
126  };
127 };
128 
129 /// A \c hboost::python result converter generator which converts standard
130 /// library pairs to tuples.
132  template <typename T>
133  struct apply {
134  typedef Tf_PyPairToTupleConverter<typename T::first_type,
135  typename T::second_type> type;
136  };
137 };
138 
139 template <typename T>
142  bool convertible() const {
143  return true;
144  }
145  PyObject *operator()(T seq) const {
146  return hboost::python::incref(TfPyCopySequenceToList(seq).ptr());
147  }
148  PyTypeObject *get_pytype() {
149  return &PyList_Type;
150  }
151 };
152 
153 template <typename T>
156  bool convertible() const {
157  return true;
158  }
159  PyObject *operator()(T seq) const {
160  return hboost::python::incref(TfPyCopySequenceToSet(seq).ptr());
161  }
162  PyTypeObject *get_pytype() {
163  return &PySet_Type;
164  }
165 };
166 
167 template <typename T>
170  // TODO: convertible() should be made more robust by checking that the
171  // value_type of the container is pair<const key_type, data_type>
172  bool convertible() const {
173  return true;
174  }
175  PyObject *operator()(T seq) const {
176  return hboost::python::incref(TfPyCopyMapToDictionary(seq).ptr());
177  }
178  PyTypeObject *get_pytype() {
179  return &PyDict_Type;
180  }
181 };
182 
183 template <typename T>
186  bool convertible() const {
187  return true;
188  }
189  PyObject *operator()(T seq) const {
190  return hboost::python::incref(TfPyCopySequenceToTuple(seq).ptr());
191  }
192  PyTypeObject *get_pytype() {
193  return &PyTuple_Type;
194  }
195 };
196 
197 template <typename First, typename Second>
199  typedef std::pair<First, Second> PairType;
200  bool convertible() const {
201  return true;
202  }
203  PyObject *operator()(PairType const& a) const {
204  hboost::python::tuple result =
205  hboost::python::make_tuple(a.first, a.second);
206  return hboost::python::incref(result.ptr());
207  }
208  PyTypeObject *get_pytype() {
209  return &PyTuple_Type;
210  }
211 };
212 
214 
215 #endif // TF_RESULT_CONVERSIONS_H
hboost::remove_reference< T >::type SeqType
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:9477
Tf_PySequenceToSetConverter< T > type
hboost::python::tuple TfPyCopySequenceToTuple(Seq const &seq)
Definition: pyUtils.h:272
Tf_PyMapToDictionaryConverter< T > type
PyObject * operator()(T seq) const
hboost::python::object TfPyCopySequenceToSet(Seq const &seq)
Definition: pyUtils.h:256
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:233
PyObject * operator()(T seq) const
Tf_PySequenceToListConverter< T > type
hboost::remove_reference< T >::type SeqType
PyObject * operator()(T seq) const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
PyObject * operator()(T seq) const
const void * ptr(const T *p)
Definition: format.h:3292
Tf_PyPairToTupleConverter< typename T::first_type, typename T::second_type > type
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
hboost::remove_reference< T >::type SeqType
GLuint64EXT * result
Definition: glew.h:14007
PyObject * operator()(PairType const &a) const
std::remove_reference< T >::type SeqType
hboost::python::list TfPyCopySequenceToList(Seq const &seq)
Definition: pyUtils.h:242
type
Definition: core.h:528