From: konrad Date: Sun, 16 May 2010 10:49:33 +0000 (+0000) Subject: make nullable more convenient X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=a21343751fc353a3531197fa3e36cc57a3fc0885;p=web%2Fkonrad%2Fpack.git make nullable more convenient git-svn-id: https://silmor.de/svn/softmagic/pack/trunk@458 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- diff --git a/qtbase/nullable.h b/qtbase/nullable.h index 1557cca..7a1410c 100644 --- a/qtbase/nullable.h +++ b/qtbase/nullable.h @@ -14,31 +14,45 @@ #ifndef WOLF_NULLABLE_H #define WOLF_NULLABLE_H +/**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();} + /**creates a wrapped non-NULL value that is equivalent to the original*/ Nullable(const T&t){isnull=false;elem=t;} + /**copies a nullable value*/ 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;} + /**copies the given nullable value*/ Nullable& operator=(const Nullable&t){isnull=t.isnull;elem=t.elem;return *this;} + /**returns whether this instance is NULL*/ bool isNull()const{return isnull;} + /**converts this nullable to the original wrapped class - uses the default constructor if it is NULL*/ operator T()const{if(isnull)return T();else return elem;} + /**returns a reference to the wrapped value - the result is undefined if it us currently NULL*/ T& value(){return elem;} + /**returns the original value, uses an instance created with the default constructor if it is currently NULL*/ T value()const{if(isnull)return T();else return elem;} - bool operator==(const T&t){if(isnull)return false;else return elem == t;} - bool operator!=(const T&t){if(isnull)return true;else return elem != t;} + /**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;} + /**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 true;else return elem != t;} - bool operator==(const Nullable&t){ + /**compares two nullable instances, if both are NULL it returns true*/ + bool operator==(const Nullable&t)const{ if(isnull != t.isnull)return false; if(isnull)return true; else return elem == t.elem;} - bool operator!=(const Nullable&t){ + /**compares two nullable instances, if both are NULL it returns false*/ + bool operator!=(const Nullable&t)const{ if(isnull != t.isnull)return true; if(isnull)return false; else return elem != t.elem;} @@ -47,6 +61,7 @@ templateclass Nullable T elem; }; +//convenience wrappers typedef Nullable Int; typedef Nullable Int32; typedef Nullable Int64; @@ -54,4 +69,9 @@ typedef Nullable UInt32; typedef Nullable UInt64; typedef Nullable NString; +inline bool operator==(Int64 i1,int i2){return i1.operator==(i2);} +inline bool operator==(UInt32 i1,int i2){return i1.operator==(i2);} +inline bool operator==(UInt64 i1,int i2){return i1.operator==(i2);} +inline bool operator==(UInt64 i1,unsigned int i2){return i1.operator==(i2);} + #endif