ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
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-2015, 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  {
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>
75  {
76  void operator()(U* p) const { delete p; }
77  };
78 
79 }
80 
82 template<class T>
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 
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 
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
shared_ptr & operator=(const shared_ptr &s)
Definition: shared_ptr.hpp:140
Interface for the reference counter inside the shared_ptr.
Definition: shared_ptr.hpp:52
void swap(shared_ptr< T > &other)
Definition: shared_ptr.hpp:133
Implementation helper for the reference counting mechanism inside shared_ptr.
Definition: shared_ptr.hpp:63
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:34
Definition: blas3.hpp:36
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
A shared pointer class similar to boost::shared_ptr. Reimplemented in order to avoid a Boost-dependen...
Definition: shared_ptr.hpp:83
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.
Reference counting class for the shared_ptr implementation.
Definition: shared_ptr.hpp:39
shared_ptr(U *pu, Deleter d)
Definition: shared_ptr.hpp:96
shared_ptr(const shared_ptr< U > &s)
Definition: shared_ptr.hpp:113
shared_ptr(const shared_ptr &s)
Definition: shared_ptr.hpp:107