7#ifndef PXR_BASE_TF_PY_SINGLETON_H
8#define PXR_BASE_TF_PY_SINGLETON_H
12#include "pxr/base/tf/api.h"
19#include <boost/python/class.hpp>
20#include <boost/python/default_call_policies.hpp>
21#include <boost/python/def_visitor.hpp>
22#include <boost/python/raw_function.hpp>
27PXR_NAMESPACE_OPEN_SCOPE
29namespace Tf_PySingleton {
31namespace bp = boost::python;
34bp::object _DummyInit(bp::tuple
const & ,
39 return TfCreateWeakPtr(&t);
45 return TfConst_cast<TfWeakPtr<T> >(TfCreateWeakPtr(&t));
53template <
typename PtrType>
54PtrType _GetSingletonWeakPtr(bp::object
const & ) {
55 typedef typename PtrType::DataType Singleton;
56 return GetWeakPtr(Singleton::GetInstance());
60std::string _Repr(bp::object
const &self, std::string
const &prefix);
62struct Visitor : bp::def_visitor<Visitor> {
63 explicit Visitor(std::string
const &reprPrefix = std::string()) :
64 _reprPrefix(reprPrefix) {}
66 friend class bp::def_visitor_access;
67 template <
typename CLS>
68 void visit(CLS &c)
const {
69 typedef typename CLS::metadata::held_type PtrType;
75 c.def(
"__new__", _GetSingletonWeakPtr<PtrType>).staticmethod(
"__new__");
77 c.def(
"__init__", bp::raw_function(_DummyInit));
80 if (!_reprPrefix.empty())
82 make_function(std::bind(
83 _Repr, std::placeholders::_1, _reprPrefix),
84 bp::default_call_policies(),
85 boost::mpl::vector2<std::string,
86 bp::object
const &>()));
89 std::string _reprPrefix;
95Tf_PySingleton::Visitor TfPySingleton();
97Tf_PySingleton::Visitor TfPySingleton(std::string
const &reprPrefix);
99PXR_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.