OwnPtr.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include <cstddef>
  19. namespace AP_HAL {
  20. /* Poor's man std::unique_ptr
  21. *
  22. * OwnPtr is a container for a pointer declaring ownership.
  23. *
  24. * The goal is to allow to own object, pass it around and automatically delete
  25. * the pointer when the container goes out of scope.
  26. *
  27. * In order to pass it around the move constructor or move assignment operator
  28. * must be used. operator*, operator-> and the get() method can be used to get
  29. * the pointer contained in the OwnPtr container.
  30. *
  31. * The OwnPtr name comes from similar class in WebKit, before they switched to
  32. * std::unique_ptr. The implementation is different/simpler. We need our own
  33. * implementation since the <memory> header (and thus std::unique_ptr) is not
  34. * compatible with the PX4 toolchain and/or nuttx headers.
  35. */
  36. template<typename T>
  37. class OwnPtr {
  38. public:
  39. OwnPtr() : _ptr(nullptr) { }
  40. OwnPtr(std::nullptr_t) : _ptr(nullptr) { }
  41. /* non-copyable */
  42. OwnPtr(const OwnPtr<T> &other) = delete;
  43. /* Allow construction from a derived class U */
  44. template<typename U>
  45. OwnPtr(OwnPtr<U>&& other) : _ptr(other.leak()) { }
  46. OwnPtr(T *ptr) : _ptr(ptr) { }
  47. OwnPtr<T>& operator=(std::nullptr_t) { clear(); return *this; }
  48. template<typename U>
  49. OwnPtr<T>& operator=(OwnPtr<U>&& other)
  50. {
  51. T *old = _ptr;
  52. _ptr = other.leak();
  53. delete old;
  54. return *this;
  55. }
  56. template<typename U>
  57. OwnPtr<T>& operator=(U *other)
  58. {
  59. T *old = _ptr;
  60. _ptr = other;
  61. delete old;
  62. return *this;
  63. }
  64. ~OwnPtr() { delete _ptr; }
  65. void clear()
  66. {
  67. delete leak();
  68. }
  69. T *leak()
  70. {
  71. T *old = _ptr;
  72. _ptr = nullptr;
  73. return old;
  74. }
  75. T *get() const
  76. {
  77. return _ptr;
  78. }
  79. T& operator*() const { return *_ptr; }
  80. T *operator->() const { return _ptr; }
  81. bool operator !() const { return !_ptr; }
  82. explicit operator bool() const { return _ptr != nullptr; }
  83. private:
  84. T *_ptr;
  85. };
  86. template<typename T>
  87. inline bool operator==(T* a, const OwnPtr<T>& b)
  88. {
  89. return a == b.get();
  90. }
  91. template<typename T>
  92. inline bool operator==(const OwnPtr<T>& a, T* b)
  93. {
  94. return a.get() == b;
  95. }
  96. template<typename T>
  97. inline bool operator!=(T* a, const OwnPtr<T>& b)
  98. {
  99. return a != b.get();
  100. }
  101. template<typename T>
  102. inline bool operator!=(const OwnPtr<T>& a, T* b)
  103. {
  104. return a.get() != b;
  105. }
  106. }