diff --git a/doc/examples/pichunkstream.cpp b/doc/examples/pichunkstream.cpp index ec2048eb..dc6003ce 100644 --- a/doc/examples/pichunkstream.cpp +++ b/doc/examples/pichunkstream.cpp @@ -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] diff --git a/libs/main/core/pichunkstream.cpp b/libs/main/core/pichunkstream.cpp index ba6f9e4d..b5525aa3 100644 --- a/libs/main/core/pichunkstream.cpp +++ b/libs/main/core/pichunkstream.cpp @@ -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; } diff --git a/libs/main/core/pichunkstream.h b/libs/main/core/pichunkstream.h index d771143b..256c620d 100644 --- a/libs/main/core/pichunkstream.h +++ b/libs/main/core/pichunkstream.h @@ -67,8 +67,10 @@ public: //! Add data to this chunk strean with ID "id" and value "data" template PIChunkStream & add(int id, const T & data) {*this << ChunkConst(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);