Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
roc::core::Slice< T > Class Template Reference

Slice. More...

#include <slice.h>

Public Member Functions

 Slice ()
 Construct empty slice. More...
 
 Slice (const SharedPtr< Buffer< T > > &buffer)
 Construct slice pointing to the whole buffer. More...
 
 Slice (Buffer< T > &buffer, size_t from, size_t to)
 Construct slice pointing to a part of a buffer. More...
 
T * data () const
 Get slice data. More...
 
T * data_end () const
 Pointer to the next after the last element in slice. More...
 
size_t size () const
 Get number of elements in slice. More...
 
size_t capacity () const
 Get maximum possible number of elements in slice. More...
 
void reslice (size_t from, size_t to)
 Change slice beginning and ending inside the buffer. More...
 
T * extend (const size_t add_sz)
 Increase size() by add_sz. More...
 
Slice subslice (size_t from, size_t to) const
 Construct a slice pointing to a part of this slice. More...
 
void print () const
 Print slice to stderr. More...
 
T & operator[] (const size_t i) const
 Access to an element of the Slice with an array style. More...
 
 operator const struct unspecified_bool * () const
 Convert to bool. More...
 

Detailed Description

template<class T>
class roc::core::Slice< T >

Slice.

Slice<T> points to a subrange of data in Buffer<T>, where T defines the element type, e.g. uint8_t for byte buffer.

Copying a slice produces a new slice referring the same data.

Slice also acts as a kind of shared pointer to Buffer. A buffer wont be freed (returned to pool) until there are slices referring it. Copying a slice increments the buffer reference counter, and destroying a slice decrements it.

Slice has two important characteristics:

  • size - the difference between the ending end beginning pointers
  • capacity - the difference between the actual buffer end and the slice beginning pointers

Buffers are not resizable. They're allocated from pool and have fixed size, defined by the pool parameters.

Slices are resliceable, which means that their pointers to the buffer data may be moved withing the buffer.

The beginning pointer may be moved only forward. Once moved, it's not allowed to move it backward again. Moving it decreases the slice size and capacity. Capacity is affected because it's relative to the beginning pointer.

The ending pointer maybe freely moved forward and backward withing the slice capacity. Moving it affects the slice size, but not capacity.

In other words, slice capacity may be only decreased by moving beginning pointer, and slice size may be freely changed withing the slice capacity by moving both beginning and ending pointers.

Definition at line 54 of file slice.h.

Constructor & Destructor Documentation

◆ Slice() [1/3]

template<class T >
roc::core::Slice< T >::Slice ( )
inline

Construct empty slice.

Definition at line 57 of file slice.h.

◆ Slice() [2/3]

template<class T >
roc::core::Slice< T >::Slice ( const SharedPtr< Buffer< T > > &  buffer)
inline

Construct slice pointing to the whole buffer.

Definition at line 64 of file slice.h.

◆ Slice() [3/3]

template<class T >
roc::core::Slice< T >::Slice ( Buffer< T > &  buffer,
size_t  from,
size_t  to 
)
inline

Construct slice pointing to a part of a buffer.

Definition at line 76 of file slice.h.

Member Function Documentation

◆ capacity()

template<class T >
size_t roc::core::Slice< T >::capacity ( ) const
inline

Get maximum possible number of elements in slice.

Definition at line 113 of file slice.h.

◆ data()

template<class T >
T* roc::core::Slice< T >::data ( ) const
inline

Get slice data.

Definition at line 92 of file slice.h.

◆ data_end()

template<class T >
T* roc::core::Slice< T >::data_end ( ) const
inline

Pointer to the next after the last element in slice.

Definition at line 100 of file slice.h.

◆ extend()

template<class T >
T* roc::core::Slice< T >::extend ( const size_t  add_sz)
inline

Increase size() by add_sz.

Returns
Pointer to the first element of extended range.

Definition at line 145 of file slice.h.

◆ operator const struct unspecified_bool *()

template<class T >
roc::core::Slice< T >::operator const struct unspecified_bool * ( ) const
inline

Convert to bool.

Returns
true if the slice is attached to buffer, even if it has zero length.

Definition at line 202 of file slice.h.

◆ operator[]()

template<class T >
T& roc::core::Slice< T >::operator[] ( const size_t  i) const
inline

Access to an element of the Slice with an array style.

Definition at line 188 of file slice.h.

◆ print()

template<class T >
void roc::core::Slice< T >::print ( ) const
inline

Print slice to stderr.

Definition at line 179 of file slice.h.

◆ reslice()

template<class T >
void roc::core::Slice< T >::reslice ( size_t  from,
size_t  to 
)
inline

Change slice beginning and ending inside the buffer.

Remarks
  • from and to are relative to slice beginning.
  • to value can be up to capacity().

Definition at line 125 of file slice.h.

◆ size()

template<class T >
size_t roc::core::Slice< T >::size ( ) const
inline

Get number of elements in slice.

Definition at line 108 of file slice.h.

◆ subslice()

template<class T >
Slice roc::core::Slice< T >::subslice ( size_t  from,
size_t  to 
) const
inline

Construct a slice pointing to a part of this slice.

Remarks
  • from and to are relative to slice beginning.
  • to value can be up to size().

Definition at line 161 of file slice.h.


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