xoreos  0.0.5
ptrlist.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef COMMON_PTRLIST_H
26 #define COMMON_PTRLIST_H
27 
28 #include <list>
29 
30 #include <boost/noncopyable.hpp>
31 
32 #include "src/common/deallocator.h"
33 
34 namespace Common {
35 
40 template<typename T, class Deallocator = DeallocatorDefault>
41 class PtrList : boost::noncopyable, public std::list<T *> {
42 public:
44  clear();
45  }
46 
47  void clear() {
48  for (typename std::list<T *>::iterator it = std::list<T *>::begin(); it != std::list<T *>::end(); ++it)
49  Deallocator::destroy(*it);
50 
51  std::list<T *>::clear();
52  }
53 
54  void pop_front() {
55  Deallocator::destroy(std::list<T *>::front());
56  std::list<T *>::pop_front();
57  }
58 
59  void pop_back() {
60  Deallocator::destroy(std::list<T *>::back());
61  std::list<T *>::pop_back();
62  }
63 
64  typename std::list<T *>::iterator erase(typename std::list<T *>::iterator position) {
65  Deallocator::destroy(*position);
66  return std::list<T *>::erase(position);
67  }
68 
69  typename std::list<T *>::iterator erase(typename std::list<T *>::iterator first,
70  typename std::list<T *>::iterator last) {
71 
72  for (typename std::list<T *>::iterator it = std::list<T *>::begin(); it != std::list<T *>::end(); ++it)
73  Deallocator::destroy(*it);
74 
75  return std::list<T *>::erase(first, last);
76  }
77 
78  void resize(typename std::list<T *>::size_type n,
79  typename std::list<T *>::value_type val = typename std::list<T *>::value_type()) {
80 
81  typename std::list<T *>::size_type s = std::list<T *>::size();
82 
83  if (s < n) {
84  while (s++ < n)
85  std::list<T *>::push_back(val);
86 
87  return;
88  }
89 
90  while (s-- > n)
91  pop_back();
92  }
93 
94  void remove(const typename std::list<T *>::value_type &val) {
95  for (typename std::list<T *>::iterator it = std::list<T *>::begin(); it != std::list<T *>::end(); ) {
96  if (*it == val)
97  it = erase(it);
98  else
99  ++it;
100  }
101  }
102 
103  template<class Predicate>
104  void remove_if(Predicate pred) {
105  for (typename std::list<T *>::iterator it = std::list<T *>::begin(); it != std::list<T *>::end(); ) {
106  if (pred(*it))
107  it = erase(it);
108  else
109  ++it;
110  }
111  }
112 
113  template<class InputIterator>
114  void assign(InputIterator first, InputIterator last) {
115  clear();
116  std::list<T *>::assign(first, last);
117  }
118 
119  void assign(typename std::list<T *>::size_type n, const typename std::list<T *>::value_type &val) {
120  clear();
121  std::list<T *>::assign(n, val);
122  }
123 };
124 
125 } // End of namespace Common
126 
127 #endif // COMMON_PTRLIST_H
std::list< T * >::iterator erase(typename std::list< T *>::iterator first, typename std::list< T *>::iterator last)
Definition: ptrlist.h:69
Definition: 2dafile.h:39
void assign(typename std::list< T *>::size_type n, const typename std::list< T *>::value_type &val)
Definition: ptrlist.h:119
void pop_back()
Definition: ptrlist.h:59
void resize(typename std::list< T *>::size_type n, typename std::list< T *>::value_type val=typename std::list< T *>::value_type())
Definition: ptrlist.h:78
A list of pointer to objects, with automatic deletion.
Definition: ptrlist.h:41
void remove_if(Predicate pred)
Definition: ptrlist.h:104
std::list< T * >::iterator erase(typename std::list< T *>::iterator position)
Definition: ptrlist.h:64
void assign(InputIterator first, InputIterator last)
Definition: ptrlist.h:114
Simple deallocator concept.
void clear()
Definition: ptrlist.h:47
void pop_front()
Definition: ptrlist.h:54