glibmm 2.66.5
Public Types | Public Member Functions | List of all members
Glib::Container_Helpers::ArrayKeeper< Tr > Class Template Reference

A keeper class for C array. More...

#include <glibmm/vectorutils.h>

Public Types

using CppType = typename Tr::CppType
 
using CType = typename Tr::CType
 

Public Member Functions

 ArrayKeeper (const CType * array, std::size_t array_size, Glib::OwnershipType ownership)
 Constructs an ArrayKeeper holding array of size array_size. More...
 
 ArrayKeeper (const ArrayKeeper & keeper)
 
 ~ArrayKeeper () noexcept
 
CTypedata () const
 Gets data the keeper holds. More...
 

Detailed Description

template<typename Tr>
class Glib::Container_Helpers::ArrayKeeper< Tr >

A keeper class for C array.

Primarily used by C++ wrappers like gtkmm.

Its main purpose is to free its data when they are not needed. What will be destroyed depends on passed ownership upon construction.

The most common usage of Glib::ArrayKeeper is getting its data when converting std::vector to a C array:

void G::Temp::do_something(const std::vector<int>& v)
{
g_temp_do_something(gobj(), Glib::ArrayHandler<int>::vector_to_array(v).data());
}
A utility for converting between std::vector and plain C arrays.
Definition: vectorutils.h:467
CType * data() const
Gets data the keeper holds.

Variables of this class are seldom defined directly - it is mostly used as a temporary variable returned by Glib::ArrayHandler::vector_to_array().

Note that the usage above is correct with regards to C++ standard point 12.2.3. That means that data returned by data() method is valid through whole g_temp_do_something function and is destroyed, when this function returns.

Member Typedef Documentation

◆ CppType

template <typename Tr >
using Glib::Container_Helpers::ArrayKeeper< Tr >::CppType = typename Tr::CppType

◆ CType

template <typename Tr >
using Glib::Container_Helpers::ArrayKeeper< Tr >::CType = typename Tr::CType

Constructor & Destructor Documentation

◆ ArrayKeeper() [1/2]

template <typename Tr >
Glib::Container_Helpers::ArrayKeeper< Tr >::ArrayKeeper ( const CType array,
std::size_t  array_size,
Glib::OwnershipType  ownership 
)
inlineexplicit

Constructs an ArrayKeeper holding array of size array_size.

ownership tells what should be destroyed with keeper destruction:

Parameters
array- C array to hold.
array_size- length of array.
ownership- ownership definition.

◆ ArrayKeeper() [2/2]

template <typename Tr >
Glib::Container_Helpers::ArrayKeeper< Tr >::ArrayKeeper ( const ArrayKeeper< Tr > &  keeper)
inline

◆ ~ArrayKeeper()

template <typename Tr >
Glib::Container_Helpers::ArrayKeeper< Tr >::~ArrayKeeper ( )
noexcept

Member Function Documentation

◆ data()

template <typename Tr >
CType * Glib::Container_Helpers::ArrayKeeper< Tr >::data ( ) const
inline

Gets data the keeper holds.

Note that this data is owned by the keeper, so there is no need to free it.

Returns
C array owned by ArrayKeeper.