#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<typename T> operator T*()const{return (T*)0;}
+
+ 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;}
+};
+
+///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*/
template<class T>class 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>&t){isnull=t.isnull;elem=t.elem;}
+ Nullable(const Nullable<T>&t):isnull(t.isnull),elem(t.elem){}
/**makes this instance non-NULL and equivalent to the given value*/
Nullable<T>& operator=(const T&t){isnull=false;elem=t;return *this;}
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;
#include<QString>
#include<QMetaType>
//convenience wrappers
+Q_DECLARE_METATYPE(NullValue);
Q_DECLARE_METATYPE(Nullable<bool>)
Q_DECLARE_METATYPE(Nullable<qint32>)
Q_DECLARE_METATYPE(Nullable<qint64>)