00001 
00004 #ifndef FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KSCOPEDPTR_H
00005 #define FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KSCOPEDPTR_H
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 #include "kdebug.h"
00044 
00045 
00046 #include <fbxfilesdk/fbxfilesdk_nsbegin.h> 
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 
00056 
00057 
00058 template<class T>
00059 class DefaultDeletionPolicy
00060 {
00061 public:
00062     static inline void DeleteIt(T** ptr)
00063     {
00064         if ( *ptr != NULL )
00065         {
00066             delete *ptr;
00067             *ptr = NULL;
00068         }
00069     }
00070 };
00071 
00072 template<class T>
00073 class FreeDeletionPolicy
00074 {
00075 public:
00076     static inline void DeleteIt(T** ptr)
00077     {
00078         if ( *ptr != NULL )
00079         {
00080             free( *ptr );
00081             *ptr = NULL;
00082         }
00083     }
00084 };
00085 
00086 
00087 template<class T, class DeletionPolicyT = DefaultDeletionPolicy<T> >
00088 class KScopedPtr
00089 {
00090 private:
00091     T* ptr;
00092 
00093     
00094     KScopedPtr(KScopedPtr const &);
00095     KScopedPtr& operator=(KScopedPtr const &);
00096 
00097     typedef KScopedPtr<T, DeletionPolicyT> ThisType;
00098     typedef DeletionPolicyT DeletionPolicy;
00099 
00100 public:
00102     explicit KScopedPtr(T* p = 0): ptr(p){}
00103 
00105     ~KScopedPtr()
00106     {
00107         DeletionPolicy::DeleteIt(&ptr);
00108     }
00109 
00111     inline void Reset(T* p = 0)
00112     {
00113         K_ASSERT(p == 0 || p != ptr); 
00114         ThisType(p).Swap(*this);
00115     }
00116 
00118     inline T & operator*() const
00119     {
00120         K_ASSERT(ptr != 0);
00121         return *ptr;
00122     }
00123 
00125     inline T* operator->() const
00126     {
00127         K_ASSERT(ptr != 0);
00128         return ptr;
00129     }
00130 
00132     inline T* Get() const
00133     {
00134         return ptr;
00135     }
00136 
00137     inline operator T* () const
00138     {
00139         return ptr;
00140     }
00141 
00143     
00144     operator bool () const
00145     {
00146         return ptr != 0;
00147     }
00148 
00150     bool operator! () const
00151     {
00152         return ptr == 0;
00153     }
00154 
00156     void Swap(KScopedPtr & b)
00157     {
00158         T * tmp = b.ptr;
00159         b.ptr = ptr;
00160         ptr = tmp;
00161     }
00162 
00164     T* Release()
00165     {
00166         T* tmp = ptr;
00167         ptr = NULL;
00168 
00169         return tmp;
00170     }
00171 };
00172 
00173 
00174 
00175 
00176 
00177 
00178 
00179 template <class FBXObjectT>
00180 class FBXObjectDeletionPolicy
00181 {
00182 public:
00183     static inline void DeleteIt(FBXObjectT** ptr)
00184     {
00185         if (*ptr != NULL)
00186         {
00187             (*ptr)->Destroy();
00188             *ptr = NULL;
00189         }
00190     }
00191 };
00192 
00193 
00194 template <class FBXObjectT>
00195 class KFBXObjectScopedPtr: public KScopedPtr<FBXObjectT, FBXObjectDeletionPolicy<FBXObjectT> >
00196 {
00197 public:
00198     explicit KFBXObjectScopedPtr(FBXObjectT* p = 0):KScopedPtr<FBXObjectT, FBXObjectDeletionPolicy<FBXObjectT> >(p){}
00199 };
00200 
00201 
00202 #include <fbxfilesdk/fbxfilesdk_nsend.h>
00203 
00204 #endif // FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KSCOPEDPTR_H
00205