src/core/simple-ref-count.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu Nov 12 13:01:01 2009 +0100 (2009-11-12)
changeset 5505 c0ac392289c3
child 5840 c2b3762932e8
permissions -rw-r--r--
replace RefCountBase with SimpleRefCount<> to avoid duplicate refcounting implementations.
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2007 Georgia Tech Research Corporation, INRIA
     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  * Authors: George Riley <riley@ece.gatech.edu>
    19  *          Gustavo Carneiro <gjcarneiro@gmail.com>,
    20  *          Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    21  */
    22 #ifndef SIMPLE_REF_COUNT_H
    23 #define SIMPLE_REF_COUNT_H
    24 
    25 #include "empty.h"
    26 #include <stdint.h>
    27 
    28 namespace ns3 {
    29 
    30 /**
    31  * \brief A template-based reference counting class
    32  *
    33  * This template can be used to give reference-counting powers
    34  * to a class. This template does not require this class to
    35  * have a virtual destructor or no parent class.
    36  * 
    37  * Note: if you are moving to this template from the RefCountBase class,
    38  * you need to be careful to mark appropriately your destructor virtual
    39  * if needed. i.e., if your class has subclasses, _do_ mark your destructor
    40  * virtual.
    41  *
    42  * \internal
    43  * Note: we rely on the fairly common EBCO (empty base class optimization)
    44  * to get rid of the empty parent when the user does not provide
    45  * a non-trivial parent.
    46  */
    47 template <typename T, typename PARENT = empty>
    48 class SimpleRefCount : public PARENT
    49 {
    50 public:
    51   SimpleRefCount ()
    52     : m_count (1)
    53   {}
    54   SimpleRefCount (const SimpleRefCount &o)
    55     : m_count (1)
    56   {}
    57   SimpleRefCount &operator = (const SimpleRefCount &o)
    58   {
    59     return *this;
    60   }
    61   /**
    62    * Increment the reference count. This method should not be called
    63    * by user code. SimpleRefCount instances are expected to be used in
    64    * conjunction with the Ptr template which would make calling Ref
    65    * unecessary and dangerous.
    66    */
    67   inline void Ref (void) const
    68   {
    69     m_count++;
    70   }
    71   /**
    72    * Decrement the reference count. This method should not be called
    73    * by user code. SimpleRefCount instances are expected to be used in 
    74    * conjunction with the Ptr template which would make calling Ref
    75    * unecessary and dangerous.
    76    */
    77   inline void Unref (void) const
    78   {
    79     m_count--;
    80     if (m_count == 0)
    81       {
    82 	delete static_cast<T*> (const_cast<SimpleRefCount *> (this));
    83       }
    84   }
    85 
    86   /**
    87    * Get the reference count of the object.  
    88    * Normally not needed; for language bindings.
    89    */
    90   uint32_t GetReferenceCount (void) const
    91   {
    92     return m_count;
    93   }
    94 
    95 private:
    96   // Note we make this mutable so that the const methods can still
    97   // change it.
    98   mutable uint32_t m_count;
    99 };
   100 
   101 } // namespace ns3
   102 
   103 #endif /* SIMPLE_REF_COUNT_H */