reference_counter.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 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 
36 
37 
45 // ------------------------------------------------------------
46 // ReferenceCounter class definition
48 {
49 protected:
50 
57 
58 public:
59 
64 
68  static std::string get_info ();
69 
73  static void print_info (std::ostream& out = libMesh::out);
74 
79  static unsigned int n_objects ()
80  { return _n_objects; }
81 
86  static void enable_print_counter_info();
87  static void disable_print_counter_info();
88 
89 
90 protected:
91 
92 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
93 
99  void increment_constructor_count (const std::string& name);
100 
106  void increment_destructor_count (const std::string& name);
107 
112  typedef std::map<std::string, std::pair<unsigned int,
113  unsigned int> > Counts;
114 
118  static Counts _counts;
119 
120 #endif
121 
127 
132 
138 };
139 
140 
141 
142 // ------------------------------------------------------------
143 // ReferenceCounter class inline methods
145 {
146  _n_objects++;
147 }
148 
149 
150 
152 {
153  _n_objects--;
154 }
155 
156 
157 
158 
159 
160 
161 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
162 inline
164 {
165  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
166  std::pair<unsigned int, unsigned int>& p = _counts[name];
167 
168  p.first++;
169 }
170 #endif
171 
172 
173 
174 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
175 inline
177 {
178  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
179  std::pair<unsigned int, unsigned int>& p = _counts[name];
180 
181  p.second++;
182 }
183 #endif
184 
185 
186 } // namespace libMesh
187 
188 
189 #endif // LIBMESH_REFERENCE_COUNTER_H

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:07 UTC

Hosted By:
SourceForge.net Logo