Matter SDK Coverage Report
Current view: top level - lib/support - ReferenceCountedPtr.h (source / functions) Coverage Total Hit
Test: SHA:4a1dff731dfd17f3623faad23ee07f07df60cba7 Lines: 100.0 % 30 30
Test Date: 2026-02-11 08:13:42 Functions: 96.2 % 52 50

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2025 Project CHIP Authors
       4              :  *
       5              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       6              :  *    you may not use this file except in compliance with the License.
       7              :  *    You may obtain a copy of the License at
       8              :  *
       9              :  *        http://www.apache.org/licenses/LICENSE-2.0
      10              :  *
      11              :  *    Unless required by applicable law or agreed to in writing, software
      12              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      13              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      14              :  *    See the License for the specific language governing permissions and
      15              :  *    limitations under the License.
      16              :  */
      17              : 
      18              : #pragma once
      19              : 
      20              : #include "ReferenceCountedHandle.h"
      21              : #include <lib/support/CodeUtils.h>
      22              : 
      23              : namespace chip {
      24              : 
      25              : /** ReferenceCountedPtr acts like a shared_ptr to an object derived from ReferenceCounted. */
      26              : template <typename T>
      27              : class ReferenceCountedPtr
      28              : {
      29              : public:
      30       137297 :     ReferenceCountedPtr() : mRefCounted(nullptr) {}
      31         1385 :     ReferenceCountedPtr(T * releasable) : mRefCounted(releasable ? releasable->Ref() : nullptr) {}
      32          226 :     ReferenceCountedPtr(const ReferenceCountedPtr & src) : ReferenceCountedPtr(src.mRefCounted) {}
      33              :     ReferenceCountedPtr(const ReferenceCountedHandle<T> & src) : ReferenceCountedPtr(&src.Get()) {}
      34          101 :     ReferenceCountedPtr(ReferenceCountedPtr && src) noexcept : mRefCounted(src.mRefCounted) { src.mRefCounted = nullptr; }
      35              : 
      36          730 :     ReferenceCountedPtr & operator=(const ReferenceCountedPtr & src)
      37              :     {
      38          730 :         Set(src.mRefCounted);
      39          730 :         return *this;
      40              :     }
      41          267 :     ReferenceCountedPtr & operator=(ReferenceCountedPtr && src) noexcept
      42              :     {
      43          267 :         if (this != &src)
      44              :         {
      45          267 :             Release();
      46          267 :             mRefCounted     = src.mRefCounted;
      47          267 :             src.mRefCounted = nullptr;
      48              :         }
      49          267 :         return *this;
      50              :     }
      51              : 
      52       138783 :     ~ReferenceCountedPtr() { Release(); }
      53              : 
      54         1104 :     inline T * operator->() const { return mRefCounted; }
      55          133 :     inline T & operator*() const { return *mRefCounted; }
      56              : 
      57          160 :     inline operator bool() { return mRefCounted != nullptr; }
      58         1673 :     inline bool IsNull() const { return mRefCounted == nullptr; }
      59              : 
      60           61 :     inline bool operator==(const ReferenceCountedPtr & other) const { return this->mRefCounted == other.mRefCounted; }
      61            2 :     inline bool operator!=(const ReferenceCountedPtr & other) const { return this->mRefCounted != other.mRefCounted; }
      62              :     inline bool operator==(const ReferenceCountedHandle<T> & other) const { return this->mRefCounted == &other.Get(); }
      63              :     inline bool operator!=(const ReferenceCountedHandle<T> & other) const { return this->mRefCounted != &other.Get(); }
      64           32 :     inline bool operator==(const T & other) const { return this->mRefCounted == &other; }
      65              :     inline bool operator!=(const T & other) const { return this->mRefCounted != &other; }
      66              : 
      67       139430 :     void Release()
      68              :     {
      69       139430 :         VerifyOrReturn(mRefCounted != nullptr);
      70          729 :         mRefCounted->Unref();
      71          729 :         mRefCounted = nullptr;
      72              :     }
      73              : 
      74          730 :     void Set(T * releasable)
      75              :     {
      76          730 :         if (mRefCounted != releasable)
      77              :         {
      78           89 :             Release();
      79           89 :             mRefCounted = releasable ? releasable->Ref() : nullptr;
      80              :         }
      81          730 :     }
      82              : 
      83              : protected:
      84              :     T * mRefCounted = nullptr;
      85              : };
      86              : 
      87              : } // namespace chip
        

Generated by: LCOV version 2.0-1