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         }
00055 #endif // !defined(DOXYGEN_IGNORE)
00056 };
00057 
00060 const null_type null = null_type();
00061 
00062 
00070 struct NullisNull
00071 {
00072 #if !defined(DOXYGEN_IGNORE)
00073 // Doxygen will not generate documentation for this section.
00074         static null_type null_is() { return null_type(); }
00075 
00076         static std::ostream& null_ostr(std::ostream& o)
00077         {
00078                 o << "(NULL)";
00079                 return o;
00080         }
00081 #endif // !defined(DOXYGEN_IGNORE)
00082 };
00083 
00084 
00091 struct NullisZero
00092 {
00093 #if !defined(DOXYGEN_IGNORE)
00094 // Doxygen will not generate documentation for this section.
00095         static int null_is() { return 0; }
00096         
00097         static std::ostream& null_ostr(std::ostream& o)
00098         {
00099                 o << 0;
00100                 return o;
00101         }
00102 #endif // !defined(DOXYGEN_IGNORE)
00103 };
00104 
00111 struct NullisBlank
00112 {
00113 #if !defined(DOXYGEN_IGNORE)
00114 // Doxygen will not generate documentation for this section.
00115         static const char *null_is() { return ""; }
00116         
00117         static std::ostream& null_ostr(std::ostream& o)
00118         {
00119                 o << "";
00120                 return o;
00121         }
00122 #endif // !defined(DOXYGEN_IGNORE)
00123 };
00124 
00125 
00145 template <class Type, class Behavior = NullisNull> class Null
00146 {
00147 public:
00149         Type data;
00150         
00154         bool is_null;
00155 
00158         typedef Type value_type;
00159 
00163         Null()
00164         {
00165         }
00166 
00174         Null(Type x) :
00175         data(x),
00176         is_null(false)
00177         {
00178         }
00179 
00188         Null(const null_type& n) :
00189         is_null(true)
00190         {
00191         }
00192 
00200         operator Type&()
00201         {
00202                 if (is_null)
00203                         return data = Behavior::null_is();
00204                 else
00205                         return data;
00206         }
00207 
00211         Null& operator =(const Type& x)
00212         {
00213                 data = x;
00214                 is_null = false;
00215                 return *this;
00216         }
00217 
00222         Null& operator =(const null_type& n)
00223         {
00224                 is_null = true;
00225                 return *this;
00226         }
00227 };
00228 
00229 
00230 #if !defined(DOXYGEN_IGNORE)
00231 // Doxygen will not generate documentation for this section.
00232 
00233 // Specialization the Null template for \c void
00234 template <> class Null<void>
00235 {
00236 public:
00237         bool is_null;
00238         typedef void value_type;
00239 
00240         Null() :
00241         is_null(false)
00242         {
00243         }
00244         
00245         Null(const null_type&) :
00246         is_null(true)
00247         {
00248         }
00249 
00250         Null& operator =(const null_type&)
00251         {
00252                 is_null = true;
00253                 return *this;
00254         }
00255 };
00256 
00257 #endif // !defined(DOXYGEN_IGNORE)
00258 
00259 
00263 template <class Type, class Behavior>
00264 inline std::ostream& operator <<(std::ostream& o,
00265                 const Null<Type, Behavior>& n)
00266 {
00267         if (n.is_null)
00268                 return Behavior::null_ostr(o);
00269         else
00270                 return o << n.data;
00271 }
00272 
00273 } // end namespace mysqlpp
00274 
00275 #endif

Generated on Thu May 26 09:39:58 2005 for MySQL++ by doxygen1.2.18