00001 00004 #ifndef FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KSCOPEDPTR_H 00005 #define FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KSCOPEDPTR_H 00006 00007 /************************************************************************************** 00008 00009 Copyright (C) 2001 - 2009 Autodesk, Inc. and/or its licensors. 00010 All Rights Reserved. 00011 00012 The coded instructions, statements, computer programs, and/or related material 00013 (collectively the "Data") in these files contain unpublished information 00014 proprietary to Autodesk, Inc. and/or its licensors, which is protected by 00015 Canada and United States of America federal copyright law and by international 00016 treaties. 00017 00018 The Data may not be disclosed or distributed to third parties, in whole or in 00019 part, without the prior written consent of Autodesk, Inc. ("Autodesk"). 00020 00021 THE DATA IS PROVIDED "AS IS" AND WITHOUT WARRANTY. 00022 ALL WARRANTIES ARE EXPRESSLY EXCLUDED AND DISCLAIMED. AUTODESK MAKES NO 00023 WARRANTY OF ANY KIND WITH RESPECT TO THE DATA, EXPRESS, IMPLIED OR ARISING 00024 BY CUSTOM OR TRADE USAGE, AND DISCLAIMS ANY IMPLIED WARRANTIES OF TITLE, 00025 NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OR USE. 00026 WITHOUT LIMITING THE FOREGOING, AUTODESK DOES NOT WARRANT THAT THE OPERATION 00027 OF THE DATA WILL BE UNINTERRUPTED OR ERROR FREE. 00028 00029 IN NO EVENT SHALL AUTODESK, ITS AFFILIATES, PARENT COMPANIES, LICENSORS 00030 OR SUPPLIERS ("AUTODESK GROUP") BE LIABLE FOR ANY LOSSES, DAMAGES OR EXPENSES 00031 OF ANY KIND (INCLUDING WITHOUT LIMITATION PUNITIVE OR MULTIPLE DAMAGES OR OTHER 00032 SPECIAL, DIRECT, INDIRECT, EXEMPLARY, INCIDENTAL, LOSS OF PROFITS, REVENUE 00033 OR DATA, COST OF COVER OR CONSEQUENTIAL LOSSES OR DAMAGES OF ANY KIND), 00034 HOWEVER CAUSED, AND REGARDLESS OF THE THEORY OF LIABILITY, WHETHER DERIVED 00035 FROM CONTRACT, TORT (INCLUDING, BUT NOT LIMITED TO, NEGLIGENCE), OR OTHERWISE, 00036 ARISING OUT OF OR RELATING TO THE DATA OR ITS USE OR ANY OTHER PERFORMANCE, 00037 WHETHER OR NOT AUTODESK HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSS 00038 OR DAMAGE. 00039 00040 **************************************************************************************/ 00041 00042 // FBX includes 00043 #include "kdebug.h" 00044 00045 // Begin FBX namespace 00046 #include <fbxfilesdk/fbxfilesdk_nsbegin.h> // namespace 00047 00048 // 00049 // KScopedPtr mimics a built-in pointer except that it guarantees deletion 00050 // of the object pointed to, either on destruction of the KScopedPtr or via 00051 // an explicit Reset() 00052 // 00053 00054 // 00055 // Deletion policy dictates the way the pointer is destroyed 00056 // By default, KScopedPtr uses the DefaultDeletionPolicy 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 FbxSdkFree( *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 // Non copyable object 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); // catch self-reset errors 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 // Implicit conversion to "bool" 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 // Deletion policy dictates the way the pointer is destroyed 00176 // The FBXObjectDeletionPolicy, dictate the way we destroy 00177 // KFbxObject. This policy is used by KFBXObjectScopedPtr 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 template <class FBXObjectT> 00194 class KFBXObjectScopedPtr: public KScopedPtr<FBXObjectT, FBXObjectDeletionPolicy<FBXObjectT> > 00195 { 00196 public: 00197 explicit KFBXObjectScopedPtr(FBXObjectT* p = 0):KScopedPtr<FBXObjectT, FBXObjectDeletionPolicy<FBXObjectT> >(p){} 00198 }; 00199 00200 //--------------------------------- 00201 00202 00203 template <class T> 00204 class FbxSdkDeletionPolicy 00205 { 00206 public: 00207 static inline void DeleteIt(T** ptr) 00208 { 00209 if (*ptr != NULL) 00210 { 00211 FbxSdkDelete(*ptr); 00212 *ptr = NULL; 00213 } 00214 } 00215 }; 00216 00217 //--------------------------------- 00218 template <class T> 00219 class KFbxSdkScopedPtr: public KScopedPtr<T, FbxSdkDeletionPolicy<T> > 00220 { 00221 public: 00222 explicit KFbxSdkScopedPtr(T* p = 0):KScopedPtr<T, FbxSdkDeletionPolicy<T> >(p){} 00223 }; 00224 00225 // End FBX namespace 00226 #include <fbxfilesdk/fbxfilesdk_nsend.h> 00227 00228 #endif // FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KSCOPEDPTR_H 00229