Pol  Revision:3cfda13
weakptr.h
Go to the documentation of this file.
1 
10 #ifndef __WEAKPTR_H
11 #define __WEAKPTR_H
12 
13 #include "refptr.h"
14 #include <atomic>
15 #include <boost/noncopyable.hpp>
16 
17 //
18 // weak_ptr_owner<T>
19 // -------- ref_ptr to: +---------------------+
20 // -------> | weak_ptr_link |
21 // | ref_ptr to T* |
22 // +---------------------+
23 // ^ ^
24 // weak_ptr<T> ref_ptr to ---------+ |
25 // |
26 // weak_ptr<T> ref_ptr to -------------+
27 //
28 
29 template <class T>
31 
32 template <class T>
33 class weak_ptr_link : public ref_counted
34 {
35 public:
36  weak_ptr_link();
37  void clear_weakptr();
38  T* get_weakptr() const;
39  void set_weakptr( T* ptr );
40 
41 private:
42  std::atomic<T*> _ptr;
43 };
44 
45 template <class T>
46 class weak_ptr : protected ref_ptr<weak_ptr_link<T>>
47 {
49 
50 public:
51  explicit weak_ptr( weak_ptr_link<T>* wpl );
52 
53  weak_ptr& operator=( const weak_ptr_link<T>* wpl );
54  weak_ptr& operator=( const weak_ptr_owner<T>& wpo );
55 
56  T* get_weakptr() const;
57  bool exists() const;
58  void clear();
59 
60  T* operator->() const;
61  T& operator*() const;
62 };
63 
64 template <class T>
65 class weak_ptr_owner : public weak_ptr<T>, boost::noncopyable
66 {
67 public:
69  ~weak_ptr_owner();
70  void set( T* obptr );
71 };
72 
73 template <class T>
75 {
76 }
77 template <class T>
79 {
80  _ptr = 0;
81 }
82 template <class T>
84 {
85  return _ptr;
86 }
87 template <class T>
89 {
90  _ptr = ptr;
91 }
92 
93 template <class T>
95 {
96 }
97 template <class T>
99 {
100  base::set( wpl );
101  return *this;
102 }
103 template <class T>
105 {
106  base::set( wpo.get() );
107  return *this;
108 }
109 template <class T>
111 {
112  return this->get()->get_weakptr();
113 }
114 template <class T>
116 {
117  return ( this->get() != 0 ) && ( this->get()->get_weakptr() != 0 );
118 }
119 template <class T>
121 {
122  base::set( 0 );
123 }
124 template <class T>
126 {
127  return this->get()->get_weakptr();
128 }
129 template <class T>
131 {
132  return *this->get();
133 }
134 
135 template <class T>
137 {
138 }
139 template <class T>
141 {
142  this->ref_ptr<weak_ptr_link<T>>::get()->clear_weakptr();
143 }
144 template <class T>
145 void weak_ptr_owner<T>::set( T* obptr )
146 {
147  this->ref_ptr<weak_ptr_link<T>>::get()->set_weakptr( obptr );
148 }
149 
150 #endif // __WEAKPTR_H
weak_ptr(weak_ptr_link< T > *wpl)
Definition: weakptr.h:94
void set(T *obptr)
Definition: weakptr.h:145
weak_ptr_link< T > * get() const
ref_counted & operator=(const ref_counted &)
T * get_weakptr() const
Definition: weakptr.h:110
T * operator->() const
Definition: weakptr.h:125
ref_ptr< weak_ptr_link< T > > base
Definition: weakptr.h:48
Definition: refptr.h:64
bool exists() const
Definition: weakptr.h:115
weak_ptr & operator=(const weak_ptr_link< T > *wpl)
Definition: weakptr.h:98
T & operator*() const
Definition: weakptr.h:130
void clear()
Definition: weakptr.h:120
void set(weak_ptr_link< T > *ptr)