Skyward boardcore
Loading...
Searching...
No Matches
RegistrySerializer.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#pragma once
23
24#include <utils/Debug.h>
25
26#include <cstdint>
27#include <cstring>
28#include <functional>
29#include <unordered_map>
30#include <vector>
31
32#include "RegistryTypes.h"
33
34namespace Boardcore
35{
36
38 std::unordered_map<ConfigurationId, EntryStructsUnion>;
39
45{
46 uint64_t startBytes;
47 uint32_t totalSize;
48 uint32_t nrEntries;
49};
50
56{
57 uint32_t checksum;
58};
59
65{
66public:
73 explicit RegistrySerializer(std::vector<uint8_t>& vector);
74
94
118 RegistryConfiguration& configuration);
119
120private:
121 std::vector<uint8_t>& serializationVector;
122 uint32_t vectorWritePosition;
123
129 uint32_t computeChecksum();
130
142 template <typename T>
143 RegistryError deserialize(T& element)
144 {
145 size_t elSize = sizeof(T);
146
147 if (serializationVector.size() < vectorWritePosition + elSize)
149
150 std::memcpy(&element, serializationVector.data() + vectorWritePosition,
151 elSize);
152
153 vectorWritePosition += elSize;
154 return RegistryError::OK;
155 }
156
169 template <typename T>
170 RegistryError serialize(T& element)
171 {
172 size_t elSize = sizeof(T);
173
174 if (serializationVector.size() < vectorWritePosition + elSize)
176
177 std::memcpy(serializationVector.data() + vectorWritePosition, &element,
178 elSize);
179
180 vectorWritePosition += elSize;
181 return RegistryError::OK;
182 }
183
192 size_t size(RegistryConfiguration& configuration);
193};
194
195} // namespace Boardcore
Serialization and de-serialization class for the registry. It does serialize and deserialize the conf...
RegistryError deserializeConfiguration(RegistryConfiguration &configuration)
De-serializes the data from a serialized vector into the configuration map. In case of malformed seri...
RegistrySerializer(std::vector< uint8_t > &vector)
Construct a new Registry Serializer object.
RegistryError serializeConfiguration(RegistryConfiguration &configuration)
Serializes the configuration map into the uint8_t vector for serialized data.
This file includes all the types the logdecoder script will decode.
std::unordered_map< ConfigurationId, EntryStructsUnion > RegistryConfiguration
RegistryError
RegistryError enumeration as return type.
@ MALFORMED_SERIALIZED_DATA
Malformed data while deserializing.
@ OK
Correct condition.
@ WRONG_WRITES_SIZE
Cannot write due to wrong data size.
Serialization header, with useful information about the serialized data. Header to the actually seria...
uint32_t totalSize
Total size of serialized data in bytes.
uint64_t startBytes
Bytes at start, initialized as 1.
uint32_t nrEntries
Nr of configuration entries.