riley@2775: /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ riley@2775: /* riley@2775: * Copyright (c) 2007 Georgia Tech Research Corporation riley@2775: * riley@2775: * This program is free software; you can redistribute it and/or modify riley@2775: * it under the terms of the GNU General Public License version 2 as riley@2775: * published by the Free Software Foundation; riley@2775: * riley@2775: * This program is distributed in the hope that it will be useful, riley@2775: * but WITHOUT ANY WARRANTY; without even the implied warranty of riley@2775: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the riley@2775: * GNU General Public License for more details. riley@2775: * riley@2775: * You should have received a copy of the GNU General Public License riley@2775: * along with this program; if not, write to the Free Software riley@2775: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA riley@2775: * riley@2775: * Author: George Riley riley@2775: * Adapted from original code in object.h by: riley@2775: * Authors: Gustavo Carneiro , riley@2775: * Mathieu Lacage riley@2775: */ riley@2775: #ifndef __REF_COUNT_BASE_H__ riley@2775: #define __REF_COUNT_BASE_H__ riley@2775: riley@2775: #include riley@2775: riley@2775: namespace ns3 { riley@2775: riley@2775: /** riley@2775: * \brief a base class that provides implementations of reference counting riley@2775: * operations. riley@2775: * riley@2775: * A base class that provides implementations of reference counting riley@2775: * operations, for classes that wish to use the templatized smart riley@2775: * pointer for memory management but that do not wish to derive from riley@2775: * class ns3::Object. riley@2775: * riley@2775: */ riley@2775: class RefCountBase riley@2775: { riley@2775: public: riley@2775: RefCountBase(); mathieu@2946: RefCountBase (const RefCountBase &o); mathieu@2946: RefCountBase &operator = (const RefCountBase &o); riley@2775: virtual ~RefCountBase (); riley@2775: /** riley@2775: * Increment the reference count. This method should not be called riley@2775: * by user code. RefCountBase instances are expected to be used in riley@2775: * conjunction with the Ptr template which would make calling Ref riley@2775: * unecessary and dangerous. riley@2775: */ mathieu@3392: inline void Ref (void) const; riley@2775: /** riley@2775: * Decrement the reference count. This method should not be called riley@2775: * by user code. RefCountBase instances are expected to be used in riley@2775: * conjunction with the Ptr template which would make calling Ref riley@2775: * unecessary and dangerous. riley@2775: */ mathieu@3392: inline void Unref (void) const; mathieu@3392: mathieu@3393: /** mathieu@3393: * Get the reference count of the object. Normally not needed; for language bindings. mathieu@3393: */ mathieu@3395: uint32_t GetReferenceCount (void) const; mathieu@3393: riley@2775: private: riley@2775: // Note we make this mutable so that the const methods can still riley@2775: // change it. riley@2775: mutable uint32_t m_count; // Reference count riley@2775: }; riley@2775: riley@2775: } // namespace ns3 riley@2775: riley@2775: namespace ns3 { riley@2775: riley@2775: // Implementation of the in-line methods riley@2775: void mathieu@3392: RefCountBase::Ref (void) const riley@2775: { riley@2775: m_count++; riley@2775: } riley@2775: riley@2775: void mathieu@3392: RefCountBase::Unref (void) const riley@2775: { mathieu@3392: m_count--; mathieu@3392: if (m_count == 0) riley@2775: { // All references removed, ok to delete riley@2775: delete this; riley@2775: } riley@2775: } riley@2775: riley@2775: } // namespace ns3 riley@2775: riley@2775: #endif /* __REF_COUNT_BASE_H__*/