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/python/class.hpp>
37#include <boost/python/default_call_policies.hpp>
38#include <boost/python/def_visitor.hpp>
39#include <boost/python/raw_function.hpp>
44PXR_NAMESPACE_OPEN_SCOPE
46namespace Tf_PySingleton {
48namespace bp = boost::python;
51bp::object _DummyInit(bp::tuple
const & ,
56 return TfCreateWeakPtr(&t);
62 return TfConst_cast<TfWeakPtr<T> >(TfCreateWeakPtr(&t));
70template <
typename PtrType>
71PtrType _GetSingletonWeakPtr(bp::object
const & ) {
72 typedef typename PtrType::DataType Singleton;
73 return GetWeakPtr(Singleton::GetInstance());
77std::string _Repr(bp::object
const &self, std::string
const &prefix);
79struct Visitor : bp::def_visitor<Visitor> {
80 explicit Visitor(std::string
const &reprPrefix = std::string()) :
81 _reprPrefix(reprPrefix) {}
83 friend class bp::def_visitor_access;
84 template <
typename CLS>
85 void visit(CLS &c)
const {
86 typedef typename CLS::metadata::held_type PtrType;
92 c.def(
"__new__", _GetSingletonWeakPtr<PtrType>).staticmethod(
"__new__");
94 c.def(
"__init__", bp::raw_function(_DummyInit));
97 if (!_reprPrefix.empty())
99 make_function(std::bind(
100 _Repr, std::placeholders::_1, _reprPrefix),
101 bp::default_call_policies(),
102 boost::mpl::vector2<std::string,
103 bp::object
const &>()));
106 std::string _reprPrefix;
112Tf_PySingleton::Visitor TfPySingleton();
114Tf_PySingleton::Visitor TfPySingleton(std::string
const &reprPrefix);
116PXR_NAMESPACE_CLOSE_SCOPE
Miscellaneous Utilities for dealing with script.
Pointer storage with deletion detection.
Enables wrapping of Weak or Ref & Weak held types to python.
Manage a single instance of an object.
Pointer storage with deletion detection.