// // Copyright 2016 Pixar // // Licensed under the Apache License, Version 2.0 (the "Apache License") // with the following modification; you may not use this file except in // compliance with the Apache License and the following modification to it: // Section 6. Trademarks. is deleted and replaced with: // // 6. Trademarks. This License does not grant permission to use the trade // names, trademarks, service marks, or product names of the Licensor // and its affiliates, except as required to comply with Section 4(c) of // the License and to reproduce the content of the NOTICE file. // // You may obtain a copy of the Apache License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the Apache License with the above modification is // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. See the Apache License for the specific // language governing permissions and limitations under the Apache License. // #ifndef PXR_BASE_TF_PY_RESULT_CONVERSIONS_H #define PXR_BASE_TF_PY_RESULT_CONVERSIONS_H #include "pxr/pxr.h" #include "pxr/base/tf/pyUtils.h" #include #include #include #include #include #include PXR_NAMESPACE_OPEN_SCOPE template struct Tf_PySequenceToListConverter; template struct Tf_PySequenceToSetConverter; template struct Tf_PyMapToDictionaryConverter; template struct Tf_PySequenceToTupleConverter; template struct Tf_PyPairToTupleConverter; /// \class TfPySequenceToList /// /// A \c boost::python result converter generator which converts standard /// library sequences to lists. /// /// The way to use this is as a return value policy for a function which /// returns a sequence or a const reference to a sequence. For example this /// function: /// \code /// vector getDoubles() { /// vector ret; /// ret.push_back(1.0); /// ret.push_back(2.0); /// ret.push_back(3.0); /// return ret; /// } /// \endcode /// /// May be wrapped as: /// \code /// def("getDoubles", &getDoubles, return_value_policy()) /// \endcode struct TfPySequenceToList { template struct apply { typedef Tf_PySequenceToListConverter type; }; }; /// \class TfPySequenceToSet /// /// A \c boost::python result converter generator which converts standard /// library sequences to sets. /// /// The way to use this is as a return value policy for a function which /// returns a sequence or a const reference to a sequence. For example this /// function: /// \code /// unordered_set getDoubles() { /// unordered_set ret; /// ret.insert(1.0); /// ret.insert(2.0); /// ret.insert(3.0); /// return ret; /// } /// \endcode /// /// May be wrapped as: /// \code /// def("getDoubles", &getDoubles, return_value_policy()) /// \endcode struct TfPySequenceToSet { template struct apply { typedef Tf_PySequenceToSetConverter type; }; }; /// \class TfPyMapToDictionary /// /// A \c boost::python result converter generator which converts standard /// library maps to dictionaries. struct TfPyMapToDictionary { template struct apply { typedef Tf_PyMapToDictionaryConverter type; }; }; /// \class TfPySequenceToTuple /// /// A \c boost::python result converter generator which converts standard /// library sequences to tuples. /// \see TfPySequenceToList. struct TfPySequenceToTuple { template struct apply { typedef Tf_PySequenceToTupleConverter type; }; }; /// A \c boost::python result converter generator which converts standard /// library pairs to tuples. struct TfPyPairToTuple { template struct apply { typedef Tf_PyPairToTupleConverter type; }; }; template struct Tf_PySequenceToListConverter { typedef typename boost::remove_reference::type SeqType; bool convertible() const { return true; } PyObject *operator()(T seq) const { return boost::python::incref(TfPyCopySequenceToList(seq).ptr()); } PyTypeObject *get_pytype() { return &PyList_Type; } }; template struct Tf_PySequenceToSetConverter { typedef typename std::remove_reference::type SeqType; bool convertible() const { return true; } PyObject *operator()(T seq) const { return boost::python::incref(TfPyCopySequenceToSet(seq).ptr()); } PyTypeObject *get_pytype() { return &PySet_Type; } }; template struct Tf_PyMapToDictionaryConverter { typedef typename boost::remove_reference::type SeqType; // TODO: convertible() should be made more robust by checking that the // value_type of the container is pair bool convertible() const { return true; } PyObject *operator()(T seq) const { return boost::python::incref(TfPyCopyMapToDictionary(seq).ptr()); } PyTypeObject *get_pytype() { return &PyDict_Type; } }; template struct Tf_PySequenceToTupleConverter { typedef typename boost::remove_reference::type SeqType; bool convertible() const { return true; } PyObject *operator()(T seq) const { return boost::python::incref(TfPyCopySequenceToTuple(seq).ptr()); } PyTypeObject *get_pytype() { return &PyTuple_Type; } }; template struct Tf_PyPairToTupleConverter { typedef std::pair PairType; bool convertible() const { return true; } PyObject *operator()(PairType const& a) const { boost::python::tuple result = boost::python::make_tuple(a.first, a.second); return boost::python::incref(result.ptr()); } PyTypeObject *get_pytype() { return &PyTuple_Type; } }; PXR_NAMESPACE_CLOSE_SCOPE #endif // TF_RESULT_CONVERSIONS_H