All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyClassMethod.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_CLASS_METHOD_H
25 #define PXR_BASE_TF_PY_CLASS_METHOD_H
26 
27 #include "pxr/pxr.h"
28 
29 #include <boost/python/class.hpp>
30 #include <boost/python/dict.hpp>
31 #include <boost/python/object.hpp>
32 #include <boost/python/def_visitor.hpp>
33 
34 PXR_NAMESPACE_OPEN_SCOPE
35 
36 namespace Tf_PyClassMethod {
37 
38 using namespace boost::python;
39 
40 // Visitor for wrapping functions as Python class methods.
41 // See typedef below for docs.
42 // This is very similar to the staticmethod() method on boost::python::class,
43 // except it uses PyClassMethod_New() instead of PyStaticMethod_New().
44 struct _TfPyClassMethod : def_visitor<_TfPyClassMethod>
45 {
46  friend class def_visitor_access;
47 
48  _TfPyClassMethod(const std::string &methodName) :
49  _methodName(methodName) {}
50  explicit _TfPyClassMethod(const char *methodName) :
51  _methodName(methodName) {}
52 
53  template <typename CLS>
54  void visit(CLS &c) const
55  {
56  PyTypeObject* self = downcast<PyTypeObject>( c.ptr() );
57  dict d((handle<>(borrowed(self->tp_dict))));
58 
59  object method(d[_methodName]);
60 
61  c.attr(_methodName.c_str()) = object(
62  handle<>( PyClassMethod_New((_CallableCheck)(method.ptr()) )));
63  }
64 
65 private:
66 
67  PyObject* _CallableCheck(PyObject* callable) const
68  {
69  if (PyCallable_Check(expect_non_null(callable)))
70  return callable;
71 
72  PyErr_Format( PyExc_TypeError,
73  "classmethod expects callable object; got an object of type %s, "
74  "which is not callable",
75  callable->ob_type->tp_name);
76 
77  throw_error_already_set();
78  return 0;
79  }
80 
81  const std::string _methodName;
82 };
83 
84 }
85 
98 typedef Tf_PyClassMethod::_TfPyClassMethod TfPyClassMethod;
99 
100 PXR_NAMESPACE_CLOSE_SCOPE
101 
102 #endif // PXR_BASE_TF_PY_CLASS_METHOD_H