#pragma once class CIOStream { public: enum class Mode_t { NONE = 0, READ, WRITE }; CIOStream(); CIOStream(const fs::path& fsFileFullPath, Mode_t eMode); ~CIOStream(); bool Open(const fs::path& fsFileFullPath, Mode_t eMode); void Close(); void Flush(); void ComputeFileSize(); std::streampos GetPosition(); void SetPosition(std::streampos nOffset); const std::filebuf* GetData() const; const std::streampos GetSize() const; bool IsReadable(); bool IsWritable() const; bool IsEof() const; //----------------------------------------------------------------------------- // Purpose: reads any value from the file //----------------------------------------------------------------------------- template void Read(T& tValue) { if (IsReadable()) m_Stream.read(reinterpret_cast(&tValue), sizeof(tValue)); } //----------------------------------------------------------------------------- // Purpose: reads any value from the file with specified size //----------------------------------------------------------------------------- template void Read(T& tValue, size_t nSize) { if (IsReadable()) m_Stream.read(reinterpret_cast(&tValue), nSize); } //----------------------------------------------------------------------------- // Purpose: reads any value from the file and returns it //----------------------------------------------------------------------------- template T Read() { T value{}; if (!IsReadable()) return value; m_Stream.read(reinterpret_cast(&value), sizeof(value)); return value; } string ReadString(); //----------------------------------------------------------------------------- // Purpose: writes any value to the file //----------------------------------------------------------------------------- template void Write(T tValue) { if (!IsWritable()) return; m_Stream.write(reinterpret_cast(&tValue), sizeof(tValue)); } //----------------------------------------------------------------------------- // Purpose: writes any value to the file with specified size //----------------------------------------------------------------------------- template void Write(T tValue, size_t nSize) { if (!IsWritable()) return; m_Stream.write(reinterpret_cast(tValue), nSize); } void WriteString(const string& svInput); private: std::streampos m_nSize; // Size of ifstream. Mode_t m_eCurrentMode; // Current active mode. fstream m_Stream; // I/O file stream. };