123 lines
3.7 KiB
C++
123 lines
3.7 KiB
C++
/*
|
|
* CircularBinaryRemoteBuffer.hh
|
|
*
|
|
* Created on: 24 àïð. 2020 ã.
|
|
* Author: LeonidTitov
|
|
*/
|
|
|
|
#ifndef UMLIBRARY_DRIVER_CIRCULARBINARYREMOTEBUFFER_HH_
|
|
#define UMLIBRARY_DRIVER_CIRCULARBINARYREMOTEBUFFER_HH_
|
|
|
|
#include <utility>
|
|
|
|
#include "../peripheral/IMemoryAccess.hh"
|
|
|
|
#include <cstddef>
|
|
#include <limits>
|
|
|
|
namespace driver {
|
|
|
|
class CircularBinaryRemoteBuffer {
|
|
public:
|
|
|
|
//!Îòîáðàæàåò àäðåñ è êîëè÷åñòâî çíà÷åíèé â êîëüöåâîì áóôåðå
|
|
class Index {
|
|
|
|
friend class CircularBinaryRemoteBuffer;
|
|
|
|
public:
|
|
Index() : position( std::numeric_limits<std::size_t>::max() ), size( 0 ) {}
|
|
Index( const Index & right ) : position(right.position), size(right.size) {}
|
|
Index & operator=( const Index & right ) {
|
|
position = right.position;
|
|
size = right.size;
|
|
|
|
return *this;
|
|
}
|
|
|
|
Index relative( std::size_t index ) {
|
|
std::size_t next = ( position + index ) < size ? position + index : position + index - size;
|
|
return Index( next, size );
|
|
}
|
|
|
|
protected:
|
|
Index( std::size_t p, std::size_t size ) : position(p), size(size) {}
|
|
|
|
std::size_t position; //!<Èíäåêñ êàäðà â êîëüöåâîì áóôåðå
|
|
std::size_t size; //!<Êîëè÷åñòâî êàäðîâ â êîëüöåâîì áóôåðå
|
|
};
|
|
|
|
//!Ïîëó÷èòü ðàçìåð ôðåéìà.
|
|
std::size_t get_frame_size() const;
|
|
|
|
//!Äîáàâèòü íîâóþ ïîðöèþ äàííûõ - êàäð.
|
|
bool put( const char * frame );
|
|
|
|
//!Î÷èñòèòü âñå ïðèâÿçêè, ñáðîñèòü êåø.
|
|
void clear();
|
|
|
|
//!Íà÷àòü çàïèñü ñ èíäåêñà.
|
|
void restart( const Index & frame_index );
|
|
|
|
//!Çàôèêñèðîâàòü èíäåêñ êàê áàçîâûé äëÿ çàïðîñîâ äàííûõ.
|
|
/*!
|
|
* \param[in] desired_pretrigger_frames æåëàåìîå êîëè÷åñòâî êàäðîâ äî ôèêñàöèè.
|
|
* \return Èíäåêñ ñòàðòîâîãî êàäðà è îáùåå êîëè÷åñòâî êàäðîâ, ôàêòè÷åñêè çàïèñàííîå êîëè÷åñòâî êàäðîâ äî ôèêñàöèè.
|
|
*/
|
|
std::pair<Index, size_t> fixate( std::size_t desired_pretrigger_frames = 0 );
|
|
|
|
//!Îáùåå êîëè÷åñòâî äîñòóïíûõ äëÿ ÷òåíèÿ ýëåìåíòîâ.
|
|
std::size_t fixed( const Index & frame_index ) const;
|
|
|
|
//todo: Áîëåå àäåêâàòíàÿ ðàáîòà ñ áóôôåðîì:
|
|
//áóôôåðèçèðîâàòü êàäð â êýø, ñáðîñèòü êýø,
|
|
|
|
//!Ïîëó÷èòü êàäð ïî èíäåêñó.
|
|
const char * get( const Index & frame_index );
|
|
|
|
//!Åìêîñòü áóôåðà.
|
|
std::size_t capacity() const;
|
|
|
|
CircularBinaryRemoteBuffer( peripheral::IMemoryAccess & memory, std::size_t frame_size,
|
|
char * put_cache, std::size_t put_cache_size,
|
|
char * get_cache, std::size_t get_cache_size );
|
|
|
|
CircularBinaryRemoteBuffer( const CircularBinaryRemoteBuffer & right ) = delete;
|
|
CircularBinaryRemoteBuffer & operator=( const CircularBinaryRemoteBuffer & right ) = delete;
|
|
~CircularBinaryRemoteBuffer() = default;
|
|
private:
|
|
const std::size_t useful_capacity; //!<Èñïîëüçóåìàÿ åìêîñòü áóôåðà ïàìÿòè.
|
|
const std::size_t frame_size; //!<Ðàçìåð êàðäà.
|
|
const std::size_t number_of_frames; //!<Êîëè÷åñòâî êàäðîâ.
|
|
|
|
peripheral::IMemoryAccess & memory;
|
|
|
|
std::size_t index_in_memory;
|
|
std::size_t position_in_memory; //!<Òåêóùàÿ ïîçèöèÿ â ïàìÿòè.
|
|
|
|
char * const put_cache_begin;
|
|
char * const put_cache_end;
|
|
|
|
char * put_cache_writed;
|
|
char * put_cache_stored;
|
|
|
|
char * const get_cache_begin;
|
|
const std::size_t get_cache_capacity;
|
|
std::size_t cached_index_begin;
|
|
std::size_t cached_index_end;
|
|
|
|
std::size_t index;
|
|
std::size_t loaded_frames;
|
|
|
|
std::size_t wrap_index( std::size_t next_index );
|
|
std::size_t wrap_position( std::size_t next_position );
|
|
std::size_t cut_part( std::size_t next_part );
|
|
std::size_t available_in_memory( const Index & frame_index );
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* UMLIBRARY_DRIVER_CIRCULARBINARYREMOTEBUFFER_HH_ */
|