24#ifndef PXR_BASE_TF_PY_IDENTITY_H
25#define PXR_BASE_TF_PY_IDENTITY_H
29#include "pxr/base/tf/api.h"
30#include "pxr/base/tf/pyLock.h"
40#include <boost/python/class.hpp>
41#include <boost/python/handle.hpp>
42#include <boost/python/object.hpp>
44#include "pxr/base/tf/hashmap.h"
48namespace boost {
namespace python {
51template <
template <
class>
class X,
class Y>
52struct pointee< PXR_NS::TfWeakPtrFacade<X, Y> > {
58struct pointee< PXR_NS::
TfRefPtr<T> > {
64PXR_NAMESPACE_OPEN_SCOPE
66struct Tf_PyIdentityHelper
71 static void Set(
void const *
id, PyObject *obj);
76 static PyObject *Get(
void const *
id);
79 static void Erase(
void const *
id);
84 static void Acquire(
void const *
id);
89 static void Release(
void const *
id);
94void Tf_PyReleasePythonIdentity(Ptr
const &ptr, PyObject *obj)
96 Tf_PySetPythonIdentity(ptr, obj);
97 Tf_PyIdentityHelper::Release(ptr.GetUniqueIdentifier());
100void Tf_PyOwnershipRefBaseUniqueChanged(
TfRefBase const *refBase,
103struct Tf_PyOwnershipPtrMap
105 typedef TfHashMap<TfRefBase const *, void const *, TfHash>
108 static void Insert(
TfRefBase *refBase,
void const *uniqueId);
110 static void const *Lookup(
TfRefBase const *refBase);
114 static _CacheType _cache;
126template <
class Ptr,
typename Enable =
void>
127struct Tf_PyOwnershipHelper {
128 template <
typename U>
129 static void Add(U
const &,
const void *, PyObject *) {}
130 template <
typename U>
131 static void Remove(U
const &, PyObject *) {}
134template <
typename Ptr>
135struct Tf_PyOwnershipHelper<Ptr,
137 std::is_same<TfRefPtr<typename Ptr::DataType>, Ptr>::value &&
138 std::is_base_of<TfRefBase, typename Ptr::DataType>::value>>
140 struct _RefPtrHolder {
141 static boost::python::object
142 Get(Ptr
const &refptr) {
145 return boost::python::object(_RefPtrHolder(refptr));
147 static void _WrapIfNecessary() {
149 if (
TfPyIsNone(TfPyGetClassObject<_RefPtrHolder>())) {
156 boost::python::class_<_RefPtrHolder>(name.c_str(),
157 boost::python::no_init);
161 explicit _RefPtrHolder(Ptr
const &refptr) : _refptr(refptr) {}
165 static void Add(Ptr ptr,
const void *uniqueId, PyObject *self) {
170 int ret = PyObject_SetAttrString(self,
"__owner",
171 _RefPtrHolder::Get(ptr).ptr());
174 TF_WARN(
"Could not set __owner attribute on python object!");
180 static_cast<TfRefBase *
>(get_pointer(ptr));
181 Tf_PyOwnershipPtrMap::Insert(refBase, uniqueId);
184 static void Remove(Ptr ptr, PyObject *obj) {
194 if (PyObject_HasAttrString(obj,
"__owner")) {
203 Tf_PyOwnershipPtrMap::Erase(get_pointer(ptr));
205 if (PyObject_DelAttrString(obj,
"__owner") == -1) {
207 TF_WARN(
"Undeletable __owner attribute on python object!");
218template <
typename Ptr>
219struct Tf_PyIsRefPtr {
220 static const bool value =
false;
225 static const bool value =
true;
230std::enable_if_t<Tf_PyIsRefPtr<Ptr>::value>
231Tf_PySetPythonIdentity(Ptr
const &, PyObject *)
236std::enable_if_t<!Tf_PyIsRefPtr<Ptr>::value>
237Tf_PySetPythonIdentity(Ptr
const &ptr, PyObject *obj)
239 if (ptr.GetUniqueIdentifier()) {
240 Tf_PyIdentityHelper::Set(ptr.GetUniqueIdentifier(), obj);
243 ptr.EnableExtraNotification();
248PyObject *Tf_PyGetPythonIdentity(Ptr
const &ptr)
250 PyObject *ret = Tf_PyIdentityHelper::Get(ptr.GetUniqueIdentifier());
255void Tf_PyRemovePythonOwnership(Ptr
const &t, PyObject *obj)
257 Tf_PyOwnershipHelper<Ptr>::Remove(t, obj);
261void Tf_PyAddPythonOwnership(Ptr
const &t,
const void *uniqueId, PyObject *obj)
263 Tf_PyOwnershipHelper<Ptr>::Add(t, uniqueId, obj);
266PXR_NAMESPACE_CLOSE_SCOPE
Low-level utilities for informing users of various internal and external diagnostic conditions.
Miscellaneous Utilities for dealing with script.
TF_API bool TfPyIsNone(boost::python::object const &obj)
Return true iff obj is None.
Convenience class for accessing the Python Global Interpreter Lock.
Enable a concrete base class for use with TfRefPtr.
Reference-counted smart pointer utility class.
Demangle C++ typenames generated by the typeid() facility.
std::string ArchGetDemangled()
Return demangled RTTI generated-type name.
#define TF_AXIOM(cond)
Aborts if the condition cond is not met.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
#define TF_WARN(...)
Issue a warning, but continue execution.
TF_API std::string TfStringReplace(const std::string &source, const std::string &from, const std::string &to)
Replaces all occurrences of string from with to in source.
Safely compare C++ RTTI type structures.
Definitions of basic string utilities in tf.
Pointer storage with deletion detection.