Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

pqxx-object/field.h

Go to the documentation of this file.
00001 // database field container                                      -*- C++ -*-
00002 // $Id: field.h,v 1.4 2004/03/11 10:05:18 roger Exp $
00003 //
00004 // Copyright (C) 2003  Roger Leigh <rleigh@debian.org>
00005 //
00006 //
00007 //  All rights reserved.
00008 //
00009 //  Redistribution and use in source and binary forms, with or without
00010 //  modification, are permitted provided that the following conditions
00011 //  are met:
00012 //
00013 //  * Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 //  * Redistributions in binary form must reproduce the above
00016 //    copyright notice, this list of conditions and the following
00017 //    disclaimer in the documentation and/or other materials provided
00018 //    with the distribution.
00019 //  * Neither the name of the author, nor the names of other
00020 //    contributors may be used to endorse or promote products derived
00021 //    from this software without specific prior written permission.
00022 //
00023 //  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
00024 //  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
00025 //  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00026 //  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 //  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
00028 //  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00029 //  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00030 //  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00031 //  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00032 //  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
00033 //  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00034 //  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00035 //  SUCH DAMAGE.
00036 //
00038 
00039 #ifndef PQXX_OBJECT_FIELD_H
00040 #define PQXX_OBJECT_FIELD_H
00041 
00042 #include <cassert>
00043 
00044 #include <sigc++/object.h>
00045 #include <sigc++/signal.h>
00046 
00047 #include <pqxx-object/field_base.h>
00048 
00049 namespace pqxxobject
00050 {
00068   template<typename T>
00069   class field : public pqxxobject::field_base
00070   {
00071   public:
00072     typedef T value_type;
00073 
00075     explicit field(const value_type& value):
00076       m_value(value)
00077     {
00078       // No signal_changed on construction (signal handlers not yet
00079       // connected).
00080     }
00081 
00083     field(const field<value_type>& rhs):
00084       field_base(rhs),
00085       m_value(rhs.m_value)
00086     {
00087       // No signal_changed on copy (signal handlers not yet
00088       // connected).
00089     }
00090 
00091     virtual ~field()
00092     {}
00093 
00095     const value_type *operator -> () const
00096     {
00097       return &m_value;
00098     }
00099 
00100     const value_type& operator * () const
00101     {
00102       return m_value;
00103     }
00104 
00106     field<value_type>& operator = (const field<value_type>& rhs)
00107     {
00108       if (this != &rhs)
00109         set_value(rhs.m_value);
00110       return *this;
00111     }
00112 
00114     field<value_type>& operator = (const value_type& rhs)
00115     {
00116       set_value(rhs);
00117       return *this;
00118     }
00119 
00120     field<value_type>& operator += (const field<value_type>& rhs)
00121     {
00122       m_value += rhs.m_value;
00123       m_signal_changed.emit();
00124       return *this;
00125     }
00126 
00127     field<value_type>& operator -= (const field<value_type>& rhs)
00128     {
00129       m_value -= rhs.m_value;
00130       m_signal_changed.emit();
00131       return *this;
00132     }
00133 
00134     field<value_type>& operator *= (const field<value_type>& rhs)
00135     {
00136       m_value *= rhs.m_value;
00137       m_signal_changed.emit();
00138       return *this;
00139     }
00140 
00141     field<value_type>& operator /= (const field<value_type>& rhs)
00142     {
00143       m_value /= rhs.m_value;
00144       m_signal_changed.emit();
00145       return *this;
00146     }
00147 
00148     field<value_type>& operator %= (const field<value_type>& rhs)
00149     {
00150       m_value %= rhs.m_value;
00151       m_signal_changed.emit();
00152       return *this;
00153     }
00154 
00155     field<value_type>& operator ++ ()
00156     {
00157       m_value++;
00158       m_signal_changed.emit();
00159       return *this;
00160     }
00161 
00162     field<value_type> operator ++ (int)
00163     {
00164       field<value_type> ret(*this);
00165       m_value++;
00166       m_signal_changed.emit();
00167       return ret;
00168     }
00169 
00170     field<value_type>& operator -- ()
00171     {
00172       m_value--;
00173       m_signal_changed.emit();
00174       return *this;
00175     }
00176 
00177     field<value_type> operator -- (int)
00178     {
00179       field<value_type> ret(*this);
00180       m_value--;
00181       m_signal_changed.emit();
00182       return ret;
00183     }
00184 
00185     friend field<value_type> operator + <> (const field<value_type>& lhs,
00186                                             const field<value_type>& rhs);
00187 
00188     friend field<value_type> operator - <> (const field<value_type>& lhs,
00189                                             const field<value_type>& rhs);
00190 
00191     friend field<value_type> operator * <> (const field<value_type>& lhs,
00192                                             const field<value_type>& rhs);
00193 
00194     friend field<value_type> operator / <> (const field<value_type>& lhs,
00195                                             const field<value_type>& rhs);
00196 
00197     friend field<value_type> operator % <> (const field<value_type>& lhs,
00198                                             const field<value_type>& rhs);
00199 
00200     friend bool operator == <> (const field<value_type>& lhs,
00201                                 const field<value_type>& rhs);
00202 
00203     friend bool operator != <> (const field<value_type>& lhs,
00204                                 const field<value_type>& rhs);
00205 
00206     friend field<value_type> operator - <> (const field<value_type>& rhs);
00207 
00208     friend field<value_type> operator + <> (const field<value_type>& rhs);
00209 
00210     friend std::ostream& operator << <> (std::ostream& output_stream,
00211                                          const field<value_type>& rhs);
00212 
00213 //     friend std::istream& operator >> <> (std::istream& input_stream,
00214 //                                       const field<value_type>& rhs);
00215 
00217     operator const value_type& () const
00218     {
00219       return m_value;
00220     }
00221 
00226     value_type& get_value()
00227     {
00228       return m_value;
00229     }
00230 
00235     const value_type& get_value() const
00236     {
00237       return m_value;
00238     }
00239 
00244     void set_value(const value_type& value)
00245     {
00246       m_value = value;
00247       m_signal_changed.emit();
00248     }
00249 
00254     bool is_null() const
00255     {
00256       return false;
00257     }
00258 
00263     bool is_not_null() const
00264     {
00265       return true;
00266     }
00267 
00268   private:
00270     value_type m_value;
00271 
00272   }; // class field
00273 
00274 
00275   template<typename T>
00276   inline field<T> operator + (const field<T>& lhs,
00277                                        const field<T>& rhs)
00278   {
00279     return field<T>(lhs) += rhs;
00280   }
00281 
00282   template<typename T>
00283   inline field<T> operator - (const field<T>& lhs,
00284                                        const field<T>& rhs)
00285   {
00286     return field<T>(lhs) -= rhs;
00287   }
00288 
00289   template<typename T>
00290   inline field<T> operator * (const field<T>& lhs,
00291                                        const field<T>& rhs)
00292   {
00293     return field<T>(lhs) *= rhs;
00294   }
00295 
00296   template<typename T>
00297   inline field<T> operator / (const field<T>& lhs,
00298                                        const field<T>& rhs)
00299   {
00300     return field<T>(lhs) /= rhs;
00301   }
00302 
00303   template<typename T>
00304   inline field<T> operator % (const field<T>& lhs,
00305                                        const field<T>& rhs)
00306   {
00307     return field<T>(lhs) %= rhs;
00308   }
00309 
00310   template<typename T>
00311   inline bool operator == (const field<T>& lhs,
00312                            const field<T>& rhs)
00313   {
00314     return (lhs.m_value == rhs.m_value);
00315   }
00316 
00317   template<typename T>
00318   inline bool operator != (const field<T>& lhs,
00319                            const field<T>& rhs)
00320   {
00321     return !(lhs == rhs);
00322   }
00323 
00324   template<typename T>
00325   inline field<T> operator - (const field<T>& rhs)
00326   {
00327     field<T> ret(rhs);
00328     ret.m_value = -ret.m_value;
00329     return ret;
00330   }
00331 
00332   template<typename T>
00333   inline field<T> operator + (const field<T>& rhs)
00334   {
00335     field<T> ret(rhs);
00336     ret.m_value = +ret.m_value;
00337     return ret;
00338   }
00339 
00340   template<typename T>
00341   std::ostream& operator << (std::ostream& os, const field<T>& rhs)
00342   {
00343     if (rhs.is_not_null())
00344       os << *rhs;
00345     else
00346       os << "NULL";
00347     return os;
00348   }
00349 
00350 //   template<typename T>
00351 //   std::istream& operator >> (std::istream& is,
00352 //                           field<T>& rhs)
00353 //   {
00354 //     T tmp;
00355 //     is >> tmp;
00356 //     if (is)
00357 //       set_value(tmp);
00358 //     return is;
00359 //   }
00360 
00361 
00362 }; // namespace pqxxobject
00363 
00364 #endif // PQXX_OBJECT_FIELD_H

Generated on Thu Apr 1 10:37:56 2004 for pqxx-object API Reference by doxygen 1.3.5