src/core/ref-count-base.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 3393 10369bf4b3e2
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
     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 namespace ns3 {
    29 
    30 /**
    31  * \brief a base class that provides implementations of reference counting
    32  *    operations.
    33  *  
    34  * A base class that provides implementations of reference counting 
    35  * operations, for classes that wish to use the templatized smart 
    36  * pointer for memory management but that do not wish to derive from
    37  * class ns3::Object.
    38  *
    39  */
    40 class RefCountBase 
    41 { 
    42 public:
    43   RefCountBase();
    44   RefCountBase (const RefCountBase &o);
    45   RefCountBase &operator = (const RefCountBase &o);
    46   virtual ~RefCountBase ();
    47   /**
    48    * Increment the reference count. This method should not be called
    49    * by user code. RefCountBase instances are expected to be used in
    50    * conjunction with the Ptr template which would make calling Ref
    51    * unecessary and dangerous.
    52    */
    53   inline void Ref (void) const;
    54   /**
    55    * Decrement the reference count. This method should not be called
    56    * by user code. RefCountBase instances are expected to be used in 
    57    * conjunction with the Ptr template which would make calling Ref
    58    * unecessary and dangerous.
    59    */
    60   inline void Unref (void) const;
    61 
    62   /**
    63    * Get the reference count of the object.  Normally not needed; for language bindings.
    64    */
    65   uint32_t GetReferenceCount (void) const;
    66 
    67 private:
    68   // Note we make this mutable so that the const methods can still
    69   // change it.
    70   mutable uint32_t m_count;  // Reference count
    71 };
    72 
    73 } // namespace ns3
    74 
    75 namespace ns3 {
    76 
    77 // Implementation of the in-line methods
    78 void
    79 RefCountBase::Ref (void) const
    80 {
    81   m_count++;
    82 }
    83 
    84 void
    85 RefCountBase::Unref (void) const
    86 {
    87   m_count--;
    88   if (m_count == 0)
    89     { // All references removed, ok to delete
    90       delete this;
    91     }
    92 }
    93 
    94 } // namespace ns3
    95 
    96 #endif /* __REF_COUNT_BASE_H__*/