Skyward boardcore
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RegistryFrontend.h
Go to the documentation of this file.
1/* Copyright (c) 2023 Skyward Experimental Rocketry
2 * Author: Nicolò Caruso
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
26#include <utils/Debug.h>
27
28#include <cstdint>
29#include <mutex>
30#include <unordered_map>
31#include <vector>
32
33#include "RegistryBackend.h"
34#include "RegistrySerializer.h"
35#include "RegistryTypes.h"
36
37namespace Boardcore
38{
39
49{
50public:
51 using EntryFunc = std::function<void(ConfigurationId, EntryStructsUnion&)>;
52
58 RegistryFrontend(std::unique_ptr<RegistryBackend> backend =
59 std::make_unique<DummyBackend>());
60
65 [[nodiscard]] RegistryError start();
66
71 void arm();
72
79 void disarm();
80
89 void forEach(const EntryFunc& predicate);
90
99 bool isEntryConfigured(const ConfigurationId configurationIndex);
100
107 bool isEmpty();
108
109 // TYPE UNSAFE INTERFACE METHODS
110
126 template <typename T>
127 RegistryError getUnsafe(const ConfigurationId configurationIndex,
128 T& outValue)
129 {
130 std::lock_guard<std::recursive_mutex> lock(mutexForRegistry);
131 auto iterator = configuration.find(configurationIndex);
134 if (iterator == configuration.end())
136 if (!iterator->second.get(outValue))
138 return RegistryError::OK;
139 }
140
154 template <typename T>
155 T getOrSetDefaultUnsafe(const ConfigurationId configurationIndex,
156 T defaultValue)
157 {
158 std::lock_guard<std::recursive_mutex> lock(mutexForRegistry);
159 T returnValue;
160 if (getUnsafe(configurationIndex, returnValue) == RegistryError::OK)
161 return returnValue;
162 if (setUnsafe(configurationIndex, defaultValue) != RegistryError::OK)
163 {
164 LOG_ERR(logger,
165 "Registry - Could not insert the default configuration");
166 }
167 return defaultValue;
168 }
169
184 template <typename T>
185 RegistryError setUnsafe(ConfigurationId configurationIndex, T value)
186 {
187 std::lock_guard<std::recursive_mutex> lock(mutexForRegistry);
188 /* In case that the configuration is in an armed state it cannot be
189 * modified */
190 if (isArmed)
193 auto insert = configuration.insert({configurationIndex, entry});
194 if (!insert.second)
195 insert.first->second = entry;
196 return RegistryError::OK;
197 }
198
199 // LOAD AND SAVE TO BACKEND
200
218
234
244 void clear();
245
246private:
247 std::recursive_mutex mutexForRegistry;
248 std::unordered_map<ConfigurationId, EntryStructsUnion> configuration;
249 bool isArmed = false;
250 std::vector<uint8_t> serializationVector;
251 std::unique_ptr<RegistryBackend> backend;
252 PrintLogger logger = Logging::getLogger("registry-frontend");
253};
254
255} // namespace Boardcore
#define LOG_ERR(logger,...)
static PrintLogger getLogger(const string &name)
This is the front-end for the registry to store and load the configuration. Its methods are type unsa...
RegistryFrontend(std::unique_ptr< RegistryBackend > backend=std::make_unique< DummyBackend >())
Registry front end constructor. Initializes the configuration of the underlying objects and reserves ...
std::function< void(ConfigurationId, EntryStructsUnion &)> EntryFunc
RegistryError getUnsafe(const ConfigurationId configurationIndex, T &outValue)
Gets the value for a given configuration entry.
void forEach(const EntryFunc &predicate)
Executes immediately the predicate for each to the configuration applying the callback with the id an...
void disarm()
Enable set methods and memory allocations.
bool isEntryConfigured(const ConfigurationId configurationIndex)
Verify if there is an existing entry given its enum entry.
RegistryError start()
Start function to start frontend and other objects, such as ActiveObjects, needed to write to backend...
T getOrSetDefaultUnsafe(const ConfigurationId configurationIndex, T defaultValue)
Gets the value for a specified configuration entry. Otherwise returns default and try to set the defa...
RegistryError load()
Loads from the backend the configuration.
void clear()
Clear the configuration actually saved, resetting to empty configuration.
RegistryError save()
Saves the configuration to the backend.
void arm()
Disables the memory registry set and allocations. To be use when the rocket itself is armed and durin...
RegistryError setUnsafe(ConfigurationId configurationIndex, T value)
Sets the value for the configuration entry with the specified enum.
bool isEmpty()
Verify that the configuration is empty or exists some setted entries.
This file includes all the types the logdecoder script will decode.
uint32_t ConfigurationId
RegistryError
RegistryError enumeration as return type.
@ INCORRECT_TYPE
The typeId and value type not correspond.
@ ENTRY_NOT_FOUND
Not found such entry.
@ OK
Correct condition.
@ ARMED
The registry is armed, the operation is not allowed.
Union data struct to be stored in the map. It does contain the enumeration index and the value of suc...
static EntryStructsUnion make(float value)
Set the Union object with its float value.