00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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 };
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 };
00408
00409 #endif // PQXX_OBJECT_PTR_FIELD_H