git-svn-id: svn://db.shs.com.ru/pip@6 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
63
doc/examples/pibytearray.cpp
Normal file
63
doc/examples/pibytearray.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include "pip.h"
|
||||
|
||||
void _() {
|
||||
//! [0]
|
||||
PIByteArray ba;
|
||||
int i = -1, j = 2;
|
||||
float f = 1.;
|
||||
PIString text("123");
|
||||
ba << i << j << f << text; // form binary data
|
||||
piCout << "data =" << ba;
|
||||
i = j = 0; // clear variables
|
||||
f = 0; // clear variables
|
||||
text.clear(); // clear variables
|
||||
piCout << i << j << f << text; // show variables
|
||||
ba >> i >> j >> f >> text; // restore data
|
||||
piCout << i << j << f << text; // show variables
|
||||
piCout << "data =" << ba;
|
||||
//! [0]
|
||||
//! [1]
|
||||
struct MyType {
|
||||
MyType(int i_ = 0, const PIString & t_ = PIString()) {
|
||||
m_i = i_;
|
||||
m_text = t_;
|
||||
}
|
||||
int m_i;
|
||||
PIString m_text;
|
||||
};
|
||||
|
||||
inline PIByteArray & operator <<(PIByteArray & s, const MyType & v) {s << v.m_i << v.m_text; return s;}
|
||||
inline PIByteArray & operator >>(PIByteArray & s, MyType & v) {s >> v.m_i >> v.m_text; return s;}
|
||||
|
||||
PIByteArray ba;
|
||||
PIVector<MyType> my_vec;
|
||||
my_vec << MyType(1, "s1") << MyType(10, "s10"); // add to vector
|
||||
ba << my_vec; // store to byte array
|
||||
piCout << "data =" << ba;
|
||||
my_vec.clear(); // clear vector
|
||||
ba >> my_vec; // restore from byte array
|
||||
//! [1]
|
||||
//! [2]
|
||||
PIByteArray ba;
|
||||
const char * chars = "8 bytes";
|
||||
ba << PIByteArray::RawData(chars, 8); // form binary data
|
||||
piCout << "data =" << ba;
|
||||
char rchars[16];
|
||||
memset(rchars, 0, 16); // clear data
|
||||
ba >> PIByteArray::RawData(rchars, 8); // restore data
|
||||
piCout << rchars;
|
||||
piCout << "data =" << ba;
|
||||
//! [2]
|
||||
//! [3]
|
||||
PIByteArray ba, sba;
|
||||
uchar uc(127);
|
||||
sba << uc; // byte array with one byte
|
||||
ba << sba; // stream operator
|
||||
piCout << ba; // result
|
||||
// {1, 0, 0, 0, 127}
|
||||
ba.clear();
|
||||
ba.append(sba);
|
||||
piCout << ba; // result
|
||||
// {127}
|
||||
//! [3]
|
||||
};
|
||||
Reference in New Issue
Block a user