Uniot Core
0.8.1
Loading...
Searching...
No Matches
Register.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2024 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
26
31
32#pragma once
33
34#include <Arduino.h>
35
36#include "Array.h"
37#include "Map.h"
38
39#ifdef __cplusplus
40#include <functional>
41#endif
42
43namespace uniot {
44
57template <typename T>
58class Register {
59 public:
65 using IteratorCallback = std::function<void(const String&, SharedPointer<Array<T>>)>;
66
70 Register(Register const&) = delete;
71
75 void operator=(Register const&) = delete;
76
81 : mRegisterMap() {}
82
96 bool setRegister(const String& name, const T* values, size_t count) {
97 if (!values && count > 0) {
98 return false;
99 }
100
101 mRegisterMap.remove(name);
102
103 if (count > 0) {
104 auto newArray = MakeShared<Array<T>>(count, values);
105 if ((*newArray).size() != count) {
106 return false;
107 }
108 mRegisterMap.put(name, std::move(newArray));
109 for (size_t i = 0; i < count; ++i) {
110 _processRegister(name, values[i]);
111 }
112 }
113
114 return true;
115 }
116
130 bool addToRegister(const String& name, const T& value) {
131 SharedPointer<Array<T>> reg = mRegisterMap.get(name, nullptr);
132 if (!reg) {
133 reg = MakeShared<Array<T>>(4); // Default capacity of 4
134 mRegisterMap.put(name, reg);
135 }
136 if (reg->push(value)) {
137 _processRegister(name, value);
138 return true;
139 }
140 return false;
141 }
142
155 bool getRegisterValue(const String& name, size_t idx, T& outValue) const {
156 auto reg = mRegisterMap.get(name, nullptr);
157 if (reg) {
158 return reg->get(idx, outValue);
159 }
160 return false;
161 }
162
176 bool setRegisterValue(const String& name, size_t idx, const T& value) {
177 auto reg = mRegisterMap.get(name, nullptr);
178 if (reg && reg->set(idx, value)) {
179 _processRegister(name, value);
180 return true;
181 }
182 return false;
183 }
184
192 size_t getRegisterLength(const String& name) const {
193 auto reg = mRegisterMap.get(name, nullptr);
194 if (reg) {
195 return reg->size();
196 }
197 return 0;
198 }
199
215 void iterateRegisters(IteratorCallback callback) const {
216 if (!callback) return;
217
218 mRegisterMap.begin();
219 while (!mRegisterMap.isEnd()) {
220 auto& item = mRegisterMap.current();
221 callback(item.first, item.second);
222 mRegisterMap.next();
223 }
224 }
225
226 protected:
236 virtual void _processRegister(const String& name, const T& value) {}
237
238 private:
243};
244
245} // namespace uniot
Definition Array.h:66
Definition Map.h:41
bool setRegister(const String &name, const T *values, size_t count)
Sets or replaces a register with the given name and array of values.
Definition Register.h:96
bool addToRegister(const String &name, const T &value)
Adds a single value to an existing register or creates a new one.
Definition Register.h:130
virtual void _processRegister(const String &name, const T &value)
Hook method called when a register value is added or modified.
Definition Register.h:236
std::function< void(const String &, SharedPointer< Array< T > >)> IteratorCallback
Function type used for iterating through registers.
Definition Register.h:65
Register()
Constructs an empty Register.
Definition Register.h:80
bool setRegisterValue(const String &name, size_t idx, const T &value)
Updates a value in the register at the specified index.
Definition Register.h:176
size_t getRegisterLength(const String &name) const
Gets the number of values in the specified register.
Definition Register.h:192
Register(Register const &)=delete
Deleted copy constructor to prevent unintended copying.
bool getRegisterValue(const String &name, size_t idx, T &outValue) const
Retrieves a value from the register by name and index.
Definition Register.h:155
void operator=(Register const &)=delete
Deleted assignment operator to prevent unintended copying.
void iterateRegisters(IteratorCallback callback) const
Iterates through all registers and calls the callback function for each one.
Definition Register.h:215
std::shared_ptr< T > SharedPointer
Type alias for std::shared_ptr with cleaner syntax.
Definition Common.h:160
auto MakeShared(Args &&...args) -> decltype(std::make_shared< T >(std::forward< Args >(args)...))
Creates a shared pointer instance, alias for std::make_shared.
Definition Common.h:174
Contains all classes and functions related to the Uniot Core.