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,2010 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 #ifndef FXAUTOPTR_H
00022 #define FXAUTOPTR_H
00023 
00024 namespace FX {
00025 
00026 
00027 /// Automatic pointer
00028 template<class EType> class FXAutoPtr {
00029 private:
00030   EType* ptr;
00031 public:
00032 
00033   /// Construct from optional pointer
00034   FXAutoPtr(EType* src=NULL):ptr(src){ }
00035 
00036   /// Construct from another automatic pointer
00037   FXAutoPtr(FXAutoPtr& src):ptr(src.release()){ }
00038 
00039   /// Construct from another automatic pointer of compatible type
00040   template<class T> FXAutoPtr(FXAutoPtr<T>& src):ptr(src.release()){ }
00041 
00042   /// Assign from pointer
00043   FXAutoPtr& operator=(EType *src){ ptr=src; return *this; }
00044 
00045   /// Assign from an another automatic pointer
00046   FXAutoPtr& operator=(FXAutoPtr& src){ return reset(src.release()); }
00047 
00048   /// Assign from an automatic pointer with compatible type
00049   template<class T> FXAutoPtr& operator=(FXAutoPtr<T>& src){ return reset(src.release()); }
00050 
00051   /// Convert to true/false
00052   operator FXbool() const { return !!ptr; }
00053 
00054   /// Conversion operators
00055   operator EType*() const { return ptr; }
00056 
00057   /// Dereference operator
00058   EType& operator*() const { return *ptr; }
00059 
00060   /// Follow pointer operator
00061   EType* operator->() const { return ptr; }
00062 
00063   /// Release hold on the pointer
00064   EType* release(){ EType* tmp=ptr; ptr=NULL; return tmp; }
00065 
00066   /// Delete old object, replace by new, if any
00067   FXAutoPtr& reset(EType* p=NULL){ if(p!=ptr){ delete ptr; ptr=p; } return *this; }
00068 
00069   /// Destruction deletes pointer
00070   ~FXAutoPtr(){ delete ptr; }
00071   };
00072 
00073 
00074 /// Serialize of automatic pointer
00075 template <class EType> FXStream& operator<<(FXStream& store,const FXAutoPtr<EType>& obj){
00076   EType *temp=obj; store << temp; return store;
00077   }
00078 
00079 
00080 /// Deserialize of automatic pointer
00081 template <class EType> FXStream& operator>>(FXStream& store,FXAutoPtr<EType>& obj){
00082   EType *temp; store >> temp; obj=temp; return store;
00083   }
00084 
00085 }
00086 
00087 #endif
00088 

Copyright © 1997-2010 Jeroen van der Zijp