00001 #ifndef MYSQLPP_NULL_H
00002 #define MYSQLPP_NULL_H
00003
00010
00011 #include "exceptions.h"
00012
00013 #include <iostream>
00014
00015 namespace mysqlpp {
00016
00017
00022 class null_type {
00023 public:
00024 template <class Type> operator Type () {throw BadNullConversion();}
00025 };
00026
00027 const null_type null = null_type();
00028
00030 struct NullisNull {
00031 static null_type null_is() {return null_type();}
00032 static std::ostream& null_ostr(std::ostream& o) {o << "(NULL)"; return o;}
00033 };
00034
00036 struct NullisZero {
00037 static int null_is() {return 0;}
00038 static std::ostream& null_ostr(std::ostream &o) {o << 0; return o;}
00039 };
00040
00042 struct NullisBlank {
00043 static const char * null_is() {return "";}
00044 static std::ostream& null_ostr(std::ostream &o) {o << ""; return o;}
00045 };
00046
00047
00053 template <class Type, class Behavior = NullisNull>
00054 class Null {
00055 public:
00056 Type data;
00057 bool is_null;
00058 typedef Type value_type;
00059 public:
00060 Null () {}
00061 Null (Type x) : data(x), is_null(false) {}
00062
00070 Null (const null_type &n) : is_null(true) {}
00071
00072 operator Type& () {
00073 if (is_null) return data = Behavior::null_is();
00074 else return data; }
00075
00076 Null& operator = (const Type &x) {data = x; is_null = false; return *this;}
00077
00078 Null& operator = (const null_type &n) {is_null = true; return *this;}
00079 };
00080
00081
00083
00084
00085
00086 template <> class Null<void> {
00087 public:
00088 bool is_null;
00089 typedef void value_type;
00090 public:
00091 Null () : is_null(false) { }
00092 Null (const null_type &) : is_null(true) { }
00093 Null& operator = (const null_type &) { is_null = true; return *this; }
00094 };
00095
00097
00098
00099 template <class Type, class Behavior>
00100 inline std::ostream& operator << (std::ostream &o, const Null<Type,Behavior> &n) {
00101 if (n.is_null) return Behavior::null_ostr(o);
00102 else return o << n.data;
00103 }
00104
00105 }
00106
00107 #endif
00108