Gnash  0.8.10
ref_counted.h
Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00003 //   Free Software 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 
00046         mutable Counter m_ref_count;
00047         
00048 protected:
00049 
00050         // A ref-counted object only deletes self,
00051         // must never be explicitly deleted !
00052         virtual ~ref_counted()
00053         {
00054                 assert(m_ref_count == 0);
00055         }
00056 
00057 public:
00058         ref_counted()
00059                 :
00060                 m_ref_count(0)
00061         {
00062         }
00063 
00064         ref_counted(const ref_counted&)
00065                 :
00066                 m_ref_count(0)
00067         {
00068         }
00069 
00070         void add_ref() const {
00071                 assert(m_ref_count >= 0);
00072                 ++m_ref_count;
00073         }
00074 
00075         void drop_ref() const {
00076 
00077                 assert(m_ref_count > 0);
00078                 if (!--m_ref_count) {
00079                         // Delete me!
00080                         delete this;
00081                 }
00082         }
00083 
00084         long get_ref_count() const { return m_ref_count; }
00085 };
00086 
00087 inline void
00088 intrusive_ptr_add_ref(const ref_counted* o)
00089 {
00090         o->add_ref();
00091 }
00092 
00093 inline void
00094 intrusive_ptr_release(const ref_counted* o)
00095 {
00096         o->drop_ref();
00097 }
00098 
00099 } // namespace gnash
00100 
00101 #endif // GNASH_REF_COUNTED_H