/* * LMDB Abstraction Layer. * Copyright (C) 2023 Yury Gubich * * 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 . */ #ifndef LMDBAL_SERIALIZER_HPP #define LMDBAL_SERIALIZER_HPP #include "serializer.h" /** * \class LMDBAL::Serializer * \brief A class handling serialization/deserialization * * A class that is constructed in every LMDBAL::Storage * to serialize or deserialize keys and values. * * It serializes to and deserializes from MDB_val * * \tparam K type of the keys of the storage * \tparam V type of the values of the storage */ /** * \brief Creates an empty Serializer */ template LMDBAL::Serializer::Serializer() : bytes(), buffer(&bytes), stream(&buffer) { buffer.open(QIODevice::ReadWrite); } /** * \brief Creates a Serializer with some data in it * * The data automatically gets serialized * * \param[in] value - a value that is assigned to the serializer */ template LMDBAL::Serializer::Serializer(const T& value) : bytes(), buffer(&bytes), stream(&buffer) { buffer.open(QIODevice::ReadWrite); _setData(value); } /** * \brief Destoys the serializer */ template LMDBAL::Serializer::~Serializer() { buffer.close(); } /** * \brief Sets the data to the seriazer * * This is a normal way to seriaze value * * \param[in] value - a value you want to serialize * * \returns serialized value */ template MDB_val LMDBAL::Serializer::setData(const T& value) { clear(); _setData(value); return getData(); } /** * \brief Deserializes value * * This is a normal way to deseriaze value * * \param[in] value - a value you want to deserialize * * \returns deserialized value */ template T LMDBAL::Serializer::deserialize(const MDB_val& value) { T result; deserialize(value, result); return result; } /** * \brief Deserializes value * * This is a normal way to deseriaze value * * \param[in] value - a value you want to deserialize * \param[out] result - deserialized value */ template void LMDBAL::Serializer::deserialize(const MDB_val& value, T& result) { clear(); bytes.setRawData((char*)value.mv_data, value.mv_size); stream >> result; } /** * \brief Private function that handles serialization * * \param[in] value - a value you want to serialize */ template void LMDBAL::Serializer::_setData(const T& value) { stream << value; } /** * \brief Clears the state of serializer * * Normally you don't need to call this function */ template void LMDBAL::Serializer::clear() { if (buffer.pos() > 0) { buffer.seek(0); } } /** * \brief Returns the data if it already was serialized * * Normally you don't need to call this function * * This may be usefull if you called LMDBAL::Serilizer::setData() but lost the result * * \returns Serialized data */ template MDB_val LMDBAL::Serializer::getData() { MDB_val val; val.mv_size = buffer.pos(); val.mv_data = (char*)bytes.data(); return val; } #endif //LMDBAL_SERIALIZER_HPP