LMDBAL 0.6.2
LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer
Loading...
Searching...
No Matches
serializer_qbytearray.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 <QByteArray>
22
23namespace LMDBAL {
24
25template<>
26class Serializer<QByteArray> {
27public:
28 Serializer():value() {};
29 Serializer(const QByteArray& p_value):value(p_value) {};
30 ~Serializer() {};
31
32 QByteArray deserialize(const MDB_val& data) {
33 QByteArray result;
34 deserialize(data, result);
35
36 return result;
37 };
38 void deserialize(const MDB_val& data, QByteArray& result) {
39#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
40 if (data.mv_size > static_cast<size_t>(std::numeric_limits<qsizetype>::max()))
41 throw std::runtime_error("Data size exceeds QByteArray capacity");
42
43 result.setRawData(static_cast<char *>(data.mv_data), static_cast<qsizetype>(data.mv_size));
44#else
45 if (data.mv_size > static_cast<size_t>(std::numeric_limits<uint>::max()))
46 throw std::runtime_error("Data size exceeds QByteArray capacity");
47
48 result.setRawData(static_cast<char *>(data.mv_data), static_cast<uint>(data.mv_size));
49#endif
50 }
51 MDB_val setData(const QByteArray& data) {
52 value = data;
53 return getData();
54 };
55 MDB_val getData() {
56 MDB_val result;
57 result.mv_data = value.data();
58 result.mv_size = static_cast<size_t>(value.size());
59 return result;
60 };
61 void clear() {
62 value.clear();
63 };
64
65private:
66 QByteArray value;
67};
68
69}
Serializer()
Creates an empty Serializer.
Definition serializer.hpp:45
MDB_val getData()
Returns the data if it already was serialized.
Definition serializer.hpp:185
T deserialize(const MDB_val &value)
Deserializes value.
Definition serializer.hpp:108
void clear()
Clears the state of serializer.
Definition serializer.hpp:170
MDB_val setData(const T &value)
Sets the data to the serializer.
Definition serializer.hpp:92
Destroys a cache.
Definition base.h:36