symbian_auto_ptr.hpp

Go to the documentation of this file.
00001 //
00002 // Copyright (c) 2007-2009 Google Inc.
00003 // Copyright (c) 2006-2007 Jaiku Ltd.
00004 // Copyright (c) 2002-2006 Mika Raento and Renaud Petit
00005 //
00006 // This software is licensed at your choice under either 1 or 2 below.
00007 //
00008 // 1. MIT License
00009 //
00010 // Permission is hereby granted, free of charge, to any person obtaining a copy
00011 // of this software and associated documentation files (the "Software"), to deal
00012 // in the Software without restriction, including without limitation the rights
00013 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00014 // copies of the Software, and to permit persons to whom the Software is
00015 // furnished to do so, subject to the following conditions:
00016 //
00017 // The above copyright notice and this permission notice shall be included in
00018 // all copies or substantial portions of the Software.
00019 //
00020 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00021 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00022 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00023 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00024 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00025 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00026 // THE SOFTWARE.
00027 //
00028 // 2. Gnu General Public license 2.0
00029 //
00030 // This program is free software; you can redistribute it and/or
00031 // modify it under the terms of the GNU General Public License
00032 // as published by the Free Software Foundation; either version 2
00033 // of the License, or (at your option) any later version.
00034 //
00035 // This program is distributed in the hope that it will be useful,
00036 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00037 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00038 // GNU General Public License for more details.
00039 //
00040 // You should have received a copy of the GNU General Public License
00041 // along with this program; if not, write to the Free Software
00042 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00043 //
00044 
00045 #ifndef __E_AUTO_PTR_H__
00046 #define __E_AUTO_PTR_H__  
00047 
00048 #ifndef __E32BASE_H__
00049 #include <e32base.h>
00050 #endif
00051 
00052 void CloseCBaseIndirect(TAny* aPtr);
00053 void CloseHBufC16Indirect(TAny* aPtr);
00054 void CloseHBufC8Indirect(TAny* aPtr);
00055 
00056 template<class Y> struct e_auto_ptr_ref
00057 {
00058   Y* iPtr;
00059   e_auto_ptr_ref(Y* aPtr) : iPtr(aPtr) {}
00060 };
00061 
00062 template<class X> class e_auto_ptr
00063 {
00064 public:  
00065   typedef X element_type;
00066 
00067   e_auto_ptr(X* aPtr = 0): iBasePtr(aPtr)
00068   {
00069 #ifndef __LEAVE_EQUALS_THROW__
00070     CleanupStack::PushL(TCleanupItem(CloseCBaseIndirect, (void*)&iBasePtr));
00071 #endif
00072   }  
00073   
00074   e_auto_ptr(e_auto_ptr& aPtr): iBasePtr(aPtr.release())
00075   {
00076 #ifndef __LEAVE_EQUALS_THROW__
00077     CleanupStack::PushL(TCleanupItem(CloseCBaseIndirect, (void*)&iBasePtr));
00078 #endif
00079   }
00080 
00081   e_auto_ptr<X>& operator=(e_auto_ptr<X>& aRhs)
00082   {
00083     if (&aRhs != this)
00084     {
00085       delete iBasePtr;
00086       iBasePtr = aRhs.release();
00087     }
00088     return (*this); 
00089   }
00090 
00091   ~e_auto_ptr() 
00092   { 
00093 #ifndef __LEAVE_EQUALS_THROW__
00094     CleanupStack::Pop();
00095 #endif
00096     delete iBasePtr;
00097   }
00098 
00099   X& operator *() const { return *(X*)iBasePtr; }
00100   X* operator ->() const { return (X*)iBasePtr; }
00101    
00102   X* get() const { return (X*)iBasePtr; }
00103 
00104   X* release()
00105   { 
00106     X* result = (X*)iBasePtr;
00107     iBasePtr = 0;
00108     return result; 
00109   }
00110 
00111   void reset(X* aPtr = 0) {
00112     if ( aPtr != iBasePtr) {
00113       delete iBasePtr;
00114       iBasePtr = aPtr;
00115     }
00116   }
00117 
00118 
00119   e_auto_ptr(e_auto_ptr_ref<X> aRef): iBasePtr(aRef.iBasePtr)
00120   {
00121 #ifndef __LEAVE_EQUALS_THROW__
00122     CleanupStack::PushL(TCleanupItem(CloseCBaseIndirect, (void*)&iBasePtr));
00123 #endif
00124   }
00125 
00126 
00127   template <class Y> operator e_auto_ptr_ref<Y>() 
00128     { return e_auto_ptr_ref<Y>(this->release()); }
00129 
00130 private:
00131   CBase* iBasePtr;
00132 };
00133 
00134 template<> class e_auto_ptr<HBufC16>
00135 {
00136 public:  
00137 
00138   e_auto_ptr(HBufC16* aPtr = 0): iPtr(aPtr)
00139   {
00140 #ifndef __LEAVE_EQUALS_THROW__
00141     CleanupStack::PushL(TCleanupItem(CloseHBufC16Indirect, (void*)&iPtr));
00142 #endif
00143   }  
00144   
00145   e_auto_ptr(e_auto_ptr& aPtr): iPtr(aPtr.release())
00146   {
00147 #ifndef __LEAVE_EQUALS_THROW__
00148     CleanupStack::PushL(TCleanupItem(CloseHBufC16Indirect, (void*)&iPtr));
00149 #endif
00150   }
00151 
00152   e_auto_ptr<HBufC16>& operator=(e_auto_ptr<HBufC16>& aRhs)
00153   {
00154     if (&aRhs != this)
00155     {
00156       delete iPtr;
00157       iPtr = iPtr = aRhs.release();
00158     }
00159     return (*this); 
00160   }
00161 
00162   ~e_auto_ptr() 
00163   { 
00164 #ifndef __LEAVE_EQUALS_THROW__
00165     CleanupStack::Pop();
00166 #endif
00167     delete iPtr;
00168   }
00169 
00170   const TDesC16& operator *() const { return *iPtr; }
00171   HBufC16* operator ->() const { return iPtr; }
00172    
00173   HBufC16* get() const { return iPtr; }
00174 
00175   HBufC16* release()
00176   { 
00177     HBufC16* result = iPtr;
00178     iPtr = 0;
00179   return result; 
00180   }
00181 
00182   void reset(HBufC16* aPtr = 0) {
00183     if (aPtr != iPtr) {
00184       delete iPtr;
00185       iPtr = aPtr;
00186     }
00187   }
00188 
00189 
00190   e_auto_ptr(e_auto_ptr_ref<HBufC16> aRef): iPtr(aRef.iPtr)
00191   {
00192 #ifndef __LEAVE_EQUALS_THROW__
00193     CleanupStack::PushL(TCleanupItem(CloseHBufC16Indirect, (void*)&iPtr));
00194 #endif
00195   }
00196 
00197 
00198   template <class Y> operator e_auto_ptr_ref<Y>() 
00199     { return e_auto_ptr_ref<Y>(this->release()); }
00200   
00201 private:
00202   HBufC16* iPtr;  
00203 };
00204 
00205 template<> class e_auto_ptr<HBufC8>
00206 {
00207 public:  
00208 
00209   e_auto_ptr(HBufC8* aPtr = 0): iPtr(aPtr)
00210   {
00211 #ifndef __LEAVE_EQUALS_THROW__
00212     CleanupStack::PushL(TCleanupItem(CloseHBufC8Indirect, (void*)&iPtr));
00213 #endif
00214   }  
00215   
00216   e_auto_ptr(e_auto_ptr& aPtr): iPtr(aPtr.release())
00217   {
00218 #ifndef __LEAVE_EQUALS_THROW__
00219     CleanupStack::PushL(TCleanupItem(CloseHBufC8Indirect, (void*)&iPtr));
00220 #endif
00221   }
00222 
00223   e_auto_ptr<HBufC8>& operator=(e_auto_ptr<HBufC8>& aRhs)
00224   {
00225     if (&aRhs != this)
00226     {
00227       delete iPtr;
00228       iPtr = iPtr = aRhs.release();
00229     }
00230     return (*this); 
00231   }
00232 
00233   ~e_auto_ptr() 
00234   { 
00235 #ifndef __LEAVE_EQUALS_THROW__
00236     CleanupStack::Pop();
00237 #endif
00238     delete iPtr;
00239   }
00240 
00241   const TDesC8& operator *() const { return *iPtr; }
00242   HBufC8* operator ->() const { return iPtr; }
00243    
00244   HBufC8* get() const { return iPtr; }
00245 
00246   HBufC8* release()
00247   { 
00248     HBufC8* result = iPtr;
00249     iPtr = 0;
00250   return result; 
00251   }
00252 
00253   void reset(HBufC8* aPtr = 0) {
00254     if (aPtr != iPtr) {
00255       delete iPtr;
00256       iPtr = aPtr;
00257     }
00258   }
00259 
00260 
00261   e_auto_ptr(e_auto_ptr_ref<HBufC8> aRef): iPtr(aRef.iPtr)
00262   {
00263 #ifndef __LEAVE_EQUALS_THROW__
00264     CleanupStack::PushL(TCleanupItem(CloseHBufC8Indirect, (void*)&iPtr));
00265 #endif
00266   }
00267 
00268 
00269   template <class Y> operator e_auto_ptr_ref<Y>() 
00270     { return e_auto_ptr_ref<Y>(this->release()); }
00271   
00272 private:
00273   HBufC8* iPtr;  
00274 };
00275 
00276 #endif // __auto_set_to_zero_H__

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:56 2011 by Doxygen 1.6.1