LCOV - code coverage report
Current view: top level - memilio/utils - memory.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 13 15 86.7 %
Date: 2024-11-18 12:45:26 Functions: 8 9 88.9 %

          Line data    Source code
       1             : /* 
       2             : * Copyright (C) 2020-2024 MEmilio
       3             : *
       4             : * Authors: Daniel Abele
       5             : *
       6             : * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
       7             : *
       8             : * Licensed under the Apache License, Version 2.0 (the "License");
       9             : * you may not use this file except in compliance with the License.
      10             : * You may obtain a copy of the License at
      11             : *
      12             : *     http://www.apache.org/licenses/LICENSE-2.0
      13             : *
      14             : * Unless required by applicable law or agreed to in writing, software
      15             : * distributed under the License is distributed on an "AS IS" BASIS,
      16             : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      17             : * See the License for the specific language governing permissions and
      18             : * limitations under the License.
      19             : */
      20             : #ifndef MEMORY_H
      21             : #define MEMORY_H
      22             : 
      23             : #include <utility>
      24             : 
      25             : namespace mio
      26             : {
      27             : 
      28             : /**
      29             :  * @brief A non-owning pointer
      30             :  *
      31             :  * This class is identical to a raw pointer. However, the sematics
      32             :  * of the name makes the ownership more obvious.
      33             :  * One difference to the raw pointer is that it can't be deleted.
      34             :  */
      35             : template <typename T>
      36             : class observer_ptr
      37             : {
      38             : public:
      39        1738 :     observer_ptr(T* p)
      40        1738 :         : m_raw_ptr(p)
      41             :     {
      42        1738 :     }
      43             : 
      44             :     observer_ptr(const observer_ptr& other) = default;
      45             : 
      46             :     /**
      47             :      * @brief Replaces the watched object
      48             :      */
      49             :     void reset(T* p = nullptr)
      50             :     {
      51             :         m_raw_ptr = p;
      52             :     }
      53             : 
      54             :     /**
      55             :      * @brief Returns a raw pointer to the watched object
      56             :      */
      57        1287 :     T* get() const
      58             :     {
      59        1287 :         return m_raw_ptr;
      60             :     }
      61             : 
      62           1 :     T* operator->() const
      63             :     {
      64           1 :         return m_raw_ptr;
      65             :     }
      66             : 
      67         446 :     T& operator*() const
      68             :     {
      69         446 :         return *m_raw_ptr;
      70             :     }
      71             : 
      72           0 :     operator bool() const
      73             :     {
      74           0 :         return m_raw_ptr != nullptr;
      75             :     }
      76             : 
      77           2 :     bool operator==(const observer_ptr& other) const
      78             :     {
      79           2 :         return m_raw_ptr == other.m_raw_ptr;
      80             :     }
      81           1 :     bool operator!=(const observer_ptr& other) const
      82             :     {
      83           1 :         return !(*this == other);
      84             :     }
      85             : 
      86             : private:
      87             :     T* m_raw_ptr;
      88             : };
      89             : 
      90             : template <typename T>
      91             : observer_ptr<T> make_observer(T* p)
      92             : {
      93             :     return observer_ptr<T>(p);
      94             : }
      95             : 
      96             : } // namespace mio
      97             : 
      98             : #endif // MEMORY_H

Generated by: LCOV version 1.14