Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
src/blfwk/smart_ptr.h
1 /*
2  * Copyright (c) 2013-14, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #if !defined(_smart_ptr_h_)
31 #define _smart_ptr_h_
32 
35 
41 template <typename T>
42 class smart_ptr
43 {
44 public:
45  typedef T data_type;
46  typedef T *ptr_type;
47  typedef const T *const_ptr_type;
48  typedef T &ref_type;
49  typedef const T &const_ref_type;
50 
53  : _p(0)
54  {
55  }
56 
58  smart_ptr(ptr_type p)
59  : _p(p)
60  {
61  }
62 
65  virtual ~smart_ptr() { safe_delete(); }
67  ptr_type get() { return _p; }
69  const_ptr_type get() const { return _p; }
74  void set(ptr_type p)
75  {
76  if (_p && p != _p)
77  {
78  safe_delete();
79  }
80  _p = p;
81  }
82 
84  void reset() { _p = 0; }
86  void clear() { safe_delete(); }
90  virtual void safe_delete()
91  {
92  if (_p)
93  {
94  delete _p;
95  _p = 0;
96  }
97  }
98 
100 
101 
103  operator ptr_type() { return _p; }
105  operator const_ptr_type() const { return _p; }
107  operator ref_type() { return *_p; }
109  operator const_ref_type() const { return *_p; }
111  operator bool() const { return _p != 0; }
113  smart_ptr<T> &operator=(const_ptr_type p)
114  {
115  set(const_cast<ptr_type>(p));
116  return *this;
117  }
118 
120  ptr_type operator->() { return _p; }
122  const_ptr_type operator->() const { return _p; }
123  // //! Pointer dereferencing operator.
124  // ref_type operator * () const { return *_p; }
125  //
126  // //! Const version of the pointer dereference operator.
127  // const_ref_type operator * () const { return *_p; }
128 
130 
131 protected:
132  ptr_type _p;
133 };
134 
144 template <typename T>
145 class smart_array_ptr
146 {
147 public:
148  typedef T data_type;
149  typedef T *ptr_type;
150  typedef const T *const_ptr_type;
151  typedef T &ref_type;
152  typedef const T &const_ref_type;
153 
156  : _p(0)
157  {
158  }
159 
161  smart_array_ptr(ptr_type p)
162  : _p(p)
163  {
164  }
165 
168  virtual ~smart_array_ptr() { safe_delete(); }
170  ptr_type get() { return _p; }
172  const_ptr_type get() const { return _p; }
177  void set(ptr_type p)
178  {
179  if (_p && p != _p)
180  {
181  safe_delete();
182  }
183  _p = p;
184  }
185 
187  void reset() { _p = 0; }
189  void clear() { safe_delete(); }
193  virtual void safe_delete()
194  {
195  if (_p)
196  {
197  delete[] _p;
198  _p = 0;
199  }
200  }
201 
203 
204 
206  operator ptr_type() { return _p; }
208  operator const_ptr_type() const { return _p; }
210  operator ref_type() { return *_p; }
212  operator const_ref_type() const { return *_p; }
214  operator bool() const { return _p != 0; }
216  smart_array_ptr<T> &operator=(const_ptr_type p)
217  {
218  set(const_cast<ptr_type>(p));
219  return *this;
220  }
221 
223  ptr_type operator->() { return _p; }
225  const_ptr_type operator->() const { return _p; }
227  ref_type operator[](unsigned index) { return _p[index]; }
229  const_ref_type operator[](unsigned index) const { return _p[index]; }
231 
232 protected:
233  ptr_type _p;
234 };
235 
237 
238 #endif // _smart_ptr_h_
void clear()
Dissociates a previously set pointer value, deleting it at the same time.
Definition: src/blfwk/smart_ptr.h:86
virtual void safe_delete()
Definition: apps/elftosb/common/smart_ptr.h:66
smart_array_ptr()
Default constuctor. Initialises with no pointer set.
Definition: src/blfwk/smart_ptr.h:155
virtual ~smart_array_ptr()
Definition: src/blfwk/smart_ptr.h:168
virtual void safe_delete()
Definition: src/blfwk/smart_ptr.h:193
smart_ptr()
Default constuctor. Initialises with no pointer set.
Definition: src/blfwk/smart_ptr.h:52
smart_array_ptr< T > & operator=(const_ptr_type p)
To allow setting the pointer directly. Equivalent to a call to set().
Definition: src/blfwk/smart_ptr.h:216
smart_ptr(ptr_type p)
This constructor takes a pointer to the object to be deleted.
Definition: src/blfwk/smart_ptr.h:58
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: src/blfwk/smart_ptr.h:84
void reset()
Dissociates any previously set pointer value without deleting it.
Definition: src/blfwk/smart_ptr.h:187
void clear()
Dissociates a previously set pointer value, deleting it at the same time.
Definition: src/blfwk/smart_ptr.h:189
ptr_type _p
The wrapped pointer.
Definition: apps/elftosb/common/smart_ptr.h:108
Keyblob specification.
Definition: Keyblob.h:27
const_ptr_type operator->() const
Another operator to allow you to treat the object just like a pointer.
Definition: src/blfwk/smart_ptr.h:225
smart_array_ptr(ptr_type p)
This constructor takes a pointer to the object to be deleted.
Definition: src/blfwk/smart_ptr.h:161
const_ptr_type operator->() const
Another operator to allow you to treat the object just like a pointer.
Definition: src/blfwk/smart_ptr.h:122
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: src/blfwk/smart_ptr.h:120
smart_ptr< T > & operator=(const_ptr_type p)
To allow setting the pointer directly. Equivalent to a call to set().
Definition: src/blfwk/smart_ptr.h:113
ptr_type operator->()
Another operator to allow you to treat the object just like a pointer.
Definition: src/blfwk/smart_ptr.h:223
ref_type operator[](unsigned index)
Indexing operator.
Definition: src/blfwk/smart_ptr.h:227
const_ref_type operator[](unsigned index) const
Indexing operator.
Definition: src/blfwk/smart_ptr.h:229
virtual ~smart_ptr()
Definition: src/blfwk/smart_ptr.h:65