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
riley@2775
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
riley@2775
     2
/*
riley@2775
     3
 * Copyright (c) 2007 Georgia Tech Research Corporation
riley@2775
     4
 *
riley@2775
     5
 * This program is free software; you can redistribute it and/or modify
riley@2775
     6
 * it under the terms of the GNU General Public License version 2 as
riley@2775
     7
 * published by the Free Software Foundation;
riley@2775
     8
 *
riley@2775
     9
 * This program is distributed in the hope that it will be useful,
riley@2775
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
riley@2775
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
riley@2775
    12
 * GNU General Public License for more details.
riley@2775
    13
 *
riley@2775
    14
 * You should have received a copy of the GNU General Public License
riley@2775
    15
 * along with this program; if not, write to the Free Software
riley@2775
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
riley@2775
    17
 *
riley@2775
    18
 * Author:  George Riley <riley@ece.gatech.edu>
riley@2775
    19
 * Adapted from original code in object.h by:
riley@2775
    20
 * Authors: Gustavo Carneiro <gjcarneiro@gmail.com>,
riley@2775
    21
 *          Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
riley@2775
    22
 */
riley@2775
    23
#ifndef __REF_COUNT_BASE_H__
riley@2775
    24
#define __REF_COUNT_BASE_H__
riley@2775
    25
riley@2775
    26
#include <stdint.h>
riley@2775
    27
riley@2775
    28
namespace ns3 {
riley@2775
    29
riley@2775
    30
/**
riley@2775
    31
 * \brief a base class that provides implementations of reference counting
riley@2775
    32
 *    operations.
riley@2775
    33
 *  
riley@2775
    34
 * A base class that provides implementations of reference counting 
riley@2775
    35
 * operations, for classes that wish to use the templatized smart 
riley@2775
    36
 * pointer for memory management but that do not wish to derive from
riley@2775
    37
 * class ns3::Object.
riley@2775
    38
 *
riley@2775
    39
 */
riley@2775
    40
class RefCountBase 
riley@2775
    41
{ 
riley@2775
    42
public:
riley@2775
    43
  RefCountBase();
mathieu@2946
    44
  RefCountBase (const RefCountBase &o);
mathieu@2946
    45
  RefCountBase &operator = (const RefCountBase &o);
riley@2775
    46
  virtual ~RefCountBase ();
riley@2775
    47
  /**
riley@2775
    48
   * Increment the reference count. This method should not be called
riley@2775
    49
   * by user code. RefCountBase instances are expected to be used in
riley@2775
    50
   * conjunction with the Ptr template which would make calling Ref
riley@2775
    51
   * unecessary and dangerous.
riley@2775
    52
   */
mathieu@3392
    53
  inline void Ref (void) const;
riley@2775
    54
  /**
riley@2775
    55
   * Decrement the reference count. This method should not be called
riley@2775
    56
   * by user code. RefCountBase instances are expected to be used in 
riley@2775
    57
   * conjunction with the Ptr template which would make calling Ref
riley@2775
    58
   * unecessary and dangerous.
riley@2775
    59
   */
mathieu@3392
    60
  inline void Unref (void) const;
mathieu@3392
    61
mathieu@3393
    62
  /**
mathieu@3393
    63
   * Get the reference count of the object.  Normally not needed; for language bindings.
mathieu@3393
    64
   */
mathieu@3395
    65
  uint32_t GetReferenceCount (void) const;
mathieu@3393
    66
riley@2775
    67
private:
riley@2775
    68
  // Note we make this mutable so that the const methods can still
riley@2775
    69
  // change it.
riley@2775
    70
  mutable uint32_t m_count;  // Reference count
riley@2775
    71
};
riley@2775
    72
riley@2775
    73
} // namespace ns3
riley@2775
    74
riley@2775
    75
namespace ns3 {
riley@2775
    76
riley@2775
    77
// Implementation of the in-line methods
riley@2775
    78
void
mathieu@3392
    79
RefCountBase::Ref (void) const
riley@2775
    80
{
riley@2775
    81
  m_count++;
riley@2775
    82
}
riley@2775
    83
riley@2775
    84
void
mathieu@3392
    85
RefCountBase::Unref (void) const
riley@2775
    86
{
mathieu@3392
    87
  m_count--;
mathieu@3392
    88
  if (m_count == 0)
riley@2775
    89
    { // All references removed, ok to delete
riley@2775
    90
      delete this;
riley@2775
    91
    }
riley@2775
    92
}
riley@2775
    93
riley@2775
    94
} // namespace ns3
riley@2775
    95
riley@2775
    96
#endif /* __REF_COUNT_BASE_H__*/