Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_Store.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: Oct 5, 2021
5 * License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)
6 */
7
8#pragma once
9
10#include <type_traits>
11
24template <class RecordType, unsigned int MaxItemCount>
25class Store {
26private:
32 uint16_t _currentSize = 0;
33
34public:
35 RecordType _records[MaxItemCount] = {};
36
37 RecordType* begin() { return _records; }
38
39 RecordType* end() { return _records + _currentSize; }
40
48 typedef typename std::remove_reference<decltype(((RecordType*)nullptr)->id())>::type IdType;
49
53 void clear() {
54 _currentSize = 0;
55 for (auto& rec : *this) {
56 rec.invalidate();
57 }
58 }
59
65 constexpr RecordType* get(const IdType& id) {
66 for (auto& rec : *this) {
67 if (rec.isValid() && rec.id() == id) {
68 return &rec;
69 }
70 }
71 return nullptr;
72 }
73
81 template <class UnaryPredicate>
82 constexpr RecordType* get(UnaryPredicate p) {
83 for (auto& obj : *this) {
84 if (p(obj)) {
85 return &obj;
86 }
87 }
88 return nullptr;
89 }
90
101 template <class ValueFunction>
102 constexpr RecordType* getMin(ValueFunction getValue) {
103 RecordType* smallest = get([](auto rec) { return rec.isValid(); });
104
105 if (smallest == nullptr) {
106 return nullptr;
107 }
108
109 for (RecordType* obj = smallest + 1; obj != end(); obj++) {
110 if (!obj->isValid()) {
111 continue;
112 }
113
114 if (getValue(*obj) < getValue(*smallest)) {
115 smallest = obj;
116 }
117 }
118
119 return smallest;
120 }
121
128 RecordType* getOrAdd(IdType id) {
129 RecordType* retval = nullptr;
130 for (auto& rec : *this) {
131 if (!rec.isValid()) {
132 retval = &rec;
133 }
134 else if (rec.id() == id) {
135 return &rec;
136 }
137 }
138
139 if (retval != nullptr) {
140 // not found, but encountered an invalid record in the loop.
141 return retval;
142 }
143
144 return addAtEnd();
145 }
146
152 constexpr RecordType* add() {
153 for (auto& rec : *this) {
154 if (!rec.isValid()) {
155 return &rec;
156 }
157 }
158 return nullptr;
159 }
160
165 constexpr RecordType* addAtEnd() {
166 if (_currentSize < MaxItemCount) {
167 // still have space, increment size and return ref to last index.
168 RecordType* retval = &_records[_currentSize++]; // must postcrement
169 retval->invalidate();
170 return retval;
171 }
172
173 return nullptr;
174 }
175
176 uint16_t size() { return _currentSize; }
177
181 template <class UnaryPredicate>
182 constexpr uint16_t countIf(UnaryPredicate p) {
183 uint16_t s = 0;
184 for (auto& rec : *this) {
185 s += p(rec) ? 1 : 0;
186 }
187 return s;
188 }
189
193 constexpr uint16_t count() {
194 return countIf([](auto& rec) { rec.isValid(); });
195 }
196
200 constexpr bool full() { return count() == MaxItemCount; }
201};
A variable size storage utility for objects of type RecordType with absolute maximum MaxItemCount.
Definition: cs_Store.h:25
constexpr RecordType * getMin(ValueFunction getValue)
Returns a pointer to the element in the store which minimizes the value function val.
Definition: cs_Store.h:102
uint16_t size()
Definition: cs_Store.h:176
constexpr RecordType * get(UnaryPredicate p)
returns the first object obj in the store satisfying p(obj) == true.
Definition: cs_Store.h:82
std::remove_reference< decltype(((RecordType *) nullptr) ->id())>::type IdType
Identifies and simplifies the type returned by the id() method of RecordType.
Definition: cs_Store.h:48
constexpr uint16_t countIf(UnaryPredicate p)
returns number of elements that satisfy the predicate.
Definition: cs_Store.h:182
uint16_t _currentSize
Current maximal number of valid records.
Definition: cs_Store.h:32
constexpr uint16_t count()
returns number of valid elements.
Definition: cs_Store.h:193
constexpr bool full()
returns true if all elements are occupied and valid.
Definition: cs_Store.h:200
constexpr RecordType * addAtEnd()
increases size of the store if possible and returns the entry at the end(), after invalidating.
Definition: cs_Store.h:165
RecordType * getOrAdd(IdType id)
same as get, but will return pointer to an invalid element if such element exists,...
Definition: cs_Store.h:128
RecordType _records[MaxItemCount]
Definition: cs_Store.h:35
RecordType * begin()
Definition: cs_Store.h:37
constexpr RecordType * add()
returns a pointer to the first invalid record.
Definition: cs_Store.h:152
RecordType * end()
Definition: cs_Store.h:39
void clear()
invalidate all records.
Definition: cs_Store.h:53
constexpr RecordType * get(const IdType &id)
linear search for the first record which has id() == id.
Definition: cs_Store.h:65