lmdbal/operators.hpp

210 lines
4.8 KiB
C++

// Squawk messenger.
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef CORE_OPERATORS_HPP
#define CORE_OPERATORS_HPP
#include <map>
#include <set>
#include <vector>
#include <list>
#include <deque>
#include <QDataStream>
template <class K, class V>
QDataStream& operator << (QDataStream &out, const std::map<K, V>& container) {
uint32_t size = container.size();
out << size;
for (const std::pair<const K, V>& pair : container) {
out << pair;
}
return out;
}
template <class K, class V>
QDataStream& operator >> (QDataStream &in, std::map<K, V>& container) {
uint32_t size;
in >> size;
for (uint32_t i = 0; i < size; ++i) {
std::pair<K, V> pair;
in >> pair;
container.insert(pair);
}
return in;
}
template <class K, class V>
QDataStream& operator << (QDataStream &out, const std::multimap<K, V>& container) {
uint32_t size = container.size();
out << size;
for (const std::pair<const K, V>& pair : container) {
out << pair;
}
return out;
}
template <class K, class V>
QDataStream& operator >> (QDataStream &in, std::multimap<K, V>& container) {
uint32_t size;
in >> size;
for (uint32_t i = 0; i < size; ++i) {
std::pair<K, V> pair;
in >> pair;
container.insert(pair);
}
return in;
}
template <class K, class V>
QDataStream& operator << (QDataStream &out, const std::pair<K, V>& pair) {
out << pair.first;
out << pair.second;
return out;
}
template <class K, class V>
QDataStream& operator >> (QDataStream &in, std::pair<K, V>& container) {
in >> container.first;
in >> container.second;
return in;
}
template <class K>
QDataStream& operator << (QDataStream &out, const std::set<K>& container) {
uint32_t size = container.size();
out << size;
for (const K& value : container) {
out << value;
}
return out;
}
template <class K>
QDataStream& operator >> (QDataStream &in, std::set<K>& container) {
uint32_t size;
in >> size;
for (uint32_t i = 0; i < size; ++i) {
K value;
in >> value;
container.insert(value);
}
return in;
}
template <class K>
QDataStream& operator << (QDataStream &out, const std::multiset<K>& container) {
uint32_t size = container.size();
out << size;
for (const K& value : container) {
out << value;
}
return out;
}
template <class K>
QDataStream& operator >> (QDataStream &in, std::multiset<K>& container) {
uint32_t size;
in >> size;
for (uint32_t i = 0; i < size; ++i) {
K value;
in >> value;
container.insert(value);
}
return in;
}
template <class K>
QDataStream& operator << (QDataStream &out, const std::vector<K>& container) {
uint32_t size = container.size();
out << size;
for (const K& value : container) {
out << value;
}
return out;
}
template <class K>
QDataStream& operator >> (QDataStream &in, std::vector<K>& container) {
uint32_t size;
in >> size;
container.resize(size);
for (uint32_t i = 0; i < size; ++i) {
in >> container[i];
}
return in;
}
template <class K>
QDataStream& operator << (QDataStream &out, const std::deque<K>& container) {
uint32_t size = container.size();
out << size;
for (const K& value : container) {
out << value;
}
return out;
}
template <class K>
QDataStream& operator >> (QDataStream &in, std::deque<K>& container) {
uint32_t size;
in >> size;
container.resize(size);
for (uint32_t i = 0; i < size; ++i) {
in >> container[i];
}
return in;
}
template <class K>
QDataStream& operator << (QDataStream &out, const std::list<K>& container) {
uint32_t size = container.size();
out << size;
for (const K& value : container) {
out << value;
}
return out;
}
template <class K>
QDataStream& operator >> (QDataStream &in, std::list<K>& container) {
uint32_t size;
in >> size;
for (uint32_t i = 0; i < size; ++i) {
typename std::list<K>::iterator itr = container.emplace(container.end());
in >> *itr;
}
return in;
}
#endif //CORE_OPERATORS_HPP