ViennaCL - The Vienna Computing Library: viennacl/tools/shared_ptr.hpp Source File

ViennaCL - The Vienna Computing Library 1.7.1 Free open-source GPU-accelerated linear algebra and solver library. |

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shared_ptr.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_TOOLS_SHARED_PTR_HPP
2 #define VIENNACL_TOOLS_SHARED_PTR_HPP
3
4 /* =========================================================================
5 Copyright (c) 2010-2016, Institute for Microelectronics,
6 Institute for Analysis and Scientific Computing,
7 TU Wien.
8 Portions of this software are copyright by UChicago Argonne, LLC.
9
10 -----------------
11 ViennaCL - The Vienna Computing Library
12 -----------------
13
14 Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15
16 (A list of authors and contributors can be found in the manual)
17
18 License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20
27 #include <cstdlib>
28 #include <algorithm>
29
30 namespace viennacl
31 {
32 namespace tools
33 {
34
35 namespace detail
36 {
37
39 class count
40 {
41 public:
42 count(unsigned int val) : val_(val){ }
43 void dec(){ --val_; }
44 void inc(){ ++val_; }
45 bool is_null(){ return val_ == 0; }
46 unsigned int val(){ return val_; }
47 private:
48 unsigned int val_;
49 };
50
52 struct aux
53 {
54 detail::count count;
55
56 aux() :count(1) {}
57 virtual void destroy()=0;
58 virtual ~aux() {}
59 };
60
62 template<class U, class Deleter>
63 struct auximpl: public detail::aux
64 {
65 U* p;
66 Deleter d;
67
68 auximpl(U* pu, Deleter x) :p(pu), d(x) {}
69 virtual void destroy() { d(p); }
70 };
71
73 template<class U>
74 struct default_deleter
75 {
76 void operator()(U* p) const { delete p; }
77 };
78
79 }
80
82 template<class T>
83 class shared_ptr
84 {
85 template<class U>
86 friend class shared_ptr;
87
88 detail::aux* pa;
89 T* pt;
90
91 public:
92
93 shared_ptr() :pa(NULL), pt(NULL) {}
94
95 template<class U, class Deleter>
96 shared_ptr(U* pu, Deleter d) : pa(new detail::auximpl<U, Deleter>(pu, d)), pt(pu) {}
97
98 template<class U>
99 explicit shared_ptr(U* pu) : pa(new detail::auximpl<U, detail::default_deleter<U> >(pu, detail::default_deleter<U>())), pt(pu) {}
100
101 T* get() const { return pt; }
102
103 T* operator->() const { return pt; }
104
105 T& operator*() const { return *pt; }
106
107 shared_ptr(const shared_ptr& s) :pa(s.pa), pt(s.pt)
108 {
109 inc();
110 }
111
112 template<class U>
113 shared_ptr(const shared_ptr<U>& s) :pa(s.pa), pt(s.pt)
114 {
115 inc();
116 }
117
118 ~shared_ptr()
119 {
120 dec();
121 }
122
123 void reset()
124 {
125 shared_ptr<T>().swap(*this);
126 }
127
128 void reset(T * ptr)
129 {
130 shared_ptr<T>(ptr).swap(*this);
131 }
132
133 void swap(shared_ptr<T> & other)
134 {
135 std::swap(pt,other.pt);
136 std::swap(pa, other.pa);
137 }
138
139
140 shared_ptr& operator=(const shared_ptr& s)
141 {
142 if (this!=&s)
143 {
144 dec();
145 pa = s.pa;
146 pt = s.pt;
147 inc();
148 }
149 return *this;
150 }
151
152
153
154 void inc()
155 {
156 if (pa) pa->count.inc();
157 }
158
159 void dec()
160 {
161 if (pa)
162 {
163 pa->count.dec();
164
165 if (pa->count.is_null())
166 {
167 pa->destroy();
168 delete pa;
169 pa = NULL;
170 }
171 }
172 }
173
174 };
175
176 }
177 }
178
179 #endif // VIENNACL_UTILS_SHARED_PTR_HPP
viennacl::tools::detail::aux::aux
aux()
Definition: shared_ptr.hpp:56
viennacl::tools::shared_ptr::operator=
shared_ptr & operator=(const shared_ptr &s)
Definition: shared_ptr.hpp:140
viennacl::tools::detail::aux::~aux
virtual ~aux()
Definition: shared_ptr.hpp:58
viennacl::tools::detail::aux::count
detail::count count
Definition: shared_ptr.hpp:54
viennacl::tools::shared_ptr::reset
void reset(T *ptr)
Definition: shared_ptr.hpp:128
viennacl::tools::shared_ptr::inc
void inc()
Definition: shared_ptr.hpp:154
viennacl::tools::detail::auximpl::d
Deleter d
Definition: shared_ptr.hpp:66
viennacl::tools::shared_ptr::dec
void dec()
Definition: shared_ptr.hpp:159
viennacl::tools::detail::default_deleter::operator()
void operator()(U *p) const
Definition: shared_ptr.hpp:76
viennacl::tools::detail::aux
Interface for the reference counter inside the shared_ptr.
Definition: shared_ptr.hpp:52
viennacl::tools::shared_ptr::swap
void swap(shared_ptr< T > &other)
Definition: shared_ptr.hpp:133
viennacl::tools::detail::auximpl
Implementation helper for the reference counting mechanism inside shared_ptr.
Definition: shared_ptr.hpp:63
viennacl::tools::shared_ptr::shared_ptr
shared_ptr(U *pu)
Definition: shared_ptr.hpp:99
viennacl::tools::detail::default_deleter
Default deleter class for a pointer. The default is to just call 'delete' on the pointer. Provide your own implementations for 'delete[]' and 'free'.
Definition: shared_ptr.hpp:74
viennacl::tools::detail::auximpl::destroy
virtual void destroy()
Definition: shared_ptr.hpp:69
viennacl::tools::detail::auximpl::p
U * p
Definition: shared_ptr.hpp:65
viennacl::tools::shared_ptr::operator->
T * operator->() const
Definition: shared_ptr.hpp:103
viennacl::tools::shared_ptr::~shared_ptr
~shared_ptr()
Definition: shared_ptr.hpp:118
viennacl::tools::shared_ptr
A shared pointer class similar to boost::shared_ptr. Reimplemented in order to avoid a Boost-dependen...
Definition: shared_ptr.hpp:83
viennacl::tools::shared_ptr::operator*
T & operator*() const
Definition: shared_ptr.hpp:105
viennacl::linalg::cuda::swap
viennacl::enable_if< viennacl::is_scalar< ScalarT1 >::value &&viennacl::is_scalar< ScalarT2 >::value >::type swap(ScalarT1 &s1, ScalarT2 &s2)
Swaps the contents of two scalars, data is copied.
Definition: scalar_operations.hpp:361
viennacl::tools::detail::count::val
unsigned int val()
Definition: shared_ptr.hpp:46
viennacl::tools::detail::count
Reference counting class for the shared_ptr implementation.
Definition: shared_ptr.hpp:39
viennacl::tools::shared_ptr::shared_ptr
shared_ptr(U *pu, Deleter d)
Definition: shared_ptr.hpp:96
viennacl::tools::detail::aux::destroy
virtual void destroy()=0
viennacl::tools::detail::count::inc
void inc()
Definition: shared_ptr.hpp:44
viennacl::tools::shared_ptr::reset
void reset()
Definition: shared_ptr.hpp:123
viennacl::tools::detail::count::count
count(unsigned int val)
Definition: shared_ptr.hpp:42
viennacl::tools::detail::count::is_null
bool is_null()
Definition: shared_ptr.hpp:45
viennacl::tools::detail::count::dec
void dec()
Definition: shared_ptr.hpp:43
viennacl::tools::detail::auximpl::auximpl
auximpl(U *pu, Deleter x)
Definition: shared_ptr.hpp:68
viennacl::tools::shared_ptr::shared_ptr
shared_ptr()
Definition: shared_ptr.hpp:93
viennacl::tools::shared_ptr::shared_ptr
shared_ptr(const shared_ptr< U > &s)
Definition: shared_ptr.hpp:113
viennacl::tools::shared_ptr::shared_ptr
shared_ptr(const shared_ptr &s)
Definition: shared_ptr.hpp:107