Loading...
Searching...
No Matches
pySingleton.h
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_SINGLETON_H
25#define PXR_BASE_TF_PY_SINGLETON_H
26
27#include "pxr/pxr.h"
28
29#include "pxr/base/tf/api.h"
31#include "pxr/base/tf/pyUtils.h"
32
34#include "pxr/base/tf/weakPtr.h"
35
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>
40
41#include <functional>
42#include <string>
43
44PXR_NAMESPACE_OPEN_SCOPE
45
46namespace Tf_PySingleton {
47
48namespace bp = boost::python;
49
50TF_API
51bp::object _DummyInit(bp::tuple const & /* args */,
52 bp::dict const & /* kw */);
53
54template <class T>
55TfWeakPtr<T> GetWeakPtr(T &t) {
56 return TfCreateWeakPtr(&t);
57}
58
59template <class T>
60TfWeakPtr<T> GetWeakPtr(T const &t) {
61 // cast away constness for python...
62 return TfConst_cast<TfWeakPtr<T> >(TfCreateWeakPtr(&t));
63}
64
65template <class T>
66TfWeakPtr<T> GetWeakPtr(TfWeakPtr<T> const &t) {
67 return t;
68}
69
70template <typename PtrType>
71PtrType _GetSingletonWeakPtr(bp::object const & /* classObj */) {
72 typedef typename PtrType::DataType Singleton;
73 return GetWeakPtr(Singleton::GetInstance());
74}
75
76TF_API
77std::string _Repr(bp::object const &self, std::string const &prefix);
78
79struct Visitor : bp::def_visitor<Visitor> {
80 explicit Visitor(std::string const &reprPrefix = std::string()) :
81 _reprPrefix(reprPrefix) {}
82
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;
87
88 // Singleton implies WeakPtr.
89 c.def(TfPyWeakPtr());
90
91 // Wrap __new__ to return a weak pointer to the singleton instance.
92 c.def("__new__", _GetSingletonWeakPtr<PtrType>).staticmethod("__new__");
93 // Make __init__ do nothing.
94 c.def("__init__", bp::raw_function(_DummyInit));
95
96 // If they supplied a repr prefix, provide a repr implementation.
97 if (!_reprPrefix.empty())
98 c.def("__repr__",
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 &>()));
104 }
105private:
106 std::string _reprPrefix;
107};
108
109}
110
111TF_API
112Tf_PySingleton::Visitor TfPySingleton();
113TF_API
114Tf_PySingleton::Visitor TfPySingleton(std::string const &reprPrefix);
115
116PXR_NAMESPACE_CLOSE_SCOPE
117
118#endif // PXR_BASE_TF_PY_SINGLETON_H
Miscellaneous Utilities for dealing with script.
Pointer storage with deletion detection.
Definition: weakPtr.h:145
Enables wrapping of Weak or Ref & Weak held types to python.
Manage a single instance of an object.
Pointer storage with deletion detection.