TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
nda::mem::handle_sso< T, Size >

#include <nda/mem/handle.hpp>

Detailed Description

template<typename T, size_t Size>
struct nda::mem::handle_sso< T, Size >

A handle for a memory block on the heap or stack depending on the size of the data.

If the size of the data is less than or equal to the template parameter Size, the data is stored on the stack, otherwise it is stored on the heap (nda::mem::mallocator is used to allocate/deallocate memory on the heap).

It simulates the small string optimization (SSO) for strings.

Note
This handle only works with the Host nda::mem::AddressSpace.
Template Parameters
TValue type of the data.
SizeMax. size of the data to store on the stack (number of elements).

Definition at line 492 of file handle.hpp.

Public Types

using value_type = T
 Value type of the data.
 

Public Member Functions

 handle_sso ()
 Default constructor.
 
template<OwningHandle< value_type > H>
 handle_sso (H const &h)
 Construct a handle by making a deep copy of the data from another owning handle.
 
 handle_sso (handle_sso &&h) noexcept
 Move constructor either copies the heap pointers or makes a deep copy of the stack data.
 
 handle_sso (handle_sso const &h)
 Copy construct a handle by making a deep copy of the data from the source handle.
 
 handle_sso (long size)
 Construct a handle for the data of a given size and initialize it depending on the value type.
 
 handle_sso (long size, do_not_initialize_t)
 Construct a handle for the data of a given size and do not initialize it.
 
 handle_sso (long size, init_zero_t)
 Construct a handle for the data of a given size and initialize it to zero (only for scalar and complex types).
 
 ~handle_sso () noexcept
 Destructor for the handle.
 
T * data () const noexcept
 Get a pointer to the stored data.
 
bool is_null () const noexcept
 Check if the handle is in a null state.
 
bool on_heap () const
 Check if the data is/should be stored on the heap.
 
handle_ssooperator= (handle_sso &&h) noexcept
 Move assignment operator first releases the resources held by the current handle and then either copies the heap pointers or makes a deep copy of the stack data.
 
handle_ssooperator= (handle_sso const &h) noexcept
 Copy assignment operator first cleans up the current handle and then makes a deep copy of the data from the source handle.
 
T const & operator[] (long i) const noexcept
 Subscript operator to access the data.
 
T & operator[] (long i) noexcept
 Subscript operator to access the data.
 
long size () const noexcept
 Get the size of the handle.
 

Static Public Attributes

static constexpr auto address_space = Host
 nda::mem::AddressSpace in which the memory is allocated.
 

Constructor & Destructor Documentation

◆ ~handle_sso()

template<typename T , size_t Size>
nda::mem::handle_sso< T, Size >::~handle_sso ( )
inlinenoexcept

Destructor for the handle.

For non-trivial objects, it explicitly calls their destructors. Additionally, it deallocates the memory if it is stored on the heap and resets the handle to its null state.

Definition at line 535 of file handle.hpp.

◆ handle_sso() [1/6]

template<typename T , size_t Size>
nda::mem::handle_sso< T, Size >::handle_sso ( handle_sso< T, Size > && h)
inlinenoexcept

Move constructor either copies the heap pointers or makes a deep copy of the stack data.

In both cases, it resets the source handle to a null state.

Parameters
hSource handle.

Definition at line 542 of file handle.hpp.

◆ handle_sso() [2/6]

template<typename T , size_t Size>
nda::mem::handle_sso< T, Size >::handle_sso ( handle_sso< T, Size > const & h)
inline

Copy construct a handle by making a deep copy of the data from the source handle.

Parameters
hSource handle.

Definition at line 583 of file handle.hpp.

◆ handle_sso() [3/6]

template<typename T , size_t Size>
template<OwningHandle< value_type > H>
nda::mem::handle_sso< T, Size >::handle_sso ( H const & h)
inlineexplicit

Construct a handle by making a deep copy of the data from another owning handle.

Depending on the size, the memory of the data is either allocated on the heap or on the stack.

Parameters
hSource handle.

Definition at line 620 of file handle.hpp.

◆ handle_sso() [4/6]

template<typename T , size_t Size>
nda::mem::handle_sso< T, Size >::handle_sso ( long size,
do_not_initialize_t  )
inline

Construct a handle for the data of a given size and do not initialize it.

Depending on the size, the memory of the data is either allocated on the heap or on the stack.

Parameters
sizeSize of the data (number of elements).

Definition at line 636 of file handle.hpp.

◆ handle_sso() [5/6]

template<typename T , size_t Size>
nda::mem::handle_sso< T, Size >::handle_sso ( long size,
init_zero_t  )
inline

Construct a handle for the data of a given size and initialize it to zero (only for scalar and complex types).

Depending on the size, the memory of the data is either allocated on the heap or on the stack.

Parameters
sizeSize of the data (number of elements).

Definition at line 656 of file handle.hpp.

◆ handle_sso() [6/6]

template<typename T , size_t Size>
nda::mem::handle_sso< T, Size >::handle_sso ( long size)
inline

Construct a handle for the data of a given size and initialize it depending on the value type.

The data is initialized as follows:

  • If T is std::complex and nda::mem::init_dcmplx is true, the data is initialized to zero.
  • If T is not trivial and not complex, the data is default constructed by placement new operator calls.
  • Otherwise, the data is not initialized.
Parameters
sizeSize of the data (number of elements).

Definition at line 680 of file handle.hpp.

Member Function Documentation

◆ data()

template<typename T , size_t Size>
T * nda::mem::handle_sso< T, Size >::data ( ) const
inlinenodiscardnoexcept

Get a pointer to the stored data.

Returns
Pointer to the start of the handled memory.

Definition at line 738 of file handle.hpp.

◆ is_null()

template<typename T , size_t Size>
bool nda::mem::handle_sso< T, Size >::is_null ( ) const
inlinenodiscardnoexcept

Check if the handle is in a null state.

Returns
True if the data is a nullptr (and the size is 0).

Definition at line 727 of file handle.hpp.

◆ on_heap()

template<typename T , size_t Size>
bool nda::mem::handle_sso< T, Size >::on_heap ( ) const
inlinenodiscard

Check if the data is/should be stored on the heap.

Returns
True if the size is greater than the Size.

Definition at line 721 of file handle.hpp.

◆ operator=() [1/2]

template<typename T , size_t Size>
handle_sso & nda::mem::handle_sso< T, Size >::operator= ( handle_sso< T, Size > && h)
inlinenoexcept

Move assignment operator first releases the resources held by the current handle and then either copies the heap pointers or makes a deep copy of the stack data.

In both cases, it resets the source handle to a null state.

Parameters
hSource handle.

Definition at line 563 of file handle.hpp.

◆ operator=() [2/2]

template<typename T , size_t Size>
handle_sso & nda::mem::handle_sso< T, Size >::operator= ( handle_sso< T, Size > const & h)
inlinenoexcept

Copy assignment operator first cleans up the current handle and then makes a deep copy of the data from the source handle.

Parameters
hSource handle.

Definition at line 598 of file handle.hpp.

◆ operator[]() [1/2]

template<typename T , size_t Size>
T const & nda::mem::handle_sso< T, Size >::operator[] ( long i) const
inlinenodiscardnoexcept

Subscript operator to access the data.

Parameters
iIndex of the element to access.
Returns
Const reference to the element at the given index.

Definition at line 715 of file handle.hpp.

◆ operator[]() [2/2]

template<typename T , size_t Size>
T & nda::mem::handle_sso< T, Size >::operator[] ( long i)
inlinenodiscardnoexcept

Subscript operator to access the data.

Parameters
iIndex of the element to access.
Returns
Reference to the element at the given index.

Definition at line 707 of file handle.hpp.

◆ size()

template<typename T , size_t Size>
long nda::mem::handle_sso< T, Size >::size ( ) const
inlinenodiscardnoexcept

Get the size of the handle.

Returns
Number of elements of type T in the handled memory.

Definition at line 744 of file handle.hpp.


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