MythTV  master
quicksp.h
Go to the documentation of this file.
1 #ifndef QUICKSP_H_
2 #define QUICKSP_H_
3 
4 #include "mythmetaexp.h"
5 
6 struct NoLock
7 {
8  void lock() {}
9  void unlock() {}
10 };
11 
12 // TODO: implement for threads
13 // If implemented for threads move the instance pointer to simple_ref_ptr
14 // so simple access isn't synchronized.
15 struct ThreadLock
16 {
17  void lock() {}
18 
19  void unlock() {}
20 };
21 
22 // TODO: Get a real reference counted smart pointer in libmyth
23 template <typename T, class Locker = NoLock>
25 {
26  public:
27  simple_ref_ptr() : m_ref(nullptr)
28  {
29  }
30 
32  {
33  m_ref = new ref(ptr);
34  }
35 
36  simple_ref_ptr(const simple_ref_ptr &rhs) : m_ref(nullptr)
37  {
38  *this = rhs;
39  }
40 
42  {
43  unref();
44  }
45 
47  {
48  rhs.m_ref->inc();
49  unref();
50  m_ref = rhs.m_ref;
51 
52  return *this;
53  }
54 
55  T *operator->() const
56  {
57  return get();
58  }
59 
60  T &operator*() const
61  {
62  return *get();
63  }
64 
65  T *get() const
66  {
67  if (m_ref) return m_ref->get();
68 
69  return nullptr;
70  }
71 
72  void reset(T *ptr)
73  {
74  unref();
75  m_ref = new ref(ptr);
76  }
77 
78  typedef T *(simple_ref_ptr<T>::*fake_bool)() const;
79 
80  operator fake_bool() const
81  {
82  return m_ref == nullptr ? nullptr : &simple_ref_ptr<T>::get;
83  }
84 
85  bool operator!() const
86  {
87  return m_ref == nullptr;
88  }
89 
90  private:
91  class ref : public Locker
92  {
93  public:
94  explicit ref(T *ptr) : m_count(1), m_type(ptr) {}
95 
96  ~ref()
97  {
98  delete m_type;
99  }
100 
101  unsigned int inc()
102  {
103  this->lock();
104  ++m_count;
105  this->unlock();
106  return m_count;
107  }
108 
109  unsigned int dec()
110  {
111  this->lock();
112  --m_count;
113  this->unlock();
114  return m_count;
115  }
116 
117  T *get()
118  {
119  return m_type;
120  }
121 
122  T *get() const
123  {
124  return m_type;
125  }
126 
127  private:
128  unsigned int m_count;
129  T *m_type;
130  };
131 
132  void unref()
133  {
134  if (m_ref && m_ref->dec() <= 0)
135  {
136  delete m_ref;
137  m_ref = nullptr;
138  }
139  }
140 
141  private:
142  ref *m_ref;
143 };
144 
145 template <typename T>
146 bool operator==(const simple_ref_ptr<T> &lhs, const simple_ref_ptr<T> &rhs)
147 {
148  return lhs.get() == rhs.get();
149 }
150 
151 template <typename T>
152 bool operator!=(const simple_ref_ptr<T> &lhs, const simple_ref_ptr<T> &rhs)
153 {
154  return lhs.get() != rhs.get();
155 }
156 
157 #endif // QUICKSP_H_
ref * m_ref
Definition: quicksp.h:142
T *(simple_ref_ptr< T >::* fake_bool)() const
Definition: quicksp.h:78
~simple_ref_ptr()
Definition: quicksp.h:41
T * get() const
Definition: quicksp.h:122
simple_ref_ptr(T *ptr)
Definition: quicksp.h:31
bool operator!=(const simple_ref_ptr< T > &lhs, const simple_ref_ptr< T > &rhs)
Definition: quicksp.h:152
unsigned int m_count
Definition: quicksp.h:128
unsigned int inc()
Definition: quicksp.h:101
simple_ref_ptr & operator=(const simple_ref_ptr &rhs)
Definition: quicksp.h:46
void unlock()
Definition: quicksp.h:19
Definition: quicksp.h:6
bool operator!() const
Definition: quicksp.h:85
T * operator->() const
Definition: quicksp.h:55
bool operator==(const simple_ref_ptr< T > &lhs, const simple_ref_ptr< T > &rhs)
Definition: quicksp.h:146
void unlock()
Definition: quicksp.h:9
void reset(T *ptr)
Definition: quicksp.h:72
void lock()
Definition: quicksp.h:17
T * get() const
Definition: quicksp.h:65
unsigned int dec()
Definition: quicksp.h:109
simple_ref_ptr(const simple_ref_ptr &rhs)
Definition: quicksp.h:36
void lock()
Definition: quicksp.h:8
T & operator*() const
Definition: quicksp.h:60
simple_ref_ptr()
Definition: quicksp.h:27
void unref()
Definition: quicksp.h:132