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
16template <class T>
18{
19public:
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
45private:
46 T *r; //NOLINT(readability-identifier-naming)
47};
48
53template <class T>
54
55class RefCountedList : public QList<RefCountHandler<T> >
56{
57public:
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 }
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;
189};
190
192
193#endif /* defined(MYTHTV_REFERENCECOUNTERLIST_H) */
const T * operator*() const
const T * operator->() const
RefCountHandler & operator=(const RefCountHandler &other)
RefCountHandler(const RefCountHandler &other)
General purpose reference counted list.
RefCountHandler< T > takeFirstAndDecr(void)
Removes the first item in the list and returns it.
RefCountedList< T > operator+(const RefCountedList< T > &other) const
RefCountHandler< T > takeLastAndDecr(void)
Removes the last item in the list and returns it.
RefCountedList< T > & operator=(const RefCountedList< T > &other)
RefCountedList< T > mid(int pos, int length=-1) const
RefCountedList< T > & operator+=(const RefCountedList< T > &other)
RefCountedList< T > & operator+=(const RefCountHandler< T > &value)
RefCountHandler< T > takeAtAndDecr(int i)
Removes the item at index position i and returns it.
T * takeAt(int i)
Removes the item at index position i and returns it.
RefCountedList< T > & operator<<(const RefCountedList< T > &other)
T * takeFirst(void)
Removes the first item in the list and returns it.
RefCountedList(const RefCountedList &)=default
RefCountedList()=default
T * takeLast(void)
Removes the last item in the list and returns it.