xoreos  0.0.5
ptrvector.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_PTRVECTOR_H
26 #define COMMON_PTRVECTOR_H
27 
28 #include <vector>
29 
30 #include <boost/noncopyable.hpp>
31 
32 #include "src/common/deallocator.h"
33 
34 namespace Common {
35 
43 template<typename T, class Deallocator = DeallocatorDefault>
44 class PtrVector : boost::noncopyable, public std::vector<T *> {
45 public:
47  clear();
48  }
49 
50  void clear() {
51  for (typename std::vector<T *>::iterator it = std::vector<T *>::begin(); it != std::vector<T *>::end(); ++it)
52  Deallocator::destroy(*it);
53 
54  std::vector<T *>::clear();
55  }
56 
57  void pop_back() {
58  Deallocator::destroy(std::vector<T *>::back());
59  std::vector<T *>::pop_back();
60  }
61 
62  typename std::vector<T *>::iterator erase(typename std::vector<T *>::iterator position) {
63  Deallocator::destroy(*position);
64  return std::vector<T *>::erase(position);
65  }
66 
67  typename std::vector<T *>::iterator erase(typename std::vector<T *>::iterator first,
68  typename std::vector<T *>::iterator last) {
69 
70  for (typename std::vector<T *>::iterator it = std::vector<T *>::begin(); it != std::vector<T *>::end(); ++it)
71  Deallocator::destroy(*it);
72 
73  return std::vector<T *>::erase(first, last);
74  }
75 
76  void resize(typename std::vector<T *>::size_type n,
77  typename std::vector<T *>::value_type val = typename std::vector<T *>::value_type()) {
78 
79  typename std::vector<T *>::size_type s = std::vector<T *>::size();
80 
81  if (s < n) {
82  while (s++ < n)
83  std::vector<T *>::push_back(val);
84 
85  return;
86  }
87 
88  while (s-- > n)
89  pop_back();
90  }
91 
92  template<class InputIterator>
93  void assign(InputIterator first, InputIterator last) {
94  clear();
95  std::vector<T *>::assign(first, last);
96  }
97 
98  void assign(typename std::vector<T *>::size_type n, const typename std::vector<T *>::value_type &val) {
99  clear();
100  std::vector<T *>::assign(n, val);
101  }
102 };
103 
104 } // End of namespace Common
105 
106 #endif // COMMON_PTRVECTOR_H
Definition: 2dafile.h:39
void resize(typename std::vector< T *>::size_type n, typename std::vector< T *>::value_type val=typename std::vector< T *>::value_type())
Definition: ptrvector.h:76
A vector of pointer to objects, with automatic deletion.
Definition: ptrvector.h:44
void assign(typename std::vector< T *>::size_type n, const typename std::vector< T *>::value_type &val)
Definition: ptrvector.h:98
Simple deallocator concept.
void assign(InputIterator first, InputIterator last)
Definition: ptrvector.h:93
std::vector< T * >::iterator erase(typename std::vector< T *>::iterator first, typename std::vector< T *>::iterator last)
Definition: ptrvector.h:67
std::vector< T * >::iterator erase(typename std::vector< T *>::iterator position)
Definition: ptrvector.h:62