HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pySingleton.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_TF_PY_SINGLETON_H
8 #define PXR_BASE_TF_PY_SINGLETON_H
9 
10 #include "pxr/pxr.h"
11 
12 #include "pxr/base/tf/api.h"
14 #include "pxr/base/tf/pyUtils.h"
15 
16 #include "pxr/base/tf/singleton.h"
17 #include "pxr/base/tf/weakPtr.h"
18 
19 #include "pxr/external/boost/python/def_visitor.hpp"
20 #include "pxr/external/boost/python/raw_function.hpp"
21 
22 #include <string>
23 
25 
26 namespace Tf_PySingleton {
27 
28 namespace bp = pxr_boost::python;
29 
30 TF_API
31 bp::object _DummyInit(bp::tuple const & /* args */,
32  bp::dict const & /* kw */);
33 
34 template <class T>
36  return TfCreateWeakPtr(&t);
37 }
38 
39 template <class T>
41  // cast away constness for python...
43 }
44 
45 template <class T>
47  return t;
48 }
49 
50 template <typename PtrType>
51 PtrType _GetSingletonWeakPtr(bp::object const & /* classObj */) {
52  typedef typename PtrType::DataType Singleton;
53  return GetWeakPtr(Singleton::GetInstance());
54 }
55 
56 TF_API
57 std::string _Repr(bp::object const &self, std::string const &prefix);
58 
59 struct Visitor : bp::def_visitor<Visitor> {
60  explicit Visitor() {}
61 
62  friend class bp::def_visitor_access;
63  template <typename CLS>
64  void visit(CLS &c) const {
65  typedef typename CLS::metadata::held_type PtrType;
66 
67  // Singleton implies WeakPtr.
68  c.def(TfPyWeakPtr());
69 
70  // Wrap __new__ to return a weak pointer to the singleton instance.
71  c.def("__new__", _GetSingletonWeakPtr<PtrType>).staticmethod("__new__");
72  // Make __init__ do nothing.
73  c.def("__init__", bp::raw_function(_DummyInit));
74  }
75 };
76 
77 }
78 
79 TF_API
81 
83 
84 #endif // PXR_BASE_TF_PY_SINGLETON_H
TfRefPtr< typename T::DataType > TfConst_cast(const TfRefPtr< const typename T::DataType > &ptr)
Definition: refPtr.h:1234
#define TF_API
Definition: api.h:23
void visit(CLS &c) const
Definition: pySingleton.h:64
friend class bp::def_visitor_access
Definition: pySingleton.h:62
TF_API std::string _Repr(bp::object const &self, std::string const &prefix)
TF_API Tf_PySingleton::Visitor TfPySingleton()
GLdouble t
Definition: glad.h:2397
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
TF_API bp::object _DummyInit(bp::tuple const &, bp::dict const &)
PtrType _GetSingletonWeakPtr(bp::object const &)
Definition: pySingleton.h:51
TfWeakPtr< U > TfCreateWeakPtr(U *p)
Definition: weakPtr.h:230
TfWeakPtr< T > GetWeakPtr(T &t)
Definition: pySingleton.h:35