next easing of PIChunkStream::extract()

This commit is contained in:
2020-10-08 22:47:32 +03:00
parent 2e3204806f
commit 51775a5ee6
3 changed files with 25 additions and 3 deletions

View File

@@ -47,3 +47,21 @@ while (!cs2.atEnd()) {
}
piCout << i << str << f << s.i << s.f << s.s;
//! [read]
//! [write_new]
PIByteArray & operator <<(PIByteArray & s, const S & value) {
PIChunkStream cs;
cs.add(1, value.i).add(2, value.f).add(3, value.s);
s << cs.data();
return s;
}
//! [write_new]
//! [read_new]
PIByteArray & operator >>(PIByteArray & s, S & value) {
PIChunkStream cs;
if (!cs.extract(s)) return s;
cs.readAll();
cs.get(1, value.i).get(2, value.f).get(3, value.s);
return b;
}
//! [read_new]

View File

@@ -127,12 +127,14 @@ PIChunkStream::~PIChunkStream() {
}
bool PIChunkStream::extract(PIByteArray & data) {
bool PIChunkStream::extract(PIByteArray & data, bool read_all) {
if (data.size_s() < 4) return false;
data >> tmp_data;
if (tmp_data.size_s() < 4) return false;
data_ = &tmp_data;
_init();
if (read_all)
readAll();
return true;
}

View File

@@ -67,8 +67,10 @@ public:
//! Add data to this chunk strean with ID "id" and value "data"
template <typename T> PIChunkStream & add(int id, const T & data) {*this << ChunkConst<T>(id, data); return *this;}
//! Extract %PIByteArray from "data" and set it current stream. Returns if has data to read.
bool extract(PIByteArray & data);
//! Extract %PIByteArray from "data" and set it current stream.
//! If "read_all" then call \a readAll() after extract.
//! Returns if has data to read.
bool extract(PIByteArray & data, bool read_all = false);
void setSource(const PIByteArray & data);
void setSource(PIByteArray * data);