fbxscopedptr.h

Go to the documentation of this file.
00001 /****************************************************************************************
00002  
00003    Copyright (C) 2012 Autodesk, Inc.
00004    All rights reserved.
00005  
00006    Use of this software is subject to the terms of the Autodesk license agreement
00007    provided at the time of installation or download, or which otherwise accompanies
00008    this software in either electronic or hard copy form.
00009  
00010 ****************************************************************************************/
00011 
00013 #ifndef _FBXSDK_CORE_BASE_SCOPED_PTR_H_
00014 #define _FBXSDK_CORE_BASE_SCOPED_PTR_H_
00015 
00016 #include <fbxsdk/fbxsdk_def.h>
00017 
00018 #include <fbxsdk/fbxsdk_nsbegin.h>
00019 
00024 template<class T> class FbxDefaultDeletionPolicy
00025 {
00026 public:
00027     static inline void DeleteIt(T** ptr)
00028     {
00029         if ( *ptr != NULL )
00030         {
00031             delete *ptr;
00032             *ptr = NULL;
00033         }
00034     }
00035 };
00036 
00040 template<class T> class FbxFreeDeletionPolicy
00041 {
00042 public:
00043     static inline void DeleteIt(T** ptr)
00044     {
00045         if ( *ptr != NULL )
00046         {
00047             FbxFree( *ptr );
00048             *ptr = NULL;
00049         }
00050     }
00051 };
00052 
00057 template<class T, class DeletionPolicyT = FbxDefaultDeletionPolicy<T> > class FbxScopedPtr
00058 {
00059 private:
00060     T* ptr;
00061 
00062     // Non copyable object
00063     FbxScopedPtr(const FbxScopedPtr&);
00064     FbxScopedPtr& operator=(const FbxScopedPtr&);
00065 
00066     typedef FbxScopedPtr<T, DeletionPolicyT> ThisType;
00067     typedef DeletionPolicyT DeletionPolicy;
00068 
00069 public:
00071     explicit FbxScopedPtr(T* p = 0): ptr(p){}
00072 
00074     ~FbxScopedPtr()
00075     {
00076         DeletionPolicy::DeleteIt(&ptr);
00077     }
00078 
00080     inline void Reset(T* p = 0)
00081     {
00082         FBX_ASSERT(p == 0 || p != ptr); // catch self-reset errors
00083         ThisType(p).Swap(*this);
00084     }
00085 
00087     inline T& operator*() const
00088     {
00089         FBX_ASSERT(ptr != 0);
00090         return *ptr;
00091     }
00092 
00094     inline T* operator->() const
00095     {
00096         FBX_ASSERT(ptr != 0);
00097         return ptr;
00098     }
00099 
00101     inline T* Get() const
00102     {
00103         return ptr;
00104     }
00105 
00107     inline operator T* () const
00108     {
00109         return ptr;
00110     }
00111 
00113     operator bool () const
00114     {
00115         return ptr != 0;
00116     }
00117 
00119     bool operator! () const
00120     {
00121         return ptr == 0;
00122     }
00123 
00125     void Swap(FbxScopedPtr & b)
00126     {
00127         T * tmp = b.ptr;
00128         b.ptr = ptr;
00129         ptr = tmp;
00130     }
00131 
00133     T* Release()
00134     {
00135         T* tmp = ptr;
00136         ptr = NULL;
00137 
00138         return tmp;
00139     }
00140 };
00141 
00146 template <class FBXObjectT> class FbxObjectDeletionPolicy
00147 {
00148 public:
00149     static inline void DeleteIt(FBXObjectT** ptr)
00150     {
00151         if (*ptr != NULL)
00152         {
00153             (*ptr)->Destroy();
00154             *ptr = NULL;
00155         }
00156     }
00157 };
00158 
00160 template <class FBXObjectT> class FbxObjectScopedPtr: public FbxScopedPtr<FBXObjectT, FbxObjectDeletionPolicy<FBXObjectT> >
00161 {
00162 public:
00163     explicit FbxObjectScopedPtr(FBXObjectT* p = 0):FbxScopedPtr<FBXObjectT, FbxObjectDeletionPolicy<FBXObjectT> >(p){}
00164 };
00165 
00169 template <class T> class FbxDeletionPolicy
00170 {
00171 public:
00172     static inline void DeleteIt(T** ptr)
00173     {
00174         if (*ptr != NULL)
00175         {
00176             FbxDelete(*ptr);
00177             *ptr = NULL;
00178         }
00179     }
00180 };
00181 
00187 template <class T> class FbxDefaultScopedPtr: public FbxScopedPtr<T, FbxDeletionPolicy<T> >
00188 {
00189 public:
00190     explicit FbxDefaultScopedPtr(T* p = 0):FbxScopedPtr<T, FbxDeletionPolicy<T> >(p){}
00191 };
00192 
00193 #include <fbxsdk/fbxsdk_nsend.h>
00194 
00195 #endif /* _FBXSDK_CORE_BASE_SCOPED_PTR_H_ */