LMDBAL 0.6.2
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 return QString::fromUtf8(QByteArray(static_cast<const 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 return QString::fromUtf8(QByteArray(static_cast<const char *>(data.mv_data), static_cast<int>(data.mv_size)));
44#endif
45 };
46 void deserialize(const MDB_val& data, QString& result) {
47#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
48 if (data.mv_size > static_cast<size_t>(std::numeric_limits<qsizetype>::max()))
49 throw std::runtime_error("Data size exceeds QByteArray capacity");
50
51 result = QString::fromUtf8(QByteArray(static_cast<const char *>(data.mv_data), static_cast<qsizetype>(data.mv_size)));
52#else
53 if (data.mv_size > static_cast<size_t>(std::numeric_limits<int>::max()))
54 throw std::runtime_error("Data size exceeds QByteArray capacity");
55
56 result = QString::fromUtf8(QByteArray(static_cast<const char *>(data.mv_data), static_cast<int>(data.mv_size)));
57#endif
58 }
59 MDB_val setData(const QString& data) {
60 value = data.toUtf8();
61 return getData();
62 };
63 MDB_val getData() {
64 MDB_val result;
65 result.mv_data = value.data();
66 result.mv_size = static_cast<size_t>(value.size());
67 return result;
68 };
69 void clear() {}; //not possible;
70
71private:
72 QByteArray value;
73};
74
75}
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