All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TfPyObjWrapper Class Reference

Boost Python object wrapper. More...

+ Inheritance diagram for TfPyObjWrapper:

Public Member Functions

TF_API TfPyObjWrapper ()
 Default construct a TfPyObjWrapper holding a reference to python None. More...
 
TF_API TfPyObjWrapper (object obj)
 Construct a TfPyObjectWrapper wrapping obj. More...
 
object const & Get () const
 Underlying object access. More...
 
TF_API PyObject * ptr () const
 Underlying PyObject* access. More...
 
TF_API bool operator== (TfPyObjWrapper const &other) const
 Equality. More...
 
TF_API bool operator!= (TfPyObjWrapper const &other) const
 Inequality. More...
 

Friends

class boost::python::api::object_operators< TfPyObjWrapper >
 
size_t hash_value (TfPyObjWrapper const &o)
 Produce a hash code for this object. More...
 

Detailed Description

Boost Python object wrapper.

Provides a wrapper around boost::python::object that works correctly for the following basic operations regardless of the GIL state: default construction, copy construction, assignment, (in)equality comparison, hash_value(), and destruction.

None of those work correctly in the presence of an unlocked GIL for boost::python::object. This class only actually acquires the GIL for default construction, destruction and for some (in)equality comparisons. The other operations do not require taking the GIL.

This is primarily useful in cases where a boost::python::object might be destroyed without a locked GIL by a client blind to that fact. This occurs when a registry, for example, holds type-erased objects. If one of the type-erased objects in the registry happens to hold a boost::python::object, that type-erased object must be destroyed while the GIL is held but it's unreasonable to require that the registry know that. This class helps solve that problem.

This class also provides many of the operators that boost::python::object provides, by virtue of deriving from boost::python::api::object_operators<T>. However it is important to note that callers must ensure the GIL is held before using these operators!

Definition at line 66 of file pyObjWrapper.h.

Constructor & Destructor Documentation

TF_API TfPyObjWrapper ( )

Default construct a TfPyObjWrapper holding a reference to python None.

The GIL need not be held by the caller.

TF_API TfPyObjWrapper ( object  obj)

Construct a TfPyObjectWrapper wrapping obj.

The GIL must be held by the caller. Note, allowing the implicit conversion is intended here.

Member Function Documentation

object const& Get ( ) const
inline

Underlying object access.

This method returns a reference, so technically, the GIL need not be held to call this. However, the caller is strongly advised to ensure the GIL is held, since assigning this object to another or otherwise operating on the returned object requires it.

Definition at line 87 of file pyObjWrapper.h.

TF_API bool operator!= ( TfPyObjWrapper const &  other) const

Inequality.

Returns false if other refers to the same python object.

TF_API bool operator== ( TfPyObjWrapper const &  other) const

Equality.

Returns true if other refers to the same python object.

TF_API PyObject* ptr ( ) const

Underlying PyObject* access.

This method returns a pointer, so technically, the GIL need not be held to call this. However, the caller is strongly advised to ensure the GIL is held, since assigning this object to another or otherwise operating on the returned object requires it. The returned PyObject * is a "borrowed reference", meaning that the underlying object's reference count has not been incremented on behalf of the caller.

Friends And Related Function Documentation

size_t hash_value ( TfPyObjWrapper const &  o)
friend

Produce a hash code for this object.

Note that this does not attempt to hash the underlying python object, it returns a hash code that's suitable for hash-table lookup of TfPyObjWrapper instances, and does not require taking the python lock.

Definition at line 104 of file pyObjWrapper.h.


The documentation for this class was generated from the following file: