libMesh
libmesh_singleton.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 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 SingletonList & get_singleton_cache()
40 {
41  static SingletonList singleton_cache;
42  return singleton_cache;
43 }
44 
45 typedef std::vector<Singleton::Setup *> SetupList;
46 SetupList & get_setup_cache()
47 {
48  static SetupList setup_cache;
49  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 
62 {
63  get_singleton_cache().push_back (this);
64 }
65 
66 
67 
69 {
70  get_setup_cache().push_back (this);
71 }
72 
73 
74 
76 {
77  SetupList & setup_cache = get_setup_cache();
78 
79  for (auto & item : setup_cache)
80  {
81  libmesh_assert (item);
82  item->setup();
83  }
84 }
85 
86 
87 
89 {
90  SingletonList & singleton_cache = get_singleton_cache();
91 
92  for (auto & item : as_range(singleton_cache.rbegin(),
93  singleton_cache.rend()))
94  {
95  libmesh_assert (item);
96  delete item;
97  item = nullptr;
98  }
99 
100  singleton_cache.clear();
101 }
102 
103 
104 
105 } // namespace libMesh
Singleton()
Constructor.
The libMesh namespace provides an interface to certain functionality in the library.
static void setup()
Setup function.
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
Helper function that allows us to treat a homogenous pair as a range.
Definition: simple_range.h:57
libmesh_assert(ctx)
static void cleanup()
Cleanup function.