start moving to binarystream

This commit is contained in:
2022-05-10 12:26:05 +03:00
parent cf4f58ed95
commit 0f9e592273
16 changed files with 350 additions and 108 deletions

View File

@@ -31,6 +31,15 @@
#include "pivector2d.h"
#define BINARY_STREAM_FRIEND(T) \
template<typename P> friend PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const T & v); \
template<typename P> friend PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, T & v);
#define BINARY_STREAM_STORE(T) \
template<typename P> inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const T & v)
#define BINARY_STREAM_RESTORE(T) \
template<typename P> inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, T & v)
//! \ingroup Core
//! \~\brief
//! \~english Binary serialization interface.
@@ -38,52 +47,52 @@
template<typename P>
class PIBinaryStream {
public:
// one should implements next methods:
// one should implement next methods:
//
// bool binaryStreamAppend(const void * d, size_t s);
// bool binaryStreamTake(void * d, size_t s);
// bool binaryStreamAppendImp(const void * d, size_t s);
// bool binaryStreamTakeImp ( void * d, size_t s);
bool append(const void * d, size_t s) {
if (!static_cast<P*>(this)->binaryStreamAppend(d, s)) {
bool binaryStreamAppend(const void * d, size_t s) {
if (!static_cast<P*>(this)->binaryStreamAppendImp(d, s)) {
return false;
printf("[PIBinaryStream] append() error\n");
printf("[PIBinaryStream] binaryStreamAppend() error\n");
}
return true;
}
bool take(void * d, size_t s) {
if (!static_cast<P*>(this)->binaryStreamTake(d, s)) {
bool binaryStreamTake(void * d, size_t s) {
if (!static_cast<P*>(this)->binaryStreamTakeImp(d, s)) {
return false;
printf("[PIBinaryStream] take() error\n");
printf("[PIBinaryStream] binaryStreamTake() error\n");
}
return true;
}
template<typename T>
void append(T v) {append(&v, sizeof(v));}
uchar takeByte() {uchar r = 0; take(&r, sizeof(r)); return r;}
int takeInt() {int r = 0; take(&r, sizeof(r)); return r;}
void binaryStreamAppend(T v) {binaryStreamAppend(&v, sizeof(v));}
uchar binaryStreamTakeByte() {uchar r = 0; binaryStreamTake(&r, sizeof(r)); return r;}
int binaryStreamTakeInt() {int r = 0; binaryStreamTake(&r, sizeof(r)); return r;}
};
// helper class to detect default operators
template<typename P>
class PIBinaryStreamRef {
class PIBinaryStreamTrivialRef {
public:
PIBinaryStreamRef(PIBinaryStream<P> & s): p(s) {}
PIBinaryStreamTrivialRef(PIBinaryStream<P> & s): p(s) {}
PIBinaryStream<P> & p;
};
template<typename P, typename T> inline PIBinaryStream<P> & operator <<(PIBinaryStreamRef<P> s, const T & v) {s.p << v; return s.p;}
template<typename P, typename T> inline PIBinaryStream<P> & operator >>(PIBinaryStreamRef<P> s, T & v) {s.p >> v; return s.p;}
template<typename P, typename T> inline PIBinaryStream<P> & operator <<(PIBinaryStreamTrivialRef<P> s, const T & v) {s.p << v; return s.p;}
template<typename P, typename T> inline PIBinaryStream<P> & operator >>(PIBinaryStreamTrivialRef<P> s, T & v) {s.p >> v; return s.p;}
// small types
template<typename P> inline PIBinaryStreamRef<P> operator <<(PIBinaryStream<P> & s, const bool v) {s.append((uchar)v); return s;}
template<typename P> inline PIBinaryStreamRef<P> operator <<(PIBinaryStream<P> & s, const char v) {s.append((uchar)v); return s;}
template<typename P> inline PIBinaryStreamRef<P> operator <<(PIBinaryStream<P> & s, const uchar v) {s.append((uchar)v); return s;}
template<typename P> inline PIBinaryStreamRef<P> operator >>(PIBinaryStream<P> & s, bool & v) {v = s.takeByte(); return s;}
template<typename P> inline PIBinaryStreamRef<P> operator >>(PIBinaryStream<P> & s, char & v) {v = s.takeByte(); return s;}
template<typename P> inline PIBinaryStreamRef<P> operator >>(PIBinaryStream<P> & s, uchar & v) {v = s.takeByte(); return s;}
template<typename P> inline PIBinaryStreamTrivialRef<P> operator <<(PIBinaryStream<P> & s, const bool v) {s.binaryStreamAppend((uchar)v); return s;}
template<typename P> inline PIBinaryStreamTrivialRef<P> operator <<(PIBinaryStream<P> & s, const char v) {s.binaryStreamAppend((uchar)v); return s;}
template<typename P> inline PIBinaryStreamTrivialRef<P> operator <<(PIBinaryStream<P> & s, const uchar v) {s.binaryStreamAppend((uchar)v); return s;}
template<typename P> inline PIBinaryStreamTrivialRef<P> operator >>(PIBinaryStream<P> & s, bool & v) {v = s.binaryStreamTakeByte(); return s;}
template<typename P> inline PIBinaryStreamTrivialRef<P> operator >>(PIBinaryStream<P> & s, char & v) {v = s.binaryStreamTakeByte(); return s;}
template<typename P> inline PIBinaryStreamTrivialRef<P> operator >>(PIBinaryStream<P> & s, uchar & v) {v = s.binaryStreamTakeByte(); return s;}
@@ -91,9 +100,20 @@ template<typename P> inline PIBinaryStreamRef<P> operator >>(PIBinaryStream<P> &
// store simple types
template<typename P, typename T, typename std::enable_if< std::is_trivially_copyable<T>::value, int>::type = 0>
inline PIBinaryStreamRef<P> operator <<(PIBinaryStream<P> & s, const T & v) {
s.append(&v, sizeof(v));
template<typename P, typename T,
typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
inline PIBinaryStreamTrivialRef<P> operator <<(PIBinaryStream<P> & s, const T & v) {
//piCout << "<< enum";
s.binaryStreamAppend((int)v);
return s;
}
template<typename P, typename T,
typename std::enable_if<!std::is_enum<T>::value, int>::type = 0,
typename std::enable_if< std::is_trivially_copyable<T>::value, int>::type = 0>
inline PIBinaryStreamTrivialRef<P> operator <<(PIBinaryStream<P> & s, const T & v) {
s.binaryStreamAppend(&v, sizeof(v));
return s;
}
@@ -102,19 +122,19 @@ inline PIBinaryStreamRef<P> operator <<(PIBinaryStream<P> & s, const T & v) {
//! \~russian Оператор сохранения для PIVector тривиальных типов
template<typename P, typename T,
typename std::enable_if< std::is_trivially_copyable<T>::value, int>::type = 0,
typename std::enable_if< std::is_same<decltype(std::declval<PIBinaryStream<P>&>() << std::declval<const T &>()), PIBinaryStreamRef<P>>::value, int>::type = 0>
typename std::enable_if< std::is_same<decltype(std::declval<PIBinaryStream<P>&>() << std::declval<const T &>()), PIBinaryStreamTrivialRef<P>>::value, int>::type = 0>
inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIVector<T> & v) {
piCout << "<< vector trivial default";
s.append((int)v.size());
s.append(v.data(), v.size() * sizeof(T));
//piCout << "<< vector trivial default";
s.binaryStreamAppend((int)v.size());
s.binaryStreamAppend(v.data(), v.size() * sizeof(T));
return s;
}
template<typename P, typename T,
typename std::enable_if< std::is_trivially_copyable<T>::value, int>::type = 0,
typename std::enable_if<!std::is_same<decltype(std::declval<PIBinaryStream<P>&>() << std::declval<const T &>()), PIBinaryStreamRef<P>>::value, int>::type = 0>
typename std::enable_if<!std::is_same<decltype(std::declval<PIBinaryStream<P>&>() << std::declval<const T &>()), PIBinaryStreamTrivialRef<P>>::value, int>::type = 0>
inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIVector<T> & v) {
piCout << "<< vector trivial custom";
s.append((int)v.size());
//piCout << "<< vector trivial custom";
s.binaryStreamAppend((int)v.size());
for (size_t i = 0; i < v.size(); ++i) s << v[i];
return s;
}
@@ -124,19 +144,19 @@ inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIVector<T>
//! \~russian Оператор сохранения для PIDeque тривиальных типов
template<typename P, typename T,
typename std::enable_if< std::is_trivially_copyable<T>::value, int>::type = 0,
typename std::enable_if< std::is_same<decltype(std::declval<PIBinaryStream<P>&>() << std::declval<const T &>()), PIBinaryStreamRef<P>>::value, int>::type = 0>
typename std::enable_if< std::is_same<decltype(std::declval<PIBinaryStream<P>&>() << std::declval<const T &>()), PIBinaryStreamTrivialRef<P>>::value, int>::type = 0>
inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIDeque<T> & v) {
piCout << "<< deque trivial default";
s.append((int)v.size());
s.append(v.data(), v.size() * sizeof(T));
//piCout << "<< deque trivial default";
s.binaryStreamAppend((int)v.size());
s.binaryStreamAppend(v.data(), v.size() * sizeof(T));
return s;
}
template<typename P, typename T,
typename std::enable_if< std::is_trivially_copyable<T>::value, int>::type = 0,
typename std::enable_if<!std::is_same<decltype(std::declval<PIBinaryStream<P>&>() << std::declval<const T &>()), PIBinaryStreamRef<P>>::value, int>::type = 0>
typename std::enable_if<!std::is_same<decltype(std::declval<PIBinaryStream<P>&>() << std::declval<const T &>()), PIBinaryStreamTrivialRef<P>>::value, int>::type = 0>
inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIDeque<T> & v) {
piCout << "<< deque trivial custom";
s.append((int)v.size());
//piCout << "<< deque trivial custom";
s.binaryStreamAppend((int)v.size());
for (size_t i = 0; i < v.size(); ++i) s << v[i];
return s;
}
@@ -146,21 +166,21 @@ inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIDeque<T> &
//! \~russian Оператор сохранения для PIVector2D тривиальных типов
template<typename P, typename T,
typename std::enable_if< std::is_trivially_copyable<T>::value, int>::type = 0,
typename std::enable_if< std::is_same<decltype(std::declval<PIBinaryStream<P>&>() << std::declval<const T &>()), PIBinaryStreamRef<P>>::value, int>::type = 0>
typename std::enable_if< std::is_same<decltype(std::declval<PIBinaryStream<P>&>() << std::declval<const T &>()), PIBinaryStreamTrivialRef<P>>::value, int>::type = 0>
inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIVector2D<T> & v) {
piCout << "<< vector2d trivial default";
s.append((int)v.rows());
s.append((int)v.cols());
s.append(v.data(), v.size() * sizeof(T));
//piCout << "<< vector2d trivial default";
s.binaryStreamAppend((int)v.rows());
s.binaryStreamAppend((int)v.cols());
s.binaryStreamAppend(v.data(), v.size() * sizeof(T));
return s;
}
template<typename P, typename T,
typename std::enable_if< std::is_trivially_copyable<T>::value, int>::type = 0,
typename std::enable_if<!std::is_same<decltype(std::declval<PIBinaryStream<P>&>() << std::declval<const T &>()), PIBinaryStreamRef<P>>::value, int>::type = 0>
typename std::enable_if<!std::is_same<decltype(std::declval<PIBinaryStream<P>&>() << std::declval<const T &>()), PIBinaryStreamTrivialRef<P>>::value, int>::type = 0>
inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIVector2D<T> & v) {
piCout << "<< vector2d trivial custom";
s.append((int)v.rows());
s.append((int)v.cols());
//piCout << "<< vector2d trivial custom";
s.binaryStreamAppend((int)v.rows());
s.binaryStreamAppend((int)v.cols());
s << v.toPlainVector();
return s;
}
@@ -183,9 +203,20 @@ inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIPair<Type0
// restore simple types
template<typename P, typename T, typename std::enable_if< std::is_trivially_copyable<T>::value, int>::type = 0>
inline PIBinaryStreamRef<P> operator >>(PIBinaryStream<P> & s, T & v) {
if (!s.take(&v, sizeof(v))) {
template<typename P, typename T,
typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
inline PIBinaryStreamTrivialRef<P> operator >>(PIBinaryStream<P> & s, T & v) {
//piCout << ">> enum";
v = (T)s.binaryStreamTakeInt();
return s;
}
template<typename P, typename T,
typename std::enable_if<!std::is_enum<T>::value, int>::type = 0,
typename std::enable_if< std::is_trivially_copyable<T>::value, int>::type = 0>
inline PIBinaryStreamTrivialRef<P> operator >>(PIBinaryStream<P> & s, T & v) {
if (!s.binaryStreamTake(&v, sizeof(v))) {
printf("error with %s\n", __PIP_TYPENAME__(T));
assert(false);
}
@@ -197,12 +228,12 @@ inline PIBinaryStreamRef<P> operator >>(PIBinaryStream<P> & s, T & v) {
//! \~russian Оператор извлечения для PIVector тривиальных типов
template<typename P, typename T,
typename std::enable_if< std::is_trivially_copyable<T>::value, int>::type = 0,
typename std::enable_if< std::is_same<decltype(std::declval<PIBinaryStream<P>&>() >> std::declval<T &>()), PIBinaryStreamRef<P>>::value, int>::type = 0>
typename std::enable_if< std::is_same<decltype(std::declval<PIBinaryStream<P>&>() >> std::declval<T &>()), PIBinaryStreamTrivialRef<P>>::value, int>::type = 0>
inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIVector<T> & v) {
piCout << ">> vector trivial default";
int sz = s.takeInt();
//piCout << ">> vector trivial default";
int sz = s.binaryStreamTakeInt();
v._resizeRaw(sz);
if (!s.take(v.data(), sz * sizeof(T))) {
if (!s.binaryStreamTake(v.data(), sz * sizeof(T))) {
printf("error with PIVector<%s>\n", __PIP_TYPENAME__(T));
assert(false);
}
@@ -210,10 +241,10 @@ inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIVector<T> & v) {
}
template<typename P, typename T,
typename std::enable_if< std::is_trivially_copyable<T>::value, int>::type = 0,
typename std::enable_if<!std::is_same<decltype(std::declval<PIBinaryStream<P>&>() >> std::declval<T &>()), PIBinaryStreamRef<P>>::value, int>::type = 0>
typename std::enable_if<!std::is_same<decltype(std::declval<PIBinaryStream<P>&>() >> std::declval<T &>()), PIBinaryStreamTrivialRef<P>>::value, int>::type = 0>
inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIVector<T> & v) {
piCout << ">> vector trivial custom";
int sz = s.takeInt();
//piCout << ">> vector trivial custom";
int sz = s.binaryStreamTakeInt();
v._resizeRaw(sz);
for (int i = 0; i < sz; ++i) s >> v[i];
return s;
@@ -224,12 +255,12 @@ inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIVector<T> & v) {
//! \~russian Оператор извлечения для PIDeque тривиальных типов
template<typename P, typename T,
typename std::enable_if< std::is_trivially_copyable<T>::value, int>::type = 0,
typename std::enable_if< std::is_same<decltype(std::declval<PIBinaryStream<P>&>() >> std::declval<T &>()), PIBinaryStreamRef<P>>::value, int>::type = 0>
typename std::enable_if< std::is_same<decltype(std::declval<PIBinaryStream<P>&>() >> std::declval<T &>()), PIBinaryStreamTrivialRef<P>>::value, int>::type = 0>
inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIDeque<T> & v) {
piCout << ">> deque trivial default";
int sz = s.takeInt();
//piCout << ">> deque trivial default";
int sz = s.binaryStreamTakeInt();
v._resizeRaw(sz);
if (!s.take(v.data(), sz * sizeof(T))) {
if (!s.binaryStreamTake(v.data(), sz * sizeof(T))) {
printf("error with PIDeque<%s>\n", __PIP_TYPENAME__(T));
assert(false);
}
@@ -237,10 +268,10 @@ inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIDeque<T> & v) {
}
template<typename P, typename T,
typename std::enable_if< std::is_trivially_copyable<T>::value, int>::type = 0,
typename std::enable_if<!std::is_same<decltype(std::declval<PIBinaryStream<P>&>() >> std::declval<T &>()), PIBinaryStreamRef<P>>::value, int>::type = 0>
typename std::enable_if<!std::is_same<decltype(std::declval<PIBinaryStream<P>&>() >> std::declval<T &>()), PIBinaryStreamTrivialRef<P>>::value, int>::type = 0>
inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIDeque<T> & v) {
piCout << ">> deque trivial custom";
int sz = s.takeInt();
//piCout << ">> deque trivial custom";
int sz = s.binaryStreamTakeInt();
v._resizeRaw(sz);
for (int i = 0; i < sz; ++i) s >> v[i];
return s;
@@ -251,14 +282,14 @@ inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIDeque<T> & v) {
//! \~russian Оператор извлечения для PIVector2D тривиальных типов
template<typename P, typename T,
typename std::enable_if< std::is_trivially_copyable<T>::value, int>::type = 0,
typename std::enable_if< std::is_same<decltype(std::declval<PIBinaryStream<P>&>() >> std::declval<T &>()), PIBinaryStreamRef<P>>::value, int>::type = 0>
typename std::enable_if< std::is_same<decltype(std::declval<PIBinaryStream<P>&>() >> std::declval<T &>()), PIBinaryStreamTrivialRef<P>>::value, int>::type = 0>
inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIVector2D<T> & v) {
piCout << ">> vector2d trivial default";
//piCout << ">> vector2d trivial default";
int r, c;
r = s.takeInt();
c = s.takeInt();
r = s.binaryStreamTakeInt();
c = s.binaryStreamTakeInt();
v._resizeRaw(r, c);
if (!s.take(v.data(), v.size() * sizeof(T))) {
if (!s.binaryStreamTake(v.data(), v.size() * sizeof(T))) {
printf("error with PIVector2D<%s>\n", __PIP_TYPENAME__(T));
assert(false);
}
@@ -266,13 +297,13 @@ inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIVector2D<T> & v)
}
template<typename P, typename T,
typename std::enable_if< std::is_trivially_copyable<T>::value, int>::type = 0,
typename std::enable_if<!std::is_same<decltype(std::declval<PIBinaryStream<P>&>() >> std::declval<T &>()), PIBinaryStreamRef<P>>::value, int>::type = 0>
typename std::enable_if<!std::is_same<decltype(std::declval<PIBinaryStream<P>&>() >> std::declval<T &>()), PIBinaryStreamTrivialRef<P>>::value, int>::type = 0>
inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIVector2D<T> & v) {
piCout << ">> vector2d trivial custom";
//piCout << ">> vector2d trivial custom";
int r, c;
PIVector<T> tmp;
r = s.takeInt();
c = s.takeInt();
r = s.binaryStreamTakeInt();
c = s.binaryStreamTakeInt();
s >> tmp;
v = PIVector2D<T>(r, c, tmp);
return s;
@@ -289,8 +320,8 @@ inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIVector2D<T> & v)
//! \~russian Оператор сохранения для PIVector сложных типов
template<typename P, typename T, typename std::enable_if<!std::is_trivially_copyable<T>::value, int>::type = 0>
inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIVector<T> & v) {
piCout << "<< vector complex";
s.append(int(v.size_s()));
//piCout << "<< vector complex";
s.binaryStreamAppend(int(v.size_s()));
for (size_t i = 0; i < v.size(); ++i) s << v[i];
return s;
}
@@ -300,8 +331,8 @@ inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIVector<T>
//! \~russian Оператор сохранения для PIDeque сложных типов
template<typename P, typename T, typename std::enable_if<!std::is_trivially_copyable<T>::value, int>::type = 0>
inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIDeque<T> & v) {
piCout << "<< deque complex";
s.append(int(v.size_s()));
//piCout << "<< deque complex";
s.binaryStreamAppend(int(v.size_s()));
for (size_t i = 0; i < v.size(); ++i) s << v[i];
return s;
}
@@ -311,9 +342,9 @@ inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIDeque<T> &
//! \~russian Оператор сохранения для PIVector2D сложных типов
template<typename P, typename T, typename std::enable_if<!std::is_trivially_copyable<T>::value, int>::type = 0>
inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIVector2D<T> & v) {
piCout << "<< vector2d complex";
s.append(int(v.rows()));
s.append(int(v.cols()));
//piCout << "<< vector2d complex";
s.binaryStreamAppend(int(v.rows()));
s.binaryStreamAppend(int(v.cols()));
s << v.toPlainVector();
return s;
}
@@ -328,12 +359,12 @@ inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIVector2D<T
//! \~russian Оператор извлечения для PIVector сложных типов
template<typename P, typename T, typename std::enable_if<!std::is_trivially_copyable<T>::value, int>::type = 0>
inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIVector<T> & v) {
piCout << ">> vector complex";
//piCout << ">> vector complex";
/*if (s.size_s() < 4) {
printf("error with PIVector<%s>\n", __PIP_TYPENAME__(T));
assert(s.size_s() >= 4);
}*/
v.resize(s.takeInt());
v.resize(s.binaryStreamTakeInt());
for (size_t i = 0; i < v.size(); ++i) s >> v[i];
return s;
}
@@ -343,12 +374,12 @@ inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIVector<T> & v) {
//! \~russian Оператор извлечения для PIDeque сложных типов
template<typename P, typename T, typename std::enable_if<!std::is_trivially_copyable<T>::value, int>::type = 0>
inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIDeque<T> & v) {
piCout << ">> deque complex";
//piCout << ">> deque complex";
/*if (s.size_s() < 4) {
printf("error with PIDeque<%s>\n", __PIP_TYPENAME__(T));
assert(s.size_s() >= 4);
}*/
v.resize(s.takeInt());
v.resize(s.binaryStreamTakeInt());
for (size_t i = 0; i < v.size(); ++i) s >> v[i];
return s;
}
@@ -358,15 +389,15 @@ inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIDeque<T> & v) {
//! \~russian Оператор извлечения для PIVector2D сложных типов
template<typename P, typename T, typename std::enable_if<!std::is_trivially_copyable<T>::value, int>::type = 0>
inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIVector2D<T> & v) {
piCout << ">> vector2d complex";
//piCout << ">> vector2d complex";
/*if (s.size_s() < 8) {
printf("error with PIVecto2Dr<%s>\n", __PIP_TYPENAME__(T));
assert(s.size_s() >= 8);
}*/
int r, c;
PIVector<T> tmp;
r = s.takeInt();
c = s.takeInt();
r = s.binaryStreamTakeInt();
c = s.binaryStreamTakeInt();
s >> tmp;
v = PIVector2D<T>(r, c, tmp);
return s;
@@ -382,9 +413,9 @@ inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIVector2D<T> & v)
//! \~russian Оператор сохранения
template <typename P, typename Key, typename T>
inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIMap<Key, T> & v) {
s.append((int)v.pim_index.size_s());
s.binaryStreamAppend((int)v.pim_index.size_s());
for (uint i = 0; i < v.size(); ++i) {
s.append((int)v.pim_index[i].index);
s.binaryStreamAppend((int)v.pim_index[i].index);
s << v.pim_index[i].key;
}
s << v.pim_content;
@@ -400,10 +431,10 @@ inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIMap<Key, T> & v)
printf("error with PIMap<%s, %s>\n", __PIP_TYPENAME__(Key), __PIP_TYPENAME__(T));
assert(s.size_s() >= 4);
}*/
int sz = s.takeInt(); v.pim_index.resize(sz);
int sz = s.binaryStreamTakeInt(); v.pim_index.resize(sz);
int ind = 0;
for (int i = 0; i < sz; ++i) {
ind = s.takeInt();
ind = s.binaryStreamTakeInt();
s >> v.pim_index[i].key;
v.pim_index[i].index = ind;
}

View File

@@ -27,9 +27,7 @@
#define PIBYTEARRAY_H
#include "pichar.h"
#include "pibitarray.h"
#include "pimap.h"
#include "pivector2d.h"
#include "pibinarystream.h"
#include <stdio.h>
class PIString;
@@ -79,12 +77,15 @@ public:
public:
//! \~english Constructs data block
//! \~russian Создает блок данных
RawData(void * data = 0, int size = 0) {d = data; s = size;}
RawData(void * data_ = 0, int size_ = 0) {d = data_; s = size_;}
RawData(const RawData & o) {d = o.d; s = o.s;}
//! \~english Constructs data block
//! \~russian Создает блок данных
RawData(const void * data, const int size) {d = const_cast<void * >(data); s = size;}
RawData(const void * data_, const int size_) {d = const_cast<void * >(data_); s = size_;}
RawData & operator =(const RawData & o) {d = o.d; s = o.s; return *this;}
void * data() {return d;}
const void * data() const {return d;}
int size() const {return s;}
private:
void * d;
int s;
@@ -676,6 +677,30 @@ inline PIByteArray & operator >>(PIByteArray & s, T & ) {
}
BINARY_STREAM_STORE(PIByteArray::RawData) {
s.binaryStreamAppend(v.data(), v.size());
return s;
}
BINARY_STREAM_RESTORE(PIByteArray::RawData) {
s.binaryStreamTake(v.data(), v.size());
return s;
}
BINARY_STREAM_STORE(PIByteArray) {
s.binaryStreamAppend((int)v.size_s());
s.binaryStreamAppend(v.data(), v.size());
return s;
}
BINARY_STREAM_RESTORE(PIByteArray) {
v.resize(s.binaryStreamTakeInt());
s.binaryStreamTake(v.data(), v.size());
return s;
}
//! \relatesalso PIByteArray
//! \~english Returns PIByteArray::hash() of "ba"
//! \~russian Возвращает PIByteArray::hash() от "ba"

View File

@@ -38,6 +38,7 @@ class PIMutexLocker;
class PIObject;
class PIString;
class PIByteArray;
template <typename P> class PIBinaryStream;
#ifndef MICRO_PIP
class PIInit;
#endif

View File

@@ -273,8 +273,12 @@ protected:
inline PIByteArray & operator <<(PIByteArray & s, const PIPropertyStorage::Property & v) {s << v.name << v.value << v.comment << v.flags; return s;}
inline PIByteArray & operator >>(PIByteArray & s, PIPropertyStorage::Property & v) {s >> v.name >> v.value >> v.comment >> v.flags; return s;}
template<typename P> inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIPropertyStorage::Property & v) {s << v.name << v.value << v.comment << v.flags; return s;}
template<typename P> inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIPropertyStorage::Property & v) {s >> v.name >> v.value >> v.comment >> v.flags; return s;}
inline PIByteArray & operator <<(PIByteArray & s, const PIPropertyStorage & v) {s << v.properties(); return s;}
inline PIByteArray & operator >>(PIByteArray & s, PIPropertyStorage & v) {s >> v.properties(); return s;}
template<typename P> inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIPropertyStorage & v) {s << v.properties(); return s;}
template<typename P> inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIPropertyStorage & v) {s >> v.properties(); return s;}
#endif // PIPROPERTYSTORAGE_H

View File

@@ -42,6 +42,7 @@ class PIP_EXPORT PIString
{
friend PIByteArray & operator >>(PIByteArray & s, PIString & v);
friend PIByteArray & operator <<(PIByteArray & s, const PIString & v);
BINARY_STREAM_FRIEND(PIString);
public:
typedef PIDeque<PIChar>::iterator iterator;
typedef PIDeque<PIChar>::const_iterator const_iterator;
@@ -1584,11 +1585,13 @@ PIP_EXPORT PICout operator <<(PICout s, const PIString & v);
//! \~english Store operator.
//! \~russian Оператор сохранения.
inline PIByteArray & operator <<(PIByteArray & s, const PIString & v) {s << v.d; return s;}
BINARY_STREAM_STORE(PIString) {s << v.d; return s;}
//! \relatesalso PIByteArray
//! \~english Restore operator.
//! \~russian Оператор извлечения.
inline PIByteArray & operator >>(PIByteArray & s, PIString & v) {v.d.clear(); s >> v.d; return s;}
BINARY_STREAM_RESTORE(PIString) {v.d.clear(); s >> v.d; return s;}
//! \~english Returns concatenated string.

View File

@@ -136,6 +136,21 @@ inline PIByteArray & operator <<(PIByteArray & s, const PIStringList & v) {s <<
//! \~russian Оператор извлечения
inline PIByteArray & operator >>(PIByteArray & s, PIStringList & v) {int sz; s >> sz; v.resize(sz); for (int i = 0; i < sz; ++i) s >> v[i]; return s;}
BINARY_STREAM_STORE(PIStringList) {
s.binaryStreamAppend(v.size());
for (int i = 0; i < v.size_s(); ++i)
s << v[i];
return s;
}
BINARY_STREAM_RESTORE(PIStringList) {
v.resize(s.binaryStreamTakeInt());
for (int i = 0; i < v.size_s(); ++i)
s >> v[i];
return s;
}
//! \relatesalso PICout
//! \~english Output operator to \a PICout
//! \~russian Оператор вывода в \a PICout

View File

@@ -803,6 +803,36 @@ inline PIByteArray & operator >>(PIByteArray & s, PIVariant & v) {
return s;
}
template<typename P> inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIVariant & v) {
s << v._content << int(v._type);
if (v._type == PIVariant::pivCustom) {
#ifdef CUSTOM_PIVARIANT
if (v._info) {
s << v._info->typeName;
} else {
s << PIStringAscii("");
}
#else
s << PIStringAscii("");
#endif
}
return s;
}
template<typename P> inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIVariant & v) {
int t(0);
s >> v._content >> t;
v._type = (PIVariant::Type)t;
if (v._type == PIVariant::pivCustom) {
PIString tn;
s >> tn;
#ifdef CUSTOM_PIVARIANT
PIByteArray vc = v._content;
v = PIVariant::fromValue(vc, tn);
#endif
}
return s;
}
inline PICout operator <<(PICout s, const PIVariant & v) {
s.space(); s.setControl(0, true);
s << "PIVariant(" << v.typeName();

View File

@@ -188,4 +188,22 @@ inline PIByteArray & operator <<(PIByteArray & s, const PIVariantTypes::IODevice
inline PIByteArray & operator >>(PIByteArray & s, PIVariantTypes::IODevice & v) {s >> v.prefix >> v.mode >> v.options >> v.props; return s;}
inline PICout operator <<(PICout s, const PIVariantTypes::IODevice & v) {s << v.toPICout(); return s;}
BINARY_STREAM_STORE (PIVariantTypes::Enumerator) {s << v.value << v.name; return s;}
BINARY_STREAM_RESTORE(PIVariantTypes::Enumerator) {s >> v.value >> v.name; return s;}
BINARY_STREAM_STORE (PIVariantTypes::Enum) {s << v.enum_name << v.selected << v.enum_list; return s;}
BINARY_STREAM_RESTORE(PIVariantTypes::Enum) {s >> v.enum_name >> v.selected >> v.enum_list; return s;}
BINARY_STREAM_STORE (PIVariantTypes::File) {s << v.file << v.filter << uchar((v.is_abs ? 1 : 0) + (v.is_save ? 2 : 0)); return s;}
BINARY_STREAM_RESTORE(PIVariantTypes::File) {uchar f(0); s >> v.file >> v.filter >> f; v.is_abs = ((f & 1) == 1); v.is_save = ((f & 2) == 2); return s;}
BINARY_STREAM_STORE (PIVariantTypes::Dir) {s << v.dir << v.is_abs; return s;}
BINARY_STREAM_RESTORE(PIVariantTypes::Dir) {s >> v.dir >> v.is_abs; return s;}
BINARY_STREAM_STORE (PIVariantTypes::Color) {s << v.rgba; return s;}
BINARY_STREAM_RESTORE(PIVariantTypes::Color) {s >> v.rgba; return s;}
BINARY_STREAM_STORE (PIVariantTypes::IODevice) {s << v.prefix << v.mode << v.options << v.props; return s;}
BINARY_STREAM_RESTORE(PIVariantTypes::IODevice) {s >> v.prefix >> v.mode >> v.options >> v.props; return s;}
#endif // PIVARIANTYPES_H