pyResultConversions.h
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 <boost/python/tuple.hpp>
32 #include <boost/python/list.hpp>
33 #include <boost/python/dict.hpp>
34 
35 #include <type_traits>
36 
37 PXR_NAMESPACE_OPEN_SCOPE
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 
68  template <typename T>
69  struct apply {
70  typedef Tf_PySequenceToListConverter<T> type;
71  };
72 };
73 
97  template <typename T>
98  struct apply {
99  typedef Tf_PySequenceToSetConverter<T> type;
100  };
101 };
102 
108  template <typename T>
109  struct apply {
110  typedef Tf_PyMapToDictionaryConverter<T> type;
111  };
112 };
113 
120  template <typename T>
121  struct apply {
122  typedef Tf_PySequenceToTupleConverter<T> type;
123  };
124 };
125 
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>
137 struct Tf_PySequenceToListConverter {
138  typedef std::remove_reference_t<T> SeqType;
139  bool convertible() const {
140  return true;
141  }
142  PyObject *operator()(T seq) const {
143  return boost::python::incref(TfPyCopySequenceToList(seq).ptr());
144  }
145  PyTypeObject *get_pytype() {
146  return &PyList_Type;
147  }
148 };
149 
150 template <typename T>
151 struct Tf_PySequenceToSetConverter {
152  typedef std::remove_reference_t<T> SeqType;
153  bool convertible() const {
154  return true;
155  }
156  PyObject *operator()(T seq) const {
157  return boost::python::incref(TfPyCopySequenceToSet(seq).ptr());
158  }
159  PyTypeObject *get_pytype() {
160  return &PySet_Type;
161  }
162 };
163 
164 template <typename T>
165 struct Tf_PyMapToDictionaryConverter {
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 boost::python::incref(TfPyCopyMapToDictionary(seq).ptr());
174  }
175  PyTypeObject *get_pytype() {
176  return &PyDict_Type;
177  }
178 };
179 
180 template <typename T>
181 struct Tf_PySequenceToTupleConverter {
182  typedef std::remove_reference_t<T> SeqType;
183  bool convertible() const {
184  return true;
185  }
186  PyObject *operator()(T seq) const {
187  return boost::python::incref(TfPyCopySequenceToTuple(seq).ptr());
188  }
189  PyTypeObject *get_pytype() {
190  return &PyTuple_Type;
191  }
192 };
193 
194 template <typename First, typename Second>
195 struct Tf_PyPairToTupleConverter {
196  typedef std::pair<First, Second> PairType;
197  bool convertible() const {
198  return true;
199  }
200  PyObject *operator()(PairType const& a) const {
201  boost::python::tuple result =
202  boost::python::make_tuple(a.first, a.second);
203  return boost::python::incref(result.ptr());
204  }
205  PyTypeObject *get_pytype() {
206  return &PyTuple_Type;
207  }
208 };
209 
210 PXR_NAMESPACE_CLOSE_SCOPE
211 
212 #endif // TF_RESULT_CONVERSIONS_H
A boost::python result converter generator which converts standard library sequences to tuples.
Miscellaneous Utilities for dealing with script.
A boost::python result converter generator which converts standard library sequences to sets.
A boost::python result converter generator which converts standard library pairs to tuples.
boost::python::object TfPyCopySequenceToSet(Seq const &seq)
Create a python set from an iterable sequence.
Definition: pyUtils.h:298
A boost::python result converter generator which converts standard library maps to dictionaries.
A boost::python result converter generator which converts standard library sequences to lists.
boost::python::dict TfPyCopyMapToDictionary(Map const &map)
Creates a python dictionary from a std::map.
Definition: pyUtils.h:275