From: Konrad Rosenbaum Date: Thu, 29 Dec 2011 09:44:26 +0000 (+0100) Subject: some fixes for Nullable, add an explicit null value X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=6a565edd00675310f956428a83791b8ba3f16a7b;p=web%2Fkonrad%2Fpack.git some fixes for Nullable, add an explicit null value --- diff --git a/qtbase/include/nullable.h b/qtbase/include/nullable.h index 94fe369..c447f2f 100644 --- a/qtbase/include/nullable.h +++ b/qtbase/include/nullable.h @@ -7,16 +7,38 @@ #ifndef WOLF_NULLABLE_H #define WOLF_NULLABLE_H +///special class for Null values +class NullValue +{ + public: + NullValue(){} + NullValue(const NullValue&){} + +// operator void*()const{return (void*)0;} + template operator T*()const{return (T*)0;} + + bool operator==(const NullValue&)const{return true;} + bool operator!=(const NullValue&)const{return false;} + + templatebool operator==(const T*p)const{return p==0;} + templatebool operator!=(const T*p)const{return p!=0;} +}; + +///special null value +const NullValue nullval; + /**wrapper around scalar values that makes them NULL-able, i.e. the virtual value NULL is added to the range of the wrapped value; wrapped classes must have a default constructor and =, == and != operators*/ templateclass Nullable { public: /**creates a NULL value*/ - Nullable(){isnull=true;elem=T();} + Nullable():isnull(true),elem(){} + /**creates a NULL value*/ + Nullable(const NullValue&):isnull(true),elem(){} /**creates a wrapped non-NULL value that is equivalent to the original*/ - Nullable(const T&t){isnull=false;elem=t;} + Nullable(const T&t):isnull(false),elem(t){} /**copies a nullable value*/ - Nullable(const Nullable&t){isnull=t.isnull;elem=t.elem;} + Nullable(const Nullable&t):isnull(t.isnull),elem(t.elem){} /**makes this instance non-NULL and equivalent to the given value*/ Nullable& operator=(const T&t){isnull=false;elem=t;return *this;} @@ -49,6 +71,11 @@ templateclass Nullable if(isnull != t.isnull)return true; if(isnull)return false; else return elem != t.elem;} + + ///compares the Nullable with the special null value + bool operator==(const NullValue&)const{return isnull;} + ///compares the Nullable with the special null value + bool operator!=(const NullValue&)const{return !isnull;} private: bool isnull; T elem; @@ -57,6 +84,7 @@ templateclass Nullable #include #include //convenience wrappers +Q_DECLARE_METATYPE(NullValue); Q_DECLARE_METATYPE(Nullable) Q_DECLARE_METATYPE(Nullable) Q_DECLARE_METATYPE(Nullable)