24 #ifndef PXR_BASE_TF_PY_SINGLETON_H 25 #define PXR_BASE_TF_PY_SINGLETON_H 29 #include "pxr/base/tf/api.h" 36 #include <boost/mpl/vector/vector10.hpp> 38 #include <boost/python/class.hpp> 39 #include <boost/python/default_call_policies.hpp> 40 #include <boost/python/def_visitor.hpp> 41 #include <boost/python/raw_function.hpp> 46 PXR_NAMESPACE_OPEN_SCOPE
48 namespace Tf_PySingleton {
50 namespace bp = boost::python;
53 bp::object _DummyInit(bp::tuple
const & ,
58 return TfCreateWeakPtr(&t);
72 template <
typename PtrType>
73 PtrType _GetSingletonWeakPtr(bp::object
const & ) {
74 typedef typename PtrType::DataType Singleton;
75 return GetWeakPtr(Singleton::GetInstance());
79 std::string _Repr(bp::object
const &
self, std::string
const &prefix);
81 struct Visitor : bp::def_visitor<Visitor> {
82 explicit Visitor(std::string
const &reprPrefix = std::string()) :
83 _reprPrefix(reprPrefix) {}
85 friend class bp::def_visitor_access;
86 template <
typename CLS>
87 void visit(CLS &c)
const {
88 typedef typename CLS::metadata::held_type PtrType;
94 c.def(
"__new__", _GetSingletonWeakPtr<PtrType>).staticmethod(
"__new__");
96 c.def(
"__init__", bp::raw_function(_DummyInit));
99 if (!_reprPrefix.empty())
101 make_function(std::bind(
102 _Repr, std::placeholders::_1, _reprPrefix),
103 bp::default_call_policies(),
104 boost::mpl::vector2<std::string,
105 bp::object
const &>()));
108 std::string _reprPrefix;
114 Tf_PySingleton::Visitor TfPySingleton();
116 Tf_PySingleton::Visitor TfPySingleton(std::string
const &reprPrefix);
118 PXR_NAMESPACE_CLOSE_SCOPE
120 #endif // PXR_BASE_TF_PY_SINGLETON_H Manage a single instance of an object.
Miscellaneous Utilities for dealing with script.
Pointer storage with deletion detection.
Enables wrapping of Weak or Ref & Weak held types to python.
Pointer storage with deletion detection.