#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<typename T> operator T*()const{return (T*)0;}
+ template<typename T> operator T*()const{return (T*)nullptr;}
bool operator==(const NullValue&)const{return true;}
bool operator!=(const NullValue&)const{return false;}
-
- template<typename T>bool operator==(const T*p)const{return p==0;}
- template<typename T>bool operator!=(const T*p)const{return p!=0;}
+ bool operator==(const nullptr_t&)const{return true;}
+ bool operator!=(const nullptr_t&)const{return false;}
+
+ template<typename T>bool operator==(const T*p)const{return p==nullptr;}
+ template<typename T>bool operator!=(const T*p)const{return p!=nullptr;}
};
///special null value
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*/
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<T*>(&elem);}
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;