libMesh
Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Static Protected Attributes | List of all members
libMesh::ReferenceCountedObject< T > Class Template Reference

This class implements reference counting. More...

#include <reference_counted_object.h>

Inheritance diagram for libMesh::ReferenceCountedObject< T >:
[legend]

Public Member Functions

 ~ReferenceCountedObject ()
 Destructor. More...
 

Static Public Member Functions

static std::string get_info ()
 Gets a string containing the reference information. More...
 
static void print_info (std::ostream &out_stream=libMesh::out)
 Prints the reference information, by default to libMesh::out. More...
 
static unsigned int n_objects ()
 Prints the number of outstanding (created, but not yet destroyed) objects. More...
 
static void enable_print_counter_info ()
 Methods to enable/disable the reference counter output from print_info() More...
 
static void disable_print_counter_info ()
 

Protected Types

typedef std::map< std::string, std::pair< unsigned int, unsigned int > > Counts
 Data structure to log the information. More...
 

Protected Member Functions

 ReferenceCountedObject ()
 Constructor. More...
 
 ReferenceCountedObject (const ReferenceCountedObject &other)
 Also, increment the counter if the copy-constructor is called. More...
 
 ReferenceCountedObject (ReferenceCountedObject &&other) noexcept
 Move constructor, must be declared noexcept. More...
 
ReferenceCountedObjectoperator= (const ReferenceCountedObject &)
 Copy assignment operator does nothing - we're copying an already-allocated object over an already-allocated object, so the counts for this class shouldn't change. More...
 
void increment_constructor_count (const std::string &name) noexcept
 Increments the construction counter. More...
 
void increment_destructor_count (const std::string &name) noexcept
 Increments the destruction counter. More...
 

Static Protected Attributes

static Counts _counts
 Actually holds the data. More...
 
static Threads::atomic< unsigned int_n_objects
 The number of objects. More...
 
static Threads::spin_mutex _mutex
 Mutual exclusion object to enable thread-safe reference counting. More...
 
static bool _enable_print_counter = true
 Flag to control whether reference count information is printed when print_info is called. More...
 

Detailed Description

template<typename T>
class libMesh::ReferenceCountedObject< T >

This class implements reference counting.

Any class that is properly derived from this class will get reference counted, provided that the library is configured with –enable-reference-counting and you are compiling with DEBUG defined. For example, the following is sufficient to define the class Foo as a reference counted class:

class Foo : public ReferenceCountedObject<Foo>
{
public:
Foo () {}
~Foo () {}
void bar ();
private:
};
If the library is configured with –disable-reference-counting or DEBUG is not defined then this class does nothing. All members are inlined and empty, so they should effectively disappear.
Author
Benjamin S. Kirk
Date
2002-2007

Definition at line 65 of file reference_counted_object.h.

Member Typedef Documentation

◆ Counts

typedef std::map<std::string, std::pair<unsigned int, unsigned int> > libMesh::ReferenceCounter::Counts
protectedinherited

Data structure to log the information.

The log is identified by the class name.

Definition at line 119 of file reference_counter.h.

Constructor & Destructor Documentation

◆ ReferenceCountedObject() [1/3]

template<typename T>
libMesh::ReferenceCountedObject< T >::ReferenceCountedObject ( )
inlineprotected

Constructor.

Protected so that you cannot instantiate a ReferenceCountedObject, only derive from it.

Definition at line 74 of file reference_counted_object.h.

75  {
76 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
77 
78  increment_constructor_count(typeid(T).name());
79 
80 #endif
81  }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
void increment_constructor_count(const std::string &name) noexcept
Increments the construction counter.

◆ ReferenceCountedObject() [2/3]

template<typename T>
libMesh::ReferenceCountedObject< T >::ReferenceCountedObject ( const ReferenceCountedObject< T > &  other)
inlineprotected

Also, increment the counter if the copy-constructor is called.

Definition at line 86 of file reference_counted_object.h.

87  : ReferenceCounter(other)
88  {
89 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
90 
91  increment_constructor_count(typeid(T).name());
92 
93 #endif
94  }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
void increment_constructor_count(const std::string &name) noexcept
Increments the construction counter.
ReferenceCounter()
Constructors.

◆ ReferenceCountedObject() [3/3]

template<typename T>
libMesh::ReferenceCountedObject< T >::ReferenceCountedObject ( ReferenceCountedObject< T > &&  other)
inlineprotectednoexcept

Move constructor, must be declared noexcept.

Definition at line 99 of file reference_counted_object.h.

100  : ReferenceCounter(std::move(other))
101  {
102 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
103 
104  increment_constructor_count(typeid(T).name());
105 
106 #endif
107  }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
void increment_constructor_count(const std::string &name) noexcept
Increments the construction counter.
ReferenceCounter()
Constructors.

◆ ~ReferenceCountedObject()

template<typename T>
libMesh::ReferenceCountedObject< T >::~ReferenceCountedObject ( )
inline

Destructor.

Definition at line 127 of file reference_counted_object.h.

128  {
129 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
130 
131  increment_destructor_count(typeid(T).name());
132 
133 #endif
134  }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
void increment_destructor_count(const std::string &name) noexcept
Increments the destruction counter.

Member Function Documentation

◆ disable_print_counter_info()

void libMesh::ReferenceCounter::disable_print_counter_info ( )
staticinherited

Definition at line 100 of file reference_counter.C.

References libMesh::ReferenceCounter::_enable_print_counter.

101 {
102  _enable_print_counter = false;
103  return;
104 }
static bool _enable_print_counter
Flag to control whether reference count information is printed when print_info is called...

◆ enable_print_counter_info()

void libMesh::ReferenceCounter::enable_print_counter_info ( )
staticinherited

Methods to enable/disable the reference counter output from print_info()

Definition at line 94 of file reference_counter.C.

References libMesh::ReferenceCounter::_enable_print_counter.

95 {
96  _enable_print_counter = true;
97  return;
98 }
static bool _enable_print_counter
Flag to control whether reference count information is printed when print_info is called...

◆ get_info()

std::string libMesh::ReferenceCounter::get_info ( )
staticinherited

Gets a string containing the reference information.

Definition at line 47 of file reference_counter.C.

References libMesh::ReferenceCounter::_counts, and libMesh::Quality::name().

Referenced by libMesh::ReferenceCounter::print_info().

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 (const auto & [name, cd] : _counts)
59  oss << "| " << name << " reference count information:\n"
60  << "| Creations: " << cd.first << '\n'
61  << "| Destructions: " << cd.second << '\n';
62 
63  oss << " ---------------------------------------------------------------------------- \n";
64 
65  return oss.str();
66 
67 #else
68 
69  return "";
70 
71 #endif
72 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
static Counts _counts
Actually holds the data.

◆ increment_constructor_count()

void libMesh::ReferenceCounter::increment_constructor_count ( const std::string &  name)
inlineprotectednoexceptinherited

Increments the construction counter.

Should be called in the constructor of any derived class that will be reference counted.

Definition at line 183 of file reference_counter.h.

References libMesh::err, libMesh::BasicOStreamProxy< charT, traits >::get(), libMesh::Quality::name(), and libMesh::Threads::spin_mtx.

Referenced by libMesh::ReferenceCountedObject< RBParametrized >::ReferenceCountedObject().

184 {
185  libmesh_try
186  {
187  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
188  std::pair<unsigned int, unsigned int> & p = _counts[name];
189  p.first++;
190  }
191  libmesh_catch (...)
192  {
193  auto stream = libMesh::err.get();
194  stream->exceptions(stream->goodbit); // stream must not throw
195  libMesh::err << "Encountered unrecoverable error while calling "
196  << "ReferenceCounter::increment_constructor_count() "
197  << "for a(n) " << name << " object." << std::endl;
198  std::terminate();
199  }
200 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
OStreamProxy err
static Counts _counts
Actually holds the data.
streamT * get()
Rather than implement every ostream/ios/ios_base function, we&#39;ll be lazy and make esoteric uses go th...
spin_mutex spin_mtx
A convenient spin mutex object which can be used for obtaining locks.
Definition: threads.C:30

◆ increment_destructor_count()

void libMesh::ReferenceCounter::increment_destructor_count ( const std::string &  name)
inlineprotectednoexceptinherited

Increments the destruction counter.

Should be called in the destructor of any derived class that will be reference counted.

Definition at line 207 of file reference_counter.h.

References libMesh::err, libMesh::BasicOStreamProxy< charT, traits >::get(), libMesh::Quality::name(), and libMesh::Threads::spin_mtx.

Referenced by libMesh::ReferenceCountedObject< RBParametrized >::~ReferenceCountedObject().

208 {
209  libmesh_try
210  {
211  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
212  std::pair<unsigned int, unsigned int> & p = _counts[name];
213  p.second++;
214  }
215  libmesh_catch (...)
216  {
217  auto stream = libMesh::err.get();
218  stream->exceptions(stream->goodbit); // stream must not throw
219  libMesh::err << "Encountered unrecoverable error while calling "
220  << "ReferenceCounter::increment_destructor_count() "
221  << "for a(n) " << name << " object." << std::endl;
222  std::terminate();
223  }
224 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
OStreamProxy err
static Counts _counts
Actually holds the data.
streamT * get()
Rather than implement every ostream/ios/ios_base function, we&#39;ll be lazy and make esoteric uses go th...
spin_mutex spin_mtx
A convenient spin mutex object which can be used for obtaining locks.
Definition: threads.C:30

◆ n_objects()

static unsigned int libMesh::ReferenceCounter::n_objects ( )
inlinestaticinherited

Prints the number of outstanding (created, but not yet destroyed) objects.

Definition at line 85 of file reference_counter.h.

References libMesh::ReferenceCounter::_n_objects.

Referenced by libMesh::LibMeshInit::~LibMeshInit().

86  { return _n_objects; }
static Threads::atomic< unsigned int > _n_objects
The number of objects.

◆ operator=()

template<typename T>
ReferenceCountedObject& libMesh::ReferenceCountedObject< T >::operator= ( const ReferenceCountedObject< T > &  )
inlineprotected

Copy assignment operator does nothing - we're copying an already-allocated object over an already-allocated object, so the counts for this class shouldn't change.

There are also no non-static members in this or the base class to copy, so there is nothing for us to actually do.

Definition at line 116 of file reference_counted_object.h.

117  {
118  return *this;
119  }

◆ print_info()

void libMesh::ReferenceCounter::print_info ( std::ostream &  out_stream = libMesh::out)
staticinherited

Prints the reference information, by default to libMesh::out.

Definition at line 81 of file reference_counter.C.

References libMesh::ReferenceCounter::_enable_print_counter, and libMesh::ReferenceCounter::get_info().

Referenced by libMesh::LibMeshInit::~LibMeshInit().

82 {
84  out_stream << ReferenceCounter::get_info();
85 }
static std::string get_info()
Gets a string containing the reference information.
static bool _enable_print_counter
Flag to control whether reference count information is printed when print_info is called...

Member Data Documentation

◆ _counts

ReferenceCounter::Counts libMesh::ReferenceCounter::_counts
staticprotectedinherited

Actually holds the data.

Definition at line 124 of file reference_counter.h.

Referenced by libMesh::ReferenceCounter::get_info().

◆ _enable_print_counter

bool libMesh::ReferenceCounter::_enable_print_counter = true
staticprotectedinherited

Flag to control whether reference count information is printed when print_info is called.

Definition at line 143 of file reference_counter.h.

Referenced by libMesh::ReferenceCounter::disable_print_counter_info(), libMesh::ReferenceCounter::enable_print_counter_info(), and libMesh::ReferenceCounter::print_info().

◆ _mutex

Threads::spin_mutex libMesh::ReferenceCounter::_mutex
staticprotectedinherited

Mutual exclusion object to enable thread-safe reference counting.

Definition at line 137 of file reference_counter.h.

◆ _n_objects

Threads::atomic< unsigned int > libMesh::ReferenceCounter::_n_objects
staticprotectedinherited

The number of objects.

Print the reference count information when the number returns to 0.

Definition at line 132 of file reference_counter.h.

Referenced by libMesh::ReferenceCounter::n_objects(), libMesh::ReferenceCounter::ReferenceCounter(), and libMesh::ReferenceCounter::~ReferenceCounter().


The documentation for this class was generated from the following file: