MythTV  master
autodeletedeque.h
Go to the documentation of this file.
1 #ifndef _AUTO_DELETE_DEQUE_H_
2 #define _AUTO_DELETE_DEQUE_H_
3 
4 // C++ headers
5 #include <deque>
6 
7 template<typename T>
9 {
10  public:
11  explicit AutoDeleteDeque(bool auto_delete = true) : m_autodelete(auto_delete) {}
13 
14  using List = typename std::deque< T >;
15  using iterator = typename List::iterator;
16  using const_iterator = typename List::const_iterator;
17  using reverse_iterator = typename List::reverse_iterator;
18  using const_reverse_iterator = typename List::const_reverse_iterator;
19 
20  T operator[](uint index)
21  {
22  if (index < m_list.size())
23  return m_list[index];
24  return nullptr;
25  }
26  const T operator[](uint index) const
27  {
28  if (index < m_list.size())
29  return m_list[index];
30  return nullptr;
31  }
32 
33  T take(uint i);
35  {
36  if (m_autodelete)
37  delete *it;
38  return m_list.erase(it);
39  }
40  void clear(void)
41  {
42  while (m_autodelete && !m_list.empty())
43  {
44  delete m_list.back();
45  m_list.pop_back();
46  }
47  m_list.clear();
48  }
49 
50  iterator begin(void) { return m_list.begin(); }
51  iterator end(void) { return m_list.end(); }
52  const_iterator begin(void) const { return m_list.begin(); }
53  const_iterator end(void) const { return m_list.end(); }
54  const_iterator cbegin(void) const { return m_list.begin(); }
55  const_iterator cend(void) const { return m_list.end(); }
56  reverse_iterator rbegin(void) { return m_list.rbegin(); }
57  reverse_iterator rend(void) { return m_list.rend(); }
58  const_reverse_iterator rbegin(void) const { return m_list.rbegin(); }
59  const_reverse_iterator rend(void) const { return m_list.rend(); }
60  const_reverse_iterator crbegin(void) const { return m_list.rbegin(); }
61  const_reverse_iterator crend(void) const { return m_list.rend(); }
62 
63  T back(void) { return m_list.back(); }
64  const T back(void) const { return m_list.back(); }
65 
66  bool empty(void) const { return m_list.empty(); }
67  size_t size(void) const { return m_list.size(); }
68  void push_front(T info) { m_list.push_front(info); }
69  void push_back( T info) { m_list.push_back( info); }
70 
71  // compatibility with old Q3PtrList
72  void setAutoDelete(bool auto_delete) { m_autodelete = auto_delete; }
73 
74  protected:
77 };
78 
79 template<typename T>
81 {
82  iterator it = m_list.begin();
83  for (uint i = 0; i < index; i++, ++it)
84  {
85  if (it == m_list.end())
86  return nullptr;
87  }
88  T info = *it;
89  m_list.erase(it);
90  return info;
91 }
92 
93 #endif // _AUTO_DELETE_DEQUE_H_
const T back(void) const
iterator erase(iterator it)
void setAutoDelete(bool auto_delete)
T operator[](uint index)
const_reverse_iterator rbegin(void) const
void push_back(T info)
const_iterator end(void) const
AutoDeleteDeque(bool auto_delete=true)
const_reverse_iterator crbegin(void) const
const_iterator begin(void) const
typename std::deque< ProgramInfo * > List
size_t size(void) const
typename List::const_reverse_iterator const_reverse_iterator
void push_front(T info)
iterator begin(void)
const_reverse_iterator rend(void) const
typename List::const_iterator const_iterator
reverse_iterator rbegin(void)
typename List::iterator iterator
void clear(void)
bool empty(void) const
typename List::reverse_iterator reverse_iterator
const_reverse_iterator crend(void) const
unsigned int uint
Definition: compat.h:140
const T operator[](uint index) const
iterator end(void)
reverse_iterator rend(void)
const_iterator cbegin(void) const
const_iterator cend(void) const