|
mathieu@5505
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
mathieu@5505
|
2 |
/*
|
|
mathieu@5505
|
3 |
* Copyright (c) 2007 Georgia Tech Research Corporation, INRIA
|
|
mathieu@5505
|
4 |
*
|
|
mathieu@5505
|
5 |
* This program is free software; you can redistribute it and/or modify
|
|
mathieu@5505
|
6 |
* it under the terms of the GNU General Public License version 2 as
|
|
mathieu@5505
|
7 |
* published by the Free Software Foundation;
|
|
mathieu@5505
|
8 |
*
|
|
mathieu@5505
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
mathieu@5505
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
mathieu@5505
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
mathieu@5505
|
12 |
* GNU General Public License for more details.
|
|
mathieu@5505
|
13 |
*
|
|
mathieu@5505
|
14 |
* You should have received a copy of the GNU General Public License
|
|
mathieu@5505
|
15 |
* along with this program; if not, write to the Free Software
|
|
mathieu@5505
|
16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
mathieu@5505
|
17 |
*
|
|
mathieu@5505
|
18 |
* Authors: George Riley <riley@ece.gatech.edu>
|
|
mathieu@5505
|
19 |
* Gustavo Carneiro <gjcarneiro@gmail.com>,
|
|
mathieu@5505
|
20 |
* Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
|
|
mathieu@5505
|
21 |
*/
|
|
mathieu@5505
|
22 |
#ifndef SIMPLE_REF_COUNT_H
|
|
mathieu@5505
|
23 |
#define SIMPLE_REF_COUNT_H
|
|
mathieu@5505
|
24 |
|
|
mathieu@5505
|
25 |
#include "empty.h"
|
|
mathieu@5505
|
26 |
#include <stdint.h>
|
|
mathieu@5505
|
27 |
|
|
mathieu@5505
|
28 |
namespace ns3 {
|
|
mathieu@5505
|
29 |
|
|
mathieu@5505
|
30 |
/**
|
|
mathieu@5505
|
31 |
* \brief A template-based reference counting class
|
|
mathieu@5505
|
32 |
*
|
|
mathieu@5505
|
33 |
* This template can be used to give reference-counting powers
|
|
mathieu@5505
|
34 |
* to a class. This template does not require this class to
|
|
mathieu@5505
|
35 |
* have a virtual destructor or no parent class.
|
|
mathieu@5505
|
36 |
*
|
|
mathieu@5505
|
37 |
* Note: if you are moving to this template from the RefCountBase class,
|
|
mathieu@5505
|
38 |
* you need to be careful to mark appropriately your destructor virtual
|
|
mathieu@5505
|
39 |
* if needed. i.e., if your class has subclasses, _do_ mark your destructor
|
|
mathieu@5505
|
40 |
* virtual.
|
|
mathieu@5505
|
41 |
*
|
|
mathieu@5505
|
42 |
* \internal
|
|
mathieu@5505
|
43 |
* Note: we rely on the fairly common EBCO (empty base class optimization)
|
|
mathieu@5505
|
44 |
* to get rid of the empty parent when the user does not provide
|
|
mathieu@5505
|
45 |
* a non-trivial parent.
|
|
mathieu@5505
|
46 |
*/
|
|
mathieu@5505
|
47 |
template <typename T, typename PARENT = empty>
|
|
mathieu@5505
|
48 |
class SimpleRefCount : public PARENT
|
|
mathieu@5505
|
49 |
{
|
|
mathieu@5505
|
50 |
public:
|
|
mathieu@5505
|
51 |
SimpleRefCount ()
|
|
mathieu@5505
|
52 |
: m_count (1)
|
|
mathieu@5505
|
53 |
{}
|
|
mathieu@5505
|
54 |
SimpleRefCount (const SimpleRefCount &o)
|
|
mathieu@5505
|
55 |
: m_count (1)
|
|
mathieu@5505
|
56 |
{}
|
|
mathieu@5505
|
57 |
SimpleRefCount &operator = (const SimpleRefCount &o)
|
|
mathieu@5505
|
58 |
{
|
|
mathieu@5505
|
59 |
return *this;
|
|
mathieu@5505
|
60 |
}
|
|
mathieu@5505
|
61 |
/**
|
|
mathieu@5505
|
62 |
* Increment the reference count. This method should not be called
|
|
mathieu@5505
|
63 |
* by user code. SimpleRefCount instances are expected to be used in
|
|
mathieu@5505
|
64 |
* conjunction with the Ptr template which would make calling Ref
|
|
mathieu@5505
|
65 |
* unecessary and dangerous.
|
|
mathieu@5505
|
66 |
*/
|
|
mathieu@5505
|
67 |
inline void Ref (void) const
|
|
mathieu@5505
|
68 |
{
|
|
mathieu@5505
|
69 |
m_count++;
|
|
mathieu@5505
|
70 |
}
|
|
mathieu@5505
|
71 |
/**
|
|
mathieu@5505
|
72 |
* Decrement the reference count. This method should not be called
|
|
mathieu@5505
|
73 |
* by user code. SimpleRefCount instances are expected to be used in
|
|
mathieu@5505
|
74 |
* conjunction with the Ptr template which would make calling Ref
|
|
mathieu@5505
|
75 |
* unecessary and dangerous.
|
|
mathieu@5505
|
76 |
*/
|
|
mathieu@5505
|
77 |
inline void Unref (void) const
|
|
mathieu@5505
|
78 |
{
|
|
mathieu@5505
|
79 |
m_count--;
|
|
mathieu@5505
|
80 |
if (m_count == 0)
|
|
mathieu@5505
|
81 |
{
|
|
mathieu@5505
|
82 |
delete static_cast<T*> (const_cast<SimpleRefCount *> (this));
|
|
mathieu@5505
|
83 |
}
|
|
mathieu@5505
|
84 |
}
|
|
mathieu@5505
|
85 |
|
|
mathieu@5505
|
86 |
/**
|
|
mathieu@5505
|
87 |
* Get the reference count of the object.
|
|
mathieu@5505
|
88 |
* Normally not needed; for language bindings.
|
|
mathieu@5505
|
89 |
*/
|
|
mathieu@5505
|
90 |
uint32_t GetReferenceCount (void) const
|
|
mathieu@5505
|
91 |
{
|
|
mathieu@5505
|
92 |
return m_count;
|
|
mathieu@5505
|
93 |
}
|
|
mathieu@5505
|
94 |
|
|
mathieu@5505
|
95 |
private:
|
|
mathieu@5505
|
96 |
// Note we make this mutable so that the const methods can still
|
|
mathieu@5505
|
97 |
// change it.
|
|
mathieu@5505
|
98 |
mutable uint32_t m_count;
|
|
mathieu@5505
|
99 |
};
|
|
mathieu@5505
|
100 |
|
|
mathieu@5505
|
101 |
} // namespace ns3
|
|
mathieu@5505
|
102 |
|
|
mathieu@5505
|
103 |
#endif /* SIMPLE_REF_COUNT_H */
|