All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
33 #include "pxr/base/tf/singleton.h"
34 #include "pxr/base/tf/weakPtr.h"
35 
36 #include <boost/mpl/vector/vector10.hpp>
37 
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>
42 
43 #include <functional>
44 #include <string>
45 
46 PXR_NAMESPACE_OPEN_SCOPE
47 
48 namespace Tf_PySingleton {
49 
50 namespace bp = boost::python;
51 
52 TF_API
53 bp::object _DummyInit(bp::tuple const & /* args */,
54  bp::dict const & /* kw */);
55 
56 template <class T>
57 TfWeakPtr<T> GetWeakPtr(T &t) {
58  return TfCreateWeakPtr(&t);
59 }
60 
61 template <class T>
62 TfWeakPtr<T> GetWeakPtr(T const &t) {
63  // cast away constness for python...
64  return TfConst_cast<TfWeakPtr<T> >(TfCreateWeakPtr(&t));
65 }
66 
67 template <class T>
68 TfWeakPtr<T> GetWeakPtr(TfWeakPtr<T> const &t) {
69  return t;
70 }
71 
72 template <typename PtrType>
73 PtrType _GetSingletonWeakPtr(bp::object const & /* classObj */) {
74  typedef typename PtrType::DataType Singleton;
75  return GetWeakPtr(Singleton::GetInstance());
76 }
77 
78 TF_API
79 std::string _Repr(bp::object const &self, std::string const &prefix);
80 
81 struct Visitor : bp::def_visitor<Visitor> {
82  explicit Visitor(std::string const &reprPrefix = std::string()) :
83  _reprPrefix(reprPrefix) {}
84 
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;
89 
90  // Singleton implies WeakPtr.
91  c.def(TfPyWeakPtr());
92 
93  // Wrap __new__ to return a weak pointer to the singleton instance.
94  c.def("__new__", _GetSingletonWeakPtr<PtrType>).staticmethod("__new__");
95  // Make __init__ do nothing.
96  c.def("__init__", bp::raw_function(_DummyInit));
97 
98  // If they supplied a repr prefix, provide a repr implementation.
99  if (!_reprPrefix.empty())
100  c.def("__repr__",
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 &>()));
106  }
107 private:
108  std::string _reprPrefix;
109 };
110 
111 }
112 
113 TF_API
114 Tf_PySingleton::Visitor TfPySingleton();
115 TF_API
116 Tf_PySingleton::Visitor TfPySingleton(std::string const &reprPrefix);
117 
118 PXR_NAMESPACE_CLOSE_SCOPE
119 
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 &amp; Weak held types to python.
Pointer storage with deletion detection.
Definition: refBase.h:38