SpectMorph
smobject.hh
1 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html
2 
3 #ifndef SPECTMORPH_OBJECT_HH
4 #define SPECTMORPH_OBJECT_HH
5 
6 #include <QObject>
7 #include <QMutex>
8 
9 namespace SpectMorph
10 {
11 
12 class Object : public QObject
13 {
14  QMutex object_mutex;
15  unsigned int object_ref_count;
16 
17 public:
18  Object();
19  virtual ~Object();
20 
21  void ref();
22  void unref();
23 };
24 
25 template<class T>
26 class RefPtr
27 {
28  T *ptr;
29 
30 public:
31  RefPtr (T* t = NULL)
32  {
33  ptr = t;
34  }
35  RefPtr (const RefPtr& other)
36  {
37  T *new_ptr = other.ptr;
38 
39  if (new_ptr)
40  new_ptr->ref();
41 
42  ptr = new_ptr;
43  }
44  RefPtr&
45  operator= (const RefPtr& other)
46  {
47  T *new_ptr = other.ptr;
48  T *old_ptr = ptr;
49 
50  if (new_ptr)
51  new_ptr->ref();
52 
53  ptr = new_ptr;
54 
55  if (old_ptr)
56  old_ptr->unref();
57 
58  return *this;
59  }
60  T*
61  operator->()
62  {
63  return ptr;
64  }
65  T*
66  c_ptr()
67  {
68  return ptr;
69  }
70  ~RefPtr()
71  {
72  if (ptr)
73  ptr->unref();
74  }
75  operator bool() const
76  {
77  return (ptr != 0);
78  }
79 };
80 
81 }
82 
83 #endif
Definition: smaudio.hh:15
Definition: smobject.hh:26
Definition: smobject.hh:12