PIChunkStream::set() new feature

This commit is contained in:
2021-07-16 01:05:48 +03:00
parent 040eb3b279
commit c87e48c0a5
4 changed files with 97 additions and 16 deletions

View File

@@ -76,7 +76,7 @@ public:
void setSource(PIByteArray * data);
//! Returns internal buffer with written data
PIByteArray data() const {return tmp_data;}
PIByteArray data() const;
//! Returns if there is end of stream
bool atEnd() const {return data_->size_s() <= 1;}
@@ -105,25 +105,43 @@ public:
//! Place value of chunk with id \"id\" into \"v\". You should call \a readAll() before using this function!
template <typename T>
const PIChunkStream & get(int id, T & v) const {
PIPair<int, int> pos = data_map.value(id);
if (pos.first < 0 || pos.second == 0) return *this;
PIByteArray ba(data_->data(pos.first), pos.second);
CacheEntry pos = data_map.value(id);
if (pos.start < 0 || pos.length == 0) return *this;
PIByteArray ba(data_->data(pos.start), pos.length);
if (!ba.isEmpty())
ba >> v;
return *this;
}
//! Replace value of chunk with ID \"id\" to \"v\". You should call \a readAll() before using this function!
template <typename T>
PIChunkStream & set(int id, const T & v) {
PIByteArray ba;
ba << v;
replaceChunk(id, ba);
return *this;
}
private:
void _init();
struct CacheEntry {
CacheEntry(int s = 0, int l = 0, int b = 0): start(s), length(l), size_bytes(b) {}
int start;
int length;
int size_bytes;
};
static uint readVInt(PIByteArray & s, uchar * bytes = 0);
static void writeVInt(PIByteArray & s, uint val);
static int peekVInt(Version version_, PIByteArray * data_, int pos, PIByteArray & hdr, uint & ret);
void replaceChunk(int id, const PIByteArray & v);
int last_id;
uchar version_;
PIByteArray * data_, last_data, tmp_data;
PIMap<int, PIPair<int, int>> data_map;
mutable PIByteArray * data_, last_data, tmp_data;
PIMap<int, CacheEntry> data_map;
bool first_byte_taken;
template <typename T> friend PIChunkStream & operator <<(PIChunkStream & s, const PIChunkStream::Chunk<T> & c);
template <typename T> friend PIChunkStream & operator <<(PIChunkStream & s, const PIChunkStream::ChunkConst<T> & c);