SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk::ArrayView< T > Class Template Reference

Lightweight, non-owning view over a contiguous sequence of elements. More...

#include <stk_common.h>

Public Types

typedef T value_type

Public Member Functions

 ArrayView ()
 Construct an empty ArrayView with a null pointer and zero size.
 ArrayView (T *ptr, size_t size)
 Construct an ArrayView from a raw pointer and size.
T & operator[] (size_t index) const
 Subscript operator for element access.
T * GetPtr ()
 Get pointer to the beginning of elements in the view.
const T * GetPtr () const
 Get constant pointer to the beginning of elements in the view.
size_t GetSize () const
 Get number of elements in the view.

Private Member Functions

T & UncheckedAt (size_t index) const
 Deviation MISRA-CPP-2008-5-0-15 \reason Array indexing on a base pointer is required for a dynamic-size non-owning view. The pointer and size are externally supplied. Bounds are enforced by the public API (STK_ASSERT / operator[]).

Private Attributes

T * m_ptr
 Pointer to the underlying memory block.
size_t m_size
 Total number of elements in the view.

Detailed Description

template<typename T>
class stk::ArrayView< T >

Lightweight, non-owning view over a contiguous sequence of elements.

Note
Provides a safe encapsulation over raw pointers without copying the underlying data. Includes binary serialization methods for compact state transfer on 32-bit platforms.

Usage example:

stk::ITask *tasks[TASKS_MAX];
ArrayView<stk::ITask *> view(tasks, TASKS_MAX);
ArrayView()
Construct an empty ArrayView with a null pointer and zero size.
Definition stk_common.h:260
Interface for a user task.
Definition stk_common.h:755

Definition at line 253 of file stk_common.h.

Member Typedef Documentation

◆ value_type

template<typename T>
typedef T stk::ArrayView< T >::value_type

Definition at line 256 of file stk_common.h.

Constructor & Destructor Documentation

◆ ArrayView() [1/2]

template<typename T>
stk::ArrayView< T >::ArrayView ( )
inline

Construct an empty ArrayView with a null pointer and zero size.

Definition at line 260 of file stk_common.h.

260 : m_ptr(nullptr), m_size(0U)
261 {}
Lightweight, non-owning view over a contiguous sequence of elements.
Definition stk_common.h:254
T * m_ptr
Pointer to the underlying memory block.
Definition stk_common.h:307
size_t m_size
Total number of elements in the view.
Definition stk_common.h:308

References m_ptr, and m_size.

◆ ArrayView() [2/2]

template<typename T>
stk::ArrayView< T >::ArrayView ( T * ptr,
size_t size )
inline

Construct an ArrayView from a raw pointer and size.

Parameters
[in]ptrPointer to the first element of the contiguous memory block.
[in]sizeNumber of elements available in the memory block.

Definition at line 267 of file stk_common.h.

267 : m_ptr(ptr), m_size(size)
268 {}

References m_ptr, and m_size.

Member Function Documentation

◆ GetPtr() [1/2]

template<typename T>
T * stk::ArrayView< T >::GetPtr ( )
inline

Get pointer to the beginning of elements in the view.

Returns
Pointer to the elements of array.

Definition at line 284 of file stk_common.h.

284{ return m_ptr; }

References m_ptr.

◆ GetPtr() [2/2]

template<typename T>
const T * stk::ArrayView< T >::GetPtr ( ) const
inline

Get constant pointer to the beginning of elements in the view.

Returns
Constant pointer to the elements of array.

Definition at line 289 of file stk_common.h.

289{ return m_ptr; }

References m_ptr.

◆ GetSize()

◆ operator[]()

template<typename T>
T & stk::ArrayView< T >::operator[] ( size_t index) const
inline

Subscript operator for element access.

Parameters
[in]indexElement index to access.
Returns
Reference to the element at the specified index.
Warning
Triggers STK_ASSERT if index is out of bounds in a Debug build.

Definition at line 275 of file stk_common.h.

276 {
277 STK_ASSERT(index < m_size); // enforces MISRA Rule 5-0-16
278 return UncheckedAt(index); // MISRA Rule 5-0-15 deviation centralized
279 }
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:516
T & UncheckedAt(size_t index) const
Deviation MISRA-CPP-2008-5-0-15 \reason Array indexing on a base pointer is required for a dynamic-si...
Definition stk_common.h:302

References m_size, STK_ASSERT, and UncheckedAt().

Here is the call graph for this function:

◆ UncheckedAt()

template<typename T>
T & stk::ArrayView< T >::UncheckedAt ( size_t index) const
inlineprivate

Deviation MISRA-CPP-2008-5-0-15 \reason Array indexing on a base pointer is required for a dynamic-size non-owning view. The pointer and size are externally supplied. Bounds are enforced by the public API (STK_ASSERT / operator[]).

Definition at line 302 of file stk_common.h.

303 {
304 return m_ptr[index]; // deviation: indexing non-array pointer
305 }

References __stk_forceinline, and m_ptr.

Referenced by operator[]().

Here is the caller graph for this function:

Member Data Documentation

◆ m_ptr

template<typename T>
T* stk::ArrayView< T >::m_ptr
private

Pointer to the underlying memory block.

Definition at line 307 of file stk_common.h.

Referenced by ArrayView(), ArrayView(), GetPtr(), GetPtr(), and UncheckedAt().

◆ m_size

template<typename T>
size_t stk::ArrayView< T >::m_size
private

Total number of elements in the view.

Definition at line 308 of file stk_common.h.

Referenced by ArrayView(), ArrayView(), GetSize(), and operator[]().


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