# HG changeset patch # User Tom Henderson # Date 1205762124 25200 # Node ID 601e52a37def660db94e57752f070a9edf380705 # Parent 1ef7ee05ec04b8706d81e2c9475095aa8b84fb57 RefCountBase class diff -r 1ef7ee05ec04 -r 601e52a37def src/core/ref-count-base.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/core/ref-count-base.cc Mon Mar 17 06:55:24 2008 -0700 @@ -0,0 +1,36 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 Georgia Tech Research Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: George Riley + * Adapted from original code in object.h by: + * Authors: Gustavo Carneiro , + * Mathieu Lacage + */ + +#include "ref-count-base.h" + +namespace ns3 { + +RefCountBase::RefCountBase() : m_count (0) +{ +} + +RefCountBase::~RefCountBase () +{ +} + +} // namespace ns3 diff -r 1ef7ee05ec04 -r 601e52a37def src/core/ref-count-base.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/core/ref-count-base.h Mon Mar 17 06:55:24 2008 -0700 @@ -0,0 +1,77 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 Georgia Tech Research Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: George Riley + * Adapted from original code in object.h by: + * Authors: Gustavo Carneiro , + * Mathieu Lacage + */ +#ifndef __REF_COUNT_BASE_H__ +#define __REF_COUNT_BASE_H__ + +#include +/** + * \brief a base class that provides implementations of reference counting + * operations. + * + * A base class that provides implementations of reference counting + * operations, for classes that wish to use the templatized smart + * pointer for memory management but that do not wish to derive from + * class ns3::Object. + * + */ +class RefCountBase +{ +public: + RefCountBase(); + virtual ~RefCountBase (); + /** + * Increment the reference count. This method should not be called + * by user code. RefCountBase instances are expected to be used in + * conjunction with the Ptr template which would make calling Ref + * unecessary and dangerous. + */ + inline void Ref () const; + /** + * Decrement the reference count. This method should not be called + * by user code. RefCountBase instances are expected to be used in + * conjunction with the Ptr template which would make calling Ref + * unecessary and dangerous. + */ + inline void Unref () const; +private: + // Note we make this mutable so that the const methods can still + // change it. + mutable uint32_t m_count; // Reference count +}; + +// Implementation of the in-line methods +void +RefCountBase::Ref () const +{ + m_count++; +} + +void +RefCountBase::Unref () const +{ + if (--m_count == 0) + { // All references removed, ok to delete + delete this; + } +} +#endif diff -r 1ef7ee05ec04 -r 601e52a37def src/core/wscript --- a/src/core/wscript Mon Mar 17 06:39:35 2008 -0700 +++ b/src/core/wscript Mon Mar 17 06:55:24 2008 -0700 @@ -32,6 +32,7 @@ def build(bld): 'log.cc', 'breakpoint.cc', 'object-base.cc', + 'ref-count-base.cc', 'ptr.cc', 'object.cc', 'test.cc', @@ -73,6 +74,7 @@ def build(bld): 'empty.h', 'callback.h', 'object-base.h', + 'ref-count-base.h', 'ptr.h', 'object.h', 'log.h',