PACK Qt Binding  $VERSION$
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
nullable.h
Go to the documentation of this file.
1 // Copyright (C) 2009-2011 by Konrad Rosenbaum <konrad@silmor.de>
2 // protected under the GNU LGPL version 3 or at your option any newer.
3 // See COPYING.LGPL file that comes with this distribution.
4 //
5 
6 
7 #ifndef WOLF_NULLABLE_H
8 #define WOLF_NULLABLE_H
9 
10 #ifndef WOLF_BASE_EXPORT
11 #define WOLF_BASE_EXPORT Q_DECL_IMPORT
12 #endif
13 
14 #include <cstddef>
15 
19 {
20  public:
21  NullValue(){}
22  NullValue(const NullValue&){}
23  NullValue(const std::nullptr_t&){}
24 
25 // operator void*()const{return (void*)0;}
26  template<typename T> operator T*()const{return (T*)nullptr;}
27 
28  bool operator==(const NullValue&)const{return true;}
29  bool operator!=(const NullValue&)const{return false;}
30  bool operator==(const std::nullptr_t&)const{return true;}
31  bool operator!=(const std::nullptr_t&)const{return false;}
32 
33  template<typename T>bool operator==(const T*p)const{return p==nullptr;}
34  template<typename T>bool operator!=(const T*p)const{return p!=nullptr;}
35 };
36 
39 
41 template<class T>class WOLF_BASE_EXPORT Nullable
42 {
43  public:
45  Nullable():isnull(true),elem(){}
47  Nullable(const NullValue&):isnull(true),elem(){}
49  //TODO: find an implementation that works without auto-casting 0 to nullptr while not using explicit
50  explicit Nullable(const std::nullptr_t&):isnull(true),elem(){}
52  Nullable(const T&t):isnull(false),elem(t){}
54  Nullable(const Nullable<T>&t):isnull(t.isnull),elem(t.elem){}
55 
57  Nullable<T>& operator=(const T&t){isnull=false;elem=t;return *this;}
59  Nullable<T>& operator=(const Nullable<T>&t){isnull=t.isnull;elem=t.elem;return *this;}
60 
62  bool isNull()const{return isnull;}
63 
65  operator T()const{if(isnull)return T();else return elem;}
66 
68  T& value(){return elem;}
70  T value(const T&defaultval=T())const{if(isnull)return defaultval;else return elem;}
72  T valueOrDefault()const{return value(T());}
73 
74  //convenience aliases, sometimes x.value().value().value() seems a bit weird!
76  T& data(){return elem;}
78  T data(const T&defaultval=T())const{if(isnull)return defaultval;else return elem;}
80  T dataOrDefault()const{return value(T());}
81 
83  T* operator ->()const{if(isnull)return nullptr;else return const_cast<T*>(&elem);}
84 
86  bool operator==(const T&t)const{if(isnull)return false;else return elem == t;}
88  bool operator!=(const T&t)const{if(isnull)return true;else return elem != t;}
89 
91  bool operator==(const Nullable<T>&t)const{
92  if(isnull != t.isnull)return false;
93  if(isnull)return true;
94  else return elem == t.elem;}
96  bool operator!=(const Nullable<T>&t)const{
97  if(isnull != t.isnull)return true;
98  if(isnull)return false;
99  else return elem != t.elem;}
100 
102  bool operator==(const NullValue&)const{return isnull;}
104  bool operator!=(const NullValue&)const{return !isnull;}
106  bool operator==(const std::nullptr_t&)const{return isnull;}
108  bool operator!=(const std::nullptr_t&)const{return !isnull;}
109  private:
110  bool isnull;
111  T elem;
112 };
113 
114 #include<QString>
115 #include<QMetaType>
116 //convenience wrappers
124 Q_DECLARE_METATYPE(Nullable<QByteArray>)
125 Q_DECLARE_SMART_POINTER_METATYPE(Nullable)
126 
127 inline WOLF_BASE_EXPORT bool operator==(Nullable<qint64> i1,int i2){return i1.operator==(i2);}
128 inline WOLF_BASE_EXPORT bool operator==(Nullable<quint32> i1,int i2){return i1.operator==(i2);}
129 inline WOLF_BASE_EXPORT bool operator==(Nullable<quint64> i1,int i2){return i1.operator==(i2);}
130 inline WOLF_BASE_EXPORT bool operator==(Nullable<quint64> i1,unsigned int i2){return i1.operator==(i2);}
131 
132 #endif
bool isNull() const
Definition: nullable.h:62
bool operator==(const NullValue &) const
compares the Nullable with the special null value
Definition: nullable.h:102
Definition: nullable.h:41
bool operator!=(const T &t) const
Definition: nullable.h:88
Nullable(const std::nullptr_t &)
Definition: nullable.h:50
Nullable(const Nullable< T > &t)
Definition: nullable.h:54
#define WOLF_BASE_EXPORT
Definition: nullable.h:11
bool operator!=(const NullValue &) const
compares the Nullable with the special null value
Definition: nullable.h:104
bool operator==(const Nullable< T > &t) const
Definition: nullable.h:91
bool operator!=(const Nullable< T > &t) const
Definition: nullable.h:96
NullValue(const std::nullptr_t &)
Definition: nullable.h:23
bool operator!=(const std::nullptr_t &) const
compares the Nullable with the special null value
Definition: nullable.h:108
T valueOrDefault() const
Helper to automatically convert to base type.
Definition: nullable.h:72
Nullable(const T &t)
Definition: nullable.h:52
bool operator==(const T &t) const
Definition: nullable.h:86
T dataOrDefault() const
Helper to automatically convert to base type.
Definition: nullable.h:80
T & value()
Definition: nullable.h:68
Nullable< T > & operator=(const T &t)
Definition: nullable.h:57
Definition: nullable.h:18
NullValue()
Definition: nullable.h:21
Nullable(const NullValue &)
Definition: nullable.h:47
bool operator==(const T *p) const
Definition: nullable.h:33
Q_DECLARE_METATYPE(NullValue)
bool operator==(const NullValue &) const
Definition: nullable.h:28
bool operator==(const std::nullptr_t &) const
compares the Nullable with the special null value
Definition: nullable.h:106
T data(const T &defaultval=T()) const
Definition: nullable.h:78
bool operator!=(const NullValue &) const
Definition: nullable.h:29
bool operator==(const std::nullptr_t &) const
Definition: nullable.h:30
Nullable< T > & operator=(const Nullable< T > &t)
Definition: nullable.h:59
Nullable()
Definition: nullable.h:45
bool operator!=(const std::nullptr_t &) const
Definition: nullable.h:31
const NullValue nullval
special null value
Definition: nullable.h:38
T & data()
Definition: nullable.h:76
T value(const T &defaultval=T()) const
Definition: nullable.h:70
NullValue(const NullValue &)
Definition: nullable.h:22
bool operator!=(const T *p) const
Definition: nullable.h:34
WOLF_BASE_EXPORT bool operator==(Nullable< qint64 > i1, int i2)
Definition: nullable.h:127