PixelLightAPI  .
SmartPtr.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: SmartPtr.h                                     *
00003  *
00004  *  Copyright (C) 2002-2011 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLCORE_SMARTPTR_H__
00024 #define __PLCORE_SMARTPTR_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Namespace                                             ]
00030 //[-------------------------------------------------------]
00031 namespace PLCore {
00032 
00033 
00034 //[-------------------------------------------------------]
00035 //[ Forward declarations                                  ]
00036 //[-------------------------------------------------------]
00037 template <class AType> class RefCount;
00038 
00039 
00040 //[-------------------------------------------------------]
00041 //[ Classes                                               ]
00042 //[-------------------------------------------------------]
00043 /**
00044 *  @brief
00045 *    Smart pointer template
00046 */
00047 template <class AType>
00048 class SmartPtr {
00049 
00050 
00051     //[-------------------------------------------------------]
00052     //[ Public functions                                      ]
00053     //[-------------------------------------------------------]
00054     public:
00055         /**
00056         *  @brief
00057         *    Constructor
00058         */
00059         SmartPtr();
00060 
00061         /**
00062         *  @brief
00063         *    Constructor
00064         *
00065         *  @param[in] pPtr
00066         *    Direct pointer to initialize with, can be a null pointer
00067         */
00068         SmartPtr(AType *pPtr);
00069 
00070         /**
00071         *  @brief
00072         *    Constructor
00073         *
00074         *  @param[in] pPtr
00075         *    Smart pointer to initialize with
00076         */
00077         SmartPtr(const SmartPtr<AType> &pPtr);
00078 
00079         /**
00080         *  @brief
00081         *    Destructor
00082         */
00083         ~SmartPtr();
00084 
00085         /**
00086         *  @brief
00087         *    Assign a pointer
00088         *
00089         *  @param[in] pPtr
00090         *    Direct pointer to assign, can be a null pointer
00091         *
00092         *  @return
00093         *    Reference to the smart pointer
00094         */
00095         SmartPtr<AType> &operator =(AType *pPtr);
00096 
00097         /**
00098         *  @brief
00099         *    Assign a smart pointer
00100         *
00101         *  @param[in] cPtr
00102         *    Smart pointer to assign
00103         *
00104         *  @return
00105         *    Reference to the smart pointer
00106         */
00107         SmartPtr<AType> &operator =(const SmartPtr<AType> &cPtr);
00108 
00109         /**
00110         *  @brief
00111         *    Get a direct pointer to the object
00112         *
00113         *  @return
00114         *    Pointer to the object, can be a null pointer
00115         */
00116         AType *GetPointer() const;
00117 
00118         /**
00119         *  @brief
00120         *    Get a pointer to access the object
00121         *
00122         *  @return
00123         *    Pointer to the object, can be a null pointer
00124         */
00125         AType *operator ->() const;
00126 
00127         /**
00128         *  @brief
00129         *    Cast to a pointer to the object
00130         *
00131         *  @return
00132         *    Pointer to the object, can be a null pointer
00133         */
00134         operator AType*() const;
00135 
00136         /**
00137         *  @brief
00138         *    Check if the pointer is not a null pointer
00139         *
00140         *  @return
00141         *    'true' if the pointer is not a null pointer
00142         */
00143         bool operator !() const;
00144 
00145         /**
00146         *  @brief
00147         *    Check for equality
00148         *
00149         *  @param[in] pPtr
00150         *    Direct pointer to compare with, can be a null pointer
00151         *
00152         *  @return
00153         *    'true' if the two pointers are equal
00154         */
00155         bool operator ==(AType *pPtr) const;
00156 
00157         /**
00158         *  @brief
00159         *    Check for equality
00160         *
00161         *  @param[in] cPtr
00162         *    Smart pointer to compare with
00163         *
00164         *  @return
00165         *    'true' if the two pointers are equal
00166         */
00167         bool operator ==(const SmartPtr<AType> &cPtr) const;
00168 
00169         /**
00170         *  @brief
00171         *    Check for equality
00172         *
00173         *  @param[in] pPtr
00174         *    Direct pointer to compare with, can be a null pointer
00175         *
00176         *  @return
00177         *    'true' if the two pointers are not equal
00178         */
00179         bool operator !=(AType *pPtr) const;
00180 
00181         /**
00182         *  @brief
00183         *    Check for equality
00184         *
00185         *  @param[in] cPtr
00186         *    Smart pointer to compare with
00187         *
00188         *  @return
00189         *    'true' if the two pointers are not equal
00190         */
00191         bool operator !=(const SmartPtr<AType> &cPtr) const;
00192 
00193 
00194     //[-------------------------------------------------------]
00195     //[ Private functions                                     ]
00196     //[-------------------------------------------------------]
00197     private:
00198         /**
00199         *  @brief
00200         *    Assign a pointer to an object that does not implement RefCount
00201         *
00202         *  @param[in] pPtr
00203         *    Pointer to assign, can be a null pointer
00204         */
00205         void SetPtr(void *pPtr);
00206 
00207         /**
00208         *  @brief
00209         *    Assign a pointer to an object that implements RefCount
00210         *
00211         *  @param[in] pPtr
00212         *    Pointer to assign, can be a null pointer
00213         */
00214         void SetPtr(RefCount<AType> *pPtr);
00215 
00216         /**
00217         *  @brief
00218         *    Get pointer to the reference counted object
00219         *
00220         *  @return
00221         *    Pointer to the RefCount object, can be a null pointer
00222         */
00223         RefCount<AType> *GetPtr() const;
00224 
00225 
00226     //[-------------------------------------------------------]
00227     //[ Private data                                          ]
00228     //[-------------------------------------------------------]
00229     private:
00230         RefCount<AType> *m_pPtr; /**< Pointer to reference counter, can be a null pointer */
00231 
00232 
00233 };
00234 
00235 
00236 //[-------------------------------------------------------]
00237 //[ Namespace                                             ]
00238 //[-------------------------------------------------------]
00239 } // PLCore
00240 
00241 
00242 //[-------------------------------------------------------]
00243 //[ Implementation                                        ]
00244 //[-------------------------------------------------------]
00245 #include "PLCore/Core/SmartPtr.inl"
00246 
00247 
00248 #endif // __PLCORE_SMARTPTR_H__


PixelLight PixelLight 0.9.10-R1
Copyright (C) 2002-2011 by The PixelLight Team
Last modified Fri Dec 23 2011 15:51:02
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported