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/python/class.hpp>
37 #include <hboost/python/default_call_policies.hpp>
38 #include <hboost/python/def_visitor.hpp>
39 #include <hboost/python/raw_function.hpp>
40 
41 #include <functional>
42 #include <string>
43 
45 
46 namespace Tf_PySingleton {
47 
48 namespace bp = hboost::python;
49 
50 TF_API
51 bp::object _DummyInit(bp::tuple const & /* args */,
52  bp::dict const & /* kw */);
53 
54 template <class T>
56  return TfCreateWeakPtr(&t);
57 }
58 
59 template <class T>
61  // cast away constness for python...
63 }
64 
65 template <class T>
67  return t;
68 }
69 
70 template <typename PtrType>
71 PtrType _GetSingletonWeakPtr(bp::object const & /* classObj */) {
72  typedef typename PtrType::DataType Singleton;
73  return GetWeakPtr(Singleton::GetInstance());
74 }
75 
76 TF_API
77 std::string _Repr(bp::object const &self, std::string const &prefix);
78 
79 struct 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  hboost::mpl::vector2<std::string,
103  bp::object const &>()));
104  }
105 private:
106  std::string _reprPrefix;
107 };
108 
109 }
110 
111 TF_API
113 TF_API
115 
117 
118 #endif // PXR_BASE_TF_PY_SINGLETON_H
TfRefPtr< typename T::DataType > TfConst_cast(const TfRefPtr< const typename T::DataType > &ptr)
Definition: refPtr.h:1242
#define TF_API
Definition: api.h:40
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
void visit(CLS &c) const
Definition: pySingleton.h:85
friend class bp::def_visitor_access
Definition: pySingleton.h:83
TF_API std::string _Repr(bp::object const &self, std::string const &prefix)
TF_API Tf_PySingleton::Visitor TfPySingleton()
GLdouble t
Definition: glad.h:2397
Visitor(std::string const &reprPrefix=std::string())
Definition: pySingleton.h:80
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
#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:71
TfWeakPtr< U > TfCreateWeakPtr(U *p)
Definition: weakPtr.h:247
TfWeakPtr< T > GetWeakPtr(T &t)
Definition: pySingleton.h:55