E-MailRelay
Public Types | Public Member Functions | List of all members
G::Buffer< T > Struct Template Reference

A substitute for std::vector<char> that has more useful alignment guarantees and explicitly avoids default initialisation of each element. More...

#include <gbuffer.h>

Public Types

using value_type = T
 
using iterator = T *
 
using const_iterator = const T *
 

Public Member Functions

 Buffer (std::size_t n)
 
void reserve (std::size_t n)
 
void resize (std::size_t n)
 
const T & operator[] (std::size_t i) const noexcept
 
T & operator[] (std::size_t i) noexcept
 
const T & at (std::size_t i) const
 
T & at (std::size_t i)
 
std::size_t size () const noexcept
 
std::size_t capacity () const noexcept
 
bool empty () const noexcept
 
void clear () noexcept
 
void shrink_to_fit () noexcept
 
iterator begin () noexcept
 
iterator end () noexcept
 
const value_type * data () const noexcept
 
value_type * data () noexcept
 
const_iterator begin () const noexcept
 
const_iterator end () const noexcept
 
const_iterator cbegin () const noexcept
 
const_iterator cend () const noexcept
 
iterator erase (iterator range_begin, iterator range_end) noexcept
 
template<typename U >
void insert (iterator p, U range_begin, U range_end)
 
template<typename U >
void insert (iterator p, U *range_begin, U *range_end)
 
void swap (Buffer< T > &other) noexcept
 
 Buffer (const Buffer< T > &other)
 
 Buffer (Buffer< T > &&other) noexcept
 
Buffer< T > & operator= (const Buffer< T > &other)
 
Buffer< T > & operator= (Buffer< T > &&other) noexcept
 
template<typename U >
T * aligned ()
 

Detailed Description

template<typename T>
struct G::Buffer< T >

A substitute for std::vector<char> that has more useful alignment guarantees and explicitly avoids default initialisation of each element.

The alignment is that of std::malloc(), ie. std::max_align_t. (See also posix_memalign().)

The buffer_cast() free function can be used to return a pointer to the start of the buffer for some aggregate type, throwing if the buffer is too small for a complete object:

Buffer<char> buffer( 100 ) ;
auto n = fill( buffer ) ;
buffer.resize( n ) ;
Foo * foo_p = buffer_cast<Foo*>( buffer ) ;
...
foo_p->~Foo() ;

The implementation of buffer_cast uses placement-new in order to avoid the undefined behaviour of a pointer cast. If the buffer_cast type has a non-trivial destructor then the destructor should be called explicitly through the pointer before the G::Buffer object disappears.

Definition at line 62 of file gbuffer.h.

Member Typedef Documentation

◆ const_iterator

template<typename T >
using G::Buffer< T >::const_iterator = const T *

Definition at line 67 of file gbuffer.h.

◆ iterator

template<typename T >
using G::Buffer< T >::iterator = T *

Definition at line 66 of file gbuffer.h.

◆ value_type

template<typename T >
using G::Buffer< T >::value_type = T

Definition at line 65 of file gbuffer.h.

Constructor & Destructor Documentation

◆ Buffer() [1/3]

template<typename T >
G::Buffer< T >::Buffer ( std::size_t  n)
inlineexplicit

Definition at line 69 of file gbuffer.h.

◆ ~Buffer()

template<typename T >
G::Buffer< T >::~Buffer ( )
inline

Definition at line 74 of file gbuffer.h.

◆ Buffer() [2/3]

template<typename T >
G::Buffer< T >::Buffer ( const Buffer< T > &  other)
inline

Definition at line 144 of file gbuffer.h.

◆ Buffer() [3/3]

template<typename T >
G::Buffer< T >::Buffer ( Buffer< T > &&  other)
inlinenoexcept

Definition at line 152 of file gbuffer.h.

Member Function Documentation

◆ aligned()

template<typename T >
template<typename U >
T * G::Buffer< T >::aligned ( )
inline

Definition at line 166 of file gbuffer.h.

◆ at() [1/2]

template<typename T >
T & G::Buffer< T >::at ( std::size_t  i)
inline

Definition at line 91 of file gbuffer.h.

◆ at() [2/2]

template<typename T >
const T & G::Buffer< T >::at ( std::size_t  i) const
inline

Definition at line 90 of file gbuffer.h.

◆ begin() [1/2]

template<typename T >
const_iterator G::Buffer< T >::begin ( ) const
inlinenoexcept

Definition at line 101 of file gbuffer.h.

◆ begin() [2/2]

template<typename T >
iterator G::Buffer< T >::begin ( )
inlinenoexcept

Definition at line 97 of file gbuffer.h.

◆ capacity()

template<typename T >
std::size_t G::Buffer< T >::capacity ( ) const
inlinenoexcept

Definition at line 93 of file gbuffer.h.

◆ cbegin()

template<typename T >
const_iterator G::Buffer< T >::cbegin ( ) const
inlinenoexcept

Definition at line 103 of file gbuffer.h.

◆ cend()

template<typename T >
const_iterator G::Buffer< T >::cend ( ) const
inlinenoexcept

Definition at line 104 of file gbuffer.h.

◆ clear()

template<typename T >
void G::Buffer< T >::clear ( )
inlinenoexcept

Definition at line 95 of file gbuffer.h.

◆ data() [1/2]

template<typename T >
const value_type * G::Buffer< T >::data ( ) const
inlinenoexcept

Definition at line 99 of file gbuffer.h.

◆ data() [2/2]

template<typename T >
value_type * G::Buffer< T >::data ( )
inlinenoexcept

Definition at line 100 of file gbuffer.h.

◆ empty()

template<typename T >
bool G::Buffer< T >::empty ( ) const
inlinenoexcept

Definition at line 94 of file gbuffer.h.

◆ end() [1/2]

template<typename T >
const_iterator G::Buffer< T >::end ( ) const
inlinenoexcept

Definition at line 102 of file gbuffer.h.

◆ end() [2/2]

template<typename T >
iterator G::Buffer< T >::end ( )
inlinenoexcept

Definition at line 98 of file gbuffer.h.

◆ erase()

template<typename T >
iterator G::Buffer< T >::erase ( iterator  range_begin,
iterator  range_end 
)
inlinenoexcept

Definition at line 105 of file gbuffer.h.

◆ insert() [1/2]

template<typename T >
template<typename U >
void G::Buffer< T >::insert ( iterator  p,
U *  range_begin,
U *  range_end 
)
inline

Definition at line 129 of file gbuffer.h.

◆ insert() [2/2]

template<typename T >
template<typename U >
void G::Buffer< T >::insert ( iterator  p,
range_begin,
range_end 
)
inline

Definition at line 120 of file gbuffer.h.

◆ operator=() [1/2]

template<typename T >
Buffer< T > & G::Buffer< T >::operator= ( Buffer< T > &&  other)
inlinenoexcept

Definition at line 161 of file gbuffer.h.

◆ operator=() [2/2]

template<typename T >
Buffer< T > & G::Buffer< T >::operator= ( const Buffer< T > &  other)
inline

Definition at line 156 of file gbuffer.h.

◆ operator[]() [1/2]

template<typename T >
const T & G::Buffer< T >::operator[] ( std::size_t  i) const
inlinenoexcept

Definition at line 88 of file gbuffer.h.

◆ operator[]() [2/2]

template<typename T >
T & G::Buffer< T >::operator[] ( std::size_t  i)
inlinenoexcept

Definition at line 89 of file gbuffer.h.

◆ reserve()

template<typename T >
void G::Buffer< T >::reserve ( std::size_t  n)
inline

Definition at line 78 of file gbuffer.h.

◆ resize()

template<typename T >
void G::Buffer< T >::resize ( std::size_t  n)
inline

Definition at line 87 of file gbuffer.h.

◆ shrink_to_fit()

template<typename T >
void G::Buffer< T >::shrink_to_fit ( )
inlinenoexcept

Definition at line 96 of file gbuffer.h.

◆ size()

template<typename T >
std::size_t G::Buffer< T >::size ( ) const
inlinenoexcept

Definition at line 92 of file gbuffer.h.

◆ swap()

template<typename T >
void G::Buffer< T >::swap ( Buffer< T > &  other)
inlinenoexcept

Definition at line 138 of file gbuffer.h.


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