FBX SDK Reference Guide: kscopedptr.h Source File
00001 #ifndef FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KSCOPEDPTR_H
00002 #define FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KSCOPEDPTR_H
00003 
00004 /**************************************************************************************
00005 
00006  Copyright © 2007 Autodesk, Inc. and/or its licensors.
00007  All Rights Reserved.
00008 
00009  The coded instructions, statements, computer programs, and/or related material 
00010  (collectively the "Data") in these files contain unpublished information 
00011  proprietary to Autodesk, Inc. and/or its licensors, which is protected by 
00012  Canada and United States of America federal copyright law and by international 
00013  treaties. 
00014  
00015  The Data may not be disclosed or distributed to third parties, in whole or in
00016  part, without the prior written consent of Autodesk, Inc. ("Autodesk").
00017 
00018  THE DATA IS PROVIDED "AS IS" AND WITHOUT WARRANTY.
00019  ALL WARRANTIES ARE EXPRESSLY EXCLUDED AND DISCLAIMED. AUTODESK MAKES NO
00020  WARRANTY OF ANY KIND WITH RESPECT TO THE DATA, EXPRESS, IMPLIED OR ARISING
00021  BY CUSTOM OR TRADE USAGE, AND DISCLAIMS ANY IMPLIED WARRANTIES OF TITLE, 
00022  NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OR USE. 
00023  WITHOUT LIMITING THE FOREGOING, AUTODESK DOES NOT WARRANT THAT THE OPERATION
00024  OF THE DATA WILL BE UNINTERRUPTED OR ERROR FREE. 
00025  
00026  IN NO EVENT SHALL AUTODESK, ITS AFFILIATES, PARENT COMPANIES, LICENSORS
00027  OR SUPPLIERS ("AUTODESK GROUP") BE LIABLE FOR ANY LOSSES, DAMAGES OR EXPENSES
00028  OF ANY KIND (INCLUDING WITHOUT LIMITATION PUNITIVE OR MULTIPLE DAMAGES OR OTHER
00029  SPECIAL, DIRECT, INDIRECT, EXEMPLARY, INCIDENTAL, LOSS OF PROFITS, REVENUE
00030  OR DATA, COST OF COVER OR CONSEQUENTIAL LOSSES OR DAMAGES OF ANY KIND),
00031  HOWEVER CAUSED, AND REGARDLESS OF THE THEORY OF LIABILITY, WHETHER DERIVED
00032  FROM CONTRACT, TORT (INCLUDING, BUT NOT LIMITED TO, NEGLIGENCE), OR OTHERWISE,
00033  ARISING OUT OF OR RELATING TO THE DATA OR ITS USE OR ANY OTHER PERFORMANCE,
00034  WHETHER OR NOT AUTODESK HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSS
00035  OR DAMAGE. 
00036 
00037 **************************************************************************************/
00038 
00039 // FBX includes
00040 #include "kdebug.h"
00041 
00042 // Begin FBX namespace
00043 #include <fbxfilesdk/fbxfilesdk_nsbegin.h> // namespace
00044 
00045 //
00046 //  KScopedPtr mimics a built-in pointer except that it guarantees deletion
00047 //  of the object pointed to, either on destruction of the KScopedPtr or via
00048 //  an explicit Reset()
00049 //
00050 
00051 //
00052 // Deletion policy dictates the way the pointer is destroyed
00053 // By default, KScopedPtr uses the DefaultDeletionPolicy
00054 //
00055 template<class T>
00056 class DefaultDeletionPolicy
00057 {
00058 public:
00059     static inline void DeleteIt(T** ptr)
00060     {
00061         if ( *ptr != NULL )
00062         {
00063             delete *ptr;
00064             *ptr = NULL;
00065         }
00066     }
00067 };
00068 
00069 template<class T>
00070 class FreeDeletionPolicy
00071 {
00072 public:
00073     static inline void DeleteIt(T** ptr)
00074     {
00075         if ( *ptr != NULL )
00076         {
00077             free( *ptr );
00078             *ptr = NULL;
00079         }
00080     }
00081 };
00082 
00083 //---------------------------------------------------------------------
00084 template<class T, class DeletionPolicyT = DefaultDeletionPolicy<T> >
00085 class KScopedPtr
00086 {
00087 private:
00088     T* ptr;
00089 
00090     // Non copyable object
00091     KScopedPtr(KScopedPtr const &);
00092     KScopedPtr& operator=(KScopedPtr const &);
00093 
00094     typedef KScopedPtr<T, DeletionPolicyT> ThisType;
00095     typedef DeletionPolicyT DeletionPolicy;
00096 
00097 public:
00099     explicit KScopedPtr(T* p = 0): ptr(p){}
00100 
00102     ~KScopedPtr()
00103     {
00104         DeletionPolicy::DeleteIt(&ptr);
00105     }
00106 
00108     inline void Reset(T* p = 0)
00109     {
00110         K_ASSERT(p == 0 || p != ptr); // catch self-reset errors
00111         ThisType(p).Swap(*this);
00112     }
00113 
00115     inline T & operator*() const
00116     {
00117         K_ASSERT(ptr != 0);
00118         return *ptr;
00119     }
00120 
00122     inline T* operator->() const
00123     {
00124         K_ASSERT(ptr != 0);
00125         return ptr;
00126     }
00127 
00129     inline T* Get() const
00130     {
00131         return ptr;
00132     }
00133 
00134     inline operator T* () const
00135     {
00136         return ptr;
00137     }
00138 
00140     // Implicit conversion to "bool"
00141     operator bool () const
00142     {
00143         return ptr != 0;
00144     }
00145 
00147     bool operator! () const
00148     {
00149         return ptr == 0;
00150     }
00151 
00153     void Swap(KScopedPtr & b)
00154     {
00155         T * tmp = b.ptr;
00156         b.ptr = ptr;
00157         ptr = tmp;
00158     }
00159 
00161     T* Release()
00162     {
00163         T* tmp = ptr;
00164         ptr = NULL;
00165 
00166         return tmp;
00167     }
00168 };
00169 
00170 //----------------------------------------
00171 //
00172 // Deletion policy dictates the way the pointer is destroyed
00173 // The FBXObjectDeletionPolicy, dictate the way we destroy
00174 // KFbxObject. This policy is used by KFBXObjectScopedPtr
00175 //
00176 template <class FBXObjectT>
00177 class FBXObjectDeletionPolicy
00178 {
00179 public:
00180     static inline void DeleteIt(FBXObjectT** ptr)
00181     {
00182         if (*ptr != NULL)
00183         {
00184             (*ptr)->Destroy();
00185             *ptr = NULL;
00186         }
00187     }
00188 };
00189 
00190 //---------------------------------
00191 template <class FBXObjectT>
00192 class KFBXObjectScopedPtr: public KScopedPtr<FBXObjectT, FBXObjectDeletionPolicy<FBXObjectT> >
00193 {
00194 public:
00195     explicit KFBXObjectScopedPtr(FBXObjectT* p = 0):KScopedPtr<FBXObjectT, FBXObjectDeletionPolicy<FBXObjectT> >(p){}
00196 };
00197 
00198 // End FBX namespace
00199 #include <fbxfilesdk/fbxfilesdk_nsend.h>
00200 
00201 #endif // FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KSCOPEDPTR_H
00202