HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pySingleton.h
Go to the documentation of this file.
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 <hboost/mpl/vector/vector10.hpp>
37 
38 #include <hboost/python/class.hpp>
39 #include <hboost/python/default_call_policies.hpp>
40 #include <hboost/python/def_visitor.hpp>
41 #include <hboost/python/raw_function.hpp>
42 
43 #include <functional>
44 #include <string>
45 
47 
48 namespace Tf_PySingleton {
49 
50 namespace bp = hboost::python;
51 
52 TF_API
53 bp::object _DummyInit(bp::tuple const & /* args */,
54  bp::dict const & /* kw */);
55 
56 template <class T>
58  return TfCreateWeakPtr(&t);
59 }
60 
61 template <class T>
63  // cast away constness for python...
65 }
66 
67 template <class 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  hboost::mpl::vector2<std::string,
105  bp::object const &>()));
106  }
107 private:
108  std::string _reprPrefix;
109 };
110 
111 }
112 
113 TF_API
115 TF_API
117 
119 
120 #endif // PXR_BASE_TF_PY_SINGLETON_H
TfRefPtr< typename T::DataType > TfConst_cast(const TfRefPtr< const typename T::DataType > &ptr)
Definition: refPtr.h:1292
#define TF_API
Definition: api.h:40
void visit(CLS &c) const
Definition: pySingleton.h:87
friend class bp::def_visitor_access
Definition: pySingleton.h:85
TF_API std::string _Repr(bp::object const &self, std::string const &prefix)
TF_API Tf_PySingleton::Visitor TfPySingleton()
GLuint object
Definition: glew.h:8986
const GLfloat * c
Definition: glew.h:16296
Visitor(std::string const &reprPrefix=std::string())
Definition: pySingleton.h:82
GLsizei const GLchar *const * string
Definition: glew.h:1844
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
TF_API bp::object _DummyInit(bp::tuple const &, bp::dict const &)
PtrType _GetSingletonWeakPtr(bp::object const &)
Definition: pySingleton.h:73
GLdouble GLdouble t
Definition: glew.h:1398
TfWeakPtr< U > TfCreateWeakPtr(U *p)
Definition: weakPtr.h:232
TfWeakPtr< T > GetWeakPtr(T &t)
Definition: pySingleton.h:57