version 1.12.0_beta.

Very important fix in PIByteArray! Now containers stream operators for trivial types works correct
This commit is contained in:
2020-10-17 21:16:26 +03:00
parent 80c6809e42
commit d0ef71c933
3 changed files with 116 additions and 74 deletions

View File

@@ -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)

View File

@@ -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<typename T, typename std::enable_if< std::is_trivially_copyable<T>::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<typename T, typename std::enable_if< std::is_fundamental<T>::value, int>::type = 0>
//! \relatesalso PIByteArray \brief Store operator for PIVector of any trivial copyable type
template<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<PIByteArray&>() << std::declval<const T &>()), PIByteArray::StreamRef>::value, int>::type = 0>
inline PIByteArray & operator <<(PIByteArray & s, const PIVector<T> & v) {
s << int(v.size_s());
int os = s.size_s();
@@ -208,9 +220,19 @@ inline PIByteArray & operator <<(PIByteArray & s, const PIVector<T> & v) {
}
return s;
}
template<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<PIByteArray&>() << std::declval<const T &>()), PIByteArray::StreamRef>::value, int>::type = 0>
inline PIByteArray & operator <<(PIByteArray & s, const PIVector<T> & 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<typename T, typename std::enable_if< std::is_fundamental<T>::value, int>::type = 0>
//! \relatesalso PIByteArray \brief Store operator for PIDeque of any trivial copyable type
template<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<PIByteArray&>() << std::declval<const T &>()), PIByteArray::StreamRef>::value, int>::type = 0>
inline PIByteArray & operator <<(PIByteArray & s, const PIDeque<T> & v) {
s << int(v.size_s());
int os = s.size_s();
@@ -220,9 +242,19 @@ inline PIByteArray & operator <<(PIByteArray & s, const PIDeque<T> & v) {
}
return s;
}
template<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<PIByteArray&>() << std::declval<const T &>()), PIByteArray::StreamRef>::value, int>::type = 0>
inline PIByteArray & operator <<(PIByteArray & s, const PIDeque<T> & 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<typename T, typename std::enable_if< std::is_fundamental<T>::value, int>::type = 0>
//! \relatesalso PIByteArray \brief Store operator for PIVector2D of any trivial copyable type
template<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<PIByteArray&>() << std::declval<const T &>()), PIByteArray::StreamRef>::value, int>::type = 0>
inline PIByteArray & operator <<(PIByteArray & s, const PIVector2D<T> & v) {
s << int(v.rows()) << int(v.cols());
int os = s.size_s();
@@ -232,6 +264,13 @@ inline PIByteArray & operator <<(PIByteArray & s, const PIVector2D<T> & v) {
}
return s;
}
template<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<PIByteArray&>() << std::declval<const T &>()), PIByteArray::StreamRef>::value, int>::type = 0>
inline PIByteArray & operator <<(PIByteArray & s, const PIVector2D<T> & 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<typename T, typename std::enable_if< std::is_trivially_copyable<T>::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<typename T, typename std::enable_if< std::is_fundamental<T>::value, int>::type = 0>
//! \relatesalso PIByteArray \brief Restore operator for PIVector of any trivial copyable type
template<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<PIByteArray&>() << std::declval<const T &>()), PIByteArray::StreamRef>::value, int>::type = 0>
inline PIByteArray & operator >>(PIByteArray & s, PIVector<T> & v) {
assert(s.size_s() >= 4);
int sz; s >> sz;
@@ -289,9 +330,21 @@ inline PIByteArray & operator >>(PIByteArray & s, PIVector<T> & v) {
}
return s;
}
template<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<PIByteArray&>() << std::declval<const T &>()), PIByteArray::StreamRef>::value, int>::type = 0>
inline PIByteArray & operator >>(PIByteArray & s, PIVector<T> & 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<typename T, typename std::enable_if< std::is_fundamental<T>::value, int>::type = 0>
//! \relatesalso PIByteArray \brief Restore operator for PIDeque of any trivial copyable type
template<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<PIByteArray&>() << std::declval<const T &>()), PIByteArray::StreamRef>::value, int>::type = 0>
inline PIByteArray & operator >>(PIByteArray & s, PIDeque<T> & v) {
assert(s.size_s() >= 4);
int sz; s >> sz;
@@ -302,9 +355,21 @@ inline PIByteArray & operator >>(PIByteArray & s, PIDeque<T> & v) {
}
return s;
}
template<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<PIByteArray&>() << std::declval<const T &>()), PIByteArray::StreamRef>::value, int>::type = 0>
inline PIByteArray & operator >>(PIByteArray & s, PIDeque<T> & 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<typename T, typename std::enable_if< std::is_fundamental<T>::value, int>::type = 0>
//! \relatesalso PIByteArray \brief Restore operator for PIVector2D of any trivial copyable type
template<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<PIByteArray&>() << std::declval<const T &>()), PIByteArray::StreamRef>::value, int>::type = 0>
inline PIByteArray & operator >>(PIByteArray & s, PIVector2D<T> & v) {
assert(s.size_s() >= 8);
int r, c; s >> r >> c;
@@ -316,6 +381,17 @@ inline PIByteArray & operator >>(PIByteArray & s, PIVector2D<T> & v) {
}
return s;
}
template<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<PIByteArray&>() << std::declval<const T &>()), PIByteArray::StreamRef>::value, int>::type = 0>
inline PIByteArray & operator >>(PIByteArray & s, PIVector2D<T> & v) {
assert(s.size_s() >= 8);
int r,c;
PIVector<T> tmp;
s >> r >> c >> tmp;
v = PIVector2D<T>(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<Type0, Type1> & v) {s >
//! \relatesalso PIByteArray \brief Store operator for PIVector of any compound type
template<typename T, typename std::enable_if<!std::is_fundamental<T>::value, int>::type = 0>
template<typename T, typename std::enable_if<!std::is_trivially_copyable<T>::value, int>::type = 0>
inline PIByteArray & operator <<(PIByteArray & s, const PIVector<T> & 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<T> & v) {
}
//! \relatesalso PIByteArray \brief Store operator for PIDeque of any compound type
template<typename T, typename std::enable_if<!std::is_fundamental<T>::value, int>::type = 0>
template<typename T, typename std::enable_if<!std::is_trivially_copyable<T>::value, int>::type = 0>
inline PIByteArray & operator <<(PIByteArray & s, const PIDeque<T> & 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<T> & v) {
}
//! \relatesalso PIByteArray \brief Store operator for PIVector2D of any compound type
template<typename T, typename std::enable_if<!std::is_fundamental<T>::value, int>::type = 0>
template<typename T, typename std::enable_if<!std::is_trivially_copyable<T>::value, int>::type = 0>
inline PIByteArray & operator <<(PIByteArray & s, const PIVector2D<T> & v) {
s << int(v.rows()) << int(v.cols()) << v.toPlainVector();
return s;
@@ -360,7 +436,7 @@ inline PIByteArray & operator <<(PIByteArray & s, const PIVector2D<T> & v) {
//! \relatesalso PIByteArray \brief Restore operator for PIVector of any compound type
template<typename T, typename std::enable_if<!std::is_fundamental<T>::value, int>::type = 0>
template<typename T, typename std::enable_if<!std::is_trivially_copyable<T>::value, int>::type = 0>
inline PIByteArray & operator >>(PIByteArray & s, PIVector<T> & v) {
assert(s.size_s() >= 4);
int sz; s >> sz;
@@ -370,7 +446,7 @@ inline PIByteArray & operator >>(PIByteArray & s, PIVector<T> & v) {
}
//! \relatesalso PIByteArray \brief Restore operator for PIDeque of any compound type
template<typename T, typename std::enable_if<!std::is_fundamental<T>::value, int>::type = 0>
template<typename T, typename std::enable_if<!std::is_trivially_copyable<T>::value, int>::type = 0>
inline PIByteArray & operator >>(PIByteArray & s, PIDeque<T> & v) {
assert(s.size_s() >= 4);
int sz; s >> sz;
@@ -380,7 +456,7 @@ inline PIByteArray & operator >>(PIByteArray & s, PIDeque<T> & v) {
}
//! \relatesalso PIByteArray \brief Restore operator for PIVector2D of any compound type
template<typename T, typename std::enable_if<!std::is_fundamental<T>::value, int>::type = 0>
template<typename T, typename std::enable_if<!std::is_trivially_copyable<T>::value, int>::type = 0>
inline PIByteArray & operator >>(PIByteArray & s, PIVector2D<T> & v) {
assert(s.size_s() >= 8);
int r,c;

View File

@@ -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<typename X>
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<typename T> inline void regtype() {
h = new Helper<T>();
}
};
*/
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>();
MM m;
memset(&m, 0xAC, sizeof(MM));
PIVector<MM> vm;
vm << m;
m.e = 2;
m.y = 0.1;
PIVector<MM> 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>();
// 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;
}