ViennaCL - The Vienna Computing Library  1.5.2
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-2012, 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 PDF 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>
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  shared_ptr(const shared_ptr& s) :pa(s.pa), pt(s.pt) { inc(); }
102 
103  template<class U>
104  shared_ptr(const shared_ptr<U>& s) :pa(s.pa), pt(s.pt) { inc(); }
105 
106  ~shared_ptr() { dec(); }
107 
108  void reset(){
109  shared_ptr<T>().swap(*this);
110  }
111 
112  void reset(T * ptr){
113  shared_ptr<T>(ptr).swap(*this);
114  }
115 
116  void swap(shared_ptr<T> & other){
117  std::swap(pt,other.pt);
118  std::swap(pa, other.pa);
119  }
120 
121 
123  {
124  if(this!=&s)
125  {
126  dec();
127  pa = s.pa;
128  pt = s.pt;
129  inc();
130  }
131  return *this;
132  }
133 
134  T* get() const { return pt; }
135 
136  T* operator->() const { return pt; }
137 
138  T& operator*() const { return *pt; }
139 
140  void inc() { if(pa) pa->count.inc(); }
141 
142  void dec()
143  {
144  if(pa)
145  {
146  pa->count.dec();
147 
148  if(pa->count.is_null())
149  {
150  pa->destroy();
151  delete pa;
152  pa = NULL;
153  }
154  }
155  }
156 
157  };
158 
159  }
160 
161 }
162 
163 #endif // VIENNACL_UTILS_SHARED_PTR_HPP
T * operator->() const
Definition: shared_ptr.hpp:136
auximpl(U *pu, Deleter x)
Definition: shared_ptr.hpp:68
bool is_null()
Definition: shared_ptr.hpp:45
virtual ~aux()
Definition: shared_ptr.hpp:58
shared_ptr(const shared_ptr &s)
Definition: shared_ptr.hpp:101
Interface for the reference counter inside the shared_ptr.
Definition: shared_ptr.hpp:52
shared_ptr()
Definition: shared_ptr.hpp:93
void swap(shared_ptr< T > &other)
Definition: shared_ptr.hpp:116
void dec()
Definition: shared_ptr.hpp:142
Implementation helper for the reference counting mechanism inside shared_ptr.
Definition: shared_ptr.hpp:63
void operator()(U *p) const
Definition: shared_ptr.hpp:76
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
unsigned int val()
Definition: shared_ptr.hpp:46
detail::count count
Definition: shared_ptr.hpp:54
shared_ptr(U *pu)
Definition: shared_ptr.hpp:99
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
void reset(T *ptr)
Definition: shared_ptr.hpp:112
A shared pointer class similar to boost::shared_ptr. Reimplemented in order to avoid a Boost-dependen...
Definition: shared_ptr.hpp:83
shared_ptr & operator=(const shared_ptr &s)
Definition: shared_ptr.hpp:122
shared_ptr(U *pu, Deleter d)
Definition: shared_ptr.hpp:96
shared_ptr(const shared_ptr< U > &s)
Definition: shared_ptr.hpp:104
aux()
Definition: shared_ptr.hpp:56
Reference counting class for the shared_ptr implementation.
Definition: shared_ptr.hpp:39
void inc()
Definition: shared_ptr.hpp:140
virtual void destroy()
Definition: shared_ptr.hpp:69
viennacl::enable_if< viennacl::is_scalar< S1 >::value &&viennacl::is_scalar< S2 >::value >::type swap(S1 &s1, S2 &s2)
Swaps the contents of two scalars, data is copied.
Definition: scalar_operations.hpp:366
void reset()
Definition: shared_ptr.hpp:108
void inc()
Definition: shared_ptr.hpp:44
~shared_ptr()
Definition: shared_ptr.hpp:106
Deleter d
Definition: shared_ptr.hpp:66
U * p
Definition: shared_ptr.hpp:65
count(unsigned int val)
Definition: shared_ptr.hpp:42
void dec()
Definition: shared_ptr.hpp:43
T & operator*() const
Definition: shared_ptr.hpp:138