PIObject::CONNECTU_QUEUED moved to PIVariantSimple static_assert on undeclared operators PIByteArray << and >> with complex types
159 lines
4.3 KiB
C++
159 lines
4.3 KiB
C++
/*! \file pivariantsimple.h
|
|
* \brief Variant simple type
|
|
*
|
|
* This file declares PIVariantSimple
|
|
*/
|
|
/*
|
|
PIP - Platform Independent Primitives
|
|
Variant simple type
|
|
Ivan Pelipenko peri4ko@yandex.ru
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef PIVARIANTSIMPLE_H
|
|
#define PIVARIANTSIMPLE_H
|
|
|
|
#include "pistring.h"
|
|
#include <typeinfo>
|
|
|
|
|
|
class __VariantFunctionsBase__ {
|
|
public:
|
|
virtual __VariantFunctionsBase__ * instance() {return 0;}
|
|
virtual PIString typeName() const {return PIString();}
|
|
virtual uint hash() const {return 0;}
|
|
virtual void newT(void *& ptr, const void * value) {;}
|
|
virtual void newNullT(void *& ptr) {;}
|
|
virtual void equalT(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<typename T>
|
|
class __VariantFunctions__: public __VariantFunctionsBase__ {
|
|
public:
|
|
__VariantFunctionsBase__ * instance() override {static __VariantFunctions__<T> ret; return &ret;}
|
|
PIString typeName() const override {static PIString ret(typeid(T).name()); return ret;}
|
|
uint hash() const override {static uint ret = typeName().hash(); return ret;}
|
|
void newT(void *& ptr, const void * value) override {ptr = (void*)(new T(*(const T*)value)); /*printf(" * new\n")*/;}
|
|
void newNullT(void *& ptr) override {ptr = (void*)(new T());/* printf(" * new null\n")*/;}
|
|
void equalT(void *& ptr, const void * value) override {*(T*)ptr = *(const T*)value; /*printf(" * =\n")*/;}
|
|
void deleteT(void *& ptr) override {delete (T*)(ptr); /*printf(" * del\n")*/;}
|
|
//PIByteArray toData(const void * ptr) const override {PIByteArray ret; ret << (*(const T* &)ptr); return ret;}
|
|
//void fromData(void *& ptr, PIByteArray ba) override {ba >> *(T*)ptr;}
|
|
};
|
|
|
|
class PIVariantSimple {
|
|
public:
|
|
PIVariantSimple() {ptr = 0; f = 0;}
|
|
PIVariantSimple(const PIVariantSimple & v) {
|
|
ptr = 0;
|
|
f = v.f;
|
|
if (f && v.ptr)
|
|
f->newT(ptr, v.ptr);
|
|
}
|
|
~PIVariantSimple() {destroy();}
|
|
|
|
PIVariantSimple & operator=(const PIVariantSimple & v) {
|
|
destroy();
|
|
f = v.f;
|
|
if (f && v.ptr)
|
|
f->newT(ptr, v.ptr);
|
|
return *this;
|
|
}
|
|
|
|
template <typename T>
|
|
void setValue(const T & v) {
|
|
if (f) {
|
|
if (isMyType<T>()) {
|
|
f->equalT(ptr, (const void *)&v);
|
|
return;
|
|
}
|
|
}
|
|
destroy();
|
|
f = __VariantFunctions__<T>().instance();
|
|
f->newT(ptr, (const void *)&v);
|
|
}
|
|
|
|
template <typename T>
|
|
T value() const {
|
|
if (!f) return T();
|
|
if (!isMyType<T>())
|
|
return T();
|
|
return *(T*)(ptr);
|
|
}
|
|
|
|
template <typename T>
|
|
static PIVariantSimple fromValue(const T & v) {
|
|
PIVariantSimple ret;
|
|
ret.setValue(v);
|
|
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 {
|
|
uint mh = f->hash(), th = __VariantFunctions__<T>().instance()->hash();
|
|
if (mh == 0 || th == 0) return false;
|
|
return mh == th;
|
|
}
|
|
|
|
void destroy() {
|
|
if (ptr && f)
|
|
f->deleteT(ptr);
|
|
ptr = 0;
|
|
f = 0;
|
|
}
|
|
|
|
void * ptr;
|
|
__VariantFunctionsBase__ * f;
|
|
|
|
};
|
|
|
|
|
|
#define REGISTER_PIVARIANTSIMPLE_STREAM(Type) \
|
|
STATIC_INITIALIZER_BEGIN() \
|
|
__VariantFunctionsBase__ * f = __VariantFunctions__<Type>().instance(); \
|
|
__VariantFunctionsBase__::registered()[f->hash()] = f; \
|
|
STATIC_INITIALIZER_END()
|
|
|
|
|
|
|
|
#endif // PIVARIANTSIMPLE_H
|