MySQL++ Reference Manual

Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members File Members

# null.h

Go to the documentation of this file.

    00001
    00002
    00003
    00004
    00005
    00006
    00007
    00008 /***********************************************************************
    00009  Copyright (c) 1998 by Kevin Atkinson, (c) 1999, 2000 and 2001 by
    00010  MySQL AB, and (c) 2004, 2005 by Educational Technology Resources, Inc.
    00011  Others may also hold copyrights on code in this file.  See the CREDITS
    00012  file in the top directory of the distribution for details.
    00013
    00014  This file is part of MySQL++.
    00015
    00016  MySQL++ is free software; you can redistribute it and/or modify it
    00017  under the terms of the GNU Lesser General Public License as published
    00018  by the Free Software Foundation; either version 2.1 of the License, or
    00019  (at your option) any later version.
    00020
    00021  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
    00022  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    00023  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
    00024  License for more details.
    00025
    00026  You should have received a copy of the GNU Lesser General Public
    00027  License along with MySQL++; if not, write to the Free Software
    00028  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
    00029  USA
    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 null_type
    00047 {
    00048 public:
    00049 #if !defined(DOXYGEN_IGNORE)
    00050 // Doxygen will not generate documentation for this section.
    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 // Doxygen will not generate documentation for this section.
    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 // Doxygen will not generate documentation for this section.
    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 // Doxygen will not generate documentation for this section.
    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 // Doxygen will not generate documentation for this section.
    00235
    00236 // Specialization the Null template for \c void
    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 } // end namespace mysqlpp
    00277
    00278 #endif