photosgallery/slideshow/utils/shwautoptr.h
changeset 0 4e91876724a2
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:    Thin callback wrapper
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // include guard for the whole file content
       
    22 #ifndef __SHWAUTOPTR_H__
       
    23 #define __SHWAUTOPTR_H__
       
    24 
       
    25 /**
       
    26  * TShwAutoPtr, implementation of auto_ptr
       
    27  */
       
    28 template< class T >
       
    29 class TShwAutoPtr
       
    30 	{
       
    31 	public:
       
    32 
       
    33 		/**
       
    34 		 * Default constructor
       
    35 		 */
       
    36 		TShwAutoPtr()
       
    37 			: iPtr( NULL )
       
    38 			{}
       
    39 	
       
    40 		/**
       
    41 		 * Constructor
       
    42 		 * @param aPtr the pointer to own
       
    43 		 */
       
    44 		TShwAutoPtr( T* aPtr = 0 )
       
    45 			: iPtr( aPtr )
       
    46 			{}
       
    47 
       
    48 		/**
       
    49 		 * Copy constructor, transfers ownership
       
    50 		 * @param aPtr the pointer to transfer
       
    51 		 */
       
    52 		TShwAutoPtr( TShwAutoPtr< T >& aPtr )
       
    53 			: iPtr( aPtr.Release() )
       
    54 			{}
       
    55 
       
    56 		/**
       
    57 		 * Destructor, releases the pointer
       
    58 		 */
       
    59 		~TShwAutoPtr()
       
    60 			{
       
    61 			delete iPtr;
       
    62 			}
       
    63 
       
    64 		/**
       
    65 		 * Dereference
       
    66 		 */
       
    67 		T& operator*() const
       
    68 			{ 
       
    69 			return *iPtr;
       
    70 			}
       
    71 
       
    72 		/**
       
    73 		 * Take address of pointed class
       
    74 		 */
       
    75 		T* operator &()
       
    76 			{ 
       
    77 			return iPtr;
       
    78 			}
       
    79 		
       
    80 		/**
       
    81 		 * Dereference
       
    82 		 */
       
    83 		T* operator->() const 
       
    84 			{
       
    85 			return iPtr;
       
    86 			}
       
    87 
       
    88 		/**
       
    89 		 * Gives the pointer to the caller, ownership stays
       
    90 		 * @return the pointer held, ownership does not transfer
       
    91 		 */
       
    92 		T* Pointer() const
       
    93 			{
       
    94 			return iPtr;
       
    95 			}
       
    96 
       
    97 		/**
       
    98 		 * Assignment from one pointer to another, ownership transfers
       
    99 		 * @param the auto pointer to hold
       
   100 		 * @return reference to this so that assignments can be chained
       
   101 		 */
       
   102 		TShwAutoPtr<T>& operator=( TShwAutoPtr<T>& aCopy )
       
   103 			{
       
   104 			if( this != &aCopy )
       
   105 				{
       
   106 				// transfer the pointer
       
   107 				SetPointer( aCopy.Release() );
       
   108 				}
       
   109 			return *this; 
       
   110 			}
       
   111 
       
   112 		/**
       
   113 		 * Sets the pointer, ownership is taken
       
   114 		 * @param the pointer to hold
       
   115 		 */
       
   116 		void SetPointer( T* aPointer )
       
   117 			{
       
   118 			// release the old (possible) pointer
       
   119 			delete iPtr;
       
   120 			iPtr = aPointer;
       
   121 			}
       
   122 
       
   123 		/**
       
   124 		 * Release, returns the ownership to the caller
       
   125 		 * @return the pointer held, ownership transfers
       
   126 		 */
       
   127 		T* Release()
       
   128 			{ 
       
   129 			T* temp = iPtr;
       
   130 			iPtr = 0;
       
   131 			return temp;
       
   132 			}
       
   133 
       
   134 	private:
       
   135 		
       
   136 		/// Own: the pointer to hold
       
   137 		T* iPtr;
       
   138 
       
   139 	};
       
   140 
       
   141 #endif // __SHWAUTOPTR_H__