• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

ref_counted.h

Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
00003 //   Foundation, Inc
00004 // 
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019 #ifndef GNASH_REF_COUNTED_H
00020 #define GNASH_REF_COUNTED_H
00021 
00022 #include "dsodefs.h" // for DSOEXPORT
00023 
00024 #include <cassert>
00025 #include <boost/detail/atomic_count.hpp>
00026 
00027 namespace gnash {
00028 
00034 class DSOEXPORT ref_counted
00035 {
00036 
00037 private:
00038 
00039         // We use an atomic counter to make ref-counting 
00040         // thread-safe. The drop_ref() method must be
00041         // carefully designed for this to be effective
00042         // (decrement & check in a single statement)
00043         //
00044         typedef boost::detail::atomic_count Counter;
00045         // typedef long Counter;
00046 
00047         mutable Counter m_ref_count;
00048         
00049 protected:
00050 
00051         // A ref-counted object only deletes self,
00052         // must never be explicitly deleted !
00053         virtual ~ref_counted()
00054         {
00055                 assert(m_ref_count == 0);
00056         }
00057 
00058 public:
00059         ref_counted()
00060                 :
00061                 m_ref_count(0)
00062         {
00063         }
00064 
00065         ref_counted(const ref_counted&)
00066                 :
00067                 m_ref_count(0)
00068         {
00069         }
00070 
00071         void    add_ref() const
00072         {
00073                 assert(m_ref_count >= 0);
00074                 ++m_ref_count;
00075         }
00076 
00077         void    drop_ref() const
00078         {
00079                 assert(m_ref_count > 0);
00080                 if (!--m_ref_count)
00081                 {
00082                         // Delete me!
00083                         delete this;
00084                 }
00085         }
00086 
00087         long    get_ref_count() const { return m_ref_count; }
00088 };
00089 
00090 } // namespace gnash
00091 
00092 #endif // GNASH_REF_COUNTED_H

Generated on Fri Mar 16 2012 15:46:11 for Gnash by  doxygen 1.7.1