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 
70 template <typename T, typename PARENT = empty, typename DELETER = DefaultDeleter<T> >
71 class SimpleRefCount : public PARENT
72 {
73 public:
78  : m_count (1)
79  {}
84  : m_count (1)
85  {}
90  {
91  return *this;
92  }
99  inline void Ref (void) const
100  {
101  NS_ASSERT (m_count < std::numeric_limits<uint32_t>::max());
102  m_count++;
103  }
110  inline void Unref (void) const
111  {
112  m_count--;
113  if (m_count == 0)
114  {
115  DELETER::Delete (static_cast<T*> (const_cast<SimpleRefCount *> (this)));
116  }
117  }
118 
125  inline uint32_t GetReferenceCount (void) const
126  {
127  return m_count;
128  }
129 
133  static void Cleanup (void) {}
134 private:
142  mutable uint32_t m_count;
143 };
144 
145 } // namespace ns3
146 
147 #endif /* SIMPLE_REF_COUNT_H */
SimpleRefCount(const SimpleRefCount &o)
Copy constructor.
SimpleRefCount & operator=(const SimpleRefCount &o)
Assignment.
#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
uint32_t GetReferenceCount(void) const
Get the reference count of the object.
Definition of assertion macros NS_ASSERT() and NS_ASSERT_MSG().
void Unref(void) const
Decrement the reference count.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
SimpleRefCount()
Constructor.
void Ref(void) const
Increment the reference count.
Default deletion implementation for reference-counted smart pointers.
Definition of class ns3::empty, used by callbacks.
uint32_t m_count
The reference count.
static void Cleanup(void)
Noop.
A template-based reference counting class.