DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Util::PyObject< T, > Class Template Reference

Wraps a class derived from pybind11::object and ensures that the GIL is acquired when the PyObject is destroyed. For any other operation on this wrapper, the GIL still needs to be acquired manually before! More...

Public Types

using ObjectType = T
 

Public Member Functions

 PyObject ()
 Constructs an empty PyObject. More...
 
 PyObject (const T &Object)
 Copy-constructs a PyObject from Object. More...
 
 ~PyObject ()
 Removes the owned pybind11::object after locking the GIL. More...
 
PyObjectoperator= (const T &Object)
 Locks the GIL, removes the owned object and copy-assigns from Object. More...
 
auto & Get () const noexcept
 Returns the wrapped pybind11::object. More...
 
auto & Get () noexcept
 Returns the wrapped pybind11::object. noexcept More...
 
void Reset ()
 Removes the owned pybind11::object after locking the GIL. More...
 
template<typename... ArgTs>
py::object operator() (ArgTs &&...Args) const
 Calls the wrapped object if it is not nullptr and if the wrapped object is not pybind11::none. More...
 

Private Member Functions

void Remove ()
 Removes the owned pybind11::object after locking the GIL. More...
 

Private Attributes

std::unique_ptr< T > Object
 Object derived from pybind11::object owned by this PyObject instance. More...
 

Detailed Description

template<typename T, std::enable_if_t< std::is_base_of_v< py::object, T >, int > = 0>
class Util::PyObject< T, >

Wraps a class derived from pybind11::object and ensures that the GIL is acquired when the PyObject is destroyed. For any other operation on this wrapper, the GIL still needs to be acquired manually before!

Template Parameters
TType derived from pybind11::object

Definition at line 66 of file PyUtil.h.

Member Typedef Documentation

◆ ObjectType

template<typename T , std::enable_if_t< std::is_base_of_v< py::object, T >, int > = 0>
using Util::PyObject< T, >::ObjectType = T

Definition at line 69 of file PyUtil.h.

Constructor & Destructor Documentation

◆ PyObject() [1/2]

template<typename T , std::enable_if_t< std::is_base_of_v< py::object, T >, int > = 0>
Util::PyObject< T, >::PyObject ( )
inline

Constructs an empty PyObject.

Definition at line 74 of file PyUtil.h.

◆ PyObject() [2/2]

template<typename T , std::enable_if_t< std::is_base_of_v< py::object, T >, int > = 0>
Util::PyObject< T, >::PyObject ( const T &  Object)
inline

Copy-constructs a PyObject from Object.

Parameters
ObjectObject to copy

Definition at line 80 of file PyUtil.h.

◆ ~PyObject()

template<typename T , std::enable_if_t< std::is_base_of_v< py::object, T >, int > = 0>
Util::PyObject< T, >::~PyObject ( )
inline

Removes the owned pybind11::object after locking the GIL.

Definition at line 85 of file PyUtil.h.

Member Function Documentation

◆ Get() [1/2]

template<typename T , std::enable_if_t< std::is_base_of_v< py::object, T >, int > = 0>
auto& Util::PyObject< T, >::Get ( ) const
inlinenoexcept

Returns the wrapped pybind11::object.

Returns
Owned object or nullptr if no object is owned

Definition at line 102 of file PyUtil.h.

◆ Get() [2/2]

template<typename T , std::enable_if_t< std::is_base_of_v< py::object, T >, int > = 0>
auto& Util::PyObject< T, >::Get ( )
inlinenoexcept

Returns the wrapped pybind11::object. noexcept

Returns
Owned object or nullptr if no object is owned noexcept

Definition at line 107 of file PyUtil.h.

◆ operator()()

template<typename T , std::enable_if_t< std::is_base_of_v< py::object, T >, int > = 0>
template<typename... ArgTs>
py::object Util::PyObject< T, >::operator() ( ArgTs &&...  Args) const
inline

Calls the wrapped object if it is not nullptr and if the wrapped object is not pybind11::none.

Template Parameters
...ArgTsTypes of the perfectly forward arguments
Parameters
...ArgsArguments to perfectly forward to the wrapped object's call.
Returns
Return value of the call of type derived from pybind11::object or pybind11::none if the call did not happen.

Definition at line 128 of file PyUtil.h.

◆ operator=()

template<typename T , std::enable_if_t< std::is_base_of_v< py::object, T >, int > = 0>
PyObject& Util::PyObject< T, >::operator= ( const T &  Object)
inline

Locks the GIL, removes the owned object and copy-assigns from Object.

Parameters
ObjectObject to copy
Returns
Reference to this PyObject instance

Definition at line 92 of file PyUtil.h.

◆ Remove()

template<typename T , std::enable_if_t< std::is_base_of_v< py::object, T >, int > = 0>
void Util::PyObject< T, >::Remove ( )
inlineprivate

Removes the owned pybind11::object after locking the GIL.

Definition at line 140 of file PyUtil.h.

◆ Reset()

template<typename T , std::enable_if_t< std::is_base_of_v< py::object, T >, int > = 0>
void Util::PyObject< T, >::Reset ( )
inline

Removes the owned pybind11::object after locking the GIL.

This PyObject instance does not own an object after this operation.

Definition at line 113 of file PyUtil.h.

Member Data Documentation

◆ Object

template<typename T , std::enable_if_t< std::is_base_of_v< py::object, T >, int > = 0>
std::unique_ptr<T> Util::PyObject< T, >::Object
private

Object derived from pybind11::object owned by this PyObject instance.

Definition at line 149 of file PyUtil.h.


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