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,2024 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=nullptr):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[](FXival i) const { return ptr[i]; }
86 
88  EType* release(){ EType* tmp=ptr; ptr=nullptr; return tmp; }
89 
91  FXAutoPtr& reset(EType* p=nullptr){ 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 
FXAutoPtr & operator=(FXAutoPtr< T > &src)
Assign from an automatic pointer with compatible type.
Definition: FXAutoPtr.h:67
Implicitly used FXAutoPtrRef to hand FXAutoPtr through implicitly called constructors and conversion ...
Definition: FXAutoPtr.h:31
FXAutoPtr & operator=(EType *src)
Assign from pointer.
Definition: FXAutoPtr.h:58
~FXAutoPtr()
Destruction deletes pointer.
Definition: FXAutoPtr.h:94
FXAutoPtr(FXAutoPtr< EType > &src)
Construct from another automatic pointer.
Definition: FXAutoPtr.h:49
FXAutoPtr(FXAutoPtr< T > &src)
Construct from another automatic pointer of compatible type.
Definition: FXAutoPtr.h:55
FXAutoPtr & reset(EType *p=nullptr)
Delete old object, replace by new, if any.
Definition: FXAutoPtr.h:91
FXAutoPtr(FXAutoPtrRef< EType > src)
Construct from FXAutoPtrRef.
Definition: FXAutoPtr.h:52
FXAutoPtr & operator=(FXAutoPtr< EType > &src)
Assign from an another automatic pointer.
Definition: FXAutoPtr.h:61
EType * release()
Release hold on the pointer.
Definition: FXAutoPtr.h:88
Definition: FX4Splitter.h:28
Automatic pointer.
Definition: FXAutoPtr.h:40
FXAutoPtr & operator=(FXAutoPtrRef< EType > src)
Assign from FXAutoPtrRef.
Definition: FXAutoPtr.h:64
EType & operator*() const
Dereference operator.
Definition: FXAutoPtr.h:79
EType * operator->() const
Follow pointer operator.
Definition: FXAutoPtr.h:82
EType & operator[](FXival i) const
Array indexing.
Definition: FXAutoPtr.h:85
FXAutoPtr(EType *src=nullptr)
Construct from optional pointer.
Definition: FXAutoPtr.h:46

Copyright © 1997-2022 Jeroen van der Zijp