fix PIByteArray load/store operators for containers

This commit is contained in:
2020-10-16 12:28:57 +03:00
parent 51775a5ee6
commit 8ef4c9ca23
5 changed files with 96 additions and 106 deletions

144
main.cpp
View File

@@ -2,85 +2,22 @@
#include "pivariantsimple.h"
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;
};
#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;
}
uchar on;
short unreliability;
float duration;
int max_count;
bool overload[2];
};
/*
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;
#pragma pack(push, 1)
struct MM {
int x;
int x2;
double y;
char c;
PIPointd h;
char c[16];
int e2;
int e;
//PIPointd h;
};
#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 {
public:
@@ -102,8 +39,59 @@ inline PIByteArray & operator <<(PIByteArray & ba, const A & v) {ba << v.i << v.
inline PIByteArray & operator >>(PIByteArray & ba, A & v) {ba >> v.i >> v.m; return ba;}
int main() {
A a;
PIByteArray ba;
ba >> a;
/*
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>();
}
};
*/
int main() {
PIByteArray ba;
// TypeHelper th;
// th.regtype<MM>();
MM m;
PIVector<MM> vm;
vm << m;;
ba << m;
piCout << ba.toHex();
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();
return 0;
}