MythTV  master
referencecounterlist.h
Go to the documentation of this file.
1 //
2 // referencecounterlist.h
3 // MythTV
4 //
5 // Created by Jean-Yves Avenard on 16/08/13.
6 // Copyright (c) 2013 Bubblestuff Pty Ltd. All rights reserved.
7 //
8 
9 #ifndef __MythTV__referencecounterlist__
10 #define __MythTV__referencecounterlist__
11 
12 #include "mythbaseexp.h"
13 #include "referencecounter.h"
14 #include <QList>
15 
16 template <class T>
18 {
19 public:
20  RefCountHandler() : r(new T()) { }
21  RefCountHandler(T *_r) : r(_r) { r->IncrRef(); }
22  RefCountHandler(const RefCountHandler &other) : r(other.r) { r->IncrRef(); }
24  {
25  other.r->IncrRef();
26  r->DecrRef();
27  r = other.r;
28  return *this;
29  }
30  ~RefCountHandler() { r->DecrRef(); }
31  operator T *() const { return r; } // Convert to T* automatically.
32  T *operator*() { return r; }
33  const T* operator*() const { return r; }
34  T *operator->() { return r; }
35  const T *operator->() const { return r; }
36 
37 private:
38  T *r; //NOLINT(readability-identifier-naming)
39 };
40 
45 template <class T>
46 class RefCountedList : public QList<RefCountHandler<T> >
47 {
48 public:
55  T *takeAt(int i)
56  {
57  RefCountHandler<T> rch = QList<RefCountHandler<T> >::takeAt(i);
58  rch->IncrRef();
59  return rch;
60  }
69  T *takeFirst(void)
70  {
71  RefCountHandler<T> rch = QList<RefCountHandler<T> >::takeFirst();
72  rch->IncrRef();
73  return rch;
74  }
83  T *takeLast(void)
84  {
85  RefCountHandler<T> rch = QList<RefCountHandler<T> >::takeLast();
86  rch->IncrRef();
87  return rch;
88  }
94  {
95  return QList<RefCountHandler<T> >::takeAt(i);
96  }
104  {
105  return QList<RefCountHandler<T> >::takeFirst();
106  }
114  {
115  return QList<RefCountHandler<T> >::takeLast();
116  }
117  RefCountedList<T> mid(int pos, int length = -1) const
118  {
119  if (length < 0 || pos + length > this->size())
120  {
121  length = this->size() - pos;
122  }
123  if (pos == 0 && length == this->size())
124  {
125  return *this;
126  }
127  RefCountedList<T> cpy;
128  if (length <= 0)
129  {
130  return cpy;
131  }
132  for (int i = pos; i < length; i++)
133  {
134  cpy.append(this->at(pos));
135  }
136  return cpy;
137  }
139  {
140  RefCountedList<T> list = *this;
141  list += other;
142  return list;
143  }
145  {
146  QList<RefCountHandler<T> >::operator+=(other);
147  return *this;
148  }
150  {
151  QList<RefCountHandler<T> >::operator+=(value);
152  return *this;
153  }
155  {
156  QList<RefCountHandler<T> >::operator<<(other);
157  return *this;
158  }
160  {
161  QList<RefCountHandler<T> >::operator<<(value);
162  return *this;
163  }
165  {
166  QList<RefCountHandler<T> >::operator=(other);
167  return *this;
168  }
169 
170  RefCountedList<T>() = default;
171  RefCountedList<T>(const RefCountedList<T>&) = default;
172 };
173 
175 
176 #endif /* defined(__MythTV__referencecounterlist__) */
RefCountHandler & operator=(const RefCountHandler &other)
RefCountHandler< T > takeAtAndDecr(int i)
Removes the item at index position i and returns it.
RefCountedList< T > & operator=(const RefCountedList< T > &other)
RefCountedList< T > & operator+=(const RefCountHandler< T > &value)
RefCountedList< T > & operator+=(const RefCountedList< T > &other)
RefCountHandler< T > takeFirstAndDecr(void)
Removes the first item in the list and returns it.
T * takeLast(void)
Removes the last item in the list and returns it.
RefCountedList< T > mid(int pos, int length=-1) const
T * takeFirst(void)
Removes the first item in the list and returns it.
RefCountedList< T > operator+(const RefCountedList< T > &other) const
const T * operator*() const
T * takeAt(int i)
Removes the item at index position i and returns it.
RefCountHandler< T > takeLastAndDecr(void)
Removes the last item in the list and returns it.
RefCountHandler(const RefCountHandler &other)
const T * operator->() const
RefCountedList< T > & operator<<(const RefCountedList< T > &other)
General purpose reference counted list.