update copyright date
[gnash.git] / libbase / ref_counted.h
blob8cac89e9cae232b096582aae027840cc36dc42d9
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
3 // 2011 Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifndef GNASH_REF_COUNTED_H
20 #define GNASH_REF_COUNTED_H
22 #include "dsodefs.h" // for DSOEXPORT
24 #include <cassert>
25 #include <boost/detail/atomic_count.hpp>
27 namespace gnash {
29 /// \brief
30 /// For stuff that's tricky to keep track of w/r/t ownership & cleanup.
31 /// The only use for this class seems to be for putting derived
32 /// classes in smart_ptr
33 ///
34 class DSOEXPORT ref_counted
37 private:
39 // We use an atomic counter to make ref-counting
40 // thread-safe. The drop_ref() method must be
41 // carefully designed for this to be effective
42 // (decrement & check in a single statement)
44 typedef boost::detail::atomic_count Counter;
46 mutable Counter m_ref_count;
48 protected:
50 // A ref-counted object only deletes self,
51 // must never be explicitly deleted !
52 virtual ~ref_counted()
54 assert(m_ref_count == 0);
57 public:
58 ref_counted()
60 m_ref_count(0)
64 ref_counted(const ref_counted&)
66 m_ref_count(0)
70 void add_ref() const {
71 assert(m_ref_count >= 0);
72 ++m_ref_count;
75 void drop_ref() const {
77 assert(m_ref_count > 0);
78 if (!--m_ref_count) {
79 // Delete me!
80 delete this;
84 long get_ref_count() const { return m_ref_count; }
87 } // namespace gnash
89 #endif // GNASH_REF_COUNTED_H