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_H
10 #define MYTHTV_REFERENCECOUNTERLIST_H
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(); }
23  // The first two lines of this function are the clang-tidy
24  // recommended solution to make a function properly handle
25  // self-assignment. No idea why clang-tidy doesn't recognize
26  // this, unless it has something to do with templates?
27  //
28  // NOLINTNEXTLINE(bugprone-unhandled-self-assignment,cert-oop54-cpp)
30  {
31  if (this == &other)
32  return *this;
33  other.r->IncrRef();
34  r->DecrRef();
35  r = other.r;
36  return *this;
37  }
38  ~RefCountHandler() { r->DecrRef(); }
39  operator T *() const { return r; } // Convert to T* automatically.
40  T *operator*() { return r; }
41  const T* operator*() const { return r; }
42  T *operator->() { return r; }
43  const T *operator->() const { return r; }
44 
45 private:
46  T *r; //NOLINT(readability-identifier-naming)
47 };
48 
53 template <class T>
54 
55 class RefCountedList : public QList<RefCountHandler<T> >
56 {
57 public:
64  T *takeAt(int i)
65  {
66  RefCountHandler<T> rch = QList<RefCountHandler<T> >::takeAt(i);
67  rch->IncrRef();
68  return rch;
69  }
78  T *takeFirst(void)
79  {
80  RefCountHandler<T> rch = QList<RefCountHandler<T> >::takeFirst();
81  rch->IncrRef();
82  return rch;
83  }
92  T *takeLast(void)
93  {
94  RefCountHandler<T> rch = QList<RefCountHandler<T> >::takeLast();
95  rch->IncrRef();
96  return rch;
97  }
103  {
104  return QList<RefCountHandler<T> >::takeAt(i);
105  }
113  {
114  return QList<RefCountHandler<T> >::takeFirst();
115  }
123  {
124  return QList<RefCountHandler<T> >::takeLast();
125  }
126  RefCountedList<T> mid(int pos, int length = -1) const
127  {
128  if (length < 0 || pos + length > this->size())
129  {
130  length = this->size() - pos;
131  }
132  if (pos == 0 && length == this->size())
133  {
134  return *this;
135  }
136  RefCountedList<T> cpy;
137  if (length <= 0)
138  {
139  return cpy;
140  }
141  for (int i = pos; i < length; i++)
142  {
143  cpy.append(this->at(pos));
144  }
145  return cpy;
146  }
148  {
149  RefCountedList<T> list = *this;
150  list += other;
151  return list;
152  }
154  {
155  QList<RefCountHandler<T> >::operator+=(other);
156  return *this;
157  }
159  {
160  QList<RefCountHandler<T> >::operator+=(value);
161  return *this;
162  }
164  {
165  QList<RefCountHandler<T> >::operator<<(other);
166  return *this;
167  }
169  {
170  QList<RefCountHandler<T> >::operator<<(value);
171  return *this;
172  }
173  // The first two lines of this function are the clang-tidy
174  // recommended solution to make a function properly handle
175  // self-assignment. No idea why clang-tidy doesn't recognize
176  // this, unless it has something to do with templates?
177  //
178  // NOLINTNEXTLINE(bugprone-unhandled-self-assignment,cert-oop54-cpp)
180  {
181  if (this == &other)
182  return *this;
183  QList<RefCountHandler<T> >::operator=(other);
184  return *this;
185  }
186 
187  RefCountedList() = default;
188  RefCountedList(const RefCountedList&) = default;
189 };
190 
192 
193 #endif /* defined(MYTHTV_REFERENCECOUNTERLIST_H) */
RefCountedList::takeAt
T * takeAt(int i)
Removes the item at index position i and returns it.
Definition: referencecounterlist.h:64
RefCountHandler::~RefCountHandler
~RefCountHandler()
Definition: referencecounterlist.h:38
RefCountHandler
Definition: referencecounterlist.h:17
mythbaseexp.h
RefCountedList::takeFirstAndDecr
RefCountHandler< T > takeFirstAndDecr(void)
Removes the first item in the list and returns it.
Definition: referencecounterlist.h:112
RefCountHandler::r
T * r
Definition: referencecounterlist.h:46
RefCountHandler::RefCountHandler
RefCountHandler(const RefCountHandler &other)
Definition: referencecounterlist.h:22
RefCountHandler::operator*
T * operator*()
Definition: referencecounterlist.h:40
RefCountHandler::operator->
const T * operator->() const
Definition: referencecounterlist.h:43
RefCountHandler::RefCountHandler
RefCountHandler(T *_r)
Definition: referencecounterlist.h:21
RefCountedList::takeLast
T * takeLast(void)
Removes the last item in the list and returns it.
Definition: referencecounterlist.h:92
RefCountedList::operator=
RefCountedList< T > & operator=(const RefCountedList< T > &other)
Definition: referencecounterlist.h:179
RefCountHandler::operator*
const T * operator*() const
Definition: referencecounterlist.h:41
RefCountHandler::operator->
T * operator->()
Definition: referencecounterlist.h:42
RefCountedList
General purpose reference counted list.
Definition: referencecounterlist.h:55
RefCountedList::takeAtAndDecr
RefCountHandler< T > takeAtAndDecr(int i)
Removes the item at index position i and returns it.
Definition: referencecounterlist.h:102
RefCountedList::operator+=
RefCountedList< T > & operator+=(const RefCountHandler< T > &value)
Definition: referencecounterlist.h:158
RefCountedList::takeFirst
T * takeFirst(void)
Removes the first item in the list and returns it.
Definition: referencecounterlist.h:78
referencecounter.h
RefCountHandler::operator=
RefCountHandler & operator=(const RefCountHandler &other)
Definition: referencecounterlist.h:29
RefCountedList::RefCountedList
RefCountedList()=default
RefCountedList::operator+
RefCountedList< T > operator+(const RefCountedList< T > &other) const
Definition: referencecounterlist.h:147
RefCountedList::operator<<
RefCountedList< T > & operator<<(const RefCountedList< T > &other)
Definition: referencecounterlist.h:163
RefCountHandler::RefCountHandler
RefCountHandler()
Definition: referencecounterlist.h:20
RefCountedList::operator+=
RefCountedList< T > & operator+=(const RefCountedList< T > &other)
Definition: referencecounterlist.h:153
RefCountedList::takeLastAndDecr
RefCountHandler< T > takeLastAndDecr(void)
Removes the last item in the list and returns it.
Definition: referencecounterlist.h:122
RefCountedList::mid
RefCountedList< T > mid(int pos, int length=-1) const
Definition: referencecounterlist.h:126