DiSMEC++
PyWrapper< T > Class Template Reference

Utility class used to wrap all objects we provide to python. More...

#include <binding.h>

Public Member Functions

template<class U >
 PyWrapper (U &&source, std::enable_if_t< std::is_convertible_v< U &, T & >> *a=nullptr)
 
 PyWrapper (std::shared_ptr< T > d)
 
T * operator-> ()
 
const T * operator-> () const
 
T & access ()
 
const T & access () const
 
const std::shared_ptr< T > & ptr () const
 
std::shared_ptr< T > & ptr ()
 

Private Attributes

std::shared_ptr< T > m_Data
 

Detailed Description

template<class T>
class PyWrapper< T >

Utility class used to wrap all objects we provide to python.

Template Parameters
T

In order to enable shared ownership of objects between python and C++, we put them into a shared_ptr. One option would be to specify the HolderType in the pybind bindings to use a shared_ptr. Instead, we have introduced this wrapper class, for the following reasons: 1) Converting constructor: The wrapper allows us to define a converting constructor that that allows us to construct the internal shared_ptr also from a derived class. This is very convenient. 2) Null checking: This wrapper by default checks that the wrapped shared_ptr is not nullptr, and otherwise throws an exception. This means we generally need not worry about which access is safe (in the sense of raising an error on the python side instead of crashing the application) , and in the glue code the checks do not account for any relevant performance decrease.

Definition at line 32 of file binding.h.

Constructor & Destructor Documentation

◆ PyWrapper() [1/2]

template<class T >
template<class U >
PyWrapper< T >::PyWrapper ( U &&  source,
std::enable_if_t< std::is_convertible_v< U &, T & >> *  a = nullptr 
)
inline

Definition at line 35 of file binding.h.

◆ PyWrapper() [2/2]

template<class T >
PyWrapper< T >::PyWrapper ( std::shared_ptr< T >  d)
inline

Definition at line 38 of file binding.h.

Member Function Documentation

◆ access() [1/2]

template<class T >
T& PyWrapper< T >::access ( )
inline

Definition at line 48 of file binding.h.

References PyWrapper< T >::m_Data.

Referenced by PyWrapper< T >::operator->(), and PYBIND11_MODULE().

◆ access() [2/2]

template<class T >
const T& PyWrapper< T >::access ( ) const
inline

Definition at line 55 of file binding.h.

References PyWrapper< T >::m_Data.

◆ operator->() [1/2]

template<class T >
T* PyWrapper< T >::operator-> ( )
inline

Definition at line 40 of file binding.h.

References PyWrapper< T >::access().

◆ operator->() [2/2]

template<class T >
const T* PyWrapper< T >::operator-> ( ) const
inline

Definition at line 44 of file binding.h.

References PyWrapper< T >::access().

◆ ptr() [1/2]

template<class T >
std::shared_ptr<T>& PyWrapper< T >::ptr ( )
inline

Definition at line 66 of file binding.h.

References PyWrapper< T >::m_Data.

◆ ptr() [2/2]

template<class T >
const std::shared_ptr<T>& PyWrapper< T >::ptr ( ) const
inline

Definition at line 62 of file binding.h.

References PyWrapper< T >::m_Data.

Referenced by PYBIND11_MODULE().

Member Data Documentation

◆ m_Data

template<class T >
std::shared_ptr<T> PyWrapper< T >::m_Data
private

Definition at line 70 of file binding.h.

Referenced by PyWrapper< T >::access(), and PyWrapper< T >::ptr().


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