Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::Segment< VTable > Struct Template Reference

A slice of contiguous memory, prefixed by and header containing size and capacity. More...

#include <Segment.h>

Inheritance diagram for SC::Segment< VTable >:
SC::Vector< SC::VectorMapItem< Key, Value > > SC::Vector< Value >

Public Types

using T = typename VTable::Type
 

Public Member Functions

 Segment (Segment &&other)
 
 Segment (const Segment &other)
 
Segmentoperator= (Segment &&other)
 
Segmentoperator= (const Segment &other)
 
template<typename U >
 Segment (Span< const U > span)
 
 Segment (Span< const T > span)
 
 Segment (std::initializer_list< T > list)
 
bool resizeWithoutInitializing (size_t newSize)
 Re-allocates to the requested new size, preserving its contents. More...
 
bool resize (size_t newSize, const T &value=T())
 Re-allocates to the requested new size, preserving its contents and setting new items to value. More...
 
bool reserve (size_t newCapacity)
 Reserves capacity to avoid heap-allocation during a future append, assign or resize. More...
 
bool append (Span< const T > span)
 Appends a Span to the end of the segment. More...
 
template<typename U >
bool append (Span< const U > span)
 Appends a Span of items convertible to T to the end of the segment. More...
 
bool appendMove (Segment &&other)
 Moves contents of another segment to the end of this segment. More...
 
bool shrink_to_fit ()
 Ensures capacity == size re-allocating (if capacity>size) or freeing ( if size==0) memory. More...
 
void clear ()
 Sets size to zero without freeing any memory (use shrink_to_fit() to free memory) More...
 
bool assign (Span< const T > span)
 Replaces contents with contents of the span. More...
 
bool assignMove (Segment &&other)
 Replaces content moving (possibly "stealing") content of another segment. More...
 
bool push_back (const T &value)
 Appends a single element to the end of the segment. More...
 
bool push_back (T &&value)
 Moves a single element to the end of the segment. More...
 
bool push_front (const T &value)
 Appends a single element to the start of the segment. More...
 
bool pop_back (T *removedValue=nullptr)
 Removes the last element of the segment. More...
 
bool pop_front (T *removedValue=nullptr)
 Removes the first element of the segment. More...
 
const T * data () const SC_LANGUAGE_LIFETIME_BOUND
 Access data owned by the segment or nullptr if segment is empty. More...
 
T * data () SC_LANGUAGE_LIFETIME_BOUND
 Access data owned by the segment or nullptr if segment is empty. More...
 
T * begin () SC_LANGUAGE_LIFETIME_BOUND
 
const T * begin () const SC_LANGUAGE_LIFETIME_BOUND
 
T * end () SC_LANGUAGE_LIFETIME_BOUND
 
const T * end () const SC_LANGUAGE_LIFETIME_BOUND
 
T & back () SC_LANGUAGE_LIFETIME_BOUND
 
T & front () SC_LANGUAGE_LIFETIME_BOUND
 
T & operator[] (size_t idx) SC_LANGUAGE_LIFETIME_BOUND
 
const T & back () const SC_LANGUAGE_LIFETIME_BOUND
 
const T & front () const SC_LANGUAGE_LIFETIME_BOUND
 
const T & operator[] (size_t idx) const SC_LANGUAGE_LIFETIME_BOUND
 
bool isInlineBuffer () const
 Returns true if an inline buffer is in use (false if segment is heap allocated). More...
 
bool isEmpty () const
 Check if is empty (size() == 0) More...
 
Span< T > toSpan () SC_LANGUAGE_LIFETIME_BOUND
 Obtains a Span of internal contents. More...
 
Span< const T > toSpanConst () const SC_LANGUAGE_LIFETIME_BOUND
 Obtains a Span of internal contents. More...
 
size_t size () const
 Returns current size. More...
 
size_t capacity ()
 Returns current capacity (always >= of size()) More...
 
bool removeRange (size_t start, size_t length)
 Removes the range [start, start + length] from the segment. More...
 
bool removeAt (size_t index)
 Removes the element at index. More...
 
bool insert (size_t index, Span< const T > data)
 Insert a span at the given index. More...
 
void unsafeSetHeader (SegmentHeader *newHeader)
 Sets the internal header handled by this class. More...
 
SegmentHeaderunsafeGetHeader () const
 Get the internal header handled by this class. More...
 
 Segment (SegmentHeader &inlineHeader, uint32_t capacityInBytes)
 Builds a Segment with an inlineHeader of given capacity in bytes. More...
 

Protected Attributes

SegmentHeaderheader
 

Detailed Description

template<typename VTable>
struct SC::Segment< VTable >

A slice of contiguous memory, prefixed by and header containing size and capacity.

Can act as a simple byte buffer or more of a "vector-like" class depending on the passed in VTable traits. It transparently handles going to and from an inline buffer (defined in derived classes) and the heap.

Template Parameters
VTableprovides copy / move / destruct operations (see SC::SegmentTrivial as an example)
Note
Implementation is in .inl to reduce include bloat for non-templated derived classes like SC::Buffer. This reduces header bloat as the .inl can be included where derived class is defined (typically a .cpp file).

Constructor & Destructor Documentation

◆ Segment()

template<typename VTable >
SC::Segment< VTable >::Segment ( SegmentHeader inlineHeader,
uint32_t  capacityInBytes 
)

Builds a Segment with an inlineHeader of given capacity in bytes.

Member Function Documentation

◆ append() [1/2]

template<typename VTable >
bool SC::Segment< VTable >::append ( Span< const T >  span)

Appends a Span to the end of the segment.

◆ append() [2/2]

template<typename VTable >
template<typename U >
bool SC::Segment< VTable >::append ( Span< const U >  span)

Appends a Span of items convertible to T to the end of the segment.

◆ appendMove()

template<typename VTable >
bool SC::Segment< VTable >::appendMove ( Segment< VTable > &&  other)

Moves contents of another segment to the end of this segment.

◆ assign()

template<typename VTable >
bool SC::Segment< VTable >::assign ( Span< const T >  span)

Replaces contents with contents of the span.

Note
This method allows detecting allocation failures (unlike the assignment operator)

◆ assignMove()

template<typename VTable >
bool SC::Segment< VTable >::assignMove ( Segment< VTable > &&  other)

Replaces content moving (possibly "stealing") content of another segment.

Note
This method allows detecting allocation failures (unlike the assignment operator)

◆ capacity()

template<typename VTable >
size_t SC::Segment< VTable >::capacity ( )
inline

Returns current capacity (always >= of size())

◆ clear()

template<typename VTable >
void SC::Segment< VTable >::clear ( )

Sets size to zero without freeing any memory (use shrink_to_fit() to free memory)

◆ data() [1/2]

template<typename VTable >
const T * SC::Segment< VTable >::data ( ) const
inline

Access data owned by the segment or nullptr if segment is empty.

◆ data() [2/2]

template<typename VTable >
T * SC::Segment< VTable >::data ( )
inline

Access data owned by the segment or nullptr if segment is empty.

◆ insert()

template<typename VTable >
bool SC::Segment< VTable >::insert ( size_t  index,
Span< const T >  data 
)

Insert a span at the given index.

Parameters
indexIndex where span should be inserted
dataData that will be inserted at index idx (by copy)
Returns
true if index is less than or equal to size()

◆ isEmpty()

template<typename VTable >
bool SC::Segment< VTable >::isEmpty ( ) const
inline

Check if is empty (size() == 0)

◆ isInlineBuffer()

template<typename VTable >
bool SC::Segment< VTable >::isInlineBuffer ( ) const
inline

Returns true if an inline buffer is in use (false if segment is heap allocated).

◆ pop_back()

template<typename VTable >
bool SC::Segment< VTable >::pop_back ( T *  removedValue = nullptr)

Removes the last element of the segment.

Parameters
removedValueLast item will be moved in the value if != nullptr
Returns
true if element was successfully removed (false if segment is empty)

◆ pop_front()

template<typename VTable >
bool SC::Segment< VTable >::pop_front ( T *  removedValue = nullptr)

Removes the first element of the segment.

Parameters
removedValueFirst item will be moved in the value if != nullptr
Returns
true if element was successfully removed (false if segment is empty)

◆ push_back() [1/2]

template<typename VTable >
bool SC::Segment< VTable >::push_back ( const T &  value)
inline

Appends a single element to the end of the segment.

◆ push_back() [2/2]

template<typename VTable >
bool SC::Segment< VTable >::push_back ( T &&  value)

Moves a single element to the end of the segment.

◆ push_front()

template<typename VTable >
bool SC::Segment< VTable >::push_front ( const T &  value)
inline

Appends a single element to the start of the segment.

◆ removeAt()

template<typename VTable >
bool SC::Segment< VTable >::removeAt ( size_t  index)
inline

Removes the element at index.

Parameters
indexIndex where the item must be removed
Returns
true if index is within bounds

◆ removeRange()

template<typename VTable >
bool SC::Segment< VTable >::removeRange ( size_t  start,
size_t  length 
)

Removes the range [start, start + length] from the segment.

Parameters
startIndex where the item must be removed
lengthNumber of elements that will be removed
Returns
true if start + length is within bounds

◆ reserve()

template<typename VTable >
bool SC::Segment< VTable >::reserve ( size_t  newCapacity)

Reserves capacity to avoid heap-allocation during a future append, assign or resize.

◆ resize()

template<typename VTable >
bool SC::Segment< VTable >::resize ( size_t  newSize,
const T &  value = T() 
)

Re-allocates to the requested new size, preserving its contents and setting new items to value.

◆ resizeWithoutInitializing()

template<typename VTable >
bool SC::Segment< VTable >::resizeWithoutInitializing ( size_t  newSize)

Re-allocates to the requested new size, preserving its contents.

Note
To restore link with an inline buffer, resize to its capacity (or less) and call Segment::shrink

◆ shrink_to_fit()

template<typename VTable >
bool SC::Segment< VTable >::shrink_to_fit ( )

Ensures capacity == size re-allocating (if capacity>size) or freeing ( if size==0) memory.

Note
If isInlineBuffer() == true its capacity will not shrink.

◆ size()

template<typename VTable >
size_t SC::Segment< VTable >::size ( ) const
inline

Returns current size.

◆ toSpan()

template<typename VTable >
Span< T > SC::Segment< VTable >::toSpan ( )
inline

Obtains a Span of internal contents.

◆ toSpanConst()

template<typename VTable >
Span< const T > SC::Segment< VTable >::toSpanConst ( ) const
inline

Obtains a Span of internal contents.

◆ unsafeGetHeader()

template<typename VTable >
SegmentHeader * SC::Segment< VTable >::unsafeGetHeader ( ) const
inline

Get the internal header handled by this class.

◆ unsafeSetHeader()

template<typename VTable >
void SC::Segment< VTable >::unsafeSetHeader ( SegmentHeader newHeader)
inline

Sets the internal header handled by this class.


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