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 BufferPtr &buffer)
 Construct slice pointing to the whole buffer. More...
 
 Slice (Buffer &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 points to a subrange of data in pool-allocated 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 won't 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.

While Buffer works with raw bytes, Slice<T> interprets it as array of elements of type T, and works in terms of those elements.

Slice has two important characteristics:

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

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

Slices are reslicable, which means that their pointers to the buffer data may be moved within 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 may be freely moved forward and backward within 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 within the slice capacity by moving both beginning and ending pointers.

Definition at line 55 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 58 of file slice.h.

◆ Slice() [2/3]

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

Construct slice pointing to the whole buffer.

Definition at line 65 of file slice.h.

◆ Slice() [3/3]

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

Construct slice pointing to a part of a buffer.

Definition at line 77 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 114 of file slice.h.

◆ data()

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

Get slice data.

Definition at line 93 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 101 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 146 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 204 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 190 of file slice.h.

◆ print()

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

Print slice to stderr.

Definition at line 180 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 126 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 109 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 162 of file slice.h.


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