Added ModelPropertyMapper which allows to keep track of /current/ changes in the...
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / core / qxtnullable.h
1 /****************************************************************************
2 **
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
4 **
5 ** This file is part of the QxtCore module of the Qt eXTension library
6 **
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
9 ** IBM.
10 **
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
15 **
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
20 **
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22 **
23 ****************************************************************************/
24
25 /**
26 \class QxtNullable QxtNullable
27 \ingroup QxtCore
28 \brief distinct null value compatible with any data type.
29
30 in general it's a templated abstraction to allow any data type to be
31 expressed with a null value distinct from any real value. An example
32 of such a use is for optional arguments.
33 \n
34 prepare a function for argument skipping:
35
36 \code
37 void  somefunction( qxtNull(int,a) , qxtNull(int,b) )
38 {
39
40 if (!a.isNull())
41         {
42         int i = a.value();
43         //do something with i
44         }
45  if (!b.isNull())
46         {
47         int x = b.value();
48         //do something with x
49         }
50 }
51 \endcode
52
53 usage:
54 \code
55
56 somefunction(SKIP,1,2);
57 somefunction(3,4);
58 somefunction(3,SKIP,6);
59 somefunction(1);
60 \endcode
61
62 */
63
64 #ifndef QXTNULLABLE_H
65 #define QXTNULLABLE_H
66 #include <qxtglobal.h>
67
68 /*! \relates QxtNullable
69  * defines a skipable argument with type \a t and variable name \a n
70  */
71 #define qxtNull(t,n)      QxtNullable<t> n = QxtNullable<t>()
72
73 #include <qxtnull.h>
74
75 template<typename T>
76 class /*QXT_CORE_EXPORT*/ QxtNullable
77 {
78 public:
79     QxtNullable(QxtNull);
80     QxtNullable(const T& p);
81     QxtNullable();
82
83     ///determinates if the Value is set to something meaningfull
84     bool isNull() const;
85
86     ///delete Value
87     void nullify();
88
89     T& value() const;
90     operator T() const;
91     void operator=(const T& p);
92
93 private:
94     T* val;
95 };
96
97 template<typename T>
98 QxtNullable<T>::QxtNullable(QxtNull)
99 {
100     val = 0;
101 }
102
103 template<typename T>
104 QxtNullable<T>::QxtNullable(const T& p)
105 {
106     val = const_cast<T*>(&p);
107 }
108
109 template<typename T>
110 QxtNullable<T>::QxtNullable()
111 {
112     val = 0;
113 }
114
115 template<typename T>
116 QxtNullable<T>::operator T() const
117 {
118     return *val;
119 }
120
121 template<typename T>
122 T& QxtNullable<T>::value() const
123 {
124     return *val;
125 }
126
127 template<typename T>
128 bool QxtNullable<T>::isNull() const
129 {
130     return (val==0);
131 }
132
133 template<typename T>
134 void QxtNullable<T>::nullify()
135 {
136     val=0;
137 }
138
139 template<typename T>
140 void QxtNullable<T>::operator=(const T& p)
141 {
142     val = const_cast<T*>(&p);
143 }
144
145 #endif