FBX SDK Reference Guide: kscopedptr.h Source File
00001 #ifndef _FBXSDK_SCOPED_PTR_HPP_
00002 #define _FBXSDK_SCOPED_PTR_HPP_
00003 // FBX includes
00004 #include "kdebug.h"
00005 
00006 // Begin FBX namespace
00007 #include <kbaselib_nsbegin.h> // namespace
00008 
00009 //
00010 //  KScopedPtr mimics a built-in pointer except that it guarantees deletion
00011 //  of the object pointed to, either on destruction of the KScopedPtr or via
00012 //  an explicit Reset()
00013 //
00014 
00015 //
00016 // Deletion policy dictates the way the pointer is destroyed
00017 // By default, KScopedPtr uses the DefaultDeletionPolicy
00018 //
00019 template<class T>
00020 class DefaultDeletionPolicy
00021 {
00022 public:
00023     static inline void DeleteIt(T** ptr)
00024     {
00025         if ( *ptr != NULL )
00026         {
00027             delete *ptr;
00028             *ptr = NULL;
00029         }
00030     }
00031 };
00032 
00033 template<class T>
00034 class FreeDeletionPolicy
00035 {
00036 public:
00037     static inline void DeleteIt(T** ptr)
00038     {
00039         if ( *ptr != NULL )
00040         {
00041             free( *ptr );
00042             *ptr = NULL;
00043         }
00044     }
00045 };
00046 
00047 //---------------------------------------------------------------------
00048 template<class T, class DeletionPolicyT = DefaultDeletionPolicy<T> >
00049 class KScopedPtr
00050 {
00051 private:
00052     T* ptr;
00053 
00054     // Non copyable object
00055     KScopedPtr(KScopedPtr const &);
00056     KScopedPtr& operator=(KScopedPtr const &);
00057 
00058     typedef KScopedPtr<T, DeletionPolicyT> ThisType;
00059     typedef DeletionPolicyT DeletionPolicy;
00060 
00061 public:
00063     explicit KScopedPtr(T* p = 0): ptr(p){}
00064 
00066     ~KScopedPtr()
00067     {
00068         DeletionPolicy::DeleteIt(&ptr);
00069     }
00070 
00072     inline void Reset(T* p = 0)
00073     {
00074         K_ASSERT(p == 0 || p != ptr); // catch self-reset errors
00075         ThisType(p).Swap(*this);
00076     }
00077 
00079     inline T & operator*() const
00080     {
00081         K_ASSERT(ptr != 0);
00082         return *ptr;
00083     }
00084 
00086     inline T* operator->() const
00087     {
00088         K_ASSERT(ptr != 0);
00089         return ptr;
00090     }
00091 
00093     inline T* Get() const
00094     {
00095         return ptr;
00096     }
00097 
00098     inline operator T* () const
00099     {
00100         return ptr;
00101     }
00102 
00104     // Implicit conversion to "bool"
00105     operator bool () const
00106     {
00107         return ptr != 0;
00108     }
00109 
00111     bool operator! () const
00112     {
00113         return ptr == 0;
00114     }
00115 
00117     void Swap(KScopedPtr & b)
00118     {
00119         T * tmp = b.ptr;
00120         b.ptr = ptr;
00121         ptr = tmp;
00122     }
00123 
00125     T* Release()
00126     {
00127         T* tmp = ptr;
00128         ptr = NULL;
00129 
00130         return tmp;
00131     }
00132 };
00133 
00134 //----------------------------------------
00135 //
00136 // Deletion policy dictates the way the pointer is destroyed
00137 // The FBXObjectDeletionPolicy, dictate the way we destroy
00138 // KFbxObject. This policy is used by KFBXObjectScopedPtr
00139 //
00140 template <class FBXObjectT>
00141 class FBXObjectDeletionPolicy
00142 {
00143 public:
00144     static inline void DeleteIt(FBXObjectT** ptr)
00145     {
00146         if (*ptr != NULL)
00147         {
00148             (*ptr)->Destroy();
00149             *ptr = NULL;
00150         }
00151     }
00152 };
00153 
00154 //---------------------------------
00155 template <class FBXObjectT>
00156 class KFBXObjectScopedPtr: public KScopedPtr<FBXObjectT, FBXObjectDeletionPolicy<FBXObjectT> >
00157 {
00158 public:
00159     explicit KFBXObjectScopedPtr(FBXObjectT* p = 0):KScopedPtr<FBXObjectT, FBXObjectDeletionPolicy<FBXObjectT> >(p){}
00160 };
00161 
00162 // End FBX namespace
00163 #include <kbaselib_nsend.h>
00164 
00165 #endif