- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/Programmation/Sources/Tensor2/Utils/include/frost_utils_ptr.h

Go to the documentation of this file.

    00001 // Warning : If you need to use this file, include frost_utils_types.h
    00002 namespace Frost
    00003 {
    00004     template<class T>
    00005     struct PtrTraits
    00006     {
    00007         typedef const T& CRef;
    00008         typedef T&       Ref;
    00009     };
    00010
    00011     template<>
    00012     struct PtrTraits<void>
    00013     {
    00014         typedef void CRef;
    00015         typedef void Ref;
    00016     };
    00017
    00018     /// Smart pointer
    00019     /** This is very basic pointer.<br>
    00020     *   Its only advantage is that it is initialized
    00021     *   to nullptr automatically.<br>
    00022     *   There is <b>no</b> automatic deletion.
    00023     */
    00024     template<class T>
    00025     class s_ptr
    00026     {
    00027     public :
    00028
    00029         template<class> friend class s_ptr;
    00030
    00031         /// Default constructor.
    00032         /** \note Initializes the pointer to NULL.
    00033         */
    00034         s_ptr()
    00035         {
    00036             pValue_ = nullptr;
    00037         }
    00038
    00039         /// Constructor.
    00040         s_ptr(T* pValue)
    00041         {
    00042             pValue_ = pValue;
    00043         }
    00044
    00045         /// Conversion from nullptr.
    00046         s_ptr(const s_nullptr& pNull)
    00047         {
    00048             pValue_ = nullptr;
    00049         }
    00050
    00051         /// Copy constructor.
    00052         s_ptr(const s_ptr& mPointer)
    00053         {
    00054             pValue_ = mPointer.pValue_;
    00055         }
    00056
    00057         /// Conversion constructor.
    00058         template<class N>
    00059         explicit s_ptr(const s_ptr<N>& mPointer)
    00060         {
    00061             pValue_ = mPointer.pValue_;
    00062         }
    00063
    00064         /// Returns the contained pointer.
    00065         /** \return The contained pointer
    00066         */
    00067         const T* Get() const
    00068         {
    00069             return pValue_;
    00070         }
    00071
    00072         /// Returns the contained pointer.
    00073         /** \return The contained pointer
    00074         */
    00075         T* Get()
    00076         {
    00077             return pValue_;
    00078         }
    00079
    00080         /// Checks if this pointer is usable.
    00081         /** \return 'true' is this pointer is usable
    00082         */
    00083         s_bool IsValid() const
    00084         {
    00085             return (pValue_ != 0);
    00086         }
    00087
    00088         /// Sets this pointer to NULL.
    00089         void SetNull()
    00090         {
    00091             pValue_ = nullptr;
    00092         }
    00093
    00094         /// Deletes the content of the pointer;
    00095         void Delete()
    00096         {
    00097             delete pValue_;
    00098             pValue_ = nullptr;
    00099         }
    00100
    00101         /// Returns a reference to the contained value.
    00102         /** \return A reference to the contained value
    00103         */
    00104         typename PtrTraits<T>::CRef operator * () const
    00105         {
    00106             return *pValue_;
    00107         }
    00108
    00109         /// Returns a reference to the contained value.
    00110         /** \return A reference to the contained value
    00111         */
    00112         typename PtrTraits<T>::Ref operator * ()
    00113         {
    00114             return *pValue_;
    00115         }
    00116
    00117         /// Dereferences the pointer.
    00118         /** \return The contained pointer
    00119         */
    00120         const T* operator -> () const
    00121         {
    00122             return pValue_;
    00123         }
    00124
    00125         /// Dereferences the pointer.
    00126         /** \return The contained pointer
    00127         */
    00128         T* operator -> ()
    00129         {
    00130             return pValue_;
    00131         }
    00132
    00133         /// nullptr assignation operator.
    00134         /** \param pPtr The value to copy
    00135         */
    00136         s_ptr<T>& operator = (const s_nullptr& pPtr)
    00137         {
    00138             pValue_ = nullptr;
    00139             return *this;
    00140         }
    00141
    00142         /// Assign a new pointer to this smart pointer.
    00143         /** \param pValue The new pointer
    00144         */
    00145         template<class N>
    00146         s_ptr<T>& operator = (const s_ptr<N>& pValue)
    00147         {
    00148             pValue_ = pValue.pValue_;
    00149             return *this;
    00150         }
    00151
    00152         /// Assign a new pointer to this smart pointer.
    00153         /** \param pValue The new pointer
    00154         */
    00155         s_ptr<T>& operator = (T* pValue)
    00156         {
    00157             pValue_ = pValue;
    00158             return *this;
    00159         }
    00160
    00161         /// Checks if this pointer equals another
    00162         /** \param pValue The pointer to test
    00163         */
    00164         template<class N>
    00165         s_bool operator == (const s_ptr<N>& pValue) const
    00166         {
    00167             return (pValue_ == pValue.pValue_);
    00168         }
    00169
    00170         /// Checks if this pointer equals another
    00171         /** \param pValue The pointer to test
    00172         */
    00173         template<class N>
    00174         s_bool operator == (N* pValue) const
    00175         {
    00176             return (pValue_ == pValue);
    00177         }
    00178
    00179         /// Checks if this pointer is null
    00180         /** \param pValue The null pointer
    00181         *   \return 'true' if this pointer is null
    00182         */
    00183         s_bool operator == (s_nullptr pValue) const
    00184         {
    00185             return (pValue_ == 0);
    00186         }
    00187
    00188         /// Checks if this pointer is different from another
    00189         /** \param pValue The pointer to test
    00190         *   \return 'true' if this pointer is different from another
    00191         */
    00192         template<class N>
    00193         s_bool operator != (const s_ptr<N>& pValue) const
    00194         {
    00195             return (pValue_ != pValue.pValue_);
    00196         }
    00197
    00198         /// Checks if this pointer is different from another
    00199         /** \param pValue The pointer to test
    00200         *   \return 'true' if this pointer is different from another
    00201         */
    00202         template<class N>
    00203         s_bool operator != (N* pValue) const
    00204         {
    00205             return (pValue_ != pValue);
    00206         }
    00207
    00208         /// Checks if this pointer is not null
    00209         /** \param pValue The null pointer
    00210         *   \return 'true' if this pointer is not null
    00211         */
    00212         s_bool operator != (s_nullptr pValue) const
    00213         {
    00214             return (pValue_ != 0);
    00215         }
    00216
    00217         /// Checks if this pointer's value is lower than the other
    00218         /** \param pValue The pointer to test
    00219         *   \return 'true' if this pointer's value is lower than the other
    00220         */
    00221         template<class N>
    00222         s_bool operator < (const s_ptr<N>& pValue) const
    00223         {
    00224             return (pValue_ < pValue.pValue_);
    00225         }
    00226
    00227         /// Checks if this pointer's value is lower than the other
    00228         /** \param pValue The pointer to test
    00229         *   \return 'true' if this pointer's value is lower than the other
    00230         */
    00231         template<class N>
    00232         s_bool operator < (N* pValue) const
    00233         {
    00234             return (pValue_ < pValue);
    00235         }
    00236
    00237         /// Checks if this pointer's value is lower or equal than the other
    00238         /** \param pValue The pointer to test
    00239         *   \return 'true' if this pointer's value is lower or equal than the other
    00240         */
    00241         template<class N>
    00242         s_bool operator <= (const s_ptr<N>& pValue) const
    00243         {
    00244             return (pValue_ <= pValue.pValue_);
    00245         }
    00246
    00247         /// Checks if this pointer's value is lower or equal than the other
    00248         /** \param pValue The pointer to test
    00249         *   \return 'true' if this pointer's value is lower or equal than the other
    00250         */
    00251         template<class N>
    00252         s_bool operator <= (N* pValue) const
    00253         {
    00254             return (pValue_ <= pValue);
    00255         }
    00256
    00257         /// Checks if this pointer's value is greater than the other
    00258         /** \param pValue The pointer to test
    00259         *   \return 'true' if this pointer's value is greater than the other
    00260         */
    00261         template<class N>
    00262         s_bool operator > (const s_ptr<N>& pValue) const
    00263         {
    00264             return (pValue_ > pValue.pValue_);
    00265         }
    00266
    00267         /// Checks if this pointer's value is greater than the other
    00268         /** \param pValue The pointer to test
    00269         *   \return 'true' if this pointer's value is greater than the other
    00270         */
    00271         template<class N>
    00272         s_bool operator > (N* pValue) const
    00273         {
    00274             return (pValue_ > pValue);
    00275         }
    00276
    00277         /// Checks if this pointer's value is greater or equal than the other
    00278         /** \param pValue The pointer to test
    00279         *   \return 'true' if this pointer's value is greater or equal than the other
    00280         */
    00281         template<class N>
    00282         s_bool operator >= (const s_ptr<N>& pValue) const
    00283         {
    00284             return (pValue_ >= pValue.pValue_);
    00285         }
    00286
    00287         /// Checks if this pointer's value is greater or equal than the other
    00288         /** \param pValue The pointer to test
    00289         *   \return 'true' if this pointer's value is greater or equal than the other
    00290         */
    00291         template<class N>
    00292         s_bool operator >= (N* pValue) const
    00293         {
    00294             return (pValue_ >= pValue);
    00295         }
    00296
    00297         /// Allows : "if (!pPointer)".
    00298         bool operator ! () const
    00299         {
    00300             return (pValue_ == 0);
    00301         }
    00302
    00303         typedef void (s_ptr::*MemberFn)();
    00304         /// Allows : "if (pPointer)".
    00305         operator MemberFn() const
    00306         {
    00307             if (pValue_ != 0)
    00308                 return &s_ptr::SetNull;
    00309             else
    00310                 return 0;
    00311         }
    00312
    00313         /// Allows limited implicit inheritance conversion.
    00314         template<class N>
    00315         operator s_ptr<N>()
    00316         {
    00317             return s_ptr<N>(pValue_);
    00318         }
    00319
    00320         /// Allows limited implicit inheritance conversion.
    00321         template<class N>
    00322         operator s_ptr<const N>() const
    00323         {
    00324             return s_ptr<const N>(pValue_);
    00325         }
    00326
    00327         s_ctnr< s_ptr<T> > operator , (const s_ptr<T>& pValue) const
    00328         {
    00329             s_ctnr< s_ptr<T> > mContainer;
    00330             mContainer.PushBack(*this);
    00331             mContainer.PushBack(pValue);
    00332             return mContainer;
    00333         }
    00334
    00335         /// Casts the provided pointer to this one's type.
    00336         /** \param pValue The pointer to cast
    00337         *   \return The new casted pointer
    00338         */
    00339         template<class N>
    00340         static s_ptr<T> StaticCast(const s_ptr<N>& pValue)
    00341         {
    00342             return static_cast<T*>(pValue.pValue_);
    00343         }
    00344
    00345         /// Casts the provided pointer to this one's type.
    00346         /** \param pValue The pointer to cast
    00347         *   \return The new casted pointer
    00348         */
    00349         template<class N>
    00350         static s_ptr<T> StaticCast(N* pValue)
    00351         {
    00352             return static_cast<T*>(pValue);
    00353         }
    00354
    00355         /// Tries to dynamic cast the provided pointer to this one's type.
    00356         /** \param pValue The pointer to cast
    00357         *   \return The new casted pointer
    00358         *   \note Dynamic cast can fail, and in this case, will result in
    00359         *         a NULL pointer.
    00360         */
    00361         template<class N>
    00362         static s_ptr<T> DynamicCast(const s_ptr<N>& pValue)
    00363         {
    00364             return dynamic_cast<T*>(pValue.pValue_);
    00365         }
    00366
    00367         /// Tries to dynamic cast the provided pointer to this one's type.
    00368         /** \param pValue The pointer to cast
    00369         *   \return The new casted pointer
    00370         *   \note Dynamic cast can fail, and in this case, will result in
    00371         *         a NULL pointer.
    00372         */
    00373         template<class N>
    00374         static s_ptr<T> DynamicCast(N* pValue)
    00375         {
    00376             return dynamic_cast<T*>(pValue);
    00377         }
    00378
    00379     private :
    00380
    00381         T* pValue_;
    00382     };
    00383
    00384     /** \cond NOT_REMOVE_FROM_DOC
    00385     */
    00386     template<class T> class TypeTraits<T*>
    00387     {
    00388     public :
    00389         typedef T*         Type;
    00390         typedef T*         BaseType;
    00391         typedef s_ptr<T>   FrostType;
    00392         typedef T*&        RefType;
    00393         typedef T* const & CRefType;
    00394         typedef T**        PointerType;
    00395
    00396         static inline RefType  GetValue(RefType m)  { return m; }
    00397         static inline CRefType GetValue(CRefType m) { return m; }
    00398     };
    00399
    00400     template<> class TypeTraits<void>
    00401     {
    00402     public :
    00403         typedef void  Type;
    00404         typedef void  BaseType;
    00405         typedef void  FrostType;
    00406         typedef void  RefType;
    00407         typedef void  CRefType;
    00408         typedef void* PointerType;
    00409     };
    00410
    00411     template<class T> class TypeTraits< s_ptr<T> >
    00412     {
    00413     public :
    00414         typedef s_ptr<T>        Type;
    00415         typedef T*              BaseType;
    00416         typedef s_ptr<T>        FrostType;
    00417         typedef s_ptr<T>&       RefType;
    00418         typedef const s_ptr<T>& CRefType;
    00419         typedef s_ptr<T>*       PointerType;
    00420
    00421         static inline typename TypeTraits::BaseType GetValue(RefType m)  { return m.Get(); }
    00422         static inline typename TypeTraits::BaseType GetValue(CRefType m) { return m.Get(); }
    00423     };
    00424     /** \endcond
    00425     */
    00426 }
    00427