LMDBAL 0.6.0
LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer
Loading...
Searching...
No Matches
serializer_qstring.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 <QString>
22#include <QByteArray>
23
24namespace LMDBAL {
25
26template<>
27class Serializer<QString> {
28public:
29 Serializer():value() {};
30 Serializer(const QString& p_value):value(p_value.toUtf8()) {};
31 ~Serializer() {};
32
33 QString deserialize(const MDB_val& data) {
34#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
35 if (data.mv_size > static_cast<size_t>(std::numeric_limits<qsizetype>::max()))
36 throw std::runtime_error("Data size exceeds QByteArray capacity");
37
38 value = QByteArray(static_cast<char *>(data.mv_data), static_cast<qsizetype>(data.mv_size));
39#else
40 if (data.mv_size > static_cast<size_t>(std::numeric_limits<int>::max()))
41 throw std::runtime_error("Data size exceeds QByteArray capacity");
42
43 value = QByteArray(static_cast<char *>(data.mv_data), static_cast<int>(data.mv_size));
44#endif
45
46 return QString::fromUtf8(value);
47 };
48 void deserialize(const MDB_val& data, QString& result) {
49#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
50 if (data.mv_size > static_cast<size_t>(std::numeric_limits<qsizetype>::max()))
51 throw std::runtime_error("Data size exceeds QByteArray capacity");
52
53 value = QByteArray(static_cast<char *>(data.mv_data), static_cast<qsizetype>(data.mv_size));
54#else
55 if (data.mv_size > static_cast<size_t>(std::numeric_limits<int>::max()))
56 throw std::runtime_error("Data size exceeds QByteArray capacity");
57
58 value = QByteArray(static_cast<char *>(data.mv_data), static_cast<int>(data.mv_size));
59#endif
60
61 result = QString::fromUtf8(value);
62 }
63 MDB_val setData(const QString& data) {
64 value = data.toUtf8();
65 return getData();
66 };
67 MDB_val getData() {
68 MDB_val result;
69 result.mv_data = value.data();
70 result.mv_size = static_cast<size_t>(value.size());
71 return result;
72 };
73 void clear() {}; //not possible;
74
75private:
76 QByteArray value;
77};
78
79}
~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