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

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