|
|
| 1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2007 Georgia Tech Research Corporation |
| 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 |
* Author: George Riley <riley@ece.gatech.edu> |
| 19 |
* Adapted from original code in object.h by: |
| 20 |
* Authors: Gustavo Carneiro <gjcarneiro@gmail.com>, |
| 21 |
* Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
| 22 |
*/ |
| 23 |
#ifndef __REF_COUNT_BASE_H__ |
| 24 |
#define __REF_COUNT_BASE_H__ |
| 25 |
|
| 26 |
#include <stdint.h> |
| 27 |
/** |
| 28 |
* \brief a base class that provides implementations of reference counting |
| 29 |
* operations. |
| 30 |
* |
| 31 |
* A base class that provides implementations of reference counting |
| 32 |
* operations, for classes that wish to use the templatized smart |
| 33 |
* pointer for memory management but that do not wish to derive from |
| 34 |
* class ns3::Object. |
| 35 |
* |
| 36 |
*/ |
| 37 |
class RefCountBase |
| 38 |
{ |
| 39 |
public: |
| 40 |
RefCountBase(); |
| 41 |
virtual ~RefCountBase (); |
| 42 |
/** |
| 43 |
* Increment the reference count. This method should not be called |
| 44 |
* by user code. RefCountBase instances are expected to be used in |
| 45 |
* conjunction with the Ptr template which would make calling Ref |
| 46 |
* unecessary and dangerous. |
| 47 |
*/ |
| 48 |
inline void Ref () const; |
| 49 |
/** |
| 50 |
* Decrement the reference count. This method should not be called |
| 51 |
* by user code. RefCountBase instances are expected to be used in |
| 52 |
* conjunction with the Ptr template which would make calling Ref |
| 53 |
* unecessary and dangerous. |
| 54 |
*/ |
| 55 |
inline void Unref () const; |
| 56 |
private: |
| 57 |
// Note we make this mutable so that the const methods can still |
| 58 |
// change it. |
| 59 |
mutable uint32_t m_count; // Reference count |
| 60 |
}; |
| 61 |
|
| 62 |
// Implementation of the in-line methods |
| 63 |
void |
| 64 |
RefCountBase::Ref () const |
| 65 |
{ |
| 66 |
m_count++; |
| 67 |
} |
| 68 |
|
| 69 |
void |
| 70 |
RefCountBase::Unref () const |
| 71 |
{ |
| 72 |
if (--m_count == 0) |
| 73 |
{ // All references removed, ok to delete |
| 74 |
delete this; |
| 75 |
} |
| 76 |
} |
| 77 |
#endif |