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  typedef typename std::deque< T > List;
15  typedef typename List::iterator iterator;
16  typedef typename List::const_iterator const_iterator;
17  typedef typename List::reverse_iterator reverse_iterator;
18  typedef typename List::const_reverse_iterator 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  reverse_iterator rbegin(void) { return m_list.rbegin(); }
55  reverse_iterator rend(void) { return m_list.rend(); }
56  const_reverse_iterator rbegin(void) const { return m_list.rbegin(); }
57  const_reverse_iterator rend(void) const { return m_list.rend(); }
58 
59  T back(void) { return m_list.back(); }
60  const T back(void) const { return m_list.back(); }
61 
62  bool empty(void) const { return m_list.empty(); }
63  size_t size(void) const { return m_list.size(); }
64  void push_front(T info) { m_list.push_front(info); }
65  void push_back( T info) { m_list.push_back( info); }
66 
67  // compatibility with old Q3PtrList
68  void setAutoDelete(bool auto_delete) { m_autodelete = auto_delete; }
69 
70  protected:
73 };
74 
75 template<typename T>
77 {
78  iterator it = m_list.begin();
79  for (uint i = 0; i < index; i++, ++it)
80  {
81  if (it == m_list.end())
82  return nullptr;
83  }
84  T info = *it;
85  m_list.erase(it);
86  return info;
87 }
88 
89 #endif // _AUTO_DELETE_DEQUE_H_
const T back(void) const
List::reverse_iterator reverse_iterator
iterator erase(iterator it)
void setAutoDelete(bool auto_delete)
std::deque< T > List
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_iterator begin(void) const
unsigned int uint
Definition: compat.h:140
size_t size(void) const
void push_front(T info)
iterator begin(void)
const_reverse_iterator rend(void) const
reverse_iterator rbegin(void)
void clear(void)
bool empty(void) const
const T operator[](uint index) const
iterator end(void)
reverse_iterator rend(void)
List::const_reverse_iterator const_reverse_iterator
List::const_iterator const_iterator
List::iterator iterator