LMDBAL 0.6.0
LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer
Loading...
Searching...
No Matches
operators.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 <map>
22#include <set>
23#include <vector>
24#include <list>
25#include <deque>
26
27#include <QDataStream>
28
29template <class K, class V>
30QDataStream& operator << (QDataStream &out, const std::map<K, V>& container) {
31 uint32_t size = container.size();
32 out << size;
33 for (const std::pair<const K, V>& pair : container) {
34 out << pair;
35 }
36
37 return out;
38}
39
40template <class K, class V>
41QDataStream& operator >> (QDataStream &in, std::map<K, V>& container) {
42 uint32_t size;
43 in >> size;
44 for (uint32_t i = 0; i < size; ++i) {
45 std::pair<K, V> pair;
46 in >> pair;
47 container.insert(pair);
48 }
49
50 return in;
51}
52
53template <class K, class V>
54QDataStream& operator << (QDataStream &out, const std::multimap<K, V>& container) {
55 uint32_t size = container.size();
56 out << size;
57 for (const std::pair<const K, V>& pair : container) {
58 out << pair;
59 }
60
61 return out;
62}
63
64template <class K, class V>
65QDataStream& operator >> (QDataStream &in, std::multimap<K, V>& container) {
66 uint32_t size;
67 in >> size;
68 for (uint32_t i = 0; i < size; ++i) {
69 std::pair<K, V> pair;
70 in >> pair;
71 container.insert(pair);
72 }
73
74 return in;
75}
76
77#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
78template <class K, class V>
79QDataStream& operator << (QDataStream &out, const std::pair<K, V>& pair) {
80 out << pair.first;
81 out << pair.second;
82
83 return out;
84}
85
86template <class K, class V>
87QDataStream& operator >> (QDataStream &in, std::pair<K, V>& container) {
88 in >> container.first;
89 in >> container.second;
90
91 return in;
92}
93#endif
94
95template <class K>
96QDataStream& operator << (QDataStream &out, const std::set<K>& container) {
97 uint32_t size = container.size();
98 out << size;
99 for (const K& value : container) {
100 out << value;
101 }
102
103 return out;
104}
105
106template <class K>
107QDataStream& operator >> (QDataStream &in, std::set<K>& container) {
108 uint32_t size;
109 in >> size;
110 for (uint32_t i = 0; i < size; ++i) {
111 K value;
112 in >> value;
113 container.insert(value);
114 }
115
116 return in;
117}
118
119template <class K>
120QDataStream& operator << (QDataStream &out, const std::multiset<K>& container) {
121 uint32_t size = container.size();
122 out << size;
123 for (const K& value : container) {
124 out << value;
125 }
126
127 return out;
128}
129
130template <class K>
131QDataStream& operator >> (QDataStream &in, std::multiset<K>& container) {
132 uint32_t size;
133 in >> size;
134 for (uint32_t i = 0; i < size; ++i) {
135 K value;
136 in >> value;
137 container.insert(value);
138 }
139
140 return in;
141}
142
143template <class K>
144QDataStream& operator << (QDataStream &out, const std::vector<K>& container) {
145 uint32_t size = container.size();
146 out << size;
147 for (const K& value : container) {
148 out << value;
149 }
150
151 return out;
152}
153
154template <class K>
155QDataStream& operator >> (QDataStream &in, std::vector<K>& container) {
156 uint32_t size;
157 in >> size;
158 container.resize(size);
159 for (uint32_t i = 0; i < size; ++i) {
160 in >> container[i];
161 }
162
163 return in;
164}
165
166template <class K>
167QDataStream& operator << (QDataStream &out, const std::deque<K>& container) {
168 uint32_t size = container.size();
169 out << size;
170 for (const K& value : container) {
171 out << value;
172 }
173
174 return out;
175}
176
177template <class K>
178QDataStream& operator >> (QDataStream &in, std::deque<K>& container) {
179 uint32_t size;
180 in >> size;
181 container.resize(size);
182 for (uint32_t i = 0; i < size; ++i) {
183 in >> container[i];
184 }
185
186 return in;
187}
188
189template <class K>
190QDataStream& operator << (QDataStream &out, const std::list<K>& container) {
191 uint32_t size = container.size();
192 out << size;
193 for (const K& value : container) {
194 out << value;
195 }
196
197 return out;
198}
199
200template <class K>
201QDataStream& operator >> (QDataStream &in, std::list<K>& container) {
202 uint32_t size;
203 in >> size;
204 for (uint32_t i = 0; i < size; ++i) {
205 typename std::list<K>::iterator itr = container.emplace(container.end());
206 in >> *itr;
207 }
208
209 return in;
210}