From fa5730f03da9285801f80cde02e30ef6188a3681 Mon Sep 17 00:00:00 2001 From: Konrad Rosenbaum Date: Tue, 12 Jul 2016 13:53:31 +0200 Subject: [PATCH] make nullable behave like smart pointer and better convertible - this makes it easier to use from scripts --- qtbase/include/nullable.h | 5 +++++ qtbase/src/object.cpp | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 0 deletions(-) diff --git a/qtbase/include/nullable.h b/qtbase/include/nullable.h index 3927476..750b5dd 100644 --- a/qtbase/include/nullable.h +++ b/qtbase/include/nullable.h @@ -59,6 +59,10 @@ templateclass WOLF_BASE_EXPORT Nullable T& value(){return elem;} /**returns the original value, if it is currently NULL it returns the given default value, if no default is given an instance created with the default constructor is returned*/ T value(const T&defaultval=T())const{if(isnull)return defaultval;else return elem;} + ///Helper to automatically convert to base type + T valueOrDefault()const{return value(T());} + ///converts the nullable to a pointer of the wrapped class - acting like a smart pointer + T* operator ->()const{if(isnull)return nullptr;else return const_cast(&elem);} /**compares this instance with a value of the original class, NULL is different from ANY instance of the original class*/ bool operator==(const T&t)const{if(isnull)return false;else return elem == t;} @@ -96,6 +100,7 @@ Q_DECLARE_METATYPE(Nullable) Q_DECLARE_METATYPE(Nullable) Q_DECLARE_METATYPE(Nullable) Q_DECLARE_METATYPE(Nullable) +Q_DECLARE_SMART_POINTER_METATYPE(Nullable) inline WOLF_BASE_EXPORT bool operator==(Nullable i1,int i2){return i1.operator==(i2);} inline WOLF_BASE_EXPORT bool operator==(Nullable i1,int i2){return i1.operator==(i2);} diff --git a/qtbase/src/object.cpp b/qtbase/src/object.cpp index 5bb17a7..490c017 100644 --- a/qtbase/src/object.cpp +++ b/qtbase/src/object.cpp @@ -7,13 +7,18 @@ #include "Nullable" #include +#include +#include QDomElement WObject::toXml(QDomDocument&doc,QString name) { return doc.createElement(name); } +static inline bool w(bool b){qDebug()<<"registering"< >()+ qRegisterMetaType >()+ qRegisterMetaType >()+ qRegisterMetaType >()+ @@ -21,3 +26,32 @@ static int itype0= qRegisterMetaType >()+ qRegisterMetaType >()+ qRegisterMetaType >(); + +static bool btype0= + //String/bytes to self + QMetaType::registerConverter,QString>(&Nullable::valueOrDefault)| + QMetaType::registerConverter,QByteArray>(&Nullable::valueOrDefault)| + //int/bool to self + QMetaType::registerConverter,bool>(&Nullable::valueOrDefault)| + QMetaType::registerConverter,int>(&Nullable::valueOrDefault)| + QMetaType::registerConverter,qint32>(&Nullable::valueOrDefault)| + QMetaType::registerConverter,qint64>(&Nullable::valueOrDefault)| + QMetaType::registerConverter,quint32>(&Nullable::valueOrDefault)| + QMetaType::registerConverter,quint64>(&Nullable::valueOrDefault)| + //int upgrade with keeping sign + QMetaType::registerConverter,qint64>([](Nullablev)->qint64{return v.valueOrDefault();})| + QMetaType::registerConverter,qint64>([](Nullablev)->qint64{return v.valueOrDefault();})| + QMetaType::registerConverter,quint64>([](Nullablev)->quint64{return v.valueOrDefault();})| + //risky int upgrade unsigned<->signed + QMetaType::registerConverter,quint64>([](Nullablev)->quint64{return v.valueOrDefault();})| + QMetaType::registerConverter,quint64>([](Nullablev)->quint64{return v.valueOrDefault();})| + QMetaType::registerConverter,quint64>([](Nullablev)->quint64{return v.valueOrDefault();})| + QMetaType::registerConverter,qint64>([](Nullablev)->qint64{return v.valueOrDefault();})| + QMetaType::registerConverter,qint64>([](Nullablev)->qint64{return v.valueOrDefault();})| + //int to string + QMetaType::registerConverter,QString>([](Nullable v)->QString{return QString::number(v.valueOrDefault());})| + QMetaType::registerConverter,QString>([](Nullable v)->QString{return QString::number(v.valueOrDefault());})| + QMetaType::registerConverter,QString>([](Nullable v)->QString{return QString::number(v.valueOrDefault());})| + QMetaType::registerConverter,QString>([](Nullable v)->QString{return QString::number(v.valueOrDefault());})| + QMetaType::registerConverter,QString>([](Nullable v)->QString{return QString::number(v.valueOrDefault());})| + true; -- 1.7.2.5