Skyward boardcore
Loading...
Searching...
No Matches
RegistryFrontend.cpp
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#include "RegistryFrontend.h"
24
25namespace Boardcore
26{
27RegistryFrontend::RegistryFrontend(std::unique_ptr<RegistryBackend> backend)
28 : backend{std::move(backend)}
29{
30 serializationVector.reserve(1024);
31 configuration.reserve(
32 1024 / sizeof(std::pair<ConfigurationId, EntryStructsUnion>));
33}
34
36{
37 const std::lock_guard<std::recursive_mutex> lock(mutexForRegistry);
38 if (!backend->start())
40
41 return RegistryError::OK;
42}
43
45{
46 const std::lock_guard<std::recursive_mutex> lock(mutexForRegistry);
47 isArmed = true;
48}
49
51{
52 const std::lock_guard<std::recursive_mutex> lock(mutexForRegistry);
53 isArmed = false;
54}
55
57{
58 const std::lock_guard<std::recursive_mutex> lock(mutexForRegistry);
59 for (auto& it : configuration)
60 predicate(it.first, it.second);
61}
62
64{
65 const std::lock_guard<std::recursive_mutex> lock(mutexForRegistry);
66 if (isArmed)
68
69 if (!backend->load(serializationVector))
71
72 RegistrySerializer serializer(serializationVector);
73 return serializer.deserializeConfiguration(configuration);
74}
75
77 const ConfigurationId configurationIndex)
78{
79 const std::lock_guard<std::recursive_mutex> lock(mutexForRegistry);
80 auto iterator = configuration.find(configurationIndex);
81 return !(iterator == configuration.end());
82}
83
85{
86 const std::lock_guard<std::recursive_mutex> lock(mutexForRegistry);
87 return configuration.empty();
88}
89
91{
92 const std::lock_guard<std::recursive_mutex> lock(mutexForRegistry);
93 // In case the registry is armed inhibit the saving
94 if (isArmed)
96
97 RegistrySerializer serializer(serializationVector);
98 RegistryError error = serializer.serializeConfiguration(configuration);
99 if (error != RegistryError::OK)
100 return error;
101
102 if (!backend->save(serializationVector))
104
105 return RegistryError::OK;
106}
107
109{
110 const std::lock_guard<std::recursive_mutex> lock(mutexForRegistry);
111 configuration.clear();
112};
113
114} // namespace Boardcore
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
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...
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...
bool isEmpty()
Verify that the configuration is empty or exists some setted entries.
Serialization and de-serialization class for the registry. It does serialize and deserialize the conf...
This file includes all the types the logdecoder script will decode.
uint32_t ConfigurationId
RegistryError
RegistryError enumeration as return type.
@ OK
Correct condition.
@ ARMED
The registry is armed, the operation is not allowed.
Definition WIZ5500.h:318