LCOV - code coverage report
Current view: top level - AP_HAL/utility - OwnPtr.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 36 37 97.3 %
Date: 2026-09-09 11:02:08 Functions: 52 86 60.5 %

          Line data    Source code
       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       10479 :     OwnPtr() : _ptr(nullptr) { }
      43           0 :     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       20969 :     OwnPtr(OwnPtr<U>&& other) : _ptr(other.leak()) { }
      51             : 
      52       21451 :     OwnPtr(T *ptr) : _ptr(ptr) { }
      53             : 
      54           2 :     OwnPtr<T>& operator=(std::nullptr_t) { clear(); return *this; }
      55             : 
      56             :     template<typename U>
      57       20951 :     OwnPtr<T>& operator=(OwnPtr<U>&& other)
      58             :     {
      59       20951 :         T *old = _ptr;
      60       20951 :         _ptr = other.leak();
      61       20951 :         delete old;
      62       20951 :         return *this;
      63             :     }
      64             : 
      65             :     template<typename U>
      66           4 :     OwnPtr<T>& operator=(U *other)
      67             :     {
      68           4 :         T *old = _ptr;
      69           4 :         _ptr = other;
      70           4 :         delete old;
      71           4 :         return *this;
      72             :     }
      73             : 
      74       52894 :     ~OwnPtr() { delete _ptr; }
      75             : 
      76           6 :     void clear()
      77             :     {
      78           6 :         delete leak();
      79           6 :     }
      80             : 
      81       41926 :     T *leak()
      82             :     {
      83       41926 :         T *old = _ptr;
      84       41926 :         _ptr = nullptr;
      85       41926 :         return old;
      86             :     }
      87             : 
      88          16 :     T *get() const
      89             :     {
      90          16 :         return _ptr;
      91             :     }
      92             : 
      93           2 :     T& operator*() const { return *_ptr; }
      94      490910 :     T *operator->() const { return _ptr; }
      95       20947 :     bool operator !() const { return !_ptr; }
      96           2 :     explicit operator bool() const { return _ptr != nullptr; }
      97             : 
      98             : private:
      99             :     T *_ptr;
     100             : };
     101             : 
     102             : template<typename T>
     103           2 : inline bool operator==(T* a, const OwnPtr<T>& b)
     104             : {
     105           2 :     return a == b.get();
     106             : }
     107             : 
     108             : template<typename T>
     109           2 : inline bool operator==(const OwnPtr<T>& a, T* b)
     110             : {
     111           2 :     return a.get() == b;
     112             : }
     113             : 
     114             : template<typename T>
     115           2 : inline bool operator!=(T* a, const OwnPtr<T>& b)
     116             : {
     117           2 :     return a != b.get();
     118             : }
     119             : 
     120             : template<typename T>
     121           2 : inline bool operator!=(const OwnPtr<T>& a, T* b)
     122             : {
     123           2 :     return a.get() != b;
     124             : }
     125             : 
     126             : }

Generated by: LCOV version 1.14