LCOV - code coverage report
Current view: top level - include/utils - CastUniquePointer.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 9a5f1f Lines: 22 30 73.3 %
Date: 2026-06-21 21:23:42 Functions: 15 15 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             : /**
      13             :  * These are reworked from https://stackoverflow.com/a/11003103
      14             :  *
      15             :  * New GitHub Repo here: https://github.com/friedmud/unique_ptr_cast
      16             :  */
      17             : 
      18             : #include <memory>
      19             : 
      20             : template <typename T_DEST, typename T_SRC, typename T_DELETER>
      21             : std::unique_ptr<T_DEST, T_DELETER>
      22           2 : dynamic_pointer_cast(std::unique_ptr<T_SRC, T_DELETER> & src)
      23             : {
      24           2 :   if (!src)
      25           0 :     return std::unique_ptr<T_DEST, T_DELETER>(nullptr);
      26             : 
      27           2 :   T_DEST * dest_ptr = dynamic_cast<T_DEST *>(src.get());
      28           2 :   if (!dest_ptr)
      29           2 :     return std::unique_ptr<T_DEST, T_DELETER>(nullptr);
      30             : 
      31           0 :   std::unique_ptr<T_DEST, T_DELETER> dest_temp(dest_ptr, std::move(src.get_deleter()));
      32             : 
      33           0 :   src.release();
      34             : 
      35           0 :   return dest_temp;
      36           0 : }
      37             : 
      38             : template <typename T_DEST, typename T_SRC, typename T_DELETER>
      39             : std::unique_ptr<T_DEST, T_DELETER>
      40             : dynamic_pointer_cast(std::unique_ptr<T_SRC, T_DELETER> && src)
      41             : {
      42             :   if (!src)
      43             :     return std::unique_ptr<T_DEST, T_DELETER>(nullptr);
      44             : 
      45             :   T_DEST * dest_ptr = dynamic_cast<T_DEST *>(src.get());
      46             :   if (!dest_ptr)
      47             :     return std::unique_ptr<T_DEST, T_DELETER>(nullptr);
      48             : 
      49             :   std::unique_ptr<T_DEST, T_DELETER> dest_temp(dest_ptr, std::move(src.get_deleter()));
      50             : 
      51             :   src.release();
      52             : 
      53             :   return dest_temp;
      54             : }
      55             : 
      56             : template <typename T_DEST, typename T_SRC>
      57             : std::unique_ptr<T_DEST>
      58       52528 : dynamic_pointer_cast(std::unique_ptr<T_SRC> & src)
      59             : {
      60       52528 :   if (!src)
      61           0 :     return std::unique_ptr<T_DEST>(nullptr);
      62             : 
      63       52528 :   T_DEST * dest_ptr = dynamic_cast<T_DEST *>(src.get());
      64       52528 :   if (!dest_ptr)
      65          93 :     return std::unique_ptr<T_DEST>(nullptr);
      66             : 
      67       52435 :   std::unique_ptr<T_DEST> dest_temp(dest_ptr);
      68             : 
      69       52435 :   src.release();
      70             : 
      71       52435 :   return dest_temp;
      72       52435 : }
      73             : 
      74             : template <typename T_DEST, typename T_SRC>
      75             : std::unique_ptr<T_DEST>
      76      118803 : dynamic_pointer_cast(std::unique_ptr<T_SRC> && src)
      77             : {
      78      118803 :   if (!src)
      79           0 :     return std::unique_ptr<T_DEST>(nullptr);
      80             : 
      81      118803 :   T_DEST * dest_ptr = dynamic_cast<T_DEST *>(src.get());
      82      118803 :   if (!dest_ptr)
      83           0 :     return std::unique_ptr<T_DEST>(nullptr);
      84             : 
      85      118803 :   std::unique_ptr<T_DEST> dest_temp(dest_ptr);
      86             : 
      87      118803 :   src.release();
      88             : 
      89      118803 :   return dest_temp;
      90      118803 : }

Generated by: LCOV version 1.14