eRPC Generator (erpcgen)  Rev. 1.7.0
NXP Semiconductors
smart_ptr.h
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2013-2014, Freescale Semiconductor, Inc.
4  * Copyright 2016 NXP
5  * All rights reserved.
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted (subject to the limitations in the disclaimer below) provided
10  * that the following conditions are met:
11  *
12  * o Redistributions of source code must retain the above copyright notice, this list
13  * of conditions and the following disclaimer.
14  *
15  * o Redistributions in binary form must reproduce the above copyright notice, this
16  * list of conditions and the following disclaimer in the documentation and/or
17  * other materials provided with the distribution.
18  *
19  * o Neither the name of the copyright holder nor the names of its
20  * contributors may be used to endorse or promote products derived from this
21  * software without specific prior written permission.
22  *
23  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #if !defined(_smart_ptr_h_)
36 #define _smart_ptr_h_
37 
38 #include <cstdlib>
39 
41 template <typename T>
43 {
44  static inline void del(T *v) { delete v; }
45 };
46 
48 template <typename T>
50 {
51  static inline void del(T *v) { free(v); }
52 };
53 
55 template <typename T>
57 {
58  static inline void del(T *v) { delete[] v; }
59 };
60 
66 template <typename T, class delete_policy = smart_ptr_delete<T> >
67 class smart_ptr
68 {
69 public:
70  typedef T data_type;
71  typedef T *ptr_type;
72  typedef const T *const_ptr_type;
73  typedef T &ref_type;
74  typedef const T &const_ref_type;
75 
78  : _p(nullptr)
79  {
80  }
81 
83  smart_ptr(ptr_type p)
84  : _p(p)
85  {
86  }
87 
90  : _p(other._p)
91  {
92  other._p = nullptr;
93  }
94 
97  {
98  set(other._p);
99  other._p = nullptr;
100  return *this;
101  }
102 
105  virtual ~smart_ptr() { safe_delete(); }
106 
108  ptr_type get() { return _p; }
109 
111  const_ptr_type get() const { return _p; }
112 
117  void set(ptr_type p)
118  {
119  if (_p && p != _p)
120  {
121  safe_delete();
122  }
123  _p = p;
124  }
125 
127  void reset() { _p = nullptr; }
128 
130  void clear() { safe_delete(); }
131 
135  virtual void safe_delete()
136  {
137  if (_p)
138  {
139  delete_policy::del(_p);
140  _p = nullptr;
141  }
142  }
143 
145 
146 
148  operator ptr_type() { return _p; }
149 
151  operator const_ptr_type() const { return _p; }
152 
154  operator ref_type() { return *_p; }
155 
157  operator const_ref_type() const { return *_p; }
158 
160  operator bool() const { return _p != nullptr; }
161 
163  smart_ptr<T> &operator=(ptr_type p)
164  {
165  set(p);
166  return *this;
167  }
168 
170  ptr_type operator->() { return _p; }
171 
173  const_ptr_type operator->() const { return _p; }
175 
176 protected:
177  ptr_type _p;
178 
180  smart_ptr(const smart_ptr<T> &) = delete;
181 
183  smart_ptr<T> &operator=(const smart_ptr<T> &) = delete;
184 };
185 
187 template <typename T>
189 
191 template <typename T>
193 
194 #endif // _smart_ptr_h_
const_ptr_type operator->() const
Another operator to allow you to treat the object just like a pointer.
Definition: smart_ptr.h:173
smart_ptr(smart_ptr< T > &&other)
Move copy constructor.
Definition: smart_ptr.h:89
void clear()
Dissociates a previously set pointer value, deleting it at the same time.
Definition: smart_ptr.h:130
Delete policy using free() to delete objects.
Definition: smart_ptr.h:49
ptr_type operator->()
Another operator to allow you to treat the object just like a pointer.
Definition: smart_ptr.h:170
ptr_type _p
The wrapped pointer.
Definition: smart_ptr.h:177
smart_ptr()
Default constructor. Initializes with no pointer set.
Definition: smart_ptr.h:77
Delete policy for arrays.
Definition: smart_ptr.h:56
Simple, standard smart pointer class.
Definition: smart_ptr.h:67
smart_ptr< T > & operator=(ptr_type p)
To allow setting the pointer directly. Equivalent to a call to set().
Definition: smart_ptr.h:163
virtual ~smart_ptr()
Definition: smart_ptr.h:105
smart_ptr< T > & operator=(smart_ptr< T > &&other)
Move assignment operator.
Definition: smart_ptr.h:96
Delete policy for regular objects.
Definition: smart_ptr.h:42
void reset()
Dissociates any previously set pointer value without deleting it.
Definition: smart_ptr.h:127
smart_ptr(ptr_type p)
This constructor takes a pointer to the object to be deleted.
Definition: smart_ptr.h:83
virtual void safe_delete()
Definition: smart_ptr.h:135