-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PtrVector is to boost::ptr_vector what PtrList is to boost::ptr_list.
- Loading branch information
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* Phaethon - A FLOSS resource explorer for BioWare's Aurora engine games | ||
* | ||
* Phaethon is the legal property of its developers, whose names | ||
* can be found in the AUTHORS file distributed with this source | ||
* distribution. | ||
* | ||
* Phaethon is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; either version 3 | ||
* of the License, or (at your option) any later version. | ||
* | ||
* Phaethon is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Phaethon. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** @file | ||
* A vector storing pointer to objects, with automatic deletion. | ||
*/ | ||
|
||
#ifndef COMMON_PTRVECTOR_H | ||
#define COMMON_PTRVECTOR_H | ||
|
||
#include <vector> | ||
|
||
#include <boost/noncopyable.hpp> | ||
|
||
#include "src/common/deallocator.h" | ||
|
||
namespace Common { | ||
|
||
/** A vector of pointer to objects, with automatic deletion. | ||
* | ||
* A PtrVector expects to hold sole ownership of its contents. | ||
* | ||
* Note: when overwriting elements in a PtrVector using operator[], | ||
* no automatic deletion can take place! | ||
*/ | ||
template<typename T, class Deallocator = DeallocatorDefault> | ||
class PtrVector : boost::noncopyable, public std::vector<T *> { | ||
public: | ||
~PtrVector() { | ||
clear(); | ||
} | ||
|
||
void clear() { | ||
for (typename std::vector<T *>::iterator it = std::vector<T *>::begin(); it != std::vector<T *>::end(); ++it) | ||
Deallocator::destroy(*it); | ||
|
||
std::vector<T *>::clear(); | ||
} | ||
|
||
void pop_back() { | ||
Deallocator::destroy(std::vector<T *>::back()); | ||
std::vector<T *>::pop_back(); | ||
} | ||
|
||
typename std::vector<T *>::iterator erase(typename std::vector<T *>::iterator position) { | ||
Deallocator::destroy(*position); | ||
return std::vector<T *>::erase(position); | ||
} | ||
|
||
typename std::vector<T *>::iterator erase(typename std::vector<T *>::iterator first, | ||
typename std::vector<T *>::iterator last) { | ||
|
||
for (typename std::vector<T *>::iterator it = std::vector<T *>::begin(); it != std::vector<T *>::end(); ++it) | ||
Deallocator::destroy(*it); | ||
|
||
return std::vector<T *>::erase(first, last); | ||
} | ||
|
||
void resize(typename std::vector<T *>::size_type n, | ||
typename std::vector<T *>::value_type val = typename std::vector<T *>::value_type()) { | ||
|
||
typename std::vector<T *>::size_type s = std::vector<T *>::size(); | ||
|
||
if (s < n) { | ||
while (s++ < n) | ||
std::vector<T *>::push_back(val); | ||
|
||
return; | ||
} | ||
|
||
while (s-- > n) | ||
pop_back(); | ||
} | ||
|
||
template<class InputIterator> | ||
void assign(InputIterator first, InputIterator last) { | ||
clear(); | ||
std::vector<T *>::assign(first, last); | ||
} | ||
|
||
void assign(typename std::vector<T *>::size_type n, const typename std::vector<T *>::value_type &val) { | ||
clear(); | ||
std::vector<T *>::assign(n, val); | ||
} | ||
}; | ||
|
||
} // End of namespace Common | ||
|
||
#endif // COMMON_PTRVECTOR_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters