00001 // Copyright (C) 2009-2011 by Konrad Rosenbaum <konrad@silmor.de> 00002 // protected under the GNU LGPL version 3 or at your option any newer. 00003 // See COPYING.LGPL file that comes with this distribution. 00004 // 00005 00006 00007 #ifndef WOLF_NULLABLE_H 00008 #define WOLF_NULLABLE_H 00009 00011 class NullValue 00012 { 00013 public: 00014 NullValue(){} 00015 NullValue(const NullValue&){} 00016 00017 // operator void*()const{return (void*)0;} 00018 template<typename T> operator T*()const{return (T*)0;} 00019 00020 bool operator==(const NullValue&)const{return true;} 00021 bool operator!=(const NullValue&)const{return false;} 00022 00023 template<typename T>bool operator==(const T*p)const{return p==0;} 00024 template<typename T>bool operator!=(const T*p)const{return p!=0;} 00025 }; 00026 00028 const NullValue nullval; 00029 00031 template<class T>class Nullable 00032 { 00033 public: 00035 Nullable():isnull(true),elem(){} 00037 Nullable(const NullValue&):isnull(true),elem(){} 00039 Nullable(const T&t):isnull(false),elem(t){} 00041 Nullable(const Nullable<T>&t):isnull(t.isnull),elem(t.elem){} 00042 00044 Nullable<T>& operator=(const T&t){isnull=false;elem=t;return *this;} 00046 Nullable<T>& operator=(const Nullable<T>&t){isnull=t.isnull;elem=t.elem;return *this;} 00047 00049 bool isNull()const{return isnull;} 00050 00052 operator T()const{if(isnull)return T();else return elem;} 00053 00055 T& value(){return elem;} 00057 T value(const T&defaultval=T())const{if(isnull)return defaultval;else return elem;} 00058 00060 bool operator==(const T&t)const{if(isnull)return false;else return elem == t;} 00062 bool operator!=(const T&t)const{if(isnull)return true;else return elem != t;} 00063 00065 bool operator==(const Nullable<T>&t)const{ 00066 if(isnull != t.isnull)return false; 00067 if(isnull)return true; 00068 else return elem == t.elem;} 00070 bool operator!=(const Nullable<T>&t)const{ 00071 if(isnull != t.isnull)return true; 00072 if(isnull)return false; 00073 else return elem != t.elem;} 00074 00076 bool operator==(const NullValue&)const{return isnull;} 00078 bool operator!=(const NullValue&)const{return !isnull;} 00079 private: 00080 bool isnull; 00081 T elem; 00082 }; 00083 00084 #include<QString> 00085 #include<QMetaType> 00086 //convenience wrappers 00087 Q_DECLARE_METATYPE(NullValue); 00088 Q_DECLARE_METATYPE(Nullable<bool>) 00089 Q_DECLARE_METATYPE(Nullable<qint32>) 00090 Q_DECLARE_METATYPE(Nullable<qint64>) 00091 Q_DECLARE_METATYPE(Nullable<quint32>) 00092 Q_DECLARE_METATYPE(Nullable<quint64>) 00093 Q_DECLARE_METATYPE(Nullable<QString>) 00094 Q_DECLARE_METATYPE(Nullable<QByteArray>) 00095 00096 inline bool operator==(Nullable<qint64> i1,int i2){return i1.operator==(i2);} 00097 inline bool operator==(Nullable<quint32> i1,int i2){return i1.operator==(i2);} 00098 inline bool operator==(Nullable<quint64> i1,int i2){return i1.operator==(i2);} 00099 inline bool operator==(Nullable<quint64> i1,unsigned int i2){return i1.operator==(i2);} 00100 00101 #endif