Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
apps/elftosb/common/smart_ptr.h
1 /*
2  * smart_ptr.h
3  * elftosb
4  *
5  * Created by Chris Reed on 4/18/06.
6  * Copyright 2006 __MyCompanyName__. All rights reserved.
7  *
8  */
9 #if !defined(_smart_ptr_h_)
10 #define _smart_ptr_h_
11 
17 template <typename T>
18 class smart_ptr
19 {
20 public:
21  typedef T data_type;
22  typedef T *ptr_type;
23  typedef const T *const_ptr_type;
24  typedef T &ref_type;
25  typedef const T &const_ref_type;
26 
29  : _p(0)
30  {
31  }
32 
34  smart_ptr(ptr_type p)
35  : _p(p)
36  {
37  }
38 
41  virtual ~smart_ptr() { safe_delete(); }
43  ptr_type get() { return _p; }
45  const_ptr_type get() const { return _p; }
50  void set(ptr_type p)
51  {
52  if (_p && p != _p)
53  {
54  safe_delete();
55  }
56  _p = p;
57  }
58 
60  void reset() { _p = 0; }
62  void clear() { safe_delete(); }
66  virtual void safe_delete()
67  {
68  if (_p)
69  {
70  delete _p;
71  _p = 0;
72  }
73  }
74 
76 
77 
79  operator ptr_type() { return _p; }
81  operator const_ptr_type() const { return _p; }
83  operator ref_type() { return *_p; }
85  operator const_ref_type() const { return *_p; }
87  operator bool() const { return _p != 0; }
89  smart_ptr<T> &operator=(const_ptr_type p)
90  {
91  set(const_cast<ptr_type>(p));
92  return *this;
93  }
94 
96  ptr_type operator->() { return _p; }
98  const_ptr_type operator->() const { return _p; }
99  // //! Pointer dereferencing operator.
100  // ref_type operator * () const { return *_p; }
101  //
102  // //! Const version of the pointer dereference operator.
103  // const_ref_type operator * () const { return *_p; }
104 
106 
107 protected:
108  ptr_type _p;
109 };
110 
120 template <typename T>
122 {
123 public:
124  typedef T data_type;
125  typedef T *ptr_type;
126  typedef const T *const_ptr_type;
127  typedef T &ref_type;
128  typedef const T &const_ref_type;
129 
132  : _p(0)
133  {
134  }
135 
137  smart_array_ptr(ptr_type p)
138  : _p(p)
139  {
140  }
141 
144  virtual ~smart_array_ptr() { safe_delete(); }
146  ptr_type get() { return _p; }
148  const_ptr_type get() const { return _p; }
153  void set(ptr_type p)
154  {
155  if (_p && p != _p)
156  {
157  safe_delete();
158  }
159  _p = p;
160  }
161 
163  void reset() { _p = 0; }
165  void clear() { safe_delete(); }
169  virtual void safe_delete()
170  {
171  if (_p)
172  {
173  delete[] _p;
174  _p = 0;
175  }
176  }
177 
179 
180 
182  operator ptr_type() { return _p; }
184  operator const_ptr_type() const { return _p; }
186  operator ref_type() { return *_p; }
188  operator const_ref_type() const { return *_p; }
190  operator bool() const { return _p != 0; }
192  smart_array_ptr<T> &operator=(const_ptr_type p)
193  {
194  set(const_cast<ptr_type>(p));
195  return *this;
196  }
197 
199  ptr_type operator->() { return _p; }
201  const_ptr_type operator->() const { return _p; }
203  ref_type operator[](unsigned index) { return _p[index]; }
205  const_ref_type operator[](unsigned index) const { return _p[index]; }
206  // //! Pointer dereferencing operator.
207  // ref_type operator * () const { return *_p; }
208  //
209  // //! Const version of the pointer dereference operator.
210  // const_ref_type operator * () const { return *_p; }
211 
213 
214 protected:
215  ptr_type _p;
216 };
217 
218 #endif // _smart_ptr_h_
void clear()
Dissociates a previously set pointer value, deleting it at the same time.
Definition: apps/elftosb/common/smart_ptr.h:62
virtual void safe_delete()
Definition: apps/elftosb/common/smart_ptr.h:66
smart_array_ptr()
Default constuctor. Initialises with no pointer set.
Definition: apps/elftosb/common/smart_ptr.h:131
ptr_type _p
The wrapped pointer.
Definition: apps/elftosb/common/smart_ptr.h:215
virtual ~smart_array_ptr()
Definition: apps/elftosb/common/smart_ptr.h:144
virtual void safe_delete()
Definition: apps/elftosb/common/smart_ptr.h:169
smart_ptr()
Default constuctor. Initialises with no pointer set.
Definition: apps/elftosb/common/smart_ptr.h:28
smart_array_ptr< T > & operator=(const_ptr_type p)
To allow setting the pointer directly. Equivalent to a call to set().
Definition: apps/elftosb/common/smart_ptr.h:192
smart_ptr(ptr_type p)
This constructor takes a pointer to the object to be deleted.
Definition: apps/elftosb/common/smart_ptr.h:34
Simple, standard smart pointer class.
Definition: apps/elftosb/common/smart_ptr.h:18
void reset()
Dissociates any previously set pointer value without deleting it.
Definition: apps/elftosb/common/smart_ptr.h:60
void reset()
Dissociates any previously set pointer value without deleting it.
Definition: apps/elftosb/common/smart_ptr.h:163
void clear()
Dissociates a previously set pointer value, deleting it at the same time.
Definition: apps/elftosb/common/smart_ptr.h:165
ptr_type _p
The wrapped pointer.
Definition: apps/elftosb/common/smart_ptr.h:108
const_ptr_type operator->() const
Another operator to allow you to treat the object just like a pointer.
Definition: apps/elftosb/common/smart_ptr.h:201
smart_array_ptr(ptr_type p)
This constructor takes a pointer to the object to be deleted.
Definition: apps/elftosb/common/smart_ptr.h:137
const_ptr_type operator->() const
Another operator to allow you to treat the object just like a pointer.
Definition: apps/elftosb/common/smart_ptr.h:98
Simple, standard smart pointer class that uses the array delete operator.
Definition: apps/elftosb/common/smart_ptr.h:121
ptr_type operator->()
Another operator to allow you to treat the object just like a pointer.
Definition: apps/elftosb/common/smart_ptr.h:96
smart_ptr< T > & operator=(const_ptr_type p)
To allow setting the pointer directly. Equivalent to a call to set().
Definition: apps/elftosb/common/smart_ptr.h:89
ptr_type operator->()
Another operator to allow you to treat the object just like a pointer.
Definition: apps/elftosb/common/smart_ptr.h:199
ref_type operator[](unsigned index)
Indexing operator.
Definition: apps/elftosb/common/smart_ptr.h:203
const_ref_type operator[](unsigned index) const
Indexing operator.
Definition: apps/elftosb/common/smart_ptr.h:205
virtual ~smart_ptr()
Definition: apps/elftosb/common/smart_ptr.h:41