APM:Libraries
OwnPtr.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-2016 Intel Corporation. All rights reserved.
3  *
4  * This file is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This file is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #pragma once
18 
19 #include <cstddef>
20 
21 namespace AP_HAL {
22 
23 /* Poor's man std::unique_ptr
24  *
25  * OwnPtr is a container for a pointer declaring ownership.
26  *
27  * The goal is to allow to own object, pass it around and automatically delete
28  * the pointer when the container goes out of scope.
29  *
30  * In order to pass it around the move constructor or move assignment operator
31  * must be used. operator*, operator-> and the get() method can be used to get
32  * the pointer contained in the OwnPtr container.
33  *
34  * The OwnPtr name comes from similar class in WebKit, before they switched to
35  * std::unique_ptr. The implementation is different/simpler. We need our own
36  * implementation since the <memory> header (and thus std::unique_ptr) is not
37  * compatible with the PX4 toolchain and/or nuttx headers.
38  */
39 template<typename T>
40 class OwnPtr {
41 public:
42  OwnPtr() : _ptr(nullptr) { }
43  OwnPtr(std::nullptr_t) : _ptr(nullptr) { }
44 
45  /* non-copyable */
46  OwnPtr(const OwnPtr<T> &other) = delete;
47 
48  /* Allow construction from a derived class U */
49  template<typename U>
50  OwnPtr(OwnPtr<U>&& other) : _ptr(other.leak()) { }
51 
52  OwnPtr(T *ptr) : _ptr(ptr) { }
53 
54  OwnPtr<T>& operator=(std::nullptr_t) { clear(); return *this; }
55 
56  template<typename U>
58  {
59  T *old = _ptr;
60  _ptr = other.leak();
61  delete old;
62  return *this;
63  }
64 
65  template<typename U>
66  OwnPtr<T>& operator=(U *other)
67  {
68  T *old = _ptr;
69  _ptr = other;
70  delete old;
71  return *this;
72  }
73 
74  ~OwnPtr() { delete _ptr; }
75 
76  void clear()
77  {
78  delete leak();
79  }
80 
81  T *leak()
82  {
83  T *old = _ptr;
84  _ptr = nullptr;
85  return old;
86  }
87 
88  T *get() const
89  {
90  return _ptr;
91  }
92 
93  T& operator*() const { return *_ptr; }
94  T *operator->() const { return _ptr; }
95  bool operator !() const { return !_ptr; }
96  explicit operator bool() const { return _ptr != nullptr; }
97 
98 private:
99  T *_ptr;
100 };
101 
102 template<typename T>
103 inline bool operator==(T* a, const OwnPtr<T>& b)
104 {
105  return a == b.get();
106 }
107 
108 template<typename T>
109 inline bool operator==(const OwnPtr<T>& a, T* b)
110 {
111  return a.get() == b;
112 }
113 
114 template<typename T>
115 inline bool operator!=(T* a, const OwnPtr<T>& b)
116 {
117  return a != b.get();
118 }
119 
120 template<typename T>
121 inline bool operator!=(const OwnPtr<T>& a, T* b)
122 {
123  return a.get() != b;
124 }
125 
126 }
bool operator!() const
Definition: OwnPtr.h:95
void clear()
Definition: OwnPtr.h:76
OwnPtr< T > & operator=(U *other)
Definition: OwnPtr.h:66
bool operator!=(T *a, const OwnPtr< T > &b)
Definition: OwnPtr.h:115
T * leak()
Definition: OwnPtr.h:81
OwnPtr(std::nullptr_t)
Definition: OwnPtr.h:43
T * operator->() const
Definition: OwnPtr.h:94
OwnPtr(T *ptr)
Definition: OwnPtr.h:52
T & operator*() const
Definition: OwnPtr.h:93
OwnPtr(OwnPtr< U > &&other)
Definition: OwnPtr.h:50
OwnPtr< T > & operator=(std::nullptr_t)
Definition: OwnPtr.h:54
OwnPtr< T > & operator=(OwnPtr< U > &&other)
Definition: OwnPtr.h:57
bool operator==(T *a, const OwnPtr< T > &b)
Definition: OwnPtr.h:103
T * get() const
Definition: OwnPtr.h:88