Files
pip/doc/examples/pichunkstream.cpp
Пелипенко Иван cc22bf0c67 PIChunkStream
git-svn-id: svn://db.shs.com.ru/pip@176 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
2016-01-31 09:05:23 +00:00

50 lines
1.1 KiB
C++

#include "pip.h"
//! [struct]
// Your struct
struct S {
int i;
float f;
PIString s;
};
// Operators
PIByteArray & operator <<(PIByteArray & b, const S & s) {b << s.i << s.f << s.s; return b;}
PIByteArray & operator >>(PIByteArray & b, S & s) {b >> s.i >> s.f >> s.s; return b;}
//! [struct]
//! [write]
// Write chunk stream
S s;
s.i = 99;
s.f = 0.01;
s.s = "SSS";
PIVector<float> f;
f << -1. << 2.5 << 11.;
// write some data to empty stream
PIChunkStream cs;
cs << cs.chunk(1, int(10))
<< cs.chunk(2, PIString("text"))
<< cs.chunk(4, f)
<< cs.chunk(3, s);
// now you can take cs.data() and send or place it somewhere ...
//! [write]
//! [read]
// create stream for read, cs from upper code
PIByteArray ba(cs.data());
PIChunkStream cs2(ba);
int i(0);
PIString str;
S s;
PIVector<float> f;
// read from stream
while (!cs2.atEnd()) {
switch (cs2.read()) {
case 1: i = cs2.getData<int>(); break;
case 2: str = cs2.getData<PIString>(); break;
case 3: s = cs2.getData<S>(); break;
case 4: f = cs2.getData<PIVector<float> >(); break;
}
}
piCout << i << str << f << s.i << s.f << s.s;
//! [read]