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,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 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(NULL){
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* get() const { return ptr; }
85 
87  EType& operator*() const { return *ptr; }
88 
90  EType* operator->() const { return ptr; }
91 
93  void clear(){
94  FXRefPtr<EType> tmp;
95  swap(tmp);
96  }
97 
100  if(ptr) ptr->unref();
101  }
102  };
103 
104 
106 template<typename EType> FXStream& operator<<(FXStream& store,const FXRefPtr<EType>& obj){
107  EType *temp=obj; store << temp; return store;
108  }
109 
110 
112 template<typename EType> FXStream& operator>>(FXStream& store,FXRefPtr<EType>& obj){
113  EType *temp; store >> temp; obj=temp; return store;
114  }
115 
116 }
117 
118 #endif

Copyright © 1997-2016 Jeroen van der Zijp