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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: George Riley <riley@ece.gatech.edu>
7 * Gustavo Carneiro <gjcarneiro@gmail.com>,
8 * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
9 */
10#ifndef SIMPLE_REF_COUNT_H
11#define SIMPLE_REF_COUNT_H
12
13#include "assert.h"
14#include "default-deleter.h"
15
16#include <limits>
17#include <stdint.h>
18
19/**
20 * @file
21 * @ingroup ptr
22 * ns3::SimpleRefCount declaration and template implementation.
23 */
24
25namespace ns3
26{
27
28/**
29 * @ingroup ptr
30 * @brief Empty class, used as a default parent class for SimpleRefCount
31 *
32 * Inheritance graph was not generated because of its size.
33 * @hideinheritancegraph
34 */
35class Empty
36{
37};
38
39/**
40 * @ingroup ptr
41 * @brief A template-based reference counting class
42 *
43 * This template can be used to give reference-counting powers
44 * to a class. This template does not require this class to
45 * have a virtual destructor or a specific (or any) parent class.
46 *
47 * @note If you are moving to this template from the RefCountBase class,
48 * you need to be careful to mark appropriately your destructor virtual
49 * if needed. i.e., if your class has subclasses, _do_ mark your destructor
50 * virtual.
51 *
52 *
53 * This template takes 3 arguments but only the first argument is
54 * mandatory:
55 *
56 * @tparam T \explicit The typename of the subclass which derives
57 * from this template class. Yes, this is weird but it's a
58 * common C++ template pattern whose name is CRTP (Curiously
59 * Recursive Template Pattern)
60 * @tparam PARENT \explicit The typename of the parent of this template.
61 * By default, this typename is "'ns3::Empty'" which is an empty
62 * class: compilers which implement the EBCO optimization (empty
63 * base class optimization) will make this a no-op
64 * @tparam DELETER \explicit The typename of a class which implements
65 * a public static method named 'Delete'. This method will be called
66 * whenever the SimpleRefCount template detects that no references
67 * to the object it manages exist anymore.
68 *
69 * Interesting users of this class include ns3::Object as well as ns3::Packet.
70 *
71 * Inheritance graph was not generated because of its size.
72 * @hideinheritancegraph
73 */
74template <typename T, typename PARENT = Empty, typename DELETER = DefaultDeleter<T>>
75class SimpleRefCount : public PARENT
76{
77 public:
78 /** Default constructor. */
80 : m_count(1)
81 {
82 }
83
84 /**
85 * Copy constructor
86 * @param [in] o The object to copy into this one.
87 */
88 SimpleRefCount(const SimpleRefCount& o [[maybe_unused]])
89 : m_count(1)
90 {
91 }
92
93 /**
94 * Assignment operator
95 * @param [in] o The object to copy
96 * @returns The copy of \pname{o}
97 */
98 SimpleRefCount& operator=(const SimpleRefCount& o [[maybe_unused]])
99 {
100 return *this;
101 }
102
103 /**
104 * Increment the reference count. This method should not be called
105 * by user code. SimpleRefCount instances are expected to be used in
106 * conjunction with the Ptr template which would make calling Ref
107 * unnecessary and dangerous.
108 */
109 inline void Ref() const
110 {
111 NS_ASSERT(m_count < std::numeric_limits<uint32_t>::max());
112 m_count++;
113 }
114
115 /**
116 * Decrement the reference count. This method should not be called
117 * by user code. SimpleRefCount instances are expected to be used in
118 * conjunction with the Ptr template which would make calling Ref
119 * unnecessary and dangerous.
120 */
121 inline void Unref() const
122 {
123 m_count--;
124 if (m_count == 0)
125 {
126 DELETER::Delete(static_cast<T*>(const_cast<SimpleRefCount*>(this)));
127 }
128 }
129
130 /**
131 * Get the reference count of the object.
132 * Normally not needed; for language bindings.
133 *
134 * @return The reference count.
135 */
137 {
138 return m_count;
139 }
140
141 private:
142 /**
143 * The reference count.
144 *
145 * @internal
146 * Note we make this mutable so that the const methods can still
147 * change it.
148 */
150};
151
152} // namespace ns3
153
154#endif /* SIMPLE_REF_COUNT_H */
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
Empty class, used as a default parent class for SimpleRefCount.
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:55
Every class exported by the ns3 library is enclosed in the ns3 namespace.