1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/core/simple-ref-count.h Thu Nov 12 13:01:01 2009 +0100
1.3 @@ -0,0 +1,103 @@
1.4 +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1.5 +/*
1.6 + * Copyright (c) 2007 Georgia Tech Research Corporation, INRIA
1.7 + *
1.8 + * This program is free software; you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License version 2 as
1.10 + * published by the Free Software Foundation;
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program; if not, write to the Free Software
1.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.20 + *
1.21 + * Authors: George Riley <riley@ece.gatech.edu>
1.22 + * Gustavo Carneiro <gjcarneiro@gmail.com>,
1.23 + * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
1.24 + */
1.25 +#ifndef SIMPLE_REF_COUNT_H
1.26 +#define SIMPLE_REF_COUNT_H
1.27 +
1.28 +#include "empty.h"
1.29 +#include <stdint.h>
1.30 +
1.31 +namespace ns3 {
1.32 +
1.33 +/**
1.34 + * \brief A template-based reference counting class
1.35 + *
1.36 + * This template can be used to give reference-counting powers
1.37 + * to a class. This template does not require this class to
1.38 + * have a virtual destructor or no parent class.
1.39 + *
1.40 + * Note: if you are moving to this template from the RefCountBase class,
1.41 + * you need to be careful to mark appropriately your destructor virtual
1.42 + * if needed. i.e., if your class has subclasses, _do_ mark your destructor
1.43 + * virtual.
1.44 + *
1.45 + * \internal
1.46 + * Note: we rely on the fairly common EBCO (empty base class optimization)
1.47 + * to get rid of the empty parent when the user does not provide
1.48 + * a non-trivial parent.
1.49 + */
1.50 +template <typename T, typename PARENT = empty>
1.51 +class SimpleRefCount : public PARENT
1.52 +{
1.53 +public:
1.54 + SimpleRefCount ()
1.55 + : m_count (1)
1.56 + {}
1.57 + SimpleRefCount (const SimpleRefCount &o)
1.58 + : m_count (1)
1.59 + {}
1.60 + SimpleRefCount &operator = (const SimpleRefCount &o)
1.61 + {
1.62 + return *this;
1.63 + }
1.64 + /**
1.65 + * Increment the reference count. This method should not be called
1.66 + * by user code. SimpleRefCount instances are expected to be used in
1.67 + * conjunction with the Ptr template which would make calling Ref
1.68 + * unecessary and dangerous.
1.69 + */
1.70 + inline void Ref (void) const
1.71 + {
1.72 + m_count++;
1.73 + }
1.74 + /**
1.75 + * Decrement the reference count. This method should not be called
1.76 + * by user code. SimpleRefCount instances are expected to be used in
1.77 + * conjunction with the Ptr template which would make calling Ref
1.78 + * unecessary and dangerous.
1.79 + */
1.80 + inline void Unref (void) const
1.81 + {
1.82 + m_count--;
1.83 + if (m_count == 0)
1.84 + {
1.85 + delete static_cast<T*> (const_cast<SimpleRefCount *> (this));
1.86 + }
1.87 + }
1.88 +
1.89 + /**
1.90 + * Get the reference count of the object.
1.91 + * Normally not needed; for language bindings.
1.92 + */
1.93 + uint32_t GetReferenceCount (void) const
1.94 + {
1.95 + return m_count;
1.96 + }
1.97 +
1.98 +private:
1.99 + // Note we make this mutable so that the const methods can still
1.100 + // change it.
1.101 + mutable uint32_t m_count;
1.102 +};
1.103 +
1.104 +} // namespace ns3
1.105 +
1.106 +#endif /* SIMPLE_REF_COUNT_H */