#pragma once //----------------------------------------------------------------------------- // JFile,Keeper,BlockReader - необходимо переписать //----------------------------------------------------------------------------- #include "JDefines.h" #include "JTerminal.h" #include "JSys.h" namespace Incart::Common { template class Keeper { static const bool MYDEBUG = false; protected: static constexpr int RECORD_SIZE = static_cast(sizeof(T)); QFile m_file; public: Keeper() { JT() << __PRETTY_FUNCTION__; } ~Keeper() { JT() << __PRETTY_FUNCTION__; m_file.close(); } void setFileName(QString fname, QIODevice::OpenMode mode) { JT() << __PRETTY_FUNCTION__; m_file.setFileName(fname); if ( !m_file.open(mode)) { JTerminal(JCL_RED) << __PRETTY_FUNCTION__ << "file not open" << m_file.fileName(); JSys::abort(); } } void put(T& record) { qint64 len = m_file.write(reinterpret_cast(&record), RECORD_SIZE); if(len != RECORD_SIZE) { JTerminal(JCL_RED) << __PRETTY_FUNCTION__ << "writing to file is incorrect" << m_file.fileName(); JSys::abort(); } } T& get_record() { static T record; qint64 len = m_file.read(reinterpret_cast(&record),RECORD_SIZE); if ( len != RECORD_SIZE) { JTerminal(JCL_RED) << __PRETTY_FUNCTION__ << "file read error" << m_file.fileName(); JSys::abort(); } return record; } T& get_record(qint64 num) { m_file.seek(num * RECORD_SIZE); return get_record(); } qint64 Size() { qint64 res = m_file.size(); if (res % RECORD_SIZE != 0) { JTerminal(JCL_RED) << __PRETTY_FUNCTION__ << "does not match the structure " << res << RECORD_SIZE; JSys::abort(); } return res / RECORD_SIZE; } }; } // namespace Incart::Common