7#ifndef PXR_BASE_TF_PY_IDENTITY_H
8#define PXR_BASE_TF_PY_IDENTITY_H
12#include "pxr/base/tf/api.h"
13#include "pxr/base/tf/pyLock.h"
23#include <boost/python/class.hpp>
24#include <boost/python/handle.hpp>
25#include <boost/python/object.hpp>
27#include "pxr/base/tf/hashmap.h"
31namespace boost {
namespace python {
34template <
template <
class>
class X,
class Y>
35struct pointee< PXR_NS::TfWeakPtrFacade<X, Y> > {
41struct pointee< PXR_NS::
TfRefPtr<T> > {
47PXR_NAMESPACE_OPEN_SCOPE
49struct Tf_PyIdentityHelper
54 static void Set(
void const *
id, PyObject *obj);
59 static PyObject *Get(
void const *
id);
62 static void Erase(
void const *
id);
67 static void Acquire(
void const *
id);
72 static void Release(
void const *
id);
77void Tf_PyReleasePythonIdentity(Ptr
const &ptr, PyObject *obj)
79 Tf_PySetPythonIdentity(ptr, obj);
80 Tf_PyIdentityHelper::Release(ptr.GetUniqueIdentifier());
83void Tf_PyOwnershipRefBaseUniqueChanged(
TfRefBase const *refBase,
86struct Tf_PyOwnershipPtrMap
88 typedef TfHashMap<TfRefBase const *, void const *, TfHash>
91 static void Insert(
TfRefBase *refBase,
void const *uniqueId);
93 static void const *Lookup(
TfRefBase const *refBase);
97 static _CacheType _cache;
109template <
class Ptr,
typename Enable =
void>
110struct Tf_PyOwnershipHelper {
111 template <
typename U>
112 static void Add(U
const &,
const void *, PyObject *) {}
113 template <
typename U>
114 static void Remove(U
const &, PyObject *) {}
117template <
typename Ptr>
118struct Tf_PyOwnershipHelper<Ptr,
120 std::is_same<TfRefPtr<typename Ptr::DataType>, Ptr>::value &&
121 std::is_base_of<TfRefBase, typename Ptr::DataType>::value>>
123 struct _RefPtrHolder {
124 static boost::python::object
125 Get(Ptr
const &refptr) {
128 return boost::python::object(_RefPtrHolder(refptr));
130 static void _WrapIfNecessary() {
132 if (
TfPyIsNone(TfPyGetClassObject<_RefPtrHolder>())) {
139 boost::python::class_<_RefPtrHolder>(name.c_str(),
140 boost::python::no_init);
144 explicit _RefPtrHolder(Ptr
const &refptr) : _refptr(refptr) {}
148 static void Add(Ptr ptr,
const void *uniqueId, PyObject *self) {
153 int ret = PyObject_SetAttrString(self,
"__owner",
154 _RefPtrHolder::Get(ptr).ptr());
157 TF_WARN(
"Could not set __owner attribute on python object!");
163 static_cast<TfRefBase *
>(get_pointer(ptr));
164 Tf_PyOwnershipPtrMap::Insert(refBase, uniqueId);
167 static void Remove(Ptr ptr, PyObject *obj) {
177 if (PyObject_HasAttrString(obj,
"__owner")) {
186 Tf_PyOwnershipPtrMap::Erase(get_pointer(ptr));
188 if (PyObject_DelAttrString(obj,
"__owner") == -1) {
190 TF_WARN(
"Undeletable __owner attribute on python object!");
201template <
typename Ptr>
202struct Tf_PyIsRefPtr {
203 static const bool value =
false;
208 static const bool value =
true;
213std::enable_if_t<Tf_PyIsRefPtr<Ptr>::value>
214Tf_PySetPythonIdentity(Ptr
const &, PyObject *)
219std::enable_if_t<!Tf_PyIsRefPtr<Ptr>::value>
220Tf_PySetPythonIdentity(Ptr
const &ptr, PyObject *obj)
222 if (ptr.GetUniqueIdentifier()) {
223 Tf_PyIdentityHelper::Set(ptr.GetUniqueIdentifier(), obj);
226 ptr.EnableExtraNotification();
231PyObject *Tf_PyGetPythonIdentity(Ptr
const &ptr)
233 PyObject *ret = Tf_PyIdentityHelper::Get(ptr.GetUniqueIdentifier());
238void Tf_PyRemovePythonOwnership(Ptr
const &t, PyObject *obj)
240 Tf_PyOwnershipHelper<Ptr>::Remove(t, obj);
244void Tf_PyAddPythonOwnership(Ptr
const &t,
const void *uniqueId, PyObject *obj)
246 Tf_PyOwnershipHelper<Ptr>::Add(t, uniqueId, obj);
249PXR_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.