eRPC Generator (erpcgen)  Rev. 1.11.0
NXP Semiconductors
smart_ptr.hpp
1 /*
2  * Copyright (c) 2013-2014, Freescale Semiconductor, Inc.
3  * Copyright 2016 NXP
4  * All rights reserved.
5  *
6  *
7  * SPDX-License-Identifier: BSD-3-Clause
8  */
9 #if !defined(_smart_ptr_h_)
10 #define _smart_ptr_h_
11 
12 #include <cstdlib>
13 
15 template <typename T>
17 {
18  static inline void del(T *v) { delete v; }
19 };
20 
22 template <typename T>
24 {
25  static inline void del(T *v) { free(v); }
26 };
27 
29 template <typename T>
31 {
32  static inline void del(T *v) { delete[] v; }
33 };
34 
40 template <typename T, class delete_policy = smart_ptr_delete<T> >
41 class smart_ptr
42 {
43 public:
44  typedef T data_type;
45  typedef T *ptr_type;
46  typedef const T *const_ptr_type;
47  typedef T &ref_type;
48  typedef const T &const_ref_type;
49 
52  : _p(nullptr)
53  {
54  }
55 
57  smart_ptr(ptr_type p)
58  : _p(p)
59  {
60  }
61 
64  : _p(other._p)
65  {
66  other._p = nullptr;
67  }
68 
71  {
72  set(other._p);
73  other._p = nullptr;
74  return *this;
75  }
76 
78  smart_ptr<T> &operator=(ptr_type p)
79  {
80  set(p);
81  return *this;
82  }
83 
86  virtual ~smart_ptr() { safe_delete(); }
87 
89  ptr_type get() { return _p; }
90 
92  const_ptr_type get() const { return _p; }
93 
98  void set(ptr_type p)
99  {
100  if (_p && p != _p)
101  {
102  safe_delete();
103  }
104  _p = p;
105  }
106 
108  void reset() { _p = nullptr; }
109 
111  void clear() { safe_delete(); }
112 
116  virtual void safe_delete()
117  {
118  if (_p)
119  {
120  delete_policy::del(_p);
121  _p = nullptr;
122  }
123  }
124 
126 
127 
129  operator ptr_type() { return _p; }
130 
132  operator const_ptr_type() const { return _p; }
133 
135  operator ref_type() { return *_p; }
136 
138  operator const_ref_type() const { return *_p; }
139 
141  operator bool() const { return _p != nullptr; }
142 
144  ptr_type operator->() { return _p; }
145 
147  const_ptr_type operator->() const { return _p; }
149 
150 protected:
151  ptr_type _p;
152 
154  smart_ptr(const smart_ptr<T> &) = delete;
155 
157  smart_ptr<T> &operator=(const smart_ptr<T> &) = delete;
158 };
159 
161 template <typename T>
163 
165 template <typename T>
167 
168 #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.hpp:147
smart_ptr(smart_ptr< T > &&other)
Move copy constructor.
Definition: smart_ptr.hpp:63
void clear()
Dissociates a previously set pointer value, deleting it at the same time.
Definition: smart_ptr.hpp:111
Delete policy using free() to delete objects.
Definition: smart_ptr.hpp:23
ptr_type operator->()
Another operator to allow you to treat the object just like a pointer.
Definition: smart_ptr.hpp:144
ptr_type _p
The wrapped pointer.
Definition: smart_ptr.hpp:151
smart_ptr()
Default constructor. Initializes with no pointer set.
Definition: smart_ptr.hpp:51
Delete policy for arrays.
Definition: smart_ptr.hpp:30
Simple, standard smart pointer class.
Definition: smart_ptr.hpp:41
smart_ptr< T > & operator=(ptr_type p)
To allow setting the pointer directly. Equivalent to a call to set().
Definition: smart_ptr.hpp:78
virtual ~smart_ptr()
Definition: smart_ptr.hpp:86
smart_ptr< T > & operator=(smart_ptr< T > &&other)
Move assignment operator.
Definition: smart_ptr.hpp:70
Delete policy for regular objects.
Definition: smart_ptr.hpp:16
void reset()
Dissociates any previously set pointer value without deleting it.
Definition: smart_ptr.hpp:108
smart_ptr(ptr_type p)
This constructor takes a pointer to the object to be deleted.
Definition: smart_ptr.hpp:57
virtual void safe_delete()
Definition: smart_ptr.hpp:116