UsenetSearch/include/usenetsearch/Serialize.h

130 lines
4.0 KiB
C++

/*
Copyright© 2021 John Sennesael
UsenetSearch 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.
UsenetSearch 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 UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "usenetsearch/Except.h"
#include <cstdint>
#include <locale>
#include <filesystem>
#include <fstream>
#include <vector>
namespace usenetsearch {
struct ArticleEntry;
struct NntpHeader;
struct NntpListEntry;
struct FileIOException: public UsenetSearchException
{
FileIOException(int errorCode, const std::string& message):
UsenetSearchException(errorCode, message){}
virtual ~FileIOException() = default;
};
struct SerializeException: public UsenetSearchException
{
SerializeException(int errorCode, const std::string& message):
UsenetSearchException(errorCode, message){}
virtual ~SerializeException() = default;
};
class SerializableFile
{
int m_fd{0};
std::filesystem::path m_fileName;
bool m_locked{false};
bool m_lockOnOpen{false};
public:
/**
* Constructor
*
* @param[in] lockOnOpen (optional, on by default) - If true, the file will
* automatically be locked on open and unlocked on
* close or when destructed.
*/
explicit SerializableFile(bool lockOnOpen = true);
/**
* Destructor.
*/
~SerializableFile();
void Close();
void FileLock();
void FileUnlock();
bool IsOpen() const;
void Open(const std::string& fileName);
void RangeLock(size_t offset, size_t size) const;
void RangeUnlock(size_t offset, size_t size) const;
std::string ReadStr(size_t size) const;
std::uint8_t ReadInt8() const;
std::uint32_t ReadInt32() const;
std::uint64_t ReadInt64() const;
void Seek(
size_t offset,
std::ios_base::seekdir direction = std::ios_base::beg
) const;
size_t Size() const;
size_t Tell() const;
void Write(std::uint8_t value) const;
void Write(std::uint32_t value) const;
void Write(std::uint64_t value) const;
void Write(const char* bytes, size_t size) const;
void Write(const std::string& str) const;
};
SerializableFile& operator<<(SerializableFile& out, const std::uint8_t& obj);
SerializableFile& operator>>(SerializableFile& in, std::uint8_t& obj);
SerializableFile& operator<<(SerializableFile& out, const std::uint32_t& obj);
SerializableFile& operator>>(SerializableFile& in, std::uint32_t& obj);
SerializableFile& operator<<(SerializableFile& out, const std::uint64_t& obj);
SerializableFile& operator>>(SerializableFile& in, std::uint64_t& obj);
SerializableFile& operator<<(SerializableFile& out, const std::string& str);
SerializableFile& operator>>(SerializableFile& in, std::string& str);
SerializableFile& operator<<(SerializableFile& out, const std::wstring& str);
SerializableFile& operator>>(SerializableFile& in, std::wstring& str);
SerializableFile& operator<<(
SerializableFile& out, const std::vector<std::uint64_t>& arr);
SerializableFile& operator>>(
SerializableFile& in, std::vector<std::uint64_t>& arr);
SerializableFile& operator<<(SerializableFile& out, const ArticleEntry& obj);
SerializableFile& operator>>(SerializableFile& in, ArticleEntry& obj);
SerializableFile& operator<<(SerializableFile& out, const NntpHeader& obj);
SerializableFile& operator>>(SerializableFile& in, NntpHeader& obj);
SerializableFile& operator<<(SerializableFile& out, const NntpListEntry& obj);
SerializableFile& operator>>(SerializableFile& in, NntpListEntry& obj);
} // namespace usenetsearch