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
36namespace ns3
37{
38
43class Empty
44{
45};
46
79template <typename T, typename PARENT = Empty, typename DELETER = DefaultDeleter<T>>
80class SimpleRefCount : public PARENT
81{
82 public:
85 : m_count(1)
86 {
87 }
88
93 SimpleRefCount(const SimpleRefCount& o [[maybe_unused]])
94 : m_count(1)
95 {
96 }
97
103 SimpleRefCount& operator=(const SimpleRefCount& o [[maybe_unused]])
104 {
105 return *this;
106 }
107
114 inline void Ref() const
115 {
116 NS_ASSERT(m_count < std::numeric_limits<uint32_t>::max());
117 m_count++;
118 }
119
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
142 {
143 return m_count;
144 }
145
146 private:
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.