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 
47 class RefCountedList : public QList<RefCountHandler<T> >
48 {
49 public:
56  T *takeAt(int i)
57  {
58  RefCountHandler<T> rch = QList<RefCountHandler<T> >::takeAt(i);
59  rch->IncrRef();
60  return rch;
61  }
70  T *takeFirst(void)
71  {
72  RefCountHandler<T> rch = QList<RefCountHandler<T> >::takeFirst();
73  rch->IncrRef();
74  return rch;
75  }
84  T *takeLast(void)
85  {
86  RefCountHandler<T> rch = QList<RefCountHandler<T> >::takeLast();
87  rch->IncrRef();
88  return rch;
89  }
95  {
96  return QList<RefCountHandler<T> >::takeAt(i);
97  }
105  {
106  return QList<RefCountHandler<T> >::takeFirst();
107  }
115  {
116  return QList<RefCountHandler<T> >::takeLast();
117  }
118  RefCountedList<T> mid(int pos, int length = -1) const
119  {
120  if (length < 0 || pos + length > this->size())
121  {
122  length = this->size() - pos;
123  }
124  if (pos == 0 && length == this->size())
125  {
126  return *this;
127  }
128  RefCountedList<T> cpy;
129  if (length <= 0)
130  {
131  return cpy;
132  }
133  for (int i = pos; i < length; i++)
134  {
135  cpy.append(this->at(pos));
136  }
137  return cpy;
138  }
140  {
141  RefCountedList<T> list = *this;
142  list += other;
143  return list;
144  }
146  {
147  QList<RefCountHandler<T> >::operator+=(other);
148  return *this;
149  }
151  {
152  QList<RefCountHandler<T> >::operator+=(value);
153  return *this;
154  }
156  {
157  QList<RefCountHandler<T> >::operator<<(other);
158  return *this;
159  }
161  {
162  QList<RefCountHandler<T> >::operator<<(value);
163  return *this;
164  }
166  {
167  QList<RefCountHandler<T> >::operator=(other);
168  return *this;
169  }
170 
171  RefCountedList<T>() = default;
172  RefCountedList<T>(const RefCountedList<T>&) = default;
173 };
174 
176 
177 #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.