srp_bodies.hh

Go to the documentation of this file.
00001 /*
00002    The lestes compiler suite
00003    Copyright (C) 2002, 2003, 2004, 2005 Miroslav Tichy
00004    Copyright (C) 2002, 2003, 2004, 2005 Petr Zika
00005    Copyright (C) 2002, 2003, 2004, 2005 Vojtech Hala
00006    Copyright (C) 2002, 2003, 2004, 2005 Jiri Kosina
00007    Copyright (C) 2002, 2003, 2004, 2005 Pavel Sanda
00008    Copyright (C) 2002, 2003, 2004, 2005 Jan Zouhar
00009    Copyright (C) 2002, 2003, 2004, 2005 Rudolf Thomas
00010 
00011    This program is free software; you can redistribute it and/or modify
00012    it under the terms of the GNU General Public License as published by
00013    the Free Software Foundation; version 2 of the License.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    See the full text of the GNU General Public License version 2, and
00021    the limitations in the file doc/LICENSE.
00022 
00023    By accepting the license the licensee waives any and all claims
00024    against the copyright holder(s) related in whole or in part to the
00025    work, its use, and/or the inability to use it.
00026  
00027  */
00028 #ifndef lestes__std___srp_bodies_hh___included
00029 #define lestes__std___srp_bodies_hh___included
00030 
00031 /*! \file
00032   \brief Smart pointer template bodies
00033 
00034   Method bodies for the srp template. Had to be separated from srp.hh which
00035   contains the declarations.
00036   \author pt
00037 */
00038 #include <lestes/std/srp.hh>
00039 
00040 package(lestes);
00041 package(std);
00042 
00043 /*!
00044   Creates srp, initializes with pointer.
00045   \post pointer_get() == a_pointer
00046   \param a_pointer  The initialization value.
00047 */
00048 template <typename T>
00049 inline srp<T>::srp(T *a_pointer):
00050         simple_pointer()
00051 {
00052         pointer_set(a_pointer);
00053 }
00054 
00055 /*!
00056   Creates srp, initializes with pointer.
00057   \post pointer_get() == a_pointer
00058   \param U  The type of the initializer pointer.
00059   \param a_pointer  The initialization value.
00060 */
00061 template <typename T>
00062 template <typename U>
00063 inline srp<T>::srp(U *a_pointer):
00064         simple_pointer()
00065 {
00066         pointer_set(a_pointer);
00067 }
00068 
00069 /*!
00070   Creates srp, initializes with NULL pointer.
00071   \post pointer_get() == NULL
00072 */
00073 template <typename T>
00074 inline srp<T>::srp(void):
00075         simple_pointer()
00076 {
00077         pointer_set(NULL);
00078 }
00079 
00080 /*!
00081   Creates srp, initializes with srp to the same type.
00082   Workaround to avoid implicit copy constructor creation.
00083   \post pointer_get() == other.pointer_get()
00084   \param other  The srp to initialize with.
00085 */
00086 template <typename T>
00087 inline srp<T>::srp(const srp<T> &other):
00088         simple_pointer()
00089 {
00090         pointer_set(other.pointer_get());
00091 }
00092 
00093 /*!
00094   Creates srp, initializes with srp.
00095   \post pointer_get() == other.pointer_get()
00096   \param U  The type of the initializer srp.
00097   \param other  The srp to initialize with.
00098 */
00099 template <typename T>
00100 template <typename U>
00101 inline srp<T>::srp(const srp<U> &other):
00102         simple_pointer()
00103 {
00104         pointer_set(other.pointer_get());
00105 }
00106 
00107 /*!
00108   Creates srp, initializes with ptr.
00109   \post pointer_get() == other.pointer_get()
00110   \param U  The type of the initializer ptr.
00111   \param other  The ptr to initialize with.
00112 */
00113 template <typename T>
00114 template <typename U>
00115 inline srp<T>::srp(const ptr<U> &other):
00116         simple_pointer()
00117 {
00118         pointer_set(other.pointer_get());
00119 }
00120 
00121 /*!
00122   Destroys srp.
00123 */
00124 template <typename T>
00125 inline srp<T>::~srp(void)
00126 {
00127 }
00128 
00129 /*!
00130   Returns the pointer.
00131   \return  The contained pointer.
00132 */
00133 template <typename T>
00134 inline T *srp<T>::operator->(void) const
00135 {
00136         T * result = pointer_get();
00137         lassert2( result, "Dereferencing NULL pointer." );
00138         return result;
00139 }
00140 
00141 /*!
00142   Dereferences the pointer.
00143   \return  The dereferenced pointer.
00144 */
00145 template <typename T>
00146 inline T &srp<T>::operator *(void) const
00147 {
00148         T * presult = pointer_get();
00149         lassert2( presult, "Dereferencing NULL pointer." );
00150         return *presult;
00151 }
00152 
00153 /*!
00154   Assigns pointer.
00155   \param a_pointer  The pointer to assign.
00156   \return  This srp after the assignment.
00157 */
00158 template <typename T>
00159 inline srp<T> &srp<T>::operator=(T *a_pointer)
00160 {
00161         pointer_set(a_pointer);
00162         return *this;
00163 }
00164 
00165 /*!
00166   Assigns pointer.
00167   \param U  The type of the assigned pointer.
00168   \param a_pointer  The pointer to assign.
00169   \return  This srp after the assignment.
00170 */
00171 template <typename T>
00172 template <typename U>
00173 inline srp<T> &srp<T>::operator=(U *a_pointer)
00174 {
00175         pointer_set(a_pointer);
00176         return *this;
00177 }
00178 
00179 /*!
00180   Assigns srp of the same type.
00181   Workaround to avoid implicit assignment operator creation.
00182   \param other  The srp to assign.
00183   \return  This srp after assignment.
00184 */
00185 template <typename T>
00186 inline srp<T> &srp<T>::operator=(const srp<T> &other)
00187 {
00188         pointer_set(other.pointer_get());
00189         return *this;
00190 }
00191 
00192 /*!
00193   Assigns srp.
00194   \param U  The type of the assigned srp.
00195   \param other  The srp to assign.
00196   \return  This srp after assignment.
00197 */
00198 template <typename T>
00199 template <typename U>
00200 inline srp<T> &srp<T>::operator=(const srp<U> &other)
00201 {
00202         pointer_set(other.pointer_get());
00203         return *this;
00204 }
00205 
00206 /*!
00207   Assigns ptr.
00208   \param U  The type of the assigned ptr.
00209   \param other  The ptr to assign.
00210   \return  This srp after assignment.
00211 */
00212 template <typename T>
00213 template <typename U>
00214 inline srp<T> &srp<T>::operator=(const ptr<U> &other)
00215 {
00216         pointer_set(other.pointer_get());
00217         return *this;
00218 }
00219 
00220 #ifdef LESTES_OLD_POINTER_CONDITION
00221 /*!
00222   Tests NULL pointer.
00223   \return  true if the pointer is NULL.
00224 */  
00225 template <typename T>
00226 inline srp<T>::operator bool(void) const
00227 {
00228         return pointer_get();
00229 }
00230 #else
00231 /*!
00232   Tests NULL pointer.
00233   \return  Condition convertible to true if the pointer is NULL.
00234 */  
00235 template <typename T>
00236 inline srp<T>::operator condition*(void) const
00237 {
00238         static condition c;
00239         return pointer_get() ? &c : NULL;
00240 }
00241 #endif
00242 
00243 /*!
00244   Tests non NULL pointer.
00245   \return  true if the pointer is not NULL.
00246 */
00247 template <typename T>
00248 inline bool srp<T>::operator!(void) const
00249 {
00250         return !pointer_get();
00251 }
00252 
00253 /*!
00254   Compares to pointer.
00255   \return  true if both pointers are equal.
00256 */
00257 template <typename T>
00258 inline bool srp<T>::operator==(T *a_pointer) const
00259 {
00260         return pointer_get() == a_pointer;
00261 }
00262 
00263 /*!
00264   Compares to pointer.
00265   \param U  The type of the compared pointer.
00266   \return  true if both pointers are equal.
00267 */
00268 template <typename T>
00269 template <typename U>
00270 inline bool srp<T>::operator==(U *a_pointer) const
00271 {
00272         return pointer_get() == a_pointer;
00273 }
00274 
00275 /*!
00276   Compares to NULL pointer. Workaround to avoid implicit bool conversion.
00277   \return  true if both pointers are equal.
00278 */
00279 template <typename T>
00280 inline bool srp<T>::operator==(::std::ptrdiff_t a_pointer) const
00281 {
00282 #ifdef LESTES_STRICT_CHECKING
00283         // test whether really comparing with NULL
00284         lassert(!a_pointer);
00285 #endif
00286         return !pointer_get();
00287 }
00288 
00289 /*!
00290   Compares to srp.
00291   \param U  The type of the compared pointer.
00292   \return  true if both pointers are equal.
00293 */
00294 template <typename T>
00295 template <typename U>
00296 inline bool srp<T>::operator==(const srp<U> &other) const
00297 {
00298         return pointer_get() == other.pointer_get();
00299 }
00300 
00301 /*!
00302   Compares to ptr.
00303   \param U  The type of the compared ptr.
00304   \return  true if both pointers are equal.
00305 */
00306 template <typename T>
00307 template <typename U>
00308 inline bool srp<T>::operator==(const ptr<U> &other) const
00309 {
00310         return pointer_get() == other.pointer_get();
00311 }
00312 
00313 /*!
00314   Compares to pointer.
00315   \return  false if both pointers are equal.
00316 */
00317 template <typename T>
00318 inline bool srp<T>::operator!=(T *a_pointer) const
00319 {
00320         return pointer_get() != a_pointer;
00321 }
00322 
00323 /*!
00324   Compares to pointer.
00325   \param U  The type of the compared pointer.
00326   \return  false if both pointers are equal.
00327 */
00328 template <typename T>
00329 template <typename U>
00330 inline bool srp<T>::operator!=(U *a_pointer) const
00331 {
00332         return pointer_get() != a_pointer;
00333 }
00334 
00335 /*!
00336   Compares to NULL pointer. Workaround to avoid implicit bool conversion.
00337   \return  false if both pointers are equal.
00338 */
00339 template <typename T>
00340 inline bool srp<T>::operator!=(::std::ptrdiff_t a_pointer) const
00341 {
00342 #ifdef LESTES_STRICT_CHECKING
00343         // test whether really comparing with NULL
00344         lassert(!a_pointer);
00345 #endif
00346         return pointer_get();
00347 }
00348 
00349 /*!
00350   Compares to srp.
00351   \param U  The type of the compared pointer.
00352   \return  false if both pointers are equal.
00353 */
00354 template <typename T>
00355 template <typename U>
00356 inline bool srp<T>::operator!=(const srp<U> &other) const
00357 {
00358         return pointer_get() != other.pointer_get();
00359 }
00360 
00361 /*!
00362   Compares to ptr.
00363   \param U  The type of the compared ptr.
00364   \return  false if both pointers are equal.
00365 */
00366 template <typename T>
00367 template <typename U>
00368 inline bool srp<T>::operator!=(const ptr<U> &other) const
00369 {
00370         return pointer_get() != other.pointer_get();
00371 }
00372 
00373 /*!
00374   Performs dynamic cast on pointers.
00375 
00376   When the dynamic type of the pointee is not the requested one or one derived from it,
00377   the function fails with an assertion. Null pointers do not make the function fail.
00378   
00379   \param U  The type of the target ptr.
00380   \return  New ptr initialized with this srp pointer.
00381 */
00382 template <typename T>
00383 template <typename U>
00384 inline ptr<U> srp<T>::dncast(void) const
00385 {
00386         register T * source = pointer_get();
00387         register U * result = dynamic_cast<U *>(source);
00388         // crash when dncast failed (returned NULL given non-NULL argument)
00389         lassert2( !source || result, "Dncasting to wrong type." );
00390         return ptr<U>(result);
00391 }
00392 
00393 /*!
00394   Returns the pointer.
00395   \return  The contained pointer.
00396 */
00397 template <typename T>
00398 inline T *srp<T>::pointer_get(void) const
00399 {
00400         return static_cast<T *>(simple_pointer::pointer_get());
00401 }
00402 
00403 /*!
00404   Compares the pointer with a given one.
00405   \param b  The pointer to compare with.
00406 */
00407 template <typename T>
00408 inline bool srp<T>::operator < (const srp<T> &b) const
00409 {
00410         return pointer_get() < b.pointer_get();
00411 }
00412 
00413 /*!
00414   Sets the pointer.
00415   \param a_pointer  The pointer to set.
00416 */
00417 template <typename T>
00418 inline void srp<T>::pointer_set(T *a_pointer)
00419 {
00420         simple_pointer::pointer_set(a_pointer);
00421 }
00422 
00423 end_package(std);
00424 end_package(lestes);
00425 
00426 #endif
00427 /* vim: set ft=lestes : */

Generated on Mon Feb 12 18:23:16 2007 for lestes by doxygen 1.5.1-20070107