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

FXRefPtr.h
1 /********************************************************************************
2 * *
3 * R e f e r e n c e C o u n t e d O b j e c t P o i n t e r *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2004,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 FXREFPTR_H
22 #define FXREFPTR_H
23 
24 namespace FX {
25 
26 
28 template<typename EType>
29 class FXRefPtr {
30 private:
31  EType* ptr;
32 public:
33 
35  FXRefPtr():ptr(nullptr){
36  }
37 
39  FXRefPtr(EType* p):ptr(p){
40  if(ptr) ptr->ref();
41  }
42 
44  FXRefPtr(const FXRefPtr<EType>& org):ptr(org.ptr){
45  if(ptr) ptr->ref();
46  }
47 
49  template<typename T> FXRefPtr(FXRefPtr<T>& org):ptr(org.ptr){
50  if(ptr) ptr->ref();
51  }
52 
55  other.ptr=atomicSet(&ptr,other.ptr);
56  return *this;
57  }
58 
60  FXRefPtr<EType>& operator=(EType* src){
61  FXRefPtr<EType> tmp(src);
62  swap(tmp);
63  return *this;
64  }
65 
68  FXRefPtr<EType> tmp(src);
69  swap(tmp);
70  return *this;
71  }
72 
74  template<typename T> FXRefPtr<EType>& operator=(FXRefPtr<T>& src){
75  FXRefPtr<EType> tmp(src);
76  swap(tmp);
77  return *this;
78  }
79 
81  operator EType*() const { return ptr; }
82 
84  EType& operator*() const { return *ptr; }
85 
87  EType* operator->() const { return ptr; }
88 
90  operator FXbool() const { return !!ptr; }
91 
93  FXbool operator!() const { return !ptr; }
94 
96  FXbool operator==(EType *p) const { return ptr==p; }
97 
99  FXbool operator!=(EType *p) const { return ptr!=p; }
100 
102  EType* get() const { return ptr; }
103 
105  void clear(){
106  FXRefPtr<EType> tmp;
107  swap(tmp);
108  }
109 
112  if(ptr) ptr->unref();
113  }
114  };
115 
116 
118 template<typename EType> FXStream& operator<<(FXStream& store,const FXRefPtr<EType>& obj){
119  EType *temp=obj; store << temp; return store;
120  }
121 
122 
124 template<typename EType> FXStream& operator>>(FXStream& store,FXRefPtr<EType>& obj){
125  EType *temp; store >> temp; obj=temp; return store;
126  }
127 
128 }
129 
130 #endif
FXRefPtr(EType *p)
Construct and init.
Definition: FXRefPtr.h:39
EType & operator*() const
Dereference operator.
Definition: FXRefPtr.h:84
void clear()
Clear pointer.
Definition: FXRefPtr.h:105
Pointer to shared object.
Definition: FXRefPtr.h:29
EType * operator->() const
Follow pointer operator.
Definition: FXRefPtr.h:87
FXRefPtr< EType > & operator=(EType *src)
Assignment.
Definition: FXRefPtr.h:60
Definition: FX4Splitter.h:28
FXRefPtr< EType > & swap(FXRefPtr< EType > &other)
Swap this and other, pain-free.
Definition: FXRefPtr.h:54
FXRefPtr()
Default constructor.
Definition: FXRefPtr.h:35
FXbool operator==(EType *p) const
Comparison operator.
Definition: FXRefPtr.h:96
FXbool operator!() const
Test for NULL.
Definition: FXRefPtr.h:93
FXRefPtr(FXRefPtr< T > &org)
Copy constructor from shared pointer of compatible type.
Definition: FXRefPtr.h:49
~FXRefPtr()
Destructor.
Definition: FXRefPtr.h:111
FXRefPtr< EType > & operator=(const FXRefPtr< EType > &src)
Assignment.
Definition: FXRefPtr.h:67
FXRefPtr< EType > & operator=(FXRefPtr< T > &src)
Assignment from shared pointer of compatible type.
Definition: FXRefPtr.h:74
FXbool operator!=(EType *p) const
Comparison operator.
Definition: FXRefPtr.h:99
FXRefPtr(const FXRefPtr< EType > &org)
Copy constructor.
Definition: FXRefPtr.h:44

Copyright © 1997-2022 Jeroen van der Zijp