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 */
32class Empty
33{
34};
35
36/**
37 * \ingroup ptr
38 * \brief A template-based reference counting class
39 *
40 * This template can be used to give reference-counting powers
41 * to a class. This template does not require this class to
42 * have a virtual destructor or a specific (or any) parent class.
43 *
44 * \note If you are moving to this template from the RefCountBase class,
45 * you need to be careful to mark appropriately your destructor virtual
46 * if needed. i.e., if your class has subclasses, _do_ mark your destructor
47 * virtual.
48 *
49 *
50 * This template takes 3 arguments but only the first argument is
51 * mandatory:
52 *
53 * \tparam T \explicit The typename of the subclass which derives
54 * from this template class. Yes, this is weird but it's a
55 * common C++ template pattern whose name is CRTP (Curiously
56 * Recursive Template Pattern)
57 * \tparam PARENT \explicit The typename of the parent of this template.
58 * By default, this typename is "'ns3::Empty'" which is an empty
59 * class: compilers which implement the EBCO optimization (empty
60 * base class optimization) will make this a no-op
61 * \tparam DELETER \explicit The typename of a class which implements
62 * a public static method named 'Delete'. This method will be called
63 * whenever the SimpleRefCount template detects that no references
64 * to the object it manages exist anymore.
65 *
66 * Interesting users of this class include ns3::Object as well as ns3::Packet.
67 */
68template <typename T, typename PARENT = Empty, typename DELETER = DefaultDeleter<T>>
69class SimpleRefCount : public PARENT
70{
71 public:
72 /** Default constructor. */
74 : m_count(1)
75 {
76 }
77
78 /**
79 * Copy constructor
80 * \param [in] o The object to copy into this one.
81 */
82 SimpleRefCount(const SimpleRefCount& o [[maybe_unused]])
83 : m_count(1)
84 {
85 }
86
87 /**
88 * Assignment operator
89 * \param [in] o The object to copy
90 * \returns The copy of \pname{o}
91 */
92 SimpleRefCount& operator=(const SimpleRefCount& o [[maybe_unused]])
93 {
94 return *this;
95 }
96
97 /**
98 * Increment the reference count. This method should not be called
99 * by user code. SimpleRefCount instances are expected to be used in
100 * conjunction with the Ptr template which would make calling Ref
101 * unnecessary and dangerous.
102 */
103 inline void Ref() const
104 {
105 NS_ASSERT(m_count < std::numeric_limits<uint32_t>::max());
106 m_count++;
107 }
108
109 /**
110 * Decrement the reference count. This method should not be called
111 * by user code. SimpleRefCount instances are expected to be used in
112 * conjunction with the Ptr template which would make calling Ref
113 * unnecessary and dangerous.
114 */
115 inline void Unref() const
116 {
117 m_count--;
118 if (m_count == 0)
119 {
120 DELETER::Delete(static_cast<T*>(const_cast<SimpleRefCount*>(this)));
121 }
122 }
123
124 /**
125 * Get the reference count of the object.
126 * Normally not needed; for language bindings.
127 *
128 * \return The reference count.
129 */
131 {
132 return m_count;
133 }
134
135 private:
136 /**
137 * The reference count.
138 *
139 * \internal
140 * Note we make this mutable so that the const methods can still
141 * change it.
142 */
144};
145
146} // namespace ns3
147
148#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:55
Every class exported by the ns3 library is enclosed in the ns3 namespace.