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 
25 #include "pxr/pxr.h"
26 
27 #include "pxr/base/tf/pyObjWrapper.h"
28 
29 #include <boost/python.hpp>
30 
31 using namespace boost::python;
32 
33 PXR_NAMESPACE_USING_DIRECTIVE
34 
35 namespace {
36 
37 struct Tf_PyObjWrapperFromPython {
Tf_PyObjWrapperFromPython__anon3887b6870111::Tf_PyObjWrapperFromPython38     Tf_PyObjWrapperFromPython() {
39         converter::registry::
40             push_back(&_convertible, &_construct,
41                       boost::python::type_id<TfPyObjWrapper>());
42     }
43 
44 private:
45 
46     static void*
_convertible__anon3887b6870111::Tf_PyObjWrapperFromPython47     _convertible(PyObject *o) {
48         // Can always put a python object in a TfPyObjWrapper.
49         return o;
50     }
51 
52     static void
_construct__anon3887b6870111::Tf_PyObjWrapperFromPython53     _construct(PyObject *obj_ptr,
54                converter::rvalue_from_python_stage1_data *data) {
55         void *storage =
56             ((converter::rvalue_from_python_storage<TfPyObjWrapper>*)data)
57             ->storage.bytes;
58         // Make a TfPyObjWrapper holding the Python object.
59         new (storage) TfPyObjWrapper(object(borrowed(obj_ptr)));
60         data->convertible = storage;
61     }
62 };
63 
64 struct Tf_PyObjWrapperToPython {
65     static PyObject *
convert__anon3887b6870111::Tf_PyObjWrapperToPython66     convert(TfPyObjWrapper const &val) {
67         return incref(val.Get().ptr());
68     }
69 };
70 
71 static TfPyObjWrapper
_RoundTripWrapperTest(TfPyObjWrapper const & wrapper)72 _RoundTripWrapperTest(TfPyObjWrapper const &wrapper)
73 {
74     return wrapper;
75 }
76 
77 static TfPyObjWrapper
_RoundTripWrapperCallTest(TfPyObjWrapper const & wrapper)78 _RoundTripWrapperCallTest(TfPyObjWrapper const &wrapper)
79 {
80     return wrapper();
81 }
82 
83 static TfPyObjWrapper
_RoundTripWrapperIndexTest(TfPyObjWrapper const & wrapper,int index)84 _RoundTripWrapperIndexTest(TfPyObjWrapper const &wrapper, int index)
85 {
86     return boost::python::object(wrapper[index]);
87 }
88 
89 } // anonymous namespace
90 
wrapPyObjWrapper()91 void wrapPyObjWrapper()
92 {
93     to_python_converter<TfPyObjWrapper, Tf_PyObjWrapperToPython>();
94     Tf_PyObjWrapperFromPython();
95 
96     def("_RoundTripWrapperTest", _RoundTripWrapperTest);
97     def("_RoundTripWrapperCallTest", _RoundTripWrapperCallTest);
98     def("_RoundTripWrapperIndexTest", _RoundTripWrapperIndexTest);
99 }
100