LCOV - code coverage report
Current view: top level - include/utils - CastUniquePointer.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 22 30 73.3 %
Date: 2025-07-17 01:28:37 Functions: 13 13 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : #pragma once
      11             : /**
      12             :  * These are reworked from https://stackoverflow.com/a/11003103
      13             :  *
      14             :  * New GitHub Repo here: https://github.com/friedmud/unique_ptr_cast
      15             :  */
      16             : 
      17             : #include <memory>
      18             : 
      19             : template <typename T_DEST, typename T_SRC, typename T_DELETER>
      20             : std::unique_ptr<T_DEST, T_DELETER>
      21           1 : dynamic_pointer_cast(std::unique_ptr<T_SRC, T_DELETER> & src)
      22             : {
      23           1 :   if (!src)
      24           0 :     return std::unique_ptr<T_DEST, T_DELETER>(nullptr);
      25             : 
      26           1 :   T_DEST * dest_ptr = dynamic_cast<T_DEST *>(src.get());
      27           1 :   if (!dest_ptr)
      28           1 :     return std::unique_ptr<T_DEST, T_DELETER>(nullptr);
      29             : 
      30           0 :   std::unique_ptr<T_DEST, T_DELETER> dest_temp(dest_ptr, std::move(src.get_deleter()));
      31             : 
      32           0 :   src.release();
      33             : 
      34           0 :   return dest_temp;
      35           0 : }
      36             : 
      37             : template <typename T_DEST, typename T_SRC, typename T_DELETER>
      38             : std::unique_ptr<T_DEST, T_DELETER>
      39             : dynamic_pointer_cast(std::unique_ptr<T_SRC, T_DELETER> && src)
      40             : {
      41             :   if (!src)
      42             :     return std::unique_ptr<T_DEST, T_DELETER>(nullptr);
      43             : 
      44             :   T_DEST * dest_ptr = dynamic_cast<T_DEST *>(src.get());
      45             :   if (!dest_ptr)
      46             :     return std::unique_ptr<T_DEST, T_DELETER>(nullptr);
      47             : 
      48             :   std::unique_ptr<T_DEST, T_DELETER> dest_temp(dest_ptr, std::move(src.get_deleter()));
      49             : 
      50             :   src.release();
      51             : 
      52             :   return dest_temp;
      53             : }
      54             : 
      55             : template <typename T_DEST, typename T_SRC>
      56             : std::unique_ptr<T_DEST>
      57       47303 : dynamic_pointer_cast(std::unique_ptr<T_SRC> & src)
      58             : {
      59       47303 :   if (!src)
      60           0 :     return std::unique_ptr<T_DEST>(nullptr);
      61             : 
      62       47303 :   T_DEST * dest_ptr = dynamic_cast<T_DEST *>(src.get());
      63       47303 :   if (!dest_ptr)
      64          97 :     return std::unique_ptr<T_DEST>(nullptr);
      65             : 
      66       47206 :   std::unique_ptr<T_DEST> dest_temp(dest_ptr);
      67             : 
      68       47206 :   src.release();
      69             : 
      70       47206 :   return dest_temp;
      71       47206 : }
      72             : 
      73             : template <typename T_DEST, typename T_SRC>
      74             : std::unique_ptr<T_DEST>
      75      110878 : dynamic_pointer_cast(std::unique_ptr<T_SRC> && src)
      76             : {
      77      110878 :   if (!src)
      78           0 :     return std::unique_ptr<T_DEST>(nullptr);
      79             : 
      80      110878 :   T_DEST * dest_ptr = dynamic_cast<T_DEST *>(src.get());
      81      110878 :   if (!dest_ptr)
      82           0 :     return std::unique_ptr<T_DEST>(nullptr);
      83             : 
      84      110878 :   std::unique_ptr<T_DEST> dest_temp(dest_ptr);
      85             : 
      86      110878 :   src.release();
      87             : 
      88      110878 :   return dest_temp;
      89      110878 : }

Generated by: LCOV version 1.14