LCOV - code coverage report
Current view: top level - src/base - libmesh_singleton.C (source / functions) Hit Total Coverage
Test: libMesh/libmesh: #4229 (6a9aeb) with base 727f46 Lines: 27 27 100.0 %
Date: 2025-08-19 19:27:09 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // The libMesh Finite Element Library.
       2             : // Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
       3             : 
       4             : // This library is free software; you can redistribute it and/or
       5             : // modify it under the terms of the GNU Lesser General Public
       6             : // License as published by the Free Software Foundation; either
       7             : // version 2.1 of the License, or (at your option) any later version.
       8             : 
       9             : // This library is distributed in the hope that it will be useful,
      10             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             : // Lesser General Public License for more details.
      13             : 
      14             : // You should have received a copy of the GNU Lesser General Public
      15             : // License along with this library; if not, write to the Free Software
      16             : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      17             : 
      18             : 
      19             : 
      20             : // Local includes
      21             : #include "libmesh/libmesh_singleton.h"
      22             : #include "libmesh/threads.h"
      23             : #include "libmesh/simple_range.h"
      24             : 
      25             : // C/C++ includes
      26             : #include <vector>
      27             : 
      28             : 
      29             : // --------------------------------------------------------
      30             : // Local anonymous namespace to hold miscellaneous bits
      31             : namespace
      32             : {
      33             : using namespace libMesh;
      34             : 
      35             : // global list of runtime Singleton objects - created dynamically,
      36             : // cleaned up in reverse order.
      37             : typedef std::vector<Singleton *> SingletonList;
      38             : 
      39       33125 : SingletonList & get_singleton_cache()
      40             : {
      41       33125 :   static SingletonList singleton_cache;
      42       33125 :   return singleton_cache;
      43             : }
      44             : 
      45             : typedef std::vector<Singleton::Setup *> SetupList;
      46       49159 : SetupList & get_setup_cache()
      47             : {
      48       49159 :   static SetupList setup_cache;
      49       49159 :   return setup_cache;
      50             : }
      51             : 
      52             : } // end anonymous namespace
      53             : 
      54             : 
      55             : 
      56             : // --------------------------------------------------------
      57             : // Local anonymous namespace to hold miscellaneous bits
      58             : namespace libMesh
      59             : {
      60             : 
      61       16744 : Singleton::Singleton ()
      62             : {
      63       16744 :   get_singleton_cache().push_back (this);
      64       16744 : }
      65             : 
      66             : 
      67             : 
      68       32778 : Singleton::Setup::Setup ()
      69             : {
      70       32778 :   get_setup_cache().push_back (this);
      71       32778 : }
      72             : 
      73             : 
      74             : 
      75       16381 : void Singleton::setup ()
      76             : {
      77       16381 :   SetupList & setup_cache = get_setup_cache();
      78             : 
      79       49143 :   for (auto & item : setup_cache)
      80             :     {
      81         924 :       libmesh_assert (item);
      82       32762 :       item->setup();
      83             :     }
      84       16381 : }
      85             : 
      86             : 
      87             : 
      88       16381 : void Singleton::cleanup ()
      89             : {
      90       16381 :   SingletonList & singleton_cache = get_singleton_cache();
      91             : 
      92         938 :   for (auto & item : as_range(singleton_cache.rbegin(),
      93       33587 :                               singleton_cache.rend()))
      94             :     {
      95         476 :       libmesh_assert (item);
      96       16744 :       delete item;
      97       16744 :       item = nullptr;
      98             :     }
      99             : 
     100         462 :   singleton_cache.clear();
     101       16381 : }
     102             : 
     103             : 
     104             : 
     105             : } // namespace libMesh

Generated by: LCOV version 1.14