From c214783152ecdbf1cf2b9715ab3328ec81716b2e Mon Sep 17 00:00:00 2001 From: Konrad Rosenbaum Date: Thu, 29 Dec 2016 16:22:48 +0100 Subject: [PATCH] some enhancements for nullable template Change-Id: I1353962a08035071470d7b573eb091893986b24d --- qtbase/include/nullable.h | 28 ++++++++++++++++++++++++---- 1 files changed, 24 insertions(+), 4 deletions(-) diff --git a/qtbase/include/nullable.h b/qtbase/include/nullable.h index 750b5dd..b246cc0 100644 --- a/qtbase/include/nullable.h +++ b/qtbase/include/nullable.h @@ -12,20 +12,24 @@ #endif ///special class for Null values +///this was a legacy class before nullptr was introduced, use nullptr now! class WOLF_BASE_EXPORT NullValue { public: NullValue(){} NullValue(const NullValue&){} + NullValue(const nullptr_t&){} // operator void*()const{return (void*)0;} - template operator T*()const{return (T*)0;} + template operator T*()const{return (T*)nullptr;} 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;} + bool operator==(const nullptr_t&)const{return true;} + bool operator!=(const nullptr_t&)const{return false;} + + templatebool operator==(const T*p)const{return p==nullptr;} + templatebool operator!=(const T*p)const{return p!=nullptr;} }; ///special null value @@ -39,6 +43,9 @@ templateclass WOLF_BASE_EXPORT Nullable Nullable():isnull(true),elem(){} /**creates a NULL value*/ Nullable(const NullValue&):isnull(true),elem(){} + /**creates a NULL value*/ + //TODO: find an implementation that works without auto-casting 0 to nullptr while not using explicit + explicit Nullable(const nullptr_t&):isnull(true),elem(){} /**creates a wrapped non-NULL value that is equivalent to the original*/ Nullable(const T&t):isnull(false),elem(t){} /**copies a nullable value*/ @@ -61,6 +68,15 @@ templateclass WOLF_BASE_EXPORT Nullable 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());} + + //convenience aliases, sometimes x.value().value().value() seems a bit weird! + /**returns a reference to the wrapped value - the result is undefined if it us currently NULL*/ + T& data(){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 data(const T&defaultval=T())const{if(isnull)return defaultval;else return elem;} + ///Helper to automatically convert to base type + T dataOrDefault()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);} @@ -84,6 +100,10 @@ templateclass WOLF_BASE_EXPORT Nullable bool operator==(const NullValue&)const{return isnull;} ///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 nullptr_t&)const{return isnull;} + ///compares the Nullable with the special null value + bool operator!=(const nullptr_t&)const{return !isnull;} private: bool isnull; T elem; -- 1.7.2.5