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

FXAutoPtr.h
1 /********************************************************************************
2 * *
3 * A u t o m a t i c P o i n t e r *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2007,2017 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXAUTOPTR_H
22 #define FXAUTOPTR_H
23 
24 
25 namespace FX {
26 
27 
30 template<typename EType>
31 struct FXAutoPtrRef {
32  EType* ptr;
33  explicit FXAutoPtrRef(EType* src):ptr(src){ }
34  };
35 
36 
37 
39 template<typename EType>
40 class FXAutoPtr {
41 private:
42  EType* ptr;
43 public:
44 
46  explicit FXAutoPtr(EType* src=NULL):ptr(src){ }
47 
49  FXAutoPtr(FXAutoPtr<EType>& src):ptr(src.release()){ }
50 
52  FXAutoPtr(FXAutoPtrRef<EType> src):ptr(src.ptr){ }
53 
55  template <typename T> explicit FXAutoPtr(FXAutoPtr<T>& src):ptr(src.release()){ }
56 
58  FXAutoPtr& operator=(EType *src){ return reset(src); }
59 
62 
64  FXAutoPtr& operator=(FXAutoPtrRef<EType> src){ if(src.ptr!=ptr){ delete ptr; ptr=src.ptr; } return *this; }
65 
67  template<typename T> FXAutoPtr& operator=(FXAutoPtr<T>& src){ return reset(src.release()); }
68 
70  operator EType*() const { return ptr; }
71 
73  template<typename T> operator FXAutoPtr<T>() throw() { return FXAutoPtr<T>(this->release()); }
74 
76  template<typename T> operator FXAutoPtrRef<T>() throw() { return FXAutoPtrRef<T>(this->release()); }
77 
79  EType& operator*() const { return *ptr; }
80 
82  EType* operator->() const { return ptr; }
83 
85  EType& operator[](FXint i) const { return ptr[i]; }
86 
88  EType* release(){ EType* tmp=ptr; ptr=NULL; return tmp; }
89 
91  FXAutoPtr& reset(EType* p=NULL){ if(p!=ptr){ delete ptr; ptr=p; } return *this; }
92 
94  ~FXAutoPtr(){ delete ptr; }
95  };
96 
97 
99 template <typename EType> FXStream& operator<<(FXStream& store,const FXAutoPtr<EType>& obj){
100  EType *temp=obj; store << temp; return store;
101  }
102 
103 
105 template <typename EType> FXStream& operator>>(FXStream& store,FXAutoPtr<EType>& obj){
106  EType *temp; store >> temp; obj=temp; return store;
107  }
108 
109 }
110 
111 #endif
112 

Copyright © 1997-2016 Jeroen van der Zijp