00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef MYSQLPP_NULL_H
00033 #define MYSQLPP_NULL_H
00034
00035 #include "exceptions.h"
00036
00037 #include <iostream>
00038
00039 namespace mysqlpp {
00040
00041
00046 class MYSQLPP_EXPORT null_type
00047 {
00048 public:
00049 #if !defined(DOXYGEN_IGNORE)
00050
00051 template <class Type> operator Type()
00052 {
00053 throw BadNullConversion();
00054 return Type();
00055 }
00056 #endif // !defined(DOXYGEN_IGNORE)
00057 };
00058
00061 const null_type null = null_type();
00062
00063
00071 struct NullisNull
00072 {
00073 #if !defined(DOXYGEN_IGNORE)
00074
00075 static null_type null_is() { return null_type(); }
00076
00077 static std::ostream& null_ostr(std::ostream& o)
00078 {
00079 o << "(NULL)";
00080 return o;
00081 }
00082 #endif // !defined(DOXYGEN_IGNORE)
00083 };
00084
00085
00092 struct NullisZero
00093 {
00094 #if !defined(DOXYGEN_IGNORE)
00095
00096 static int null_is() { return 0; }
00097
00098 static std::ostream& null_ostr(std::ostream& o)
00099 {
00100 o << 0;
00101 return o;
00102 }
00103 #endif // !defined(DOXYGEN_IGNORE)
00104 };
00105
00112 struct NullisBlank
00113 {
00114 #if !defined(DOXYGEN_IGNORE)
00115
00116 static const char *null_is() { return ""; }
00117
00118 static std::ostream& null_ostr(std::ostream& o)
00119 {
00120 o << "";
00121 return o;
00122 }
00123 #endif // !defined(DOXYGEN_IGNORE)
00124 };
00125
00126
00146 template <class Type, class Behavior = NullisNull> class Null
00147 {
00148 public:
00150 Type data;
00151
00155 bool is_null;
00156
00159 typedef Type value_type;
00160
00165 Null() :
00166 is_null(false)
00167 {
00168 }
00169
00177 Null(const Type& x) :
00178 data(x),
00179 is_null(false)
00180 {
00181 }
00182
00191 Null(const null_type& n) :
00192 is_null(true)
00193 {
00194 }
00195
00203 operator Type&()
00204 {
00205 if (is_null)
00206 return data = Behavior::null_is();
00207 else
00208 return data;
00209 }
00210
00214 Null& operator =(const Type& x)
00215 {
00216 data = x;
00217 is_null = false;
00218 return *this;
00219 }
00220
00225 Null& operator =(const null_type& n)
00226 {
00227 is_null = true;
00228 return *this;
00229 }
00230 };
00231
00232
00233 #if !defined(DOXYGEN_IGNORE)
00234
00235
00236
00237 template <> class Null<void>
00238 {
00239 public:
00240 bool is_null;
00241 typedef void value_type;
00242
00243 Null() :
00244 is_null(false)
00245 {
00246 }
00247
00248 Null(const null_type&) :
00249 is_null(true)
00250 {
00251 }
00252
00253 Null& operator =(const null_type&)
00254 {
00255 is_null = true;
00256 return *this;
00257 }
00258 };
00259
00260 #endif // !defined(DOXYGEN_IGNORE)
00261
00262
00266 template <class Type, class Behavior>
00267 inline std::ostream& operator <<(std::ostream& o,
00268 const Null<Type, Behavior>& n)
00269 {
00270 if (n.is_null)
00271 return Behavior::null_ostr(o);
00272 else
00273 return o << n.data;
00274 }
00275
00276 }
00277
00278 #endif