reference_counter.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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 #ifndef LIBMESH_REFERENCE_COUNTER_H
21 #define LIBMESH_REFERENCE_COUNTER_H
22 
23 // Local includes
24 #include "libmesh/libmesh_config.h"
25 #include "libmesh/threads.h"
26 #include "libmesh/libmesh.h" // libMesh::on_command_line
27 
28 // C++ includes
29 #include <iostream>
30 #include <string>
31 #include <map>
32 
33 namespace libMesh
34 {
35 
45 {
46 protected:
47 
55 
56 
60  ReferenceCounter(ReferenceCounter && other) noexcept;
61 
62 public:
63 
68 
72  static std::string get_info ();
73 
77  static void print_info (std::ostream & out = libMesh::out);
78 
83  static unsigned int n_objects ()
84  { return _n_objects; }
85 
90  static void enable_print_counter_info();
91  static void disable_print_counter_info();
92 
93 
94 protected:
95 
96 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
97 
103  void increment_constructor_count (const std::string & name);
104 
110  void increment_destructor_count (const std::string & name);
111 
116  typedef std::map<std::string, std::pair<unsigned int,
117  unsigned int>> Counts;
118 
122  static Counts _counts;
123 
124 #endif
125 
131 
136 
142 };
143 
144 
145 
146 // ------------------------------------------------------------
147 // ReferenceCounter class inline methods
149 {
150  ++_n_objects;
151 }
152 
153 
154 
156 {
157  ++_n_objects;
158 }
159 
160 
161 
163 {
164  ++_n_objects;
165 }
166 
167 
168 
170 {
171  --_n_objects;
172 }
173 
174 
175 
176 
177 
178 
179 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
180 inline
182 {
183  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
184  std::pair<unsigned int, unsigned int> & p = _counts[name];
185 
186  p.first++;
187 }
188 #endif
189 
190 
191 
192 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
193 inline
195 {
196  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
197  std::pair<unsigned int, unsigned int> & p = _counts[name];
198 
199  p.second++;
200 }
201 #endif
202 
203 
204 } // namespace libMesh
205 
206 
207 #endif // LIBMESH_REFERENCE_COUNTER_H
std::string name(const ElemQuality q)
Definition: elem_quality.C:42
static Threads::atomic< unsigned int > _n_objects
static unsigned int n_objects()
void increment_constructor_count(const std::string &name)
std::map< std::string, std::pair< unsigned int, unsigned int > > Counts
static std::string get_info()
Common base for all objects whose creations/destructions are counted.
static Threads::spin_mutex _mutex
spin_mutex spin_mtx
Definition: threads.C:29
void increment_destructor_count(const std::string &name)
static void enable_print_counter_info()
static void print_info(std::ostream &out=libMesh::out)
static void disable_print_counter_info()
OStreamProxy out(std::cout)