LMDBAL 0.6.0
LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer
Loading...
Searching...
No Matches
serializer.hpp
1/*
2 * LMDB Abstraction Layer.
3 * Copyright (C) 2023 Yury Gubich <blue@macaw.me>
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
19#pragma once
20
21#include "serializer.h"
22
39template<class T>
41 bytes(),
42 buffer(&bytes),
43 stream(&buffer)
44{
45 buffer.open(QIODevice::ReadWrite);
46}
47
55template<class T>
57 bytes(),
58 buffer(&bytes),
59 stream(&buffer)
60{
61 buffer.open(QIODevice::ReadWrite);
62 _setData(value);
63}
64
68template<class T>
70 buffer.close();
71}
72
82template<class T>
83MDB_val LMDBAL::Serializer<T>::setData(const T& value) {
84 clear();
85 _setData(value);
86 return getData();
87}
88
98template<class T>
99T LMDBAL::Serializer<T>::deserialize(const MDB_val& value) {
100 T result;
101 deserialize(value, result);
102
103 return result;
104}
105
114template<class T>
115void LMDBAL::Serializer<T>::deserialize(const MDB_val& value, T& result) {
116 clear();
117 bytes.setRawData((char*)value.mv_data, value.mv_size);
118 stream >> result;
119}
120
126template<class T>
127void LMDBAL::Serializer<T>::_setData(const T& value) {
128 stream << value;
129}
130
136template<class T>
138 if (buffer.pos() > 0) {
139 buffer.seek(0);
140 }
141}
142
152template<class T>
154 MDB_val val;
155
156 val.mv_size = buffer.pos();
157 val.mv_data = (char*)bytes.data();
158
159 return val;
160}
A class handling serialization/deserialization.
Definition serializer.h:33
~Serializer()
Destoys the serializer.
Definition serializer.hpp:69
Serializer()
Creates an empty Serializer.
Definition serializer.hpp:40
MDB_val getData()
Returns the data if it already was serialized.
Definition serializer.hpp:153
T deserialize(const MDB_val &value)
Deserializes value.
Definition serializer.hpp:99
void clear()
Clears the state of serializer.
Definition serializer.hpp:137
MDB_val setData(const T &value)
Sets the data to the seriazer.
Definition serializer.hpp:83