All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
wrapUtils.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 USD_WRAPUTILS_H
25 #define USD_WRAPUTILS_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/usd/object.h"
29 
30 #include <boost/python/def_visitor.hpp>
31 #include <boost/python/type_id.hpp>
32 #include <boost/python/converter/to_python_function_type.hpp>
33 
34 PXR_NAMESPACE_OPEN_SCOPE
35 
36 
37 // This boost.python def_visitor is used to wrap UsdObject and its subclasses.
38 // It replaces boost.python's to_python converter with one that downcasts to the
39 // most derived UsdObject subclass. This way, a wrapped C++ function that
40 // returns a UsdObject, for instance, will produce a UsdPrim or a UsdAttribute
41 // or a UsdRelationship in python, instead of a UsdObject.
42 struct Usd_ObjectSubclass : boost::python::def_visitor<Usd_ObjectSubclass>
43 {
44  friend class boost::python::def_visitor_access;
45 
46  // Function pointer type for downcasting UsdObject * to a more-derived type.
47  typedef const void *(*DowncastFn)(const UsdObject *);
48 
49  // We replace boost.python's to_python converter with one that downcasts
50  // UsdObject types to their most derived type. For example, when converting
51  // a UsdProperty to python, we downcast it to either UsdAttribute or
52  // UsdRelationship, as appropriate.
53  template <typename CLS>
54  void visit(CLS &c) const {
55  typedef typename CLS::wrapped_type Type;
56  _ReplaceConverter(boost::python::type_id<Type>(),
57  _Detail::GetObjType<Type>::Value,
58  _Convert<Type>, _Downcast<Type>);
59  }
60 
61 private:
62  // Converter implementation for UsdObject subclass T.
63  template <class T>
64  static PyObject *_Convert(const void *in) {
65  return _ConvertHelper(static_cast<const T *>(in));
66  }
67 
68  // Downcast UsdObject to T.
69  template <class T>
70  static const void *_Downcast(const UsdObject *in) {
71  return static_cast<const T *>(in);
72  }
73 
74  // Internal method that replaces the boost.python to_python converter for
75  // the type \p pti.
76  static void _ReplaceConverter(
77  boost::python::type_info pti,
78  UsdObjType objType,
79  boost::python::converter::to_python_function_t convert,
80  DowncastFn downcast);
81 
82  // Non-template helper function for _Convert.
83  static PyObject *_ConvertHelper(const UsdObject *obj);
84 };
85 
86 
87 PXR_NAMESPACE_CLOSE_SCOPE
88 
89 #endif // USD_WRAPUTILS_H
UsdObjType
Enum values to represent the various Usd object types.
Definition: object.h:48
Base class for Usd scenegraph objects, providing common API.
Definition: object.h:129