diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e0391a5..6c4173c9 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 11) +set(pip_MINOR 12) set(pip_REVISION 0) set(pip_SUFFIX beta) set(pip_COMPANY SHS) diff --git a/libs/main/core/pibytearray.h b/libs/main/core/pibytearray.h index dfeeab5d..687a4d76 100644 --- a/libs/main/core/pibytearray.h +++ b/libs/main/core/pibytearray.h @@ -119,6 +119,16 @@ public: static PIByteArray fromHex(PIString str); static PIByteArray fromBase64(const PIByteArray & base64); static PIByteArray fromBase64(const PIString & base64); + + + class StreamRef { + public: + StreamRef(PIByteArray & s): ba(s) {} + operator PIByteArray&() {return ba;} + private: + PIByteArray & ba; + }; + }; //! \relatesalso PIByteArray \brief Byte arrays compare operator @@ -177,7 +187,7 @@ inline PIByteArray & operator <<(PIByteArray & s, const uchar v) {s.push_back(v) //! \relatesalso PIByteArray \brief Store operator for any trivial copyable type template::value, int>::type = 0> -inline PIByteArray & operator <<(PIByteArray & s, const T & v) { +inline PIByteArray::StreamRef operator <<(PIByteArray & s, const T & v) { int os = s.size_s(); s.enlarge(sizeof(v)); memcpy(s.data(os), &v, sizeof(v)); @@ -197,8 +207,10 @@ inline PIByteArray & operator <<(PIByteArray & s, const PIByteArray::RawData & v return s; } -//! \relatesalso PIByteArray \brief Store operator for PIVector of any fundamental type -template::value, int>::type = 0> +//! \relatesalso PIByteArray \brief Store operator for PIVector of any trivial copyable type +template::value, int>::type = 0, + typename std::enable_if< std::is_same() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator <<(PIByteArray & s, const PIVector & v) { s << int(v.size_s()); int os = s.size_s(); @@ -208,9 +220,19 @@ inline PIByteArray & operator <<(PIByteArray & s, const PIVector & v) { } return s; } +template::value, int>::type = 0, + typename std::enable_if() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> +inline PIByteArray & operator <<(PIByteArray & s, const PIVector & v) { + s << int(v.size_s()); + for (uint i = 0; i < v.size(); ++i) s << v[i]; + return s; +} -//! \relatesalso PIByteArray \brief Store operator for PIDeque of any fundamental type -template::value, int>::type = 0> +//! \relatesalso PIByteArray \brief Store operator for PIDeque of any trivial copyable type +template::value, int>::type = 0, + typename std::enable_if< std::is_same() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator <<(PIByteArray & s, const PIDeque & v) { s << int(v.size_s()); int os = s.size_s(); @@ -220,9 +242,19 @@ inline PIByteArray & operator <<(PIByteArray & s, const PIDeque & v) { } return s; } +template::value, int>::type = 0, + typename std::enable_if() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> +inline PIByteArray & operator <<(PIByteArray & s, const PIDeque & v) { + s << int(v.size_s()); + for (uint i = 0; i < v.size(); ++i) s << v[i]; + return s; +} -//! \relatesalso PIByteArray \brief Store operator for PIVector2D of any fundamental type -template::value, int>::type = 0> +//! \relatesalso PIByteArray \brief Store operator for PIVector2D of any trivial copyable type +template::value, int>::type = 0, + typename std::enable_if< std::is_same() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator <<(PIByteArray & s, const PIVector2D & v) { s << int(v.rows()) << int(v.cols()); int os = s.size_s(); @@ -232,6 +264,13 @@ inline PIByteArray & operator <<(PIByteArray & s, const PIVector2D & v) { } return s; } +template::value, int>::type = 0, + typename std::enable_if() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> +inline PIByteArray & operator <<(PIByteArray & s, const PIVector2D & v) { + s << int(v.rows()) << int(v.cols()) << v.toPlainVector(); + return s; +} //! \relatesalso PIByteArray \brief Store operator inline PIByteArray & operator <<(PIByteArray & s, const PIBitArray & v) {s << v.size_ << v.data_; return s;} @@ -257,7 +296,7 @@ inline PIByteArray & operator >>(PIByteArray & s, uchar & v) {assert(s.size() >= //! \relatesalso PIByteArray \brief Restore operator for any trivial copyable type template::value, int>::type = 0> -inline PIByteArray & operator >>(PIByteArray & s, T & v) { +inline PIByteArray::StreamRef operator >>(PIByteArray & s, T & v) { assert(s.size() >= sizeof(v)); memcpy((void*)(&v), s.data(), sizeof(v)); s.remove(0, sizeof(v)); @@ -277,8 +316,10 @@ inline PIByteArray & operator >>(PIByteArray & s, PIByteArray::RawData v) { return s; } -//! \relatesalso PIByteArray \brief Restore operator for PIVector of any fundamental type -template::value, int>::type = 0> +//! \relatesalso PIByteArray \brief Restore operator for PIVector of any trivial copyable type +template::value, int>::type = 0, + typename std::enable_if< std::is_same() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator >>(PIByteArray & s, PIVector & v) { assert(s.size_s() >= 4); int sz; s >> sz; @@ -289,9 +330,21 @@ inline PIByteArray & operator >>(PIByteArray & s, PIVector & v) { } return s; } +template::value, int>::type = 0, + typename std::enable_if() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> +inline PIByteArray & operator >>(PIByteArray & s, PIVector & v) { + assert(s.size_s() >= 4); + int sz; s >> sz; + v.resize(sz); + for (int i = 0; i < sz; ++i) s >> v[i]; + return s; +} -//! \relatesalso PIByteArray \brief Restore operator for PIDeque of any fundamental type -template::value, int>::type = 0> +//! \relatesalso PIByteArray \brief Restore operator for PIDeque of any trivial copyable type +template::value, int>::type = 0, + typename std::enable_if< std::is_same() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator >>(PIByteArray & s, PIDeque & v) { assert(s.size_s() >= 4); int sz; s >> sz; @@ -302,9 +355,21 @@ inline PIByteArray & operator >>(PIByteArray & s, PIDeque & v) { } return s; } +template::value, int>::type = 0, + typename std::enable_if() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> +inline PIByteArray & operator >>(PIByteArray & s, PIDeque & v) { + assert(s.size_s() >= 4); + int sz; s >> sz; + v.resize(sz); + for (int i = 0; i < sz; ++i) s >> v[i]; + return s; +} -//! \relatesalso PIByteArray \brief Restore operator for PIVector2D of any fundamental type -template::value, int>::type = 0> +//! \relatesalso PIByteArray \brief Restore operator for PIVector2D of any trivial copyable type +template::value, int>::type = 0, + typename std::enable_if< std::is_same() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator >>(PIByteArray & s, PIVector2D & v) { assert(s.size_s() >= 8); int r, c; s >> r >> c; @@ -316,6 +381,17 @@ inline PIByteArray & operator >>(PIByteArray & s, PIVector2D & v) { } return s; } +template::value, int>::type = 0, + typename std::enable_if() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> +inline PIByteArray & operator >>(PIByteArray & s, PIVector2D & v) { + assert(s.size_s() >= 8); + int r,c; + PIVector tmp; + s >> r >> c >> tmp; + v = PIVector2D(r, c, tmp); + return s; +} //! \relatesalso PIByteArray \brief Restore operator inline PIByteArray & operator >>(PIByteArray & s, PIBitArray & v) {assert(s.size_s() >= 8); s >> v.size_ >> v.data_; return s;} @@ -331,7 +407,7 @@ inline PIByteArray & operator >>(PIByteArray & s, PIPair & v) {s > //! \relatesalso PIByteArray \brief Store operator for PIVector of any compound type -template::value, int>::type = 0> +template::value, int>::type = 0> inline PIByteArray & operator <<(PIByteArray & s, const PIVector & v) { s << int(v.size_s()); for (uint i = 0; i < v.size(); ++i) s << v[i]; @@ -339,7 +415,7 @@ inline PIByteArray & operator <<(PIByteArray & s, const PIVector & v) { } //! \relatesalso PIByteArray \brief Store operator for PIDeque of any compound type -template::value, int>::type = 0> +template::value, int>::type = 0> inline PIByteArray & operator <<(PIByteArray & s, const PIDeque & v) { s << int(v.size_s()); for (uint i = 0; i < v.size(); ++i) s << v[i]; @@ -347,7 +423,7 @@ inline PIByteArray & operator <<(PIByteArray & s, const PIDeque & v) { } //! \relatesalso PIByteArray \brief Store operator for PIVector2D of any compound type -template::value, int>::type = 0> +template::value, int>::type = 0> inline PIByteArray & operator <<(PIByteArray & s, const PIVector2D & v) { s << int(v.rows()) << int(v.cols()) << v.toPlainVector(); return s; @@ -360,7 +436,7 @@ inline PIByteArray & operator <<(PIByteArray & s, const PIVector2D & v) { //! \relatesalso PIByteArray \brief Restore operator for PIVector of any compound type -template::value, int>::type = 0> +template::value, int>::type = 0> inline PIByteArray & operator >>(PIByteArray & s, PIVector & v) { assert(s.size_s() >= 4); int sz; s >> sz; @@ -370,7 +446,7 @@ inline PIByteArray & operator >>(PIByteArray & s, PIVector & v) { } //! \relatesalso PIByteArray \brief Restore operator for PIDeque of any compound type -template::value, int>::type = 0> +template::value, int>::type = 0> inline PIByteArray & operator >>(PIByteArray & s, PIDeque & v) { assert(s.size_s() >= 4); int sz; s >> sz; @@ -380,7 +456,7 @@ inline PIByteArray & operator >>(PIByteArray & s, PIDeque & v) { } //! \relatesalso PIByteArray \brief Restore operator for PIVector2D of any compound type -template::value, int>::type = 0> +template::value, int>::type = 0> inline PIByteArray & operator >>(PIByteArray & s, PIVector2D & v) { assert(s.size_s() >= 8); int r,c; diff --git a/main.cpp b/main.cpp index 0496406a..cc9da2ef 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,6 @@ #include "pip.h" #include "pivariantsimple.h" - #pragma pack(push, 1) struct MM { int x; @@ -14,9 +13,6 @@ struct MM { }; #pragma pack(pop) -inline PIByteArray & operator <<(PIByteArray & ba, const MM & v) {piCout << "xep1"; ba << v.x << v.y << v.c; return ba;} -inline PIByteArray & operator >>(PIByteArray & ba, MM & v) {piCout << "xep2"; ba >> v.x >> v.y >> v.c; return ba;} - int Acnt = 0; class A { @@ -29,6 +25,7 @@ public: void swap(A & a) {piCout << "swap A()"; piSwap(i, a.i);} A & operator =(const A & a) {i = a.i; piCout << "= A&"; return *this;} A & operator =(A && a) {piSwap(i, a.i); a.moved = true; piCout << "= A&&)"; return *this;} + bool operator ==(const A & a) {return true;} PIString i; bool moved; MM m; @@ -38,61 +35,30 @@ public: inline PIByteArray & operator <<(PIByteArray & ba, const A & v) {ba << v.i << v.m; return ba;} inline PIByteArray & operator >>(PIByteArray & ba, A & v) {ba >> v.i >> v.m; return ba;} - -/* -class HelperBase { -public: - virtual void bsl(PIByteArray & ba, void * o) = 0; -}; - - -template -class Helper : public HelperBase { -public: -void bsl(PIByteArray & ba, void * o) override { - ba << (*(X*)o); - piCout << ba.size(); - ba >> (*(X*)o); - piCout << ba.size(); -} -}; - - -class TypeHelper { -public: - HelperBase * h; - template inline void regtype() { - h = new Helper(); - } -}; -*/ +inline PIByteArray & operator <<(PIByteArray & ba, const MM & v) {piCout << "<<"; ba << v.x << v.y << v.c; return ba;} +inline PIByteArray & operator >>(PIByteArray & ba, MM & v) {piCout << ">>"; ba >> v.x >> v.y >> v.c; return ba;} int main() { PIByteArray ba; -// TypeHelper th; -// th.regtype(); MM m; - memset(&m, 0xAC, sizeof(MM)); - PIVector vm; - vm << m; + m.e = 2; + m.y = 0.1; + PIVector v; + //v.resize(1,2,m); + v << m << m; + + piCout << "m:"; ba << m; - piCout << ba.toHex(); + piCout << ba; + ba >> m; + piCout << ba; + ba.clear(); - piCout << "==="; - ba << vm; - piCout << ba.toHex(); -// th.h->bsl(ba, &m); -// th.regtype(); -// CAN_Raw c; -// th.h->bsl(ba, &c); -// PIVariantSimple v; -// v.setValue(m); -// ba = v.save(); -// piCout << ba.toHex(); -// PIVariantSimple v2; -// v2.load(ba); -// piCout << v2.save().toHex(); + piCout << "v:"; + ba << v; + piCout << ba; + ba >> v; + piCout << ba; return 0; } -