ThingsBoard Client SDK 0.16.0
Client SDK to connect with ThingsBoard IoT Platform from IoT devices (Arduino, Espressif, etc.)
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
Container< T, Capacity > Class Template Reference

Custom std::array or std::vector implementation that contains a partial vector-like interface implementation. Internal implementation is changed at compile-time to either use the heap if THINGSBOARD_ENABLE_DYNAMIC is set or the stack otherwise. More...

#include <Container.h>

Inheritance diagram for Container< T, Capacity >:
[legend]

Public Types

using value_type = T
 
using size_type = size_t
 
using reference = T &
 
using const_reference = T const &
 
using pointer = T *
 
using const_pointer = T const *
 
using iterator = pointer
 
using const_iterator = const_pointer
 

Public Member Functions

 Container ()
 Default constructor, simply initalizes the underlying c-style array with the necessary capacity. That capacity always has to be bigger than 0, because initalizing a 0 length c-style array can cause certain compilers to produce errors. More...
 
 Container (size_type count, const_reference value=T{})
 Creates the constructor with the given amount of elements, either x copies of the passed value or x default constructed instances of the underlying type used. Simply forwards the arguments to the insert method, meaning if the initally allocated Capacity is not big enough to hold all elements, then this method will assert and stop the application. More...
 
template<typename InputIterator >
 Container (InputIterator const &first, InputIterator const &last)
 Copies all elements from the given start to exclusively the given end iterator into the underlying data container. Simply forwards the arguments to the insert method, meaning if the initally allocated Capacity is not big enough to hold all elements, then this method will assert and stop the application. More...
 
template<typename Iterable_Container >
 Container (Iterable_Container const &container)
 Accesses the begin and end iterator of the given data container and forwards the call to the iterator based constructor. More...
 
template<typename InputIterator >
void assign (InputIterator const &first, InputIterator const &last)
 Copies all elements from the given start to exclusively the given end iterator into the underlying data container. Simply forwards the arguments to the insert method, meaning if the initally allocated Capacity is not big enough to hold all elements, then this method will assert and stop the application. More...
 
template<typename Container >
void assign (Container const &container)
 
bool empty () const
 Returns whether there are any elements in the underlying data container. More...
 
size_type size () const
 Gets the current amount of elements in the underlying data container. More...
 
size_type constexpr capacity () const
 Gets the maximum amount of elements that can be stored in the underlying data container. More...
 
iterator begin ()
 Returns an iterator to the first element of the underlying data container. If the array is empty, the returned iterator will be equal to end() More...
 
const_iterator begin () const
 Returns an iterator to the first element of the underlying data container. If the array is empty, the returned iterator will be equal to end() More...
 
const_iterator cbegin () const
 Returns an iterator to the first element of the underlying data container. If the array is empty, the returned iterator will be equal to end() More...
 
reference front ()
 Returns a reference to the first element of the array. If the array is empty this method will assert and stop the application, because there is no valid element to return. More...
 
const_reference front () const
 Returns a reference to the first element of the array. If the array is empty this method will assert and stop the application, because there is no valid element to return. More...
 
reference back ()
 Returns a reference to the last element of the array. If the array is empty this method will assert and stop the application, because there is no valid element to return. More...
 
const_reference back () const
 Returns a reference to the last element of the array. If the array is empty this method will assert and stop the application, because there is no valid element to return. More...
 
iterator end ()
 Returns a iterator to one-past-the-last element of the underlying data container. More...
 
const_iterator end () const
 Returns a iterator to one-past-the-last element of the underlying data container. More...
 
const_iterator cend () const
 Returns a iterator to one-past-the-last element of the underlying data container. More...
 
void push_back (const_reference element)
 Appends the given element at the end of the underlying data container. More...
 
template<typename InputIterator >
void insert (iterator position, InputIterator const &first, InputIterator const &last)
 Copies all elements from the given start to exclusively the given end iterator into the underlying data container. The copying is started from the position before the given iterator (position - 1) More...
 
void erase (const_iterator position)
 Removes the element at the given position, has to move all element one to the left if the iterator does not point to the last valid element in the underlying data container. More...
 
reference at (size_type const &index)
 Returns a reference to the element at specified location index, with bounds checking. More...
 
const_reference at (size_type const &index) const
 Returns a reference to the element at specified location index, with bounds checking. More...
 
reference operator[] (size_type index)
 Returns a reference to the element at specified location index. No bounds checking is performed. More...
 
const_reference operator[] (size_type index) const
 Returns a reference to the element at specified location index. No bounds checking is performed. More...
 
void clear ()
 Erases all elements from the container. After this call, size() returns zero. More...
 

Detailed Description

template<typename T, size_t Capacity>
class Container< T, Capacity >

Custom std::array or std::vector implementation that contains a partial vector-like interface implementation. Internal implementation is changed at compile-time to either use the heap if THINGSBOARD_ENABLE_DYNAMIC is set or the stack otherwise.

Note
Allows to use the exact same method calls independent on if the custom Container implementation or std::vector is used. Support for the vector-like interface is achieved through a simple index that keeps count of the elements that have been instantiated with actual values by the push or insert method. Iterator based support is achieved through returning a pointer, which can be automatically used the same as an iterator implementation tagged as std::random_iterator_tag. This allows to use the most efficient implementation of standard algorithms, while keeping the actual internal implementation as simple as possible.

The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements. This means that a pointer to an element of a container may be passed to any function that expects a pointer to an element of a c-array

Template Parameters
TThe type of the elements in the underlying data container. Must be both CopyAssignable (have a copy assignment operator, for the push_back operation) as well as be Default-Constructible (have a default constructor, for the construction of the intial state of the underlying data container)
CapacityAmount of elements that can ever be saved into the underlying data container, allows to wrap a simple c-array and allocate it on the stack. Attempting to allocate more elements is not possible, because the size is fixed at compile-time

Member Typedef Documentation

◆ const_iterator

template<typename T , size_t Capacity>
using Container< T, Capacity >::const_iterator = const_pointer

◆ const_pointer

template<typename T , size_t Capacity>
using Container< T, Capacity >::const_pointer = T const *

◆ const_reference

template<typename T , size_t Capacity>
using Container< T, Capacity >::const_reference = T const &

◆ iterator

template<typename T , size_t Capacity>
using Container< T, Capacity >::iterator = pointer

◆ pointer

template<typename T , size_t Capacity>
using Container< T, Capacity >::pointer = T *

◆ reference

template<typename T , size_t Capacity>
using Container< T, Capacity >::reference = T &

◆ size_type

template<typename T , size_t Capacity>
using Container< T, Capacity >::size_type = size_t

◆ value_type

template<typename T , size_t Capacity>
using Container< T, Capacity >::value_type = T

Constructor & Destructor Documentation

◆ Container() [1/4]

template<typename T , size_t Capacity>
Container< T, Capacity >::Container ( )
inline

Default constructor, simply initalizes the underlying c-style array with the necessary capacity. That capacity always has to be bigger than 0, because initalizing a 0 length c-style array can cause certain compilers to produce errors.

◆ Container() [2/4]

template<typename T , size_t Capacity>
Container< T, Capacity >::Container ( size_type  count,
const_reference  value = T{} 
)
inline

Creates the constructor with the given amount of elements, either x copies of the passed value or x default constructed instances of the underlying type used. Simply forwards the arguments to the insert method, meaning if the initally allocated Capacity is not big enough to hold all elements, then this method will assert and stop the application.

Parameters
countAmount of elements that we want to create and copy the value into
valueValue that should be copied into the created elements, default = T{}

◆ Container() [3/4]

template<typename T , size_t Capacity>
template<typename InputIterator >
Container< T, Capacity >::Container ( InputIterator const &  first,
InputIterator const &  last 
)
inline

Copies all elements from the given start to exclusively the given end iterator into the underlying data container. Simply forwards the arguments to the insert method, meaning if the initally allocated Capacity is not big enough to hold all elements, then this method will assert and stop the application.

Template Parameters
InputIteratorClass that allows for forward incrementable access to data of the given data container, allows for using / passing either std::vector or std::array. See https://en.cppreference.com/w/cpp/iterator/input_iterator for more information on the requirements of the iterator
Parameters
firstIterator pointing to the first element we want to copy into our underlying data container
lastIterator pointing to one past the end of the elements we want to copy into our underlying data container

◆ Container() [4/4]

template<typename T , size_t Capacity>
template<typename Iterable_Container >
Container< T, Capacity >::Container ( Iterable_Container< T, Capacity > const &  container)
inline

Accesses the begin and end iterator of the given data container and forwards the call to the iterator based constructor.

Template Parameters
Iterable_ContainerClass that contains the actual data we want to copy into our internal data container, requires access to a cbegin() and cend() method, that point to the first element and one past the last element we want to copy respectively. Both methods need to return atleast an InputIterator, allows for using / passing either std::vector or std::array. See https://en.cppreference.com/w/cpp/iterator/input_iterator for more information on the requirements of the iterator
Parameters
containerData container with cbegin() and cend() method that we want to copy fully into our underlying data container

Member Function Documentation

◆ assign() [1/2]

template<typename T , size_t Capacity>
template<typename Container >
void Container< T, Capacity >::assign ( Container< T, Capacity > const &  container)
inline

◆ assign() [2/2]

template<typename T , size_t Capacity>
template<typename InputIterator >
void Container< T, Capacity >::assign ( InputIterator const &  first,
InputIterator const &  last 
)
inline

Copies all elements from the given start to exclusively the given end iterator into the underlying data container. Simply forwards the arguments to the insert method, meaning if the initally allocated Capacity is not big enough to hold all elements, then this method will assert and stop the application.

Template Parameters
InputIteratorClass that allows for forward incrementable access to data of the given data container, allows for using / passing either std::vector or std::array. See https://en.cppreference.com/w/cpp/iterator/input_iterator for more information on the requirements of the iterator
Parameters
firstIterator pointing to the first element we want to copy into our underlying data container
lastIterator pointing to one past the end of the elements we want to copy into our underlying data container

◆ at() [1/2]

template<typename T , size_t Capacity>
reference Container< T, Capacity >::at ( size_type const &  index)
inline

Returns a reference to the element at specified location index, with bounds checking.

Note
If index is not within the range of the container, this method will assert and halt the application
Parameters
indexIndex of the element to return

◆ at() [2/2]

template<typename T , size_t Capacity>
const_reference Container< T, Capacity >::at ( size_type const &  index) const
inline

Returns a reference to the element at specified location index, with bounds checking.

Note
If index is not within the range of the container, this method will assert and halt the application
Parameters
indexIndex of the element to return

◆ back() [1/2]

template<typename T , size_t Capacity>
reference Container< T, Capacity >::back ( )
inline

Returns a reference to the last element of the array. If the array is empty this method will assert and stop the application, because there is no valid element to return.

Returns
Reference to the last element of the array

◆ back() [2/2]

template<typename T , size_t Capacity>
const_reference Container< T, Capacity >::back ( ) const
inline

Returns a reference to the last element of the array. If the array is empty this method will assert and stop the application, because there is no valid element to return.

Returns
Reference to the last element of the array

◆ begin() [1/2]

template<typename T , size_t Capacity>
iterator Container< T, Capacity >::begin ( )
inline

Returns an iterator to the first element of the underlying data container. If the array is empty, the returned iterator will be equal to end()

Returns
Iterator pointing to the first element of the underlying data container

◆ begin() [2/2]

template<typename T , size_t Capacity>
const_iterator Container< T, Capacity >::begin ( ) const
inline

Returns an iterator to the first element of the underlying data container. If the array is empty, the returned iterator will be equal to end()

Returns
Iterator pointing to the first element of the underlying data container

◆ capacity()

template<typename T , size_t Capacity>
size_type constexpr Container< T, Capacity >::capacity ( ) const
inlineconstexpr

Gets the maximum amount of elements that can be stored in the underlying data container.

Returns
The maximum amount of items that can be stored in the underlying data container

◆ cbegin()

template<typename T , size_t Capacity>
const_iterator Container< T, Capacity >::cbegin ( ) const
inline

Returns an iterator to the first element of the underlying data container. If the array is empty, the returned iterator will be equal to end()

Returns
Iterator pointing to the first element of the underlying data container

◆ cend()

template<typename T , size_t Capacity>
const_iterator Container< T, Capacity >::cend ( ) const
inline

Returns a iterator to one-past-the-last element of the underlying data container.

Returns
Iterator pointing to one-past-the-last element of the underlying data container

◆ clear()

template<typename T , size_t Capacity>
void Container< T, Capacity >::clear ( )
inline

Erases all elements from the container. After this call, size() returns zero.

Note
Simply sets the underlying size to 0, data will only be cleared in the destructor or if new data is inserted, which will overwrite old data. Therefore the user is also cautioned if the element is itself a pointer, the pointed-to memory is not touched in any way

◆ empty()

template<typename T , size_t Capacity>
bool Container< T, Capacity >::empty ( ) const
inline

Returns whether there are any elements in the underlying data container.

Returns
Whether the underlying data container is empty or not

◆ end() [1/2]

template<typename T , size_t Capacity>
iterator Container< T, Capacity >::end ( )
inline

Returns a iterator to one-past-the-last element of the underlying data container.

Returns
Iterator pointing to one-past-the-last element of the underlying data container

◆ end() [2/2]

template<typename T , size_t Capacity>
const_iterator Container< T, Capacity >::end ( ) const
inline

Returns a iterator to one-past-the-last element of the underlying data container.

Returns
Iterator pointing to one-past-the-last element of the underlying data container

◆ erase()

template<typename T , size_t Capacity>
void Container< T, Capacity >::erase ( const_iterator  position)
inline

Removes the element at the given position, has to move all element one to the left if the iterator does not point to the last valid element in the underlying data container.

Note
The user is also cautioned that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. Furthermore this method will assert if the position iterator is outside the range of this container, meaning if we are using an iterator to another container or if we passed an invalid iterator that would cause invalid memory access if dereferenced
Template Parameters
InputIteratorClass that allows for forward incrementable access to data of the given data container, allows for using / passing either std::vector or std::array. See https://en.cppreference.com/w/cpp/iterator/input_iterator for more information on the requirements of the iterator
Parameters
positionIterator pointing to the element, that should be removed from the underlying data container

◆ front() [1/2]

template<typename T , size_t Capacity>
reference Container< T, Capacity >::front ( )
inline

Returns a reference to the first element of the array. If the array is empty this method will assert and stop the application, because there is no valid element to return.

Returns
Reference to the first element of the array

◆ front() [2/2]

template<typename T , size_t Capacity>
const_reference Container< T, Capacity >::front ( ) const
inline

Returns a reference to the first element of the array. If the array is empty this method will assert and stop the application, because there is no valid element to return.

Returns
Reference to the first element of the array

◆ insert()

template<typename T , size_t Capacity>
template<typename InputIterator >
void Container< T, Capacity >::insert ( iterator  position,
InputIterator const &  first,
InputIterator const &  last 
)
inline

Copies all elements from the given start to exclusively the given end iterator into the underlying data container. The copying is started from the position before the given iterator (position - 1)

Note
If the position before the iterator (position - 1) is outside the range of this container the method will assert, meaning if we are using an iterator to another container or if we passed an invalid iterator that would cause invalid memory access if dereferenced. Additionally, if the Container was compiled with THINGSBOARD_ENABLE_DYNAMIC set, then the method will simply increase the capacity exponentially if it is full. Otherwise if the initally allocated Capacity is not big enough to hold all elements, this method will assert and stop the application
Template Parameters
InputIteratorClass that allows for forward incrementable access to data of the given data container, allows for using / passing either std::vector or std::array. See https://en.cppreference.com/w/cpp/iterator/input_iterator for more information on the requirements of the iterator
Parameters
positionIterator before which the content will be copied too. May be the end() iterator
firstIterator pointing to the first element we want to copy into our underlying data container
lastIterator pointing to one past the end of the elements we want to copy into our underlying data container

◆ operator[]() [1/2]

template<typename T , size_t Capacity>
reference Container< T, Capacity >::operator[] ( size_type  index)
inline

Returns a reference to the element at specified location index. No bounds checking is performed.

Note
Is more efficient but it is possible to read out of bounds data, which is undefined behaviour
Parameters
indexIndex of the element to return

◆ operator[]() [2/2]

template<typename T , size_t Capacity>
const_reference Container< T, Capacity >::operator[] ( size_type  index) const
inline

Returns a reference to the element at specified location index. No bounds checking is performed.

Note
Is more efficient but it is possible to read out of bounds data, which is undefined behaviour
Parameters
indexIndex of the element to return

◆ push_back()

template<typename T , size_t Capacity>
void Container< T, Capacity >::push_back ( const_reference  element)
inline

Appends the given element at the end of the underlying data container.

Note
If the Container was compiled with THINGSBOARD_ENABLE_DYNAMIC set, then the method will simply increase the capacity exponentially if it is full. Otherwise if the interal data structure is full this method will assert and stop the application. Because if we do not we could cause an out of bounds write, which could possibly overwrite other memory. Causing hard to debug issues, therefore this behaviour is not allowed in the first place
Parameters
elementElement that should be inserted at the end

◆ size()

template<typename T , size_t Capacity>
size_type Container< T, Capacity >::size ( ) const
inline

Gets the current amount of elements in the underlying data container.

Returns
The amount of items currently in the underlying data container

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