A Discrete-Event Network Simulator
API
simple-ref-count.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 Georgia Tech Research Corporation, INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Authors: George Riley <riley@ece.gatech.edu>
19  * Gustavo Carneiro <gjcarneiro@gmail.com>,
20  * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
21  */
22 #ifndef SIMPLE_REF_COUNT_H
23 #define SIMPLE_REF_COUNT_H
24 
25 #include "empty.h"
26 #include "default-deleter.h"
27 #include "assert.h"
28 #include <stdint.h>
29 #include <limits>
30 
37 namespace ns3 {
38 
71 template <typename T, typename PARENT = empty, typename DELETER = DefaultDeleter<T> >
72 class SimpleRefCount : public PARENT
73 {
74 public:
77  : m_count (1)
78  {}
83  SimpleRefCount (const SimpleRefCount & o [[maybe_unused]])
84  : m_count (1)
85  {
86  }
92  SimpleRefCount &operator = ([[maybe_unused]] const SimpleRefCount &o)
93  {
94  return *this;
95  }
102  inline void Ref (void) const
103  {
105  m_count++;
106  }
113  inline void Unref (void) const
114  {
115  m_count--;
116  if (m_count == 0)
117  {
118  DELETER::Delete (static_cast<T*> (const_cast<SimpleRefCount *> (this)));
119  }
120  }
121 
128  inline uint32_t GetReferenceCount (void) const
129  {
130  return m_count;
131  }
132 
133 private:
141  mutable uint32_t m_count;
142 };
143 
144 } // namespace ns3
145 
146 #endif /* SIMPLE_REF_COUNT_H */
#define max(a, b)
Definition: 80211b.c:43
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
A template-based reference counting class.
void Unref(void) const
Decrement the reference count.
SimpleRefCount(const SimpleRefCount &o[[maybe_unused]])
Copy constructor.
SimpleRefCount()
Default constructor.
void Ref(void) const
Increment the reference count.
SimpleRefCount & operator=([[maybe_unused]] const SimpleRefCount &o)
Assignment operator.
uint32_t GetReferenceCount(void) const
Get the reference count of the object.
uint32_t m_count
The reference count.
ns3::DefaultDeleter declaration and template implementation, for reference-counted smart pointers.
ns3::empty declaration, used by callbacks.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
Every class exported by the ns3 library is enclosed in the ns3 namespace.