HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyClassMethod.h
Go to the documentation of this file.
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 <hboost/python/class.hpp>
30 #include <hboost/python/dict.hpp>
31 #include <hboost/python/object.hpp>
32 #include <hboost/python/def_visitor.hpp>
33 
35 
36 namespace Tf_PyClassMethod {
37 
38 using namespace hboost::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 hboost::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 
86 /// A hboost.python class visitor which replaces the named method with a
87 /// classmethod()-wrapped one.
88 ///
89 /// \code
90 /// void Foo( hboost::python::object & pyClassObject ) { /* ... */ }
91 ///
92 /// class_<...>(...)
93 /// .def("Foo", &Foo)
94 /// .def(TfPyClassMethod("Foo"))
95 /// ;
96 /// \endcode
97 ///
99 
101 
102 #endif // PXR_BASE_TF_PY_CLASS_METHOD_H
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
Tf_PyClassMethod::_TfPyClassMethod TfPyClassMethod
Definition: pyClassMethod.h:98
_TfPyClassMethod(const std::string &methodName)
Definition: pyClassMethod.h:48
_TfPyClassMethod(const char *methodName)
Definition: pyClassMethod.h:50
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91