Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXAutoPtr.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                    A u t o m a t i c   P o i n t e r                          *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2007,2009 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (at your option) any later version.                                           *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 *********************************************************************************
00021 * $Id: FXAutoPtr.h,v 1.15 2009/01/06 13:07:21 fox Exp $                         *
00022 ********************************************************************************/
00023 #ifndef FXAUTOPTR_H
00024 #define FXAUTOPTR_H
00025 
00026 namespace FX {
00027 
00028 
00029 /// Automatic pointer
00030 template<class EType> class FXAutoPtr {
00031 private:
00032   EType* ptr;
00033 public:
00034 
00035   /// Construct with optional pointer
00036   FXAutoPtr(EType* p=NULL):ptr(p){ }
00037 
00038   /// Copy constructor from an automatic pointer with compatible type
00039   template<class T> FXAutoPtr(FXAutoPtr<T>& orig):ptr(orig.release()){ }
00040 
00041   /// Assign from pointer
00042   FXAutoPtr& operator=(EType *p){ ptr=p; return *this; }
00043 
00044   /// Assign from an automatic pointer with compatible type
00045   template<class T> FXAutoPtr& operator=(FXAutoPtr<T>& orig){ reset(orig.release()); return *this; }
00046 
00047   /// Conversion operators
00048   operator EType*() const { return ptr; }
00049 
00050   /// Dereference operator
00051   EType& operator*() const { return *ptr; }
00052 
00053   /// Follow pointer operator
00054   EType* operator->() const { return ptr; }
00055 
00056   /// Release hold on the pointer
00057   EType* release(){ EType* tmp=ptr; ptr=NULL; return tmp; }
00058 
00059   /// Delete old object, replace by new, if any
00060   void reset(EType* p=NULL){ if(p!=ptr){ delete ptr; ptr=p; } }
00061 
00062   /// Destruction deletes pointer
00063   ~FXAutoPtr(){ delete ptr; }
00064   };
00065 
00066 }
00067 
00068 #endif
00069 

Copyright © 1997-2009 Jeroen van der Zijp