00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2007 Georgia Tech Research Corporation 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Author: George Riley <riley@ece.gatech.edu> 00019 * Adapted from original code in object.h by: 00020 * Authors: Gustavo Carneiro <gjcarneiro@gmail.com>, 00021 * Mathieu Lacage <mathieu.lacage@sophia.inria.fr> 00022 */ 00023 #ifndef __REF_COUNT_BASE_H__ 00024 #define __REF_COUNT_BASE_H__ 00025 00026 #include <stdint.h> 00027 00028 namespace ns3 { 00029 00030 /** 00031 * \brief a base class that provides implementations of reference counting 00032 * operations. 00033 * 00034 * A base class that provides implementations of reference counting 00035 * operations, for classes that wish to use the templatized smart 00036 * pointer for memory management but that do not wish to derive from 00037 * class ns3::Object. 00038 * 00039 */ 00040 class RefCountBase 00041 { 00042 public: 00043 RefCountBase(); 00044 RefCountBase (const RefCountBase &o); 00045 RefCountBase &operator = (const RefCountBase &o); 00046 virtual ~RefCountBase (); 00047 /** 00048 * Increment the reference count. This method should not be called 00049 * by user code. RefCountBase instances are expected to be used in 00050 * conjunction with the Ptr template which would make calling Ref 00051 * unecessary and dangerous. 00052 */ 00053 inline void Ref (void) const; 00054 /** 00055 * Decrement the reference count. This method should not be called 00056 * by user code. RefCountBase instances are expected to be used in 00057 * conjunction with the Ptr template which would make calling Ref 00058 * unecessary and dangerous. 00059 */ 00060 inline void Unref (void) const; 00061 00062 /** 00063 * Get the reference count of the object. Normally not needed; for language bindings. 00064 */ 00065 uint32_t GetReferenceCount (void) const; 00066 00067 private: 00068 // Note we make this mutable so that the const methods can still 00069 // change it. 00070 mutable uint32_t m_count; // Reference count 00071 }; 00072 00073 } // namespace ns3 00074 00075 namespace ns3 { 00076 00077 // Implementation of the in-line methods 00078 void 00079 RefCountBase::Ref (void) const 00080 { 00081 m_count++; 00082 } 00083 00084 void 00085 RefCountBase::Unref (void) const 00086 { 00087 m_count--; 00088 if (m_count == 0) 00089 { // All references removed, ok to delete 00090 delete this; 00091 } 00092 } 00093 00094 } // namespace ns3 00095 00096 #endif /* __REF_COUNT_BASE_H__*/