REGISTER_PIVARIANTSIMPLE
This commit is contained in:
@@ -252,8 +252,8 @@ inline PIByteArray & operator >>(PIByteArray & s, PIKbdListener::KeyEvent & v) {
|
||||
inline PIByteArray & operator >>(PIByteArray & s, PIKbdListener::MouseEvent & v) {int a(0); s >> v.x >> v.y >> a >> v.buttons >> v.modifiers; v.action = (PIKbdListener::MouseAction)a; return s;}
|
||||
inline PIByteArray & operator >>(PIByteArray & s, PIKbdListener::WheelEvent & v) {uchar d(0); s >> (*(PIKbdListener::MouseEvent*)&v) >> d; v.direction = d; return s;}
|
||||
|
||||
REGISTER_NS_VARIANT(PIKbdListener, KeyEvent)
|
||||
REGISTER_NS_VARIANT(PIKbdListener, MouseEvent)
|
||||
REGISTER_NS_VARIANT(PIKbdListener, WheelEvent)
|
||||
REGISTER_PIVARIANTSIMPLE(PIKbdListener::KeyEvent)
|
||||
REGISTER_PIVARIANTSIMPLE(PIKbdListener::MouseEvent)
|
||||
REGISTER_PIVARIANTSIMPLE(PIKbdListener::WheelEvent)
|
||||
|
||||
#endif // PIKBDLISTENER_H
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include "pivariant.h"
|
||||
#endif
|
||||
|
||||
|
||||
class __VariantFunctionsBase__ {
|
||||
public:
|
||||
virtual __VariantFunctionsBase__ * instance() {return 0;}
|
||||
@@ -40,23 +41,11 @@ public:
|
||||
virtual void newNullT(void *& ptr) {;}
|
||||
virtual void assignT(void *& ptr, const void * value) {;}
|
||||
virtual void deleteT(void *& ptr) {;}
|
||||
//virtual PIByteArray toData(const void * ptr) const {return PIByteArray();}
|
||||
//virtual void fromData(void *& ptr, PIByteArray ba) {;}
|
||||
//static PIMap<uint, __VariantFunctionsBase__*> & registered() {static PIMap<uint, __VariantFunctionsBase__*> ret; return ret;}
|
||||
};
|
||||
|
||||
/*
|
||||
template<class T, class Enable = void>
|
||||
class __VariantFunctions__: public __VariantFunctionsBase__ {
|
||||
public:
|
||||
__VariantFunctionsBase__ * instance() final {static __VariantFunctions__<T> ret; return &ret;}
|
||||
PIString typeName() const final {static PIString ret(typeid(T).name()); return ret;}
|
||||
uint hash() const final {static uint ret = typeName().hash(); return ret;}
|
||||
};
|
||||
*/
|
||||
|
||||
template<typename T>
|
||||
class __VariantFunctions__/*<T, typename std::enable_if< std::is_assignable<T&, const T&>::value>::type>*/: public __VariantFunctionsBase__ {
|
||||
class __VariantFunctions__: public __VariantFunctionsBase__ {
|
||||
public:
|
||||
__VariantFunctionsBase__ * instance() final {static __VariantFunctions__<T> ret; return &ret;}
|
||||
#ifdef PIP_FREERTOS
|
||||
@@ -65,26 +54,10 @@ public:
|
||||
PIString typeName() const final {static PIString ret(typeid(T).name()); return ret;}
|
||||
#endif
|
||||
uint hash() const final {static uint ret = typeName().hash(); return ret;}
|
||||
void newT(void *& ptr, const void * value) final {ptr = (void*)(new T(*(const T*)value)); /*printf(" * new\n")*/;}
|
||||
void newNullT(void *& ptr) final {ptr = (void*)(new T());/* printf(" * new null\n")*/;}
|
||||
void assignT(void *& ptr, const void * value) final {*(T*)ptr = *(const T*)value; /*printf(" * =\n")*/;}
|
||||
void deleteT(void *& ptr) final {delete (T*)(ptr); /*printf(" * del\n")*/;}
|
||||
//PIByteArray toData(const void * ptr) const final {PIByteArray ret; ret << (*(const T* &)ptr); return ret;}
|
||||
//void fromData(void *& ptr, PIByteArray ba) final {ba >> *(T*)ptr;}
|
||||
};
|
||||
|
||||
template<>
|
||||
class __VariantFunctions__<std::function<void(void*)> >: public __VariantFunctionsBase__ {
|
||||
public:
|
||||
__VariantFunctionsBase__ * instance() final {static __VariantFunctions__<std::function<void(void*)> > ret; return &ret;}
|
||||
PIString typeName() const final {static PIString ret("std::function<void(void*)>"); return ret;}
|
||||
uint hash() const final {static uint ret = typeName().hash(); return ret;}
|
||||
void newT(void *& ptr, const void * value) final {ptr = (void*)(new std::function<void(void*)>(*(const std::function<void(void*)> *)value)); /*printf(" * new\n")*/;}
|
||||
void newNullT(void *& ptr) final {ptr = (void*)(new std::function<void(void*)> ());/* printf(" * new null\n")*/;}
|
||||
void assignT(void *& ptr, const void * value) final {*(std::function<void(void*)> *)ptr = *(const std::function<void(void*)> *)value; /*printf(" * =\n")*/;}
|
||||
void deleteT(void *& ptr) final {delete (std::function<void(void*)> *)(ptr); /*printf(" * del\n")*/;}
|
||||
//PIByteArray toData(const void * ptr) const final {PIByteArray ret; ret << (*(const T* &)ptr); return ret;}
|
||||
//void fromData(void *& ptr, PIByteArray ba) final {ba >> *(T*)ptr;}
|
||||
void newT(void *& ptr, const void * value) final {ptr = (void*)(new T(*(const T*)value));}
|
||||
void newNullT(void *& ptr) final {ptr = (void*)(new T());}
|
||||
void assignT(void *& ptr, const void * value) final {*(T*)ptr = *(const T*)value;}
|
||||
void deleteT(void *& ptr) final {delete (T*)(ptr);}
|
||||
};
|
||||
|
||||
|
||||
@@ -135,27 +108,6 @@ public:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
PIByteArray save() const {
|
||||
if (!ptr || !f) return PIByteArray();
|
||||
PIByteArray ret;
|
||||
ret << f->hash();
|
||||
ret.append(f->toData(ptr));
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool load(PIByteArray ba) {
|
||||
if (ba.size_s() < 4) return false;
|
||||
uint h(0); ba >> h;
|
||||
destroy();
|
||||
f = __VariantFunctionsBase__::registered().value(h, 0);
|
||||
if (!f) return false;
|
||||
f->newNullT(ptr);
|
||||
f->fromData(ptr, ba);
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
bool isMyType() const {
|
||||
@@ -177,13 +129,20 @@ private:
|
||||
|
||||
};
|
||||
|
||||
/*
|
||||
#define REGISTER_PIVARIANTSIMPLE_STREAM(Type) \
|
||||
STATIC_INITIALIZER_BEGIN \
|
||||
__VariantFunctionsBase__ * f = __VariantFunctions__<Type>().instance(); \
|
||||
__VariantFunctionsBase__::registered()[f->hash()] = f; \
|
||||
STATIC_INITIALIZER_END
|
||||
*/
|
||||
|
||||
#define REGISTER_PIVARIANTSIMPLE(Type) \
|
||||
template<> \
|
||||
class __VariantFunctions__<Type>: public __VariantFunctionsBase__ { \
|
||||
public: \
|
||||
__VariantFunctionsBase__ * instance() final {static __VariantFunctions__<Type> ret; return &ret;} \
|
||||
PIString typeName() const final {static PIString ret(#Type); return ret;} \
|
||||
uint hash() const final {static uint ret = typeName().hash(); return ret;} \
|
||||
void newT(void *& ptr, const void * value) final {ptr = (void*)(new Type(*(const Type *)value));} \
|
||||
void newNullT(void *& ptr) final {ptr = (void*)(new Type());} \
|
||||
void assignT(void *& ptr, const void * value) final {*(Type *)ptr = *(const Type *)value;} \
|
||||
void deleteT(void *& ptr) final {delete (Type *)(ptr);} \
|
||||
};
|
||||
|
||||
REGISTER_PIVARIANTSIMPLE(std::function<void(void*)>)
|
||||
|
||||
#endif // PIVARIANTSIMPLE_H
|
||||
|
||||
Reference in New Issue
Block a user