Gnash  0.8.11dev
ref_counted.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // 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
18 
19 #ifndef GNASH_REF_COUNTED_H
20 #define GNASH_REF_COUNTED_H
21 
22 #include "dsodefs.h" // for DSOEXPORT
23 
24 #include <cassert>
25 #include <atomic>
26 
27 namespace gnash {
28 
35 {
36 
37 private:
38 
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)
43  //
44  mutable std::atomic<int> m_ref_count;
45 
46 protected:
47 
48  // A ref-counted object only deletes self,
49  // must never be explicitly deleted !
50  virtual ~ref_counted()
51  {
52  assert(m_ref_count == 0);
53  }
54 
55 public:
57  :
58  m_ref_count(0)
59  {
60  }
61 
63  :
64  m_ref_count(0)
65  {
66  }
67 
68  void add_ref() const {
69  assert(m_ref_count >= 0);
70  ++m_ref_count;
71  }
72 
73  void drop_ref() const {
74 
75  assert(m_ref_count > 0);
76  if (!--m_ref_count) {
77  // Delete me!
78  delete this;
79  }
80  }
81 
82  long get_ref_count() const { return m_ref_count; }
83 };
84 
85 inline void
87 {
88  o->add_ref();
89 }
90 
91 inline void
93 {
94  o->drop_ref();
95 }
96 
97 } // namespace gnash
98 
99 #endif // GNASH_REF_COUNTED_H
For stuff that&#39;s tricky to keep track of w/r/t ownership & cleanup. The only use for this class seems...
Definition: ref_counted.h:34
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
ref_counted(const ref_counted &)
Definition: ref_counted.h:62
Definition: GnashKey.h:161
long get_ref_count() const
Definition: ref_counted.h:82
void drop_ref() const
Definition: ref_counted.h:73
void add_ref() const
Definition: ref_counted.h:68
virtual ~ref_counted()
Definition: ref_counted.h:50
#define DSOEXPORT
Definition: dsodefs.h:55
void intrusive_ptr_release(const ref_counted *o)
Definition: ref_counted.h:92
void intrusive_ptr_add_ref(const ref_counted *o)
Definition: ref_counted.h:86
ref_counted()
Definition: ref_counted.h:56