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

/home/jeroen/FOX/fox/fox-1.7.33/include/FXAutoPtr.h
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,2012 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 
00029 template<typename EType> struct FXAutoPtrRef {
00030   EType* ptr;
00031   explicit FXAutoPtrRef(EType* src):ptr(src){ }
00032   };
00033 
00034 
00035 
00037 template <class EType> class FXAutoPtr {
00038 private:
00039   EType* ptr;
00040 public:
00041 
00043   explicit FXAutoPtr(EType* src=NULL):ptr(src){ }
00044 
00046   FXAutoPtr(FXAutoPtr<EType>& src):ptr(src.release()){ }
00047 
00049   FXAutoPtr(FXAutoPtrRef<EType> src):ptr(src.ptr){ }
00050 
00052   template <class T> explicit FXAutoPtr(FXAutoPtr<T>& src):ptr(src.release()){ }
00053 
00055   FXAutoPtr& operator=(EType *src){ return reset(src); }
00056 
00058   FXAutoPtr& operator=(FXAutoPtr<EType>& src){ return reset(src.release()); }
00059 
00061   FXAutoPtr& operator=(FXAutoPtrRef<EType> src){ if(src.ptr!=ptr){ delete ptr; ptr=src.ptr; } return *this; }
00062 
00064   template <class T> FXAutoPtr& operator=(FXAutoPtr<T>& src){ return reset(src.release()); }
00065 
00067   operator EType*() const { return ptr; }
00068 
00070   template<typename T> operator FXAutoPtr<T>() throw() { return FXAutoPtr<T>(this->release()); }
00071 
00073   template<typename T> operator FXAutoPtrRef<T>() throw() { return FXAutoPtrRef<T>(this->release()); }
00074 
00076   EType& operator*() const { return *ptr; }
00077 
00079   EType* operator->() const { return ptr; }
00080 
00082   EType* release(){ EType* tmp=ptr; ptr=NULL; return tmp; }
00083 
00085   FXAutoPtr& reset(EType* p=NULL){ if(p!=ptr){ delete ptr; ptr=p; } return *this; }
00086 
00088   ~FXAutoPtr(){ delete ptr; }
00089   };
00090 
00091 
00093 template <class EType> FXStream& operator<<(FXStream& store,const FXAutoPtr<EType>& obj){
00094   EType *temp=obj; store << temp; return store;
00095   }
00096 
00097 
00099 template <class EType> FXStream& operator>>(FXStream& store,FXAutoPtr<EType>& obj){
00100   EType *temp; store >> temp; obj=temp; return store;
00101   }
00102 
00103 }
00104 
00105 #endif
00106 

Copyright © 1997-2011 Jeroen van der Zijp