Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
Store< RecordType, MaxItemCount > Class Template Reference

A variable size storage utility for objects of type RecordType with absolute maximum MaxItemCount. More...

#include <cs_Store.h>

Public Types

typedef std::remove_reference< decltype(((RecordType *) nullptr) ->id())>::type IdType
 Identifies and simplifies the type returned by the id() method of RecordType. More...
 

Public Member Functions

RecordType * begin ()
 
RecordType * end ()
 
void clear ()
 invalidate all records. More...
 
constexpr RecordType * get (const IdType &id)
 linear search for the first record which has id() == id. More...
 
template<class UnaryPredicate >
constexpr RecordType * get (UnaryPredicate p)
 returns the first object obj in the store satisfying p(obj) == true. More...
 
template<class ValueFunction >
constexpr RecordType * getMin (ValueFunction getValue)
 Returns a pointer to the element in the store which minimizes the value function val. More...
 
RecordType * getOrAdd (IdType id)
 same as get, but will return pointer to an invalid element if such element exists, possibly incrementing _currentSize. More...
 
constexpr RecordType * add ()
 returns a pointer to the first invalid record. More...
 
constexpr RecordType * addAtEnd ()
 increases size of the store if possible and returns the entry at the end(), after invalidating. More...
 
uint16_t size ()
 
template<class UnaryPredicate >
constexpr uint16_t countIf (UnaryPredicate p)
 returns number of elements that satisfy the predicate. More...
 
constexpr uint16_t count ()
 returns number of valid elements. More...
 
constexpr bool full ()
 returns true if all elements are occupied and valid. More...
 

Public Attributes

RecordType _records [MaxItemCount] = {}
 

Private Attributes

uint16_t _currentSize = 0
 Current maximal number of valid records. More...
 

Detailed Description

template<class RecordType, unsigned int MaxItemCount>
class Store< RecordType, MaxItemCount >

A variable size storage utility for objects of type RecordType with absolute maximum MaxItemCount.

Can be stack allocated.

RecordType should implement:

  • IdType id();
  • bool isValid();
  • void invalidate();

Where IdType is freely dependent on RecordType. Furthermore it must be default constructible.

Member Typedef Documentation

◆ IdType

template<class RecordType , unsigned int MaxItemCount>
typedef std::remove_reference<decltype(((RecordType*)nullptr)->id())>::type Store< RecordType, MaxItemCount >::IdType

Identifies and simplifies the type returned by the id() method of RecordType.

Notes:

  • remove_reference to avoid complications
  • decltype doesn't dereference the nullptr

Member Function Documentation

◆ add()

template<class RecordType , unsigned int MaxItemCount>
constexpr RecordType * Store< RecordType, MaxItemCount >::add ( )
inlineconstexpr

returns a pointer to the first invalid record.

Returns nullptr if such element doesn't exist.

◆ addAtEnd()

template<class RecordType , unsigned int MaxItemCount>
constexpr RecordType * Store< RecordType, MaxItemCount >::addAtEnd ( )
inlineconstexpr

increases size of the store if possible and returns the entry at the end(), after invalidating.

◆ begin()

template<class RecordType , unsigned int MaxItemCount>
RecordType * Store< RecordType, MaxItemCount >::begin ( )
inline

◆ clear()

template<class RecordType , unsigned int MaxItemCount>
void Store< RecordType, MaxItemCount >::clear ( )
inline

invalidate all records.

◆ count()

template<class RecordType , unsigned int MaxItemCount>
constexpr uint16_t Store< RecordType, MaxItemCount >::count ( )
inlineconstexpr

returns number of valid elements.

◆ countIf()

template<class RecordType , unsigned int MaxItemCount>
template<class UnaryPredicate >
constexpr uint16_t Store< RecordType, MaxItemCount >::countIf ( UnaryPredicate  p)
inlineconstexpr

returns number of elements that satisfy the predicate.

◆ end()

template<class RecordType , unsigned int MaxItemCount>
RecordType * Store< RecordType, MaxItemCount >::end ( )
inline

◆ full()

template<class RecordType , unsigned int MaxItemCount>
constexpr bool Store< RecordType, MaxItemCount >::full ( )
inlineconstexpr

returns true if all elements are occupied and valid.

◆ get() [1/2]

template<class RecordType , unsigned int MaxItemCount>
constexpr RecordType * Store< RecordType, MaxItemCount >::get ( const IdType id)
inlineconstexpr

linear search for the first record which has id() == id.

Returns nullptr if no such element exists.

◆ get() [2/2]

template<class RecordType , unsigned int MaxItemCount>
template<class UnaryPredicate >
constexpr RecordType * Store< RecordType, MaxItemCount >::get ( UnaryPredicate  p)
inlineconstexpr

returns the first object obj in the store satisfying p(obj) == true.

(useful if comparing to other things than obj.id()

NOTE: this does not check for isValid, because you may want to find the first invalid item for example!

◆ getMin()

template<class RecordType , unsigned int MaxItemCount>
template<class ValueFunction >
constexpr RecordType * Store< RecordType, MaxItemCount >::getMin ( ValueFunction  getValue)
inlineconstexpr

Returns a pointer to the element in the store which minimizes the value function val.

Only considers records with isValid() == true.

ValueFunction is a function (or function like object) that takes an object of type RecordType as argument and returns the value that should be minimized.

◆ getOrAdd()

template<class RecordType , unsigned int MaxItemCount>
RecordType * Store< RecordType, MaxItemCount >::getOrAdd ( IdType  id)
inline

same as get, but will return pointer to an invalid element if such element exists, possibly incrementing _currentSize.

Returns nullptr if full();

◆ size()

template<class RecordType , unsigned int MaxItemCount>
uint16_t Store< RecordType, MaxItemCount >::size ( )
inline

Member Data Documentation

◆ _currentSize

template<class RecordType , unsigned int MaxItemCount>
uint16_t Store< RecordType, MaxItemCount >::_currentSize = 0
private

Current maximal number of valid records.

if _currentSize is less than MaxItemCount, range based for loops will loop over at most _currentSize items.

◆ _records

template<class RecordType , unsigned int MaxItemCount>
RecordType Store< RecordType, MaxItemCount >::_records[MaxItemCount] = {}

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