Predator  [unstable] git snapshot
clean_list.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Kamil Dudka <kdudka@redhat.com>
3  *
4  * This file is part of predator.
5  *
6  * predator is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * any later version.
10  *
11  * predator is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with predator. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef H_GUARD_CLEAN_LIST_H
21 #define H_GUARD_CLEAN_LIST_H
22 
23 #include "config.h"
24 
25 #include <vector>
26 
27 // TODO: replace this by boost::something once we find it
28 template <class T>
29 class CleanList {
30  private:
31  typedef std::vector<T *> TList;
33 
34  public:
35  // required by BOOST_FOREACH
36  typedef typename TList::const_iterator const_iterator;
37  typedef typename TList::iterator iterator;
38 
39  public:
40  CleanList() { }
41 
43  typename TList::const_reverse_iterator i;
44  for (i = cl_.rbegin(); i != cl_.rend(); ++i)
45  delete *i;
46  }
47 
48  void append(T *ptr) {
49  cl_.push_back(ptr);
50  }
51 
52  bool empty() const {
53  return cl_.empty();
54  }
55 
56  unsigned size() const {
57  return cl_.size();
58  }
59 
60  template <typename TIdx>
61  T* operator[](const TIdx idx) {
62  CL_BREAK_IF(static_cast<TIdx>(cl_.size()) <= idx);
63  return cl_[idx];
64  }
65 
66  template <typename TIdx>
67  const T* operator[](const TIdx idx) const {
68  return const_cast<CleanList *>(this)->operator[](idx);
69  }
70 
71  /// return STL-like iterator to go through the container
73  return cl_.begin();
74  }
75 
76  /// return STL-like iterator to go through the container
77  const_iterator end() const {
78  return cl_.end();
79  }
80 
81  private:
82  // copying NOT allowed
83  CleanList(const CleanList &);
84  CleanList& operator=(const CleanList &);
85 };
86 
87 #endif /* H_GUARD_CLEAN_LIST_H */