1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2020 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_COUNTED_OBJECT_H
21 #define LIBMESH_REFERENCE_COUNTED_OBJECT_H
22 
23 // Local includes
24 #include "libmesh/reference_counter.h"
25 
26 // C++ includes
27 #include <typeinfo>
28 
29 namespace libMesh
30 {
31 
32 /**
33  * This class implements reference counting. Any class that
34  * is properly derived from this class will get reference counted, provided
35  * that the library is configured with \p --enable-reference-counting
36  * and you are compiling with \p DEBUG defined.
37  * For example, the following is sufficient to define the class \p Foo
38  * as a reference counted class:
39  *
40  * \code
41  * class Foo : public ReferenceCountedObject<Foo>
42  * {
43  *  public:
44  *
45  *    Foo  () {}
46  *
47  *    ~Foo () {}
48  *
49  *    void bar ();
50  *
51  *  private:
52  * };
53  *
54  * \endcode
55  *
56  * \par
57  * If the library is configured with \p --disable-reference-counting
58  * or \p DEBUG is not defined then this class does nothing.
59  * All members are inlined and empty, so they should effectively disappear.
60  *
61  * \author Benjamin S. Kirk
62  * \date 2002-2007
63  */
64 template <typename T>
65 class ReferenceCountedObject : public ReferenceCounter
66 {
67 protected:
68 
69   /**
70    * Constructor. Protected so that you cannot
71    * instantiate a \p ReferenceCountedObject, only derive
72    * from it.
73    */
ReferenceCountedObject()74   ReferenceCountedObject ()
75   {
76 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
77 
78     increment_constructor_count(typeid(T).name());
79 
80 #endif
81   }
82 
83   /**
84    * Also, increment the counter if the copy-constructor is called.
85    */
ReferenceCountedObject(const ReferenceCountedObject & other)86   ReferenceCountedObject (const ReferenceCountedObject & other)
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   }
95 
96   /**
97    * Move constructor, must be declared noexcept.
98    */
ReferenceCountedObject(ReferenceCountedObject && other)99   ReferenceCountedObject(ReferenceCountedObject && other) noexcept
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   }
108 
109   /**
110    * Copy assignment operator does nothing - we're copying an
111    * already-allocated object over an already-allocated object, so the
112    * counts for this class shouldn't change. There are also no non-static
113    * members in this or the base class to copy, so there is nothing
114    * for us to actually do.
115    */
116   ReferenceCountedObject & operator= (const ReferenceCountedObject & /*other*/)
117   {
118     return *this;
119   }
120 
121 
122 public:
123 
124   /**
125    * Destructor.
126    */
~ReferenceCountedObject()127   ~ReferenceCountedObject ()
128   {
129 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
130 
131     increment_destructor_count(typeid(T).name());
132 
133 #endif
134   }
135 };
136 
137 
138 } // namespace libMesh
139 
140 
141 #endif // LIBMESH_REFERENCE_COUNTED_OBJECT_H
142