#pragma once //----------------------------------------------------------------------------- // с ограничением на длинну поступающих данных //----------------------------------------------------------------------------- #include "JDefines.h" #include "JTerminal.h" #include "JSys.h" namespace Incart::Common { template class LinearBuffer { const bool MYDEBUG = false; protected: T* m_data; int m_length; int m_position; int m_total_points; int m_shift_points; public: LinearBuffer() : m_data(nullptr),m_length(0),m_position(0),m_total_points(0),m_shift_points(0) { JT() << __PRETTY_FUNCTION__; } ~LinearBuffer() { JT() << __PRETTY_FUNCTION__; if (m_data != nullptr) delete[] m_data; } void set(int length) { if (m_data != nullptr) { JTerminal(JCL_RED) << __PRETTY_FUNCTION__ << "re-allocation of memory"; delete[] m_data; } m_data = new T[length]; m_length = length; m_position = 0; JTerminal(JCL_GREEN) << __PRETTY_FUNCTION__ << "allocation of memory" << length; } void reset(int total_points, int shift_points) { JT() << __FUNCTION__ << shift_points; m_position = 0; m_total_points = total_points; m_shift_points = shift_points; } void add(T* data,int len) { for(int i = 0; i < len; i++) { m_data[m_position] = data[i]; m_position++; if(m_position == m_length) { JTerminal(JCL_RED) << __PRETTY_FUNCTION__ << "not enough memory allocated "; break; } } } void add(T z) { m_data[m_position] = z; m_position++; if(m_position == m_length) { JTerminal(JCL_RED) << __PRETTY_FUNCTION__ << "not enough memory allocated "; } } void add(T z, int len) { for(int i = 0; i < len; i++) { m_data[m_position] = z; m_position++; if(m_position == m_length) { JTerminal(JCL_RED) << __PRETTY_FUNCTION__ << "not enough memory allocated"; break; } } } void close(T z) { // закрываем блок add(z,m_total_points - m_position); } int file_writer(QString fname) { if(m_shift_points > m_position) { JTerminal(JCL_RED) << __PRETTY_FUNCTION__ << "not enough points" << m_total_points << m_shift_points << m_position; JSys::abort(); } int len = 0; QFile file(fname); if ( file.open(QIODevice::WriteOnly)) { int sz = m_total_points < m_position ? m_total_points : m_position; // по факту обрезается нос и хвост len = (sz - m_shift_points) * static_cast(sizeof(T)); qint64 flen = file.write(reinterpret_cast(&m_data[m_shift_points]),len); if(flen != len) { JTerminal(JCL_RED) << __PRETTY_FUNCTION__ << "file write error" << fname; JSys::abort(); } JTerminal(JCL_GREEN) << __PRETTY_FUNCTION__ << "file write ok" << fname; file.close(); } else { JTerminal(JCL_RED) << __PRETTY_FUNCTION__ << "file not open" << fname; } return len; } }; //----------------------------------------------------------------------------- class LinearMultiBuffer { // внимание говнокод! static const bool MYDEBUG = false; protected: QVector> m_LinearBufferInt32; QVector> m_LinearBufferFloat; int SIZE; public: LinearMultiBuffer() : SIZE(0) { JT() << __PRETTY_FUNCTION__; JTEST(sizeof(qint32) == sizeof(float),"size int and float do not match"); } ~LinearMultiBuffer() { JT() << __PRETTY_FUNCTION__; } void set(int channels, int length) { JT() << __PRETTY_FUNCTION__ << channels << length; SIZE = channels; m_LinearBufferInt32.resize(SIZE); m_LinearBufferFloat.resize(SIZE); //for(int ch = 0; ch < channels; ch++) //{ // m_LinearBuffer[ch].set(length); //} for(LinearBuffer& obj : m_LinearBufferInt32) { obj.set(length); } for(LinearBuffer& obj : m_LinearBufferFloat) { obj.set(length); } } void reset(int total_points, int shift_points) { JT() << __PRETTY_FUNCTION__; for(LinearBuffer& obj : m_LinearBufferInt32) { obj.reset(total_points,shift_points); } for(LinearBuffer& obj : m_LinearBufferFloat) { obj.reset(total_points,shift_points); } } void add(QVector& vd,qint64 len) { JTEST(vd.size() == m_LinearBufferInt32.size(),"unpack err 1"); JT() << __PRETTY_FUNCTION__; for(int ch = 0; ch < SIZE; ch++) { m_LinearBufferInt32[ch].add(vd[ch],len); qint32* m = vd[ch]; for(int i = 0; i < len; i++) { m_LinearBufferFloat[ch].add(float(m[i]*1.98)); } } } void add(qint32 z, qint64 len) { for(int ch = 0; ch < SIZE; ch++) { m_LinearBufferInt32[ch].add(z,len); for(int i = 0; i < len; i++) { m_LinearBufferFloat[ch].add(float(z*1.98)); } } } void close(qint32 z) { for(int ch = 0; ch < SIZE; ch++) { m_LinearBufferInt32[ch].close(z); m_LinearBufferFloat[ch].close(z); } } void file_writer(QString pre_file_name, const std::function func) { JT() << __PRETTY_FUNCTION__; for(int ch = 0; ch < m_LinearBufferInt32.size(); ch++) { QString fname = func(pre_file_name,ch); //m_LinearBufferInt32[ch].file_writer(fname); m_LinearBufferFloat[ch].file_writer(fname); } } }; } // namespace Incart::Common