libMesh
reference_counter.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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 // C++ includes
21 #include <iostream>
22 #include <sstream>
23 
24 // Local includes
25 #include "libmesh/reference_counter.h"
26 
27 namespace libMesh
28 {
29 
30 
31 
32 // ------------------------------------------------------------
33 // ReferenceCounter class static member initializations
34 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
35 
37 
38 #endif
39 
41 Threads::atomic<unsigned int> ReferenceCounter::_n_objects;
43 
44 
45 // ------------------------------------------------------------
46 // ReferenceCounter class members
48 {
49 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
50 
51  std::ostringstream oss;
52 
53  oss << '\n'
54  << " ---------------------------------------------------------------------------- \n"
55  << "| Reference count information |\n"
56  << " ---------------------------------------------------------------------------- \n";
57 
58  for (Counts::iterator it = _counts.begin();
59  it != _counts.end(); ++it)
60  {
61  const std::string name(it->first);
62  const unsigned int creations = it->second.first;
63  const unsigned int destructions = it->second.second;
64 
65  oss << "| " << name << " reference count information:\n"
66  << "| Creations: " << creations << '\n'
67  << "| Destructions: " << destructions << '\n';
68  }
69 
70  oss << " ---------------------------------------------------------------------------- \n";
71 
72  return oss.str();
73 
74 #else
75 
76  return "";
77 
78 #endif
79 }
80 
81 
82 
83 
84 
85 // avoid unused variable warnings
86 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
87 
88 void ReferenceCounter::print_info (std::ostream & out_stream)
89 {
91  out_stream << ReferenceCounter::get_info();
92 }
93 
94 #else
95 
96 void ReferenceCounter::print_info (std::ostream & /* out_stream */)
97 {}
98 
99 #endif
100 
102 {
103  _enable_print_counter = true;
104  return;
105 }
106 
108 {
109  _enable_print_counter = false;
110  return;
111 }
112 
113 } // namespace libMesh
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:39
static Threads::atomic< unsigned int > _n_objects
The number of objects.
The libMesh namespace provides an interface to certain functionality in the library.
std::map< std::string, std::pair< unsigned int, unsigned int > > Counts
Data structure to log the information.
static std::string get_info()
Gets a string containing the reference information.
static Threads::spin_mutex _mutex
Mutual exclusion object to enable thread-safe reference counting.
static bool _enable_print_counter
Flag to control whether reference count information is printed when print_info is called...
static void enable_print_counter_info()
Methods to enable/disable the reference counter output from print_info()
static void print_info(std::ostream &out=libMesh::out)
Prints the reference information, by default to libMesh::out.
tbb::spin_mutex spin_mutex
Spin mutex.
Definition: threads_tbb.h:209
static Counts _counts
Actually holds the data.
static void disable_print_counter_info()