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

pqxx-object/ptr_field.h

Go to the documentation of this file.
00001 // database pointer field container                              -*- C++ -*-
00002 // $Id: ptr_field.h,v 1.1 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_PTR_FIELD_H
00040 #define PQXX_OBJECT_PTR_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 {
00070   template<typename T>
00071   class ptr_field : public pqxxobject::field_base
00072   {
00073   public:
00074     typedef T value_type;
00075 
00077     ptr_field():
00078       m_value(0)
00079     {}
00080 
00082     explicit ptr_field(const value_type& value):
00083       m_value(0)
00084     {
00085       set_value(value);
00086     }
00087 
00089     ptr_field(const ptr_field<value_type>& rhs):
00090       field_base(rhs),
00091       m_value(0)
00092     {
00093       if (rhs.m_value)
00094         set_value(*rhs.m_value);
00095     }
00096 
00097     virtual ~ptr_field()
00098     {
00099       if (m_value)
00100         delete m_value;
00101     }
00102 
00104     const value_type *operator -> () const
00105     {
00106       assert (m_value != 0);
00107       return m_value;
00108     }
00109 
00110     const value_type& operator * () const
00111     {
00112       assert (m_value != 0);
00113       return *m_value;
00114     }
00115 
00117     ptr_field<value_type>& operator = (const ptr_field<value_type>& rhs)
00118     {
00119       if (rhs.m_value)
00120         set_value(*rhs.m_value);
00121       else
00122         set_null();
00123       return *this;
00124     }
00125 
00127     ptr_field<value_type>& operator = (const value_type& rhs)
00128     {
00129       set_value(rhs);
00130       return *this;
00131     }
00132 
00133     ptr_field<value_type>& operator += (const ptr_field<value_type>& rhs)
00134     {
00135       assert (m_value != 0);
00136       assert (rhs.m_value != 0);
00137       *m_value += *rhs.m_value;
00138       m_signal_changed.emit();
00139       return *this;
00140     }
00141 
00142     ptr_field<value_type>& operator -= (const ptr_field<value_type>& rhs)
00143     {
00144       assert (m_value != 0);
00145       assert (rhs.m_value != 0);
00146       *m_value -= *rhs.m_value;
00147       m_signal_changed.emit();
00148       return *this;
00149     }
00150 
00151     ptr_field<value_type>& operator *= (const ptr_field<value_type>& rhs)
00152     {
00153       assert (m_value != 0);
00154       assert (rhs.m_value != 0);
00155       *m_value *= *rhs.m_value;
00156       m_signal_changed.emit();
00157       return *this;
00158     }
00159 
00160     ptr_field<value_type>& operator /= (const ptr_field<value_type>& rhs)
00161     {
00162       assert (m_value != 0);
00163       assert (rhs.m_value != 0);
00164       *m_value /= *rhs.m_value;
00165       m_signal_changed.emit();
00166       return *this;
00167     }
00168 
00169     ptr_field<value_type>& operator %= (const ptr_field<value_type>& rhs)
00170     {
00171       assert (m_value != 0);
00172       assert (rhs.m_value != 0);
00173       *m_value %= *rhs.m_value;
00174       m_signal_changed.emit();
00175       return *this;
00176     }
00177 
00178     ptr_field<value_type>& operator ++ ()
00179     {
00180       assert (m_value != 0);
00181       *m_value++;
00182       m_signal_changed.emit();
00183       return *this;
00184     }
00185 
00186     ptr_field<value_type> operator ++ (int)
00187     {
00188       assert (m_value != 0);
00189       ptr_field<value_type> ret(*this);
00190       *m_value++;
00191       m_signal_changed.emit();
00192       return ret;
00193     }
00194 
00195     ptr_field<value_type>& operator -- ()
00196     {
00197       assert (m_value != 0);
00198       *m_value--;
00199       m_signal_changed.emit();
00200       return *this;
00201     }
00202 
00203     ptr_field<value_type> operator -- (int)
00204     {
00205       assert (m_value != 0);
00206       ptr_field<value_type> ret(*this);
00207       *m_value--;
00208       m_signal_changed.emit();
00209       return ret;
00210     }
00211 
00212     friend ptr_field<value_type> operator + <> (const ptr_field<value_type>& lhs,
00213                                                 const ptr_field<value_type>& rhs);
00214 
00215     friend ptr_field<value_type> operator - <> (const ptr_field<value_type>& lhs,
00216                                                 const ptr_field<value_type>& rhs);
00217 
00218     friend ptr_field<value_type> operator * <> (const ptr_field<value_type>& lhs,
00219                                                 const ptr_field<value_type>& rhs);
00220 
00221     friend ptr_field<value_type> operator / <> (const ptr_field<value_type>& lhs,
00222                                                 const ptr_field<value_type>& rhs);
00223 
00224     friend ptr_field<value_type> operator % <> (const ptr_field<value_type>& lhs,
00225                                                 const ptr_field<value_type>& rhs);
00226 
00227     friend bool operator == <> (const ptr_field<value_type>& lhs,
00228                                 const ptr_field<value_type>& rhs);
00229 
00230     friend bool operator != <> (const ptr_field<value_type>& lhs,
00231                                 const ptr_field<value_type>& rhs);
00232 
00233     friend ptr_field<value_type> operator - <> (const ptr_field<value_type>& rhs);
00234 
00235     friend ptr_field<value_type> operator + <> (const ptr_field<value_type>& rhs);
00236 
00237     friend std::ostream& operator << <> (std::ostream& output_stream,
00238                                          const ptr_field<value_type>& rhs);
00239 
00241     operator const value_type& () const
00242     {
00243       assert (m_value != 0);
00244       return *m_value;
00245     }
00246 
00251     value_type& get_value()
00252     {
00253       assert (m_value != 0);
00254       return *m_value;
00255     }
00256 
00261     const value_type& get_value() const
00262     {
00263       assert (m_value != 0);
00264       return *m_value;
00265     }
00266 
00271     void set_value(const value_type& value)
00272     {
00273       if (m_value)
00274         *m_value = value;
00275       else
00276         m_value = new value_type(value);
00277       m_signal_changed.emit();
00278     }
00279 
00283     void set_null()
00284     {
00285       if (m_value)
00286         {
00287           delete m_value;
00288           m_value = 0;
00289         }
00290     }
00291 
00296     bool is_null() const
00297     {
00298       return m_value == 0;
00299     }
00300 
00305     bool is_not_null() const
00306     {
00307       return m_value != 0;
00308     }
00309 
00310   private:
00312     value_type *m_value;
00313 
00314   }; // class ptr_field
00315 
00316   template<typename T>
00317   inline ptr_field<T> operator + (const ptr_field<T>& lhs,
00318                                   const ptr_field<T>& rhs)
00319   {
00320     assert (lhs.m_value != 0);
00321     assert (rhs.m_value != 0);
00322     return ptr_field<T>(lhs) += rhs;
00323   }
00324 
00325   template<typename T>
00326   inline ptr_field<T> operator - (const ptr_field<T>& lhs,
00327                                   const ptr_field<T>& rhs)
00328   {
00329     assert (lhs.m_value != 0);
00330     assert (rhs.m_value != 0);
00331     return ptr_field<T>(lhs) -= rhs;
00332   }
00333 
00334   template<typename T>
00335   inline ptr_field<T> operator * (const ptr_field<T>& lhs,
00336                                   const ptr_field<T>& rhs)
00337   {
00338     assert (lhs.m_value != 0);
00339     assert (rhs.m_value != 0);
00340     return ptr_field<T>(lhs) *= rhs;
00341   }
00342 
00343   template<typename T>
00344   inline ptr_field<T> operator / (const ptr_field<T>& lhs,
00345                                   const ptr_field<T>& rhs)
00346   {
00347     assert (lhs.m_value != 0);
00348     assert (rhs.m_value != 0);
00349     return ptr_field<T>(lhs) /= rhs;
00350   }
00351 
00352   template<typename T>
00353   inline ptr_field<T> operator % (const ptr_field<T>& lhs,
00354                                   const ptr_field<T>& rhs)
00355   {
00356     assert (lhs.m_value != 0);
00357     assert (rhs.m_value != 0);
00358     return ptr_field<T>(lhs) %= rhs;
00359   }
00360 
00361   template<typename T>
00362   inline bool operator == (const ptr_field<T>& lhs,
00363                            const ptr_field<T>& rhs)
00364   {
00365     assert (lhs.m_value != 0);
00366     assert (rhs.m_value != 0);
00367     return (*lhs.m_value == *rhs.m_value);
00368   }
00369 
00370   template<typename T>
00371   inline bool operator != (const ptr_field<T>& lhs,
00372                            const ptr_field<T>& rhs)
00373   {
00374     assert (lhs.m_value != 0);
00375     assert (rhs.m_value != 0);
00376     return !(lhs == rhs);
00377   }
00378 
00379   template<typename T>
00380   inline ptr_field<T> operator - (const ptr_field<T>& rhs)
00381   {
00382     assert (rhs.m_value != 0);
00383     ptr_field<T> ret(rhs);
00384     *ret.m_value = -(*ret.m_value);
00385     return ret;
00386   }
00387 
00388   template<typename T>
00389   inline ptr_field<T> operator + (const ptr_field<T>& rhs)
00390   {
00391     assert (rhs.m_value != 0);
00392     ptr_field<T> ret(rhs);
00393     *ret.m_value = +(*ret.m_value);
00394     return ret;
00395   }
00396 
00397   template<typename T>
00398   std::ostream& operator << (std::ostream& os, const ptr_field<T>& rhs)
00399   {
00400     if (rhs.is_not_null())
00401       os << *rhs;
00402     else
00403       os << "NULL";
00404     return os;
00405   }
00406 
00407 }; // namespace pqxxobject
00408 
00409 #endif // PQXX_OBJECT_PTR_FIELD_H

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