A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
simple-ref-count.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 Georgia Tech Research Corporation, INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Authors: George Riley <riley@ece.gatech.edu>
18 * Gustavo Carneiro <gjcarneiro@gmail.com>,
19 * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20 */
21#ifndef SIMPLE_REF_COUNT_H
22#define SIMPLE_REF_COUNT_H
23
24#include "assert.h"
25#include "default-deleter.h"
26
27#include <limits>
28#include <stdint.h>
29
30/**
31 * \file
32 * \ingroup ptr
33 * ns3::SimpleRefCount declaration and template implementation.
34 */
35
36namespace ns3
37{
38
39/**
40 * \ingroup ptr
41 * \brief Empty class, used as a default parent class for SimpleRefCount
42 */
43class Empty
44{
45};
46
47/**
48 * \ingroup ptr
49 * \brief A template-based reference counting class
50 *
51 * This template can be used to give reference-counting powers
52 * to a class. This template does not require this class to
53 * have a virtual destructor or a specific (or any) parent class.
54 *
55 * \note If you are moving to this template from the RefCountBase class,
56 * you need to be careful to mark appropriately your destructor virtual
57 * if needed. i.e., if your class has subclasses, _do_ mark your destructor
58 * virtual.
59 *
60 *
61 * This template takes 3 arguments but only the first argument is
62 * mandatory:
63 *
64 * \tparam T \explicit The typename of the subclass which derives
65 * from this template class. Yes, this is weird but it's a
66 * common C++ template pattern whose name is CRTP (Curiously
67 * Recursive Template Pattern)
68 * \tparam PARENT \explicit The typename of the parent of this template.
69 * By default, this typename is "'ns3::Empty'" which is an empty
70 * class: compilers which implement the EBCO optimization (empty
71 * base class optimization) will make this a no-op
72 * \tparam DELETER \explicit The typename of a class which implements
73 * a public static method named 'Delete'. This method will be called
74 * whenever the SimpleRefCount template detects that no references
75 * to the object it manages exist anymore.
76 *
77 * Interesting users of this class include ns3::Object as well as ns3::Packet.
78 */
79template <typename T, typename PARENT = Empty, typename DELETER = DefaultDeleter<T>>
80class SimpleRefCount : public PARENT
81{
82 public:
83 /** Default constructor. */
85 : m_count(1)
86 {
87 }
88
89 /**
90 * Copy constructor
91 * \param [in] o The object to copy into this one.
92 */
93 SimpleRefCount(const SimpleRefCount& o [[maybe_unused]])
94 : m_count(1)
95 {
96 }
97
98 /**
99 * Assignment operator
100 * \param [in] o The object to copy
101 * \returns The copy of \pname{o}
102 */
103 SimpleRefCount& operator=(const SimpleRefCount& o [[maybe_unused]])
104 {
105 return *this;
106 }
107
108 /**
109 * Increment the reference count. This method should not be called
110 * by user code. SimpleRefCount instances are expected to be used in
111 * conjunction with the Ptr template which would make calling Ref
112 * unnecessary and dangerous.
113 */
114 inline void Ref() const
115 {
116 NS_ASSERT(m_count < std::numeric_limits<uint32_t>::max());
117 m_count++;
118 }
119
120 /**
121 * Decrement the reference count. This method should not be called
122 * by user code. SimpleRefCount instances are expected to be used in
123 * conjunction with the Ptr template which would make calling Ref
124 * unnecessary and dangerous.
125 */
126 inline void Unref() const
127 {
128 m_count--;
129 if (m_count == 0)
130 {
131 DELETER::Delete(static_cast<T*>(const_cast<SimpleRefCount*>(this)));
132 }
133 }
134
135 /**
136 * Get the reference count of the object.
137 * Normally not needed; for language bindings.
138 *
139 * \return The reference count.
140 */
142 {
143 return m_count;
144 }
145
146 private:
147 /**
148 * The reference count.
149 *
150 * \internal
151 * Note we make this mutable so that the const methods can still
152 * change it.
153 */
155};
156
157} // namespace ns3
158
159#endif /* SIMPLE_REF_COUNT_H */
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
Empty class, used as a default parent class for SimpleRefCount.
A template-based reference counting class.
uint32_t GetReferenceCount() const
Get the reference count of the object.
SimpleRefCount()
Default constructor.
SimpleRefCount(const SimpleRefCount &o)
Copy constructor.
void Ref() const
Increment the reference count.
SimpleRefCount & operator=(const SimpleRefCount &o)
Assignment operator.
uint32_t m_count
The reference count.
void Unref() const
Decrement the reference count.
ns3::DefaultDeleter declaration and template implementation, for reference-counted smart pointers.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
Every class exported by the ns3 library is enclosed in the ns3 namespace.