ptr_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___ptr_bodies_hh___included
00029 #define lestes__std___ptr_bodies_hh___included
00030 
00031 /*! \file
00032   \brief Smart pointer template bodies.
00033 
00034   Method bodies for the ptr template. Had to be separated from ptr.hh which
00035   contains the declarations.
00036   \author pt
00037 */
00038 #include <lestes/std/ptr.hh>
00039 #include <lestes/std/lassert.hh>
00040 
00041 package(lestes);
00042 package(std);
00043 
00044 /*!
00045   Creates ptr, initializes with pointer.
00046   \post pointer_get() == a_pointer
00047   \param a_pointer  The initialization value.
00048 */
00049 template <typename T>
00050 inline ptr<T>::ptr(T *a_pointer):
00051         root_pointer()
00052 {
00053         pointer_set(a_pointer);
00054 }
00055 
00056 /*!
00057   Creates ptr, initializes with pointer.
00058   \post pointer_get() == a_pointer
00059   \param U  The type of the initializer pointer.
00060   \param a_pointer  The initialization value.
00061 */
00062 template <typename T>
00063 template <typename U>
00064 inline ptr<T>::ptr(U *a_pointer):
00065         root_pointer()
00066 {
00067         pointer_set(a_pointer);
00068 }
00069 
00070 /*!
00071   Creates ptr, initializes with NULL pointer.
00072   \post pointer_get() == NULL
00073 */
00074 template <typename T>
00075 inline ptr<T>::ptr(void):
00076         root_pointer()
00077 {
00078         pointer_set(NULL);
00079 }
00080 
00081 /*!
00082   Creates ptr, initializes with ptr to the same type.
00083   Workaround to avoid implicit copy constructor creation.
00084   \post pointer_get() == other.pointer_get()
00085   \param other  The ptr to initialize with.
00086 */
00087 template <typename T>
00088 inline ptr<T>::ptr(const ptr<T> &other):
00089         root_pointer()
00090 {
00091         pointer_set(other.pointer_get());
00092 }
00093 
00094 /*!
00095   Creates ptr, initializes with ptr.
00096   \post pointer_get() == other.pointer_get()
00097   \param U  The type of the initializer ptr.
00098   \param other  The ptr to initialize with.
00099 */
00100 template <typename T>
00101 template <typename U>
00102 inline ptr<T>::ptr(const ptr<U> &other):
00103         root_pointer()
00104 {
00105         pointer_set(other.pointer_get());
00106 }
00107 
00108 /*!
00109   Creates ptr, initializes with srp.
00110   \post pointer_get() == other.pointer_get()
00111   \param U  The type of the initializer srp.
00112   \param other  The srp to initialize with.
00113 */
00114 template <typename T>
00115 template <typename U>
00116 inline ptr<T>::ptr(const srp<U> &other):
00117         root_pointer()
00118 {
00119         pointer_set(other.pointer_get());
00120 }
00121 
00122 /*!
00123   Destroys ptr.
00124 */
00125 template <typename T>
00126 inline ptr<T>::~ptr(void)
00127 {
00128 }
00129 
00130 /*!
00131   Returns the pointer.
00132   \return  The contained pointer.
00133 */
00134 template <typename T>
00135 inline T *ptr<T>::operator->(void) const
00136 {
00137         T * result = pointer_get();
00138         lassert2( result, "Dereferencing NULL pointer." );
00139         return result;
00140 }
00141 
00142 /*!
00143   Dereferences the pointer.
00144   \return  The dereferenced pointer.
00145 */
00146 template <typename T>
00147 inline T &ptr<T>::operator *(void) const
00148 {
00149         T * presult = pointer_get();
00150         lassert2( presult, "Dereferencing NULL pointer." );
00151         return *presult;
00152 }
00153 
00154 /*!
00155   Assigns pointer.
00156   \param a_pointer  The pointer to assign.
00157   \return  This ptr after the assignment.
00158 */
00159 template <typename T>
00160 inline ptr<T> &ptr<T>::operator=(T *a_pointer)
00161 {
00162         pointer_set(a_pointer);
00163         return *this;
00164 }
00165 
00166 /*!
00167   Assigns pointer.
00168   \param U  The type of the assigned pointer.
00169   \param a_pointer  The pointer to assign.
00170   \return  This ptr after the assignment.
00171 */
00172 template <typename T>
00173 template <typename U>
00174 inline ptr<T> &ptr<T>::operator=(U *a_pointer)
00175 {
00176         pointer_set(a_pointer);
00177         return *this;
00178 }
00179 
00180 /*!
00181   Assigns ptr of the same type.
00182   Workaround to avoid implicit assignment operator creation.
00183   \param other  The ptr to assign.
00184   \return  This ptr after assignment.
00185 */
00186 template <typename T>
00187 inline ptr<T> &ptr<T>::operator=(const ptr<T> &other)
00188 {
00189         pointer_set(other.pointer_get());
00190         return *this;
00191 }
00192 
00193 /*!
00194   Assigns ptr.
00195   \param U  The type of the assigned ptr.
00196   \param other  The ptr to assign.
00197   \return  This ptr after assignment.
00198 */
00199 template <typename T>
00200 template <typename U>
00201 inline ptr<T> &ptr<T>::operator=(const ptr<U> &other)
00202 {
00203         pointer_set(other.pointer_get());
00204         return *this;
00205 }
00206 
00207 /*!
00208   Assigns srp.
00209   \param U  The type of the assigned srp.
00210   \param other  The other srp to assign.
00211   \return  This ptr after assignment.
00212 */
00213 template <typename T>
00214 template <typename U>
00215 inline ptr<T> &ptr<T>::operator=(const srp<U> &other)
00216 {
00217         pointer_set(other.pointer_get());
00218         return *this;
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 ptr<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 ptr<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 ptr<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 ptr<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 ptr<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 ptr<T>::operator==(::std::ptrdiff_t 
00281 #ifdef LESTES_STRICT_CHECKING      
00282                 a_pointer
00283 #endif      
00284                 ) const
00285 {
00286 #ifdef LESTES_STRICT_CHECKING
00287         // test whether really comparing with NULL
00288         lassert(!a_pointer);
00289 #endif
00290         return !pointer_get();
00291 }
00292 
00293 /*!
00294   Compares to ptr.
00295   \param U  The type of the compared pointer.
00296   \return  true if both pointers are equal.
00297 */
00298 template <typename T>
00299 template <typename U>
00300 inline bool ptr<T>::operator==(const ptr<U> &other) const
00301 {
00302         return pointer_get() == other.pointer_get();
00303 }
00304 
00305 /*!
00306   Compares to other srp.
00307   \param U  The type of the compared srp.
00308   \return  true if both pointers are equal.
00309 */
00310 template <typename T>
00311 template <typename U>
00312 inline bool ptr<T>::operator==(const srp<U> &other) const
00313 {
00314         return pointer_get() == other.pointer_get();
00315 }
00316 
00317 /*!
00318   Compares to pointer.
00319   \return  false if both pointers are equal.
00320 */
00321 template <typename T>
00322 inline bool ptr<T>::operator!=(T *a_pointer) const
00323 {
00324         return pointer_get() != a_pointer;
00325 }
00326 
00327 /*!
00328   Compares to pointer.
00329   \param U  The type of the compared pointer.
00330   \return  false if both pointers are equal.
00331 */
00332 template <typename T>
00333 template <typename U>
00334 inline bool ptr<T>::operator!=(U *a_pointer) const
00335 {
00336         return pointer_get() != a_pointer;
00337 }
00338 
00339 /*!
00340   Compares to NULL pointer. Workaround to avoid implicit bool conversion.
00341   \return  false if both pointers are equal.
00342 */
00343 template <typename T>
00344 inline bool ptr<T>::operator!=(::std::ptrdiff_t a_pointer) const
00345 {
00346 #ifdef LESTES_STRICT_CHECKING
00347         // test whether really comparing with NULL
00348         lassert(!a_pointer);
00349 #endif
00350         return pointer_get();
00351 }
00352 
00353 /*!
00354   Compares to ptr.
00355   \param U  The type of the compared pointer.
00356   \return  false if both pointers are equal.
00357 */
00358 template <typename T>
00359 template <typename U>
00360 inline bool ptr<T>::operator!=(const ptr<U> &other) const
00361 {
00362         return pointer_get() != other.pointer_get();
00363 }
00364 
00365 /*!
00366   Compares to other srp.
00367   \param U  The type of the compared srp.
00368   \return  false if both pointers are equal.
00369 */
00370 template <typename T>
00371 template <typename U>
00372 inline bool ptr<T>::operator!=(const srp<U> &other) const
00373 {
00374         return pointer_get() != other.pointer_get();
00375 }
00376 
00377 /*!
00378   Performs dynamic cast on pointers.
00379 
00380   When the dynamic type of the pointee is not the requested one or one derived from it,
00381   the function fails with an assertion. Null pointers do not make the function fail.
00382 
00383   \param U  The type of the target ptr.
00384   \return  New ptr initialized with this ptr pointer.
00385 */
00386 template <typename T>
00387 template <typename U>
00388 inline ptr<U> ptr<T>::dncast(void) const
00389 {
00390         register T * source = pointer_get();
00391         register U * result = dynamic_cast<U *>(source);
00392         // crash when dncast failed (returned NULL given non-NULL argument)
00393         lassert2( !source || result, "Dncasting to wrong type." );
00394         return ptr<U>(result);
00395 }
00396 
00397 /*!
00398   Returns the pointer.
00399   \return  The contained pointer.
00400 */
00401 template <typename T>
00402 inline T *ptr<T>::pointer_get(void) const
00403 {
00404         return static_cast<T *>(root_pointer::pointer_get());
00405 }
00406 
00407 /*!
00408   Compares the pointer with a given one.
00409   \param b  The pointer to compare with.
00410 */
00411 template <typename T>
00412 inline bool ptr<T>::operator < (const ptr<T> &b) const
00413 {
00414         return pointer_get() < b.pointer_get();
00415 }
00416 
00417 /*!
00418   Sets the pointer.
00419   \param a_pointer  The pointer to set.
00420 */
00421 template <typename T>
00422 inline void ptr<T>::pointer_set(T *a_pointer)
00423 {
00424         root_pointer::pointer_set(a_pointer);
00425 }
00426 
00427 end_package(std);
00428 end_package(lestes);
00429 
00430 #endif
00431 /* vim: set ft=lestes : */

Generated on Mon Feb 12 18:27:11 2007 for lestes by doxygen 1.5.1-20070107