From 0f9e592273d34c79ab41208972fb980b92d7749d Mon Sep 17 00:00:00 2001 From: peri4 Date: Tue, 10 May 2022 12:26:05 +0300 Subject: [PATCH] start moving to binarystream --- CMakeLists.txt | 2 +- libs/main/console/pikbdlistener.h | 8 +- libs/main/console/piscreentypes.h | 7 +- libs/main/containers/pimap.h | 4 + libs/main/core/pibinarystream.h | 215 ++++++++++-------- libs/main/core/pibytearray.h | 35 ++- libs/main/core/piincludes.h | 1 + libs/main/core/pipropertystorage.h | 4 + libs/main/core/pistring.h | 3 + libs/main/core/pistringlist.h | 15 ++ libs/main/core/pivariant.h | 30 +++ libs/main/core/pivarianttypes.h | 18 ++ .../piintrospection_containers_p.h | 9 + .../introspection/piintrospection_server_p.h | 69 ++++++ .../introspection/piintrospection_threads_p.h | 9 + main.cpp | 29 ++- 16 files changed, 350 insertions(+), 108 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eaa103d9..a8f6d2be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.0) cmake_policy(SET CMP0017 NEW) # need include() with .cmake project(pip) set(pip_MAJOR 2) -set(pip_MINOR 39) +set(pip_MINOR 90) set(pip_REVISION 0) set(pip_SUFFIX ) set(pip_COMPANY SHS) diff --git a/libs/main/console/pikbdlistener.h b/libs/main/console/pikbdlistener.h index 9ce0373f..27f278de 100644 --- a/libs/main/console/pikbdlistener.h +++ b/libs/main/console/pikbdlistener.h @@ -247,14 +247,18 @@ private: }; -inline PIByteArray & operator <<(PIByteArray & s, const PIKbdListener::KeyEvent & v) {s << v.key << v.modifiers; return s;} inline PIByteArray & operator <<(PIByteArray & s, const PIKbdListener::MouseEvent & v) {s << v.x << v.y << (int)v.action << v.buttons << v.modifiers; return s;} inline PIByteArray & operator <<(PIByteArray & s, const PIKbdListener::WheelEvent & v) {s << (*(PIKbdListener::MouseEvent*)&v) << (uchar)v.direction; return s;} -inline PIByteArray & operator >>(PIByteArray & s, PIKbdListener::KeyEvent & v) {s >> v.key >> v.modifiers; return s;} inline PIByteArray & operator >>(PIByteArray & s, PIKbdListener::MouseEvent & v) {int a(0); s >> v.x >> v.y >> a >> v.buttons >> v.modifiers; v.action = (PIKbdListener::MouseAction)a; return s;} inline PIByteArray & operator >>(PIByteArray & s, PIKbdListener::WheelEvent & v) {uchar d(0); s >> (*(PIKbdListener::MouseEvent*)&v) >> d; v.direction = d; return s;} +BINARY_STREAM_STORE(PIKbdListener::MouseEvent) {s << v.x << v.y << v.action << v.buttons << v.modifiers; return s;} +BINARY_STREAM_RESTORE(PIKbdListener::MouseEvent) {s >> v.x >> v.y >> v.action >> v.buttons >> v.modifiers; return s;} + +BINARY_STREAM_STORE(PIKbdListener::WheelEvent) {s << (*(PIKbdListener::MouseEvent*)&v) << v.direction; return s;} +BINARY_STREAM_RESTORE(PIKbdListener::WheelEvent) {s >> (*(PIKbdListener::MouseEvent*)&v) >> v.direction; return s;} + REGISTER_PIVARIANTSIMPLE(PIKbdListener::KeyEvent) REGISTER_PIVARIANTSIMPLE(PIKbdListener::MouseEvent) REGISTER_PIVARIANTSIMPLE(PIKbdListener::WheelEvent) diff --git a/libs/main/console/piscreentypes.h b/libs/main/console/piscreentypes.h index 6a6b4288..4a7b15cb 100644 --- a/libs/main/console/piscreentypes.h +++ b/libs/main/console/piscreentypes.h @@ -143,12 +143,15 @@ namespace PIScreenTypes { } -inline PIByteArray & operator <<(PIByteArray & s, const PIScreenTypes::Cell & v) {s << v.format.raw_format << v.symbol; return s;} -inline PIByteArray & operator >>(PIByteArray & s, PIScreenTypes::Cell & v) {s >> v.format.raw_format >> v.symbol; return s;} +//inline PIByteArray & operator <<(PIByteArray & s, const PIScreenTypes::Cell & v) {s << v.format.raw_format << v.symbol; return s;} +//inline PIByteArray & operator >>(PIByteArray & s, PIScreenTypes::Cell & v) {s >> v.format.raw_format >> v.symbol; return s;} inline PIByteArray & operator <<(PIByteArray & s, const PIScreenTypes::TileEvent & v) {s << v.type << v.data; return s;} inline PIByteArray & operator >>(PIByteArray & s, PIScreenTypes::TileEvent & v) {s >> v.type >> v.data; return s;} +BINARY_STREAM_STORE(PIScreenTypes::TileEvent) {s << v.type << v.data; return s;} +BINARY_STREAM_RESTORE(PIScreenTypes::TileEvent) {s >> v.type >> v.data; return s;} + REGISTER_PIVARIANTSIMPLE(PIScreenTypes::TileEvent) #endif // PISCREENTYPES_H diff --git a/libs/main/containers/pimap.h b/libs/main/containers/pimap.h index 14640065..85dbddaf 100644 --- a/libs/main/containers/pimap.h +++ b/libs/main/containers/pimap.h @@ -74,6 +74,10 @@ class PIMap { template friend PIByteArray & operator >>(PIByteArray & s, PIMap & v); template friend PIByteArray & operator <<(PIByteArray & s, const PIMap & v); template friend class PIMapIterator; + template + friend PIBinaryStream

& operator <<(PIBinaryStream

& s, const PIMap & v); + template + friend PIBinaryStream

& operator >>(PIBinaryStream

& s, PIMap & v); public: PIMap() {;} PIMap(const PIMap & other) {*this = other;} diff --git a/libs/main/core/pibinarystream.h b/libs/main/core/pibinarystream.h index 16c2839b..4ff2a73b 100644 --- a/libs/main/core/pibinarystream.h +++ b/libs/main/core/pibinarystream.h @@ -31,6 +31,15 @@ #include "pivector2d.h" +#define BINARY_STREAM_FRIEND(T) \ + template friend PIBinaryStream

& operator <<(PIBinaryStream

& s, const T & v); \ + template friend PIBinaryStream

& operator >>(PIBinaryStream

& s, T & v); +#define BINARY_STREAM_STORE(T) \ + template inline PIBinaryStream

& operator <<(PIBinaryStream

& s, const T & v) +#define BINARY_STREAM_RESTORE(T) \ + template inline PIBinaryStream

& operator >>(PIBinaryStream

& s, T & v) + + //! \ingroup Core //! \~\brief //! \~english Binary serialization interface. @@ -38,52 +47,52 @@ template 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(this)->binaryStreamAppend(d, s)) { + bool binaryStreamAppend(const void * d, size_t s) { + if (!static_cast(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(this)->binaryStreamTake(d, s)) { + bool binaryStreamTake(void * d, size_t s) { + if (!static_cast(this)->binaryStreamTakeImp(d, s)) { return false; - printf("[PIBinaryStream] take() error\n"); + printf("[PIBinaryStream] binaryStreamTake() error\n"); } return true; } template - 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 -class PIBinaryStreamRef { +class PIBinaryStreamTrivialRef { public: - PIBinaryStreamRef(PIBinaryStream

& s): p(s) {} + PIBinaryStreamTrivialRef(PIBinaryStream

& s): p(s) {} PIBinaryStream

& p; }; -template inline PIBinaryStream

& operator <<(PIBinaryStreamRef

s, const T & v) {s.p << v; return s.p;} -template inline PIBinaryStream

& operator >>(PIBinaryStreamRef

s, T & v) {s.p >> v; return s.p;} +template inline PIBinaryStream

& operator <<(PIBinaryStreamTrivialRef

s, const T & v) {s.p << v; return s.p;} +template inline PIBinaryStream

& operator >>(PIBinaryStreamTrivialRef

s, T & v) {s.p >> v; return s.p;} // small types -template inline PIBinaryStreamRef

operator <<(PIBinaryStream

& s, const bool v) {s.append((uchar)v); return s;} -template inline PIBinaryStreamRef

operator <<(PIBinaryStream

& s, const char v) {s.append((uchar)v); return s;} -template inline PIBinaryStreamRef

operator <<(PIBinaryStream

& s, const uchar v) {s.append((uchar)v); return s;} -template inline PIBinaryStreamRef

operator >>(PIBinaryStream

& s, bool & v) {v = s.takeByte(); return s;} -template inline PIBinaryStreamRef

operator >>(PIBinaryStream

& s, char & v) {v = s.takeByte(); return s;} -template inline PIBinaryStreamRef

operator >>(PIBinaryStream

& s, uchar & v) {v = s.takeByte(); return s;} +template inline PIBinaryStreamTrivialRef

operator <<(PIBinaryStream

& s, const bool v) {s.binaryStreamAppend((uchar)v); return s;} +template inline PIBinaryStreamTrivialRef

operator <<(PIBinaryStream

& s, const char v) {s.binaryStreamAppend((uchar)v); return s;} +template inline PIBinaryStreamTrivialRef

operator <<(PIBinaryStream

& s, const uchar v) {s.binaryStreamAppend((uchar)v); return s;} +template inline PIBinaryStreamTrivialRef

operator >>(PIBinaryStream

& s, bool & v) {v = s.binaryStreamTakeByte(); return s;} +template inline PIBinaryStreamTrivialRef

operator >>(PIBinaryStream

& s, char & v) {v = s.binaryStreamTakeByte(); return s;} +template inline PIBinaryStreamTrivialRef

operator >>(PIBinaryStream

& s, uchar & v) {v = s.binaryStreamTakeByte(); return s;} @@ -91,9 +100,20 @@ template inline PIBinaryStreamRef

operator >>(PIBinaryStream

& // store simple types -template::value, int>::type = 0> -inline PIBinaryStreamRef

operator <<(PIBinaryStream

& s, const T & v) { - s.append(&v, sizeof(v)); +template::value, int>::type = 0> +inline PIBinaryStreamTrivialRef

operator <<(PIBinaryStream

& s, const T & v) { + //piCout << "<< enum"; + s.binaryStreamAppend((int)v); + return s; +} + + +template::value, int>::type = 0, +typename std::enable_if< std::is_trivially_copyable::value, int>::type = 0> +inline PIBinaryStreamTrivialRef

operator <<(PIBinaryStream

& s, const T & v) { + s.binaryStreamAppend(&v, sizeof(v)); return s; } @@ -102,19 +122,19 @@ inline PIBinaryStreamRef

operator <<(PIBinaryStream

& s, const T & v) { //! \~russian Оператор сохранения для PIVector тривиальных типов template::value, int>::type = 0, -typename std::enable_if< std::is_same&>() << std::declval()), PIBinaryStreamRef

>::value, int>::type = 0> +typename std::enable_if< std::is_same&>() << std::declval()), PIBinaryStreamTrivialRef

>::value, int>::type = 0> inline PIBinaryStream

& operator <<(PIBinaryStream

& s, const PIVector & 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::value, int>::type = 0, -typename std::enable_if&>() << std::declval()), PIBinaryStreamRef

>::value, int>::type = 0> +typename std::enable_if&>() << std::declval()), PIBinaryStreamTrivialRef

>::value, int>::type = 0> inline PIBinaryStream

& operator <<(PIBinaryStream

& s, const PIVector & 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

& operator <<(PIBinaryStream

& s, const PIVector //! \~russian Оператор сохранения для PIDeque тривиальных типов template::value, int>::type = 0, -typename std::enable_if< std::is_same&>() << std::declval()), PIBinaryStreamRef

>::value, int>::type = 0> +typename std::enable_if< std::is_same&>() << std::declval()), PIBinaryStreamTrivialRef

>::value, int>::type = 0> inline PIBinaryStream

& operator <<(PIBinaryStream

& s, const PIDeque & 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::value, int>::type = 0, -typename std::enable_if&>() << std::declval()), PIBinaryStreamRef

>::value, int>::type = 0> +typename std::enable_if&>() << std::declval()), PIBinaryStreamTrivialRef

>::value, int>::type = 0> inline PIBinaryStream

& operator <<(PIBinaryStream

& s, const PIDeque & 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

& operator <<(PIBinaryStream

& s, const PIDeque & //! \~russian Оператор сохранения для PIVector2D тривиальных типов template::value, int>::type = 0, -typename std::enable_if< std::is_same&>() << std::declval()), PIBinaryStreamRef

>::value, int>::type = 0> +typename std::enable_if< std::is_same&>() << std::declval()), PIBinaryStreamTrivialRef

>::value, int>::type = 0> inline PIBinaryStream

& operator <<(PIBinaryStream

& s, const PIVector2D & 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::value, int>::type = 0, -typename std::enable_if&>() << std::declval()), PIBinaryStreamRef

>::value, int>::type = 0> +typename std::enable_if&>() << std::declval()), PIBinaryStreamTrivialRef

>::value, int>::type = 0> inline PIBinaryStream

& operator <<(PIBinaryStream

& s, const PIVector2D & 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

& operator <<(PIBinaryStream

& s, const PIPair::value, int>::type = 0> -inline PIBinaryStreamRef

operator >>(PIBinaryStream

& s, T & v) { - if (!s.take(&v, sizeof(v))) { +template::value, int>::type = 0> +inline PIBinaryStreamTrivialRef

operator >>(PIBinaryStream

& s, T & v) { + //piCout << ">> enum"; + v = (T)s.binaryStreamTakeInt(); + return s; +} + + +template::value, int>::type = 0, +typename std::enable_if< std::is_trivially_copyable::value, int>::type = 0> +inline PIBinaryStreamTrivialRef

operator >>(PIBinaryStream

& 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

operator >>(PIBinaryStream

& s, T & v) { //! \~russian Оператор извлечения для PIVector тривиальных типов template::value, int>::type = 0, -typename std::enable_if< std::is_same&>() >> std::declval()), PIBinaryStreamRef

>::value, int>::type = 0> +typename std::enable_if< std::is_same&>() >> std::declval()), PIBinaryStreamTrivialRef

>::value, int>::type = 0> inline PIBinaryStream

& operator >>(PIBinaryStream

& s, PIVector & 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

& operator >>(PIBinaryStream

& s, PIVector & v) { } template::value, int>::type = 0, -typename std::enable_if&>() >> std::declval()), PIBinaryStreamRef

>::value, int>::type = 0> +typename std::enable_if&>() >> std::declval()), PIBinaryStreamTrivialRef

>::value, int>::type = 0> inline PIBinaryStream

& operator >>(PIBinaryStream

& s, PIVector & 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

& operator >>(PIBinaryStream

& s, PIVector & v) { //! \~russian Оператор извлечения для PIDeque тривиальных типов template::value, int>::type = 0, -typename std::enable_if< std::is_same&>() >> std::declval()), PIBinaryStreamRef

>::value, int>::type = 0> +typename std::enable_if< std::is_same&>() >> std::declval()), PIBinaryStreamTrivialRef

>::value, int>::type = 0> inline PIBinaryStream

& operator >>(PIBinaryStream

& s, PIDeque & 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

& operator >>(PIBinaryStream

& s, PIDeque & v) { } template::value, int>::type = 0, -typename std::enable_if&>() >> std::declval()), PIBinaryStreamRef

>::value, int>::type = 0> +typename std::enable_if&>() >> std::declval()), PIBinaryStreamTrivialRef

>::value, int>::type = 0> inline PIBinaryStream

& operator >>(PIBinaryStream

& s, PIDeque & 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

& operator >>(PIBinaryStream

& s, PIDeque & v) { //! \~russian Оператор извлечения для PIVector2D тривиальных типов template::value, int>::type = 0, -typename std::enable_if< std::is_same&>() >> std::declval()), PIBinaryStreamRef

>::value, int>::type = 0> +typename std::enable_if< std::is_same&>() >> std::declval()), PIBinaryStreamTrivialRef

>::value, int>::type = 0> inline PIBinaryStream

& operator >>(PIBinaryStream

& s, PIVector2D & 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

& operator >>(PIBinaryStream

& s, PIVector2D & v) } template::value, int>::type = 0, -typename std::enable_if&>() >> std::declval()), PIBinaryStreamRef

>::value, int>::type = 0> +typename std::enable_if&>() >> std::declval()), PIBinaryStreamTrivialRef

>::value, int>::type = 0> inline PIBinaryStream

& operator >>(PIBinaryStream

& s, PIVector2D & v) { - piCout << ">> vector2d trivial custom"; + //piCout << ">> vector2d trivial custom"; int r, c; PIVector tmp; - r = s.takeInt(); - c = s.takeInt(); + r = s.binaryStreamTakeInt(); + c = s.binaryStreamTakeInt(); s >> tmp; v = PIVector2D(r, c, tmp); return s; @@ -289,8 +320,8 @@ inline PIBinaryStream

& operator >>(PIBinaryStream

& s, PIVector2D & v) //! \~russian Оператор сохранения для PIVector сложных типов template::value, int>::type = 0> inline PIBinaryStream

& operator <<(PIBinaryStream

& s, const PIVector & 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

& operator <<(PIBinaryStream

& s, const PIVector //! \~russian Оператор сохранения для PIDeque сложных типов template::value, int>::type = 0> inline PIBinaryStream

& operator <<(PIBinaryStream

& s, const PIDeque & 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

& operator <<(PIBinaryStream

& s, const PIDeque & //! \~russian Оператор сохранения для PIVector2D сложных типов template::value, int>::type = 0> inline PIBinaryStream

& operator <<(PIBinaryStream

& s, const PIVector2D & 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

& operator <<(PIBinaryStream

& s, const PIVector2D::value, int>::type = 0> inline PIBinaryStream

& operator >>(PIBinaryStream

& s, PIVector & 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

& operator >>(PIBinaryStream

& s, PIVector & v) { //! \~russian Оператор извлечения для PIDeque сложных типов template::value, int>::type = 0> inline PIBinaryStream

& operator >>(PIBinaryStream

& s, PIDeque & 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

& operator >>(PIBinaryStream

& s, PIDeque & v) { //! \~russian Оператор извлечения для PIVector2D сложных типов template::value, int>::type = 0> inline PIBinaryStream

& operator >>(PIBinaryStream

& s, PIVector2D & 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 tmp; - r = s.takeInt(); - c = s.takeInt(); + r = s.binaryStreamTakeInt(); + c = s.binaryStreamTakeInt(); s >> tmp; v = PIVector2D(r, c, tmp); return s; @@ -382,9 +413,9 @@ inline PIBinaryStream

& operator >>(PIBinaryStream

& s, PIVector2D & v) //! \~russian Оператор сохранения template inline PIBinaryStream

& operator <<(PIBinaryStream

& s, const PIMap & 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

& operator >>(PIBinaryStream

& s, PIMap & 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; } diff --git a/libs/main/core/pibytearray.h b/libs/main/core/pibytearray.h index da738b30..0999baec 100644 --- a/libs/main/core/pibytearray.h +++ b/libs/main/core/pibytearray.h @@ -27,9 +27,7 @@ #define PIBYTEARRAY_H #include "pichar.h" -#include "pibitarray.h" -#include "pimap.h" -#include "pivector2d.h" +#include "pibinarystream.h" #include 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(data); s = size;} + RawData(const void * data_, const int size_) {d = const_cast(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" diff --git a/libs/main/core/piincludes.h b/libs/main/core/piincludes.h index 71902e27..e867a756 100644 --- a/libs/main/core/piincludes.h +++ b/libs/main/core/piincludes.h @@ -38,6 +38,7 @@ class PIMutexLocker; class PIObject; class PIString; class PIByteArray; +template class PIBinaryStream; #ifndef MICRO_PIP class PIInit; #endif diff --git a/libs/main/core/pipropertystorage.h b/libs/main/core/pipropertystorage.h index d695e197..597ad177 100644 --- a/libs/main/core/pipropertystorage.h +++ b/libs/main/core/pipropertystorage.h @@ -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 inline PIBinaryStream

& operator <<(PIBinaryStream

& s, const PIPropertyStorage::Property & v) {s << v.name << v.value << v.comment << v.flags; return s;} +template inline PIBinaryStream

& operator >>(PIBinaryStream

& 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 inline PIBinaryStream

& operator <<(PIBinaryStream

& s, const PIPropertyStorage & v) {s << v.properties(); return s;} +template inline PIBinaryStream

& operator >>(PIBinaryStream

& s, PIPropertyStorage & v) {s >> v.properties(); return s;} #endif // PIPROPERTYSTORAGE_H diff --git a/libs/main/core/pistring.h b/libs/main/core/pistring.h index c4f2940a..e404b215 100644 --- a/libs/main/core/pistring.h +++ b/libs/main/core/pistring.h @@ -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::iterator iterator; typedef PIDeque::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. diff --git a/libs/main/core/pistringlist.h b/libs/main/core/pistringlist.h index 0e6fc2a3..ba11e6a4 100644 --- a/libs/main/core/pistringlist.h +++ b/libs/main/core/pistringlist.h @@ -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 diff --git a/libs/main/core/pivariant.h b/libs/main/core/pivariant.h index 94afc609..e334617c 100644 --- a/libs/main/core/pivariant.h +++ b/libs/main/core/pivariant.h @@ -803,6 +803,36 @@ inline PIByteArray & operator >>(PIByteArray & s, PIVariant & v) { return s; } +template inline PIBinaryStream

& operator <<(PIBinaryStream

& 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 inline PIBinaryStream

& operator >>(PIBinaryStream

& 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(); diff --git a/libs/main/core/pivarianttypes.h b/libs/main/core/pivarianttypes.h index 00342f31..1c20d8a3 100644 --- a/libs/main/core/pivarianttypes.h +++ b/libs/main/core/pivarianttypes.h @@ -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 diff --git a/libs/main/introspection/piintrospection_containers_p.h b/libs/main/introspection/piintrospection_containers_p.h index cd066d00..b693367a 100644 --- a/libs/main/introspection/piintrospection_containers_p.h +++ b/libs/main/introspection/piintrospection_containers_p.h @@ -68,4 +68,13 @@ public: PIP_EXPORT PIByteArray & operator <<(PIByteArray & s, const PIIntrospectionContainers::TypeInfo & v); PIP_EXPORT PIByteArray & operator >>(PIByteArray & s, PIIntrospectionContainers::TypeInfo & v); +BINARY_STREAM_STORE (PIIntrospectionContainers::TypeInfo) { + s << PIByteArray::RawData(&v, sizeof(PIIntrospectionContainers::_Type)) << v.name; + return s; +} +BINARY_STREAM_RESTORE(PIIntrospectionContainers::TypeInfo) { + s >> PIByteArray::RawData(&v, sizeof(PIIntrospectionContainers::_Type)) >> v.name; + return s; +} + #endif // PIINTROSPECTION_CONTAINERS_P_H diff --git a/libs/main/introspection/piintrospection_server_p.h b/libs/main/introspection/piintrospection_server_p.h index 549dd044..6c3db74f 100644 --- a/libs/main/introspection/piintrospection_server_p.h +++ b/libs/main/introspection/piintrospection_server_p.h @@ -24,6 +24,7 @@ #include "piintrospection_containers_p.h" #include "piintrospection_threads.h" #include "piintrospection_threads_p.h" +#include "pichunkstream.h" #include "pisystemmonitor.h" @@ -100,4 +101,72 @@ PIP_EXPORT PIByteArray & operator >>(PIByteArray & b, PIIntrospection::ProcessIn PIP_EXPORT PIByteArray & operator <<(PIByteArray & b, const PIIntrospection::ObjectInfo & v); PIP_EXPORT PIByteArray & operator >>(PIByteArray & b, PIIntrospection::ObjectInfo & v); + +BINARY_STREAM_STORE (PIIntrospection::RequiredInfo) { + PIChunkStream cs; + cs.add(1, v.types); + s << cs.data(); + return s; +} +BINARY_STREAM_RESTORE(PIIntrospection::RequiredInfo) { + PIByteArray csba; s >> csba; + PIChunkStream cs(csba); + while (!cs.atEnd()) { + switch (cs.read()) { + case 1: cs.get(v.types); break; + default: break; + } + } + return s; +} + +BINARY_STREAM_STORE (PIIntrospection::ProcessInfo) { + PIChunkStream cs; + cs.add(1, v.architecture).add(2, v.execCommand).add(3, v.execDateTime).add(4, v.hostname).add(5, v.OS_name) + .add(6, v.OS_version).add(7, v.processorsCount).add(8, v.user).add(9, v.build_options); + s << cs.data(); + return s; +} +BINARY_STREAM_RESTORE(PIIntrospection::ProcessInfo) { + PIByteArray csba; s >> csba; + PIChunkStream cs(csba); + while (!cs.atEnd()) { + switch (cs.read()) { + case 1: cs.get(v.architecture); break; + case 2: cs.get(v.execCommand); break; + case 3: cs.get(v.execDateTime); break; + case 4: cs.get(v.hostname); break; + case 5: cs.get(v.OS_name); break; + case 6: cs.get(v.OS_version); break; + case 7: cs.get(v.processorsCount); break; + case 8: cs.get(v.user); break; + case 9: cs.get(v.build_options); break; + default: break; + } + } + return s; +} + +BINARY_STREAM_STORE (PIIntrospection::ObjectInfo) { + PIChunkStream cs; + cs.add(1, v.classname).add(2, v.name).add(3, v.parents).add(4, v.properties).add(5, v.queued_events); + s << cs.data(); + return s; +} +BINARY_STREAM_RESTORE(PIIntrospection::ObjectInfo) { + PIByteArray csba; s >> csba; + PIChunkStream cs(csba); + while (!cs.atEnd()) { + switch (cs.read()) { + case 1: cs.get(v.classname); break; + case 2: cs.get(v.name); break; + case 3: cs.get(v.parents); break; + case 4: cs.get(v.properties); break; + case 5: cs.get(v.queued_events); break; + default: break; + } + } + return s; +} + #endif // PIINTROSPECTION_SERVER_P_H diff --git a/libs/main/introspection/piintrospection_threads_p.h b/libs/main/introspection/piintrospection_threads_p.h index 3318a4d2..0164d862 100644 --- a/libs/main/introspection/piintrospection_threads_p.h +++ b/libs/main/introspection/piintrospection_threads_p.h @@ -60,4 +60,13 @@ public: PIP_EXPORT PIByteArray & operator <<(PIByteArray & b, const PIIntrospectionThreads::ThreadInfo & v); PIP_EXPORT PIByteArray & operator >>(PIByteArray & b, PIIntrospectionThreads::ThreadInfo & v); +BINARY_STREAM_STORE(PIIntrospectionThreads::ThreadInfo) { + s << v.classname << v.name << v.id << v.state << v.priority << v.delay << v.run_us << v.run_count; + return s; +} +BINARY_STREAM_RESTORE(PIIntrospectionThreads::ThreadInfo) { + s >> v.classname >> v.name >> v.id >> v.state >> v.priority >> v.delay >> v.run_us >> v.run_count; + return s; +} + #endif // PIINTROSPECTION_THREADS_P_H diff --git a/main.cpp b/main.cpp index f9e77a5b..29a9e9ba 100644 --- a/main.cpp +++ b/main.cpp @@ -9,11 +9,11 @@ using namespace PICoutManipulators; class ByteArray: public PIBinaryStream { public: PIByteArray data; - bool binaryStreamAppend(const void * d, size_t s) { + bool binaryStreamAppendImp(const void * d, size_t s) { data.append(d, s); return true; } - bool binaryStreamTake(void * d, size_t s) { + bool binaryStreamTakeImp(void * d, size_t s) { if (data.size() < s) return false; memcpy(d, data.data(), s); @@ -25,11 +25,11 @@ public: class File: public PIBinaryStream { public: PIFile file; - bool binaryStreamAppend(const void * d, size_t s) { + bool binaryStreamAppendImp(const void * d, size_t s) { file.write(d, s); return true; } - bool binaryStreamTake(void * d, size_t s) { + bool binaryStreamTakeImp(void * d, size_t s) { if (file.isEnd()) return false; file.read(d, s); @@ -58,8 +58,9 @@ int main(int argc, char * argv[]) { //f.file.open("_", PIIODevice::ReadWrite); //f.file.clear(); //PIVector vi({TS(1,20), TS(3,40)}); - PIVector vi({{'a', 10}, {'b', 20}, {'c', 30}}); + /*PIVector vi({{'a', 10}, {'b', 20}, {'c', 30}}); ba << vi; + ba.binaryStreamAppend(&ba, sizeof(ba)); piCout << "src" << vi; piCout << "s" << ba.data; vi.fill(TS()); @@ -67,6 +68,22 @@ int main(int argc, char * argv[]) { //f.file.seekToBegin(); ba >> vi; piCout << "res" << vi; - piCout << "r" << ba.data; + piCout << "r" << ba.data;*/ + PIMap map; + map[PIIODevice::ReadOnly] = {"str0", PIString::fromUTF8("русский!")}; + piMSleep(100); + map[PIIODevice::ReadWrite] = {""}; + piMSleep(100); + map[PIIODevice::WriteOnly] = {PIString::fromUTF8("русский!"), "", "1234567890", "qwertyuiop[]"}; + piCout << map; + + ba << map; + piCout << ba.data; + map.clear(); + + ba >> map; + piCout << map; + piCout << ba.data; + return 0; }