new class PIVariantSimple

PIObject::CONNECTU_QUEUED moved to PIVariantSimple
static_assert on undeclared operators PIByteArray << and >> with complex types
This commit is contained in:
2020-10-01 21:50:41 +03:00
parent 2ca8db70f3
commit 298765b7d8
7 changed files with 321 additions and 159 deletions

165
main.cpp
View File

@@ -1,80 +1,99 @@
#include "pip.h"
#include "pivariantsimple.h"
struct A {
double x1;
//PIString str;
template<typename T>
struct __VariantTypeInfo__ {
typedef T PureType;
typedef const T ConstPureType;
typedef T * PointerType;
typedef const T * ConstPointerType;
typedef T & ReferenceType;
typedef const T & ConstReferenceType;
};
//inline PIByteArray & operator <<(PIByteArray & s, const A & a) {s << a.x1/* << a.x2*/; return s;}
//inline PIByteArray & operator >>(PIByteArray & s, A & a) {s >> a.x1/* >> a.x2*/; return s;}
struct B {
B() {
x1=0;
#define __TYPEINFO_SINGLE(PT, T) \
template<> struct __PIVariantTypeInfo__<T> { \
typedef PT PureType; \
typedef const PT ConstPureType; \
typedef PT * PointerType; \
typedef const PT * ConstPointerType; \
typedef PT & ReferenceType; \
typedef const PT & ConstReferenceType; \
};
#define REGISTER_VARIANT_TYPEINFO(T) \
__TYPEINFO_SINGLE(T, T &) \
__TYPEINFO_SINGLE(T, const T) \
__TYPEINFO_SINGLE(T, const T &)
struct SwitchChannel {
SwitchChannel(bool on_ = true, float dur_ = 10.f, int m_count = 0, short unrely = -1) {
on = on_ ? 1 : 0;
duration = dur_;
max_count = m_count;
overload[0] = overload[1] = false;
unreliability = unrely;
}
B(const B & b) = default;
//B & operator =(const B & b) {x1=b.x1; return *this;}
//A aa;
double x1;
uchar on;
short unreliability;
float duration;
int max_count;
bool overload[2];
};
//__PIVECTOR_SIMPLE_TYPE__(B)
#include "piconditionvar.h"
int main() {
PIByteArray ba; ba.reserve(100*100*16);
PITimeMeasurer tm;
PIVector2D<double> vx;
PIVector2D<double> vd;
ba << vx;
ba >> vx;
vd.resize(100, 100);
vx = vd;
tm.reset();
for(int i=0; i<1000; ++i) {
vx.clear();
vx = vd;
ba << vx;
ba >> vd;
}
piCout << tm.elapsed_m();
PIVector2D<A> ax;
PIVector2D<A> ad;
ad.resize(100, 100);
ax = ad;
tm.reset();
for(int i=0; i<1000; ++i) {
ax.clear();
ax = ad;
ba << ax;
ba >> ad;
}
piCout << tm.elapsed_m();
PIVector2D<B> bx;
PIVector2D<B> bd;
bd.resize(100, 100);
bx = bd;
tm.reset();
for(int i=0; i<1000; ++i) {
bx.clear();
bx = bd;
ba << bx;
ba >> bd;
}
piCout << tm.elapsed_m();
PIVector2D<complexd> cx;
PIVector2D<complexd> cd;
cd.resize(100, 100);
cx = cd;
tm.reset();
for(int i=0; i<1000; ++i) {
cx.clear();
cx = cd;
ba << cx;
ba >> cd;
}
piCout << tm.elapsed_m();
return 0;
/*
inline PIByteArray & operator <<(PIByteArray & ba, const SwitchChannel & v) {
PIChunkStream cs;
cs << cs.chunk(1, v.on)
<< cs.chunk(2, v.duration)
<< cs.chunk(3, v.max_count)
<< cs.chunk(4, v.unreliability);
ba << cs.data();
return ba;
}
inline PIByteArray & operator >>(PIByteArray & ba, SwitchChannel & v) {
PIByteArray src; ba >> src; PIChunkStream cs(src);
while (!cs.atEnd()) {
switch (cs.read()) {
case 1: cs.get(v.on); break;
case 2: cs.get(v.duration); break;
case 3: cs.get(v.max_count); break;
case 4: cs.get(v.unreliability); break;
}
}
return ba;
}*/
inline PICout operator <<(PICout c, const SwitchChannel & v) {
c << v.on << v.duration << v.max_count << v.unreliability;
return c;
}
int Acnt = 0;
class A {
public:
A() {moved = false; i = "constructor"; piCout << "A()"; ++Acnt;}
A(const PIString & s): i(s) {moved = false; piCout << "A(String)"; ++Acnt;}
A(const A & a): i(a.i) {moved = false; piCout << "copy A(&)"; ++Acnt;}
A(A && a): i(std::move(a.i)) {moved = false; a.moved = true; piCout << "copy A(&&)"; ++Acnt;}
~A() {piCout << "~A()" << moved; --Acnt;}
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;}
PIString i;
bool moved;
static void F(int) {}
};
PIByteArray & operator <<(PIByteArray & ba, const A & v) {ba << v.i; return ba;}
PIByteArray & operator >>(PIByteArray & ba, A & v) {ba >> v.i; return ba;}
int main() {
A a;
PIByteArray ba;
ba >> a;
}