98 lines
3.6 KiB
C++
98 lines
3.6 KiB
C++
#ifndef ARBUTILS_UNIQUEPTRLIST_HPP
|
|
#define ARBUTILS_UNIQUEPTRLIST_HPP
|
|
|
|
#include <algorithm>
|
|
#include <sstream>
|
|
#include <stdexcept>
|
|
#include <vector>
|
|
#include "BorrowedPtr.hpp"
|
|
|
|
namespace ArbUt {
|
|
template <class ValueT> class UniquePtrList {
|
|
private:
|
|
std::vector<ValueT*> _vector;
|
|
using iterator = typename std::vector<ValueT*>::iterator;
|
|
using const_iterator = typename std::vector<ValueT*>::const_iterator;
|
|
|
|
public:
|
|
inline UniquePtrList() noexcept : _vector() {}
|
|
inline UniquePtrList(const std::vector<ValueT*>& vec) noexcept : _vector(vec) {}
|
|
explicit inline UniquePtrList(size_t capacity) : _vector() { _vector.reserve(capacity); }
|
|
inline UniquePtrList(const std::initializer_list<ValueT*>& l) noexcept : _vector(l) {}
|
|
inline UniquePtrList(ValueT* const* begin, ValueT* const* end) noexcept : _vector(begin, end) {}
|
|
|
|
UniquePtrList(const UniquePtrList<ValueT>&) = delete;
|
|
UniquePtrList<ValueT>& operator=(const UniquePtrList<ValueT>&) = delete;
|
|
~UniquePtrList() noexcept { Clear(); }
|
|
|
|
inline void Clear() noexcept {
|
|
for (auto& i : _vector) {
|
|
delete i;
|
|
}
|
|
}
|
|
|
|
inline BorrowedPtr<ValueT> At(size_t index) const {
|
|
#ifndef NO_ASSERT
|
|
if (index >= _vector.size()) {
|
|
std::stringstream ss;
|
|
ss << "Index " << index << " is out of bounds.";
|
|
throw std::logic_error(ss.str());
|
|
}
|
|
#endif
|
|
return _vector[index];
|
|
}
|
|
ValueT* TakeOwnership(size_t index) {
|
|
auto p = _vector[index];
|
|
_vector[index] = nullptr;
|
|
return p;
|
|
}
|
|
void Set(size_t index, ValueT* ptr) {
|
|
delete _vector[index];
|
|
_vector[index] = ptr;
|
|
}
|
|
|
|
inline void Remove(size_t index) {
|
|
#ifndef NO_ASSERT
|
|
if (index >= _vector.size()) {
|
|
std::stringstream ss;
|
|
ss << "Index " << index << " is out of bounds.";
|
|
throw std::logic_error(ss.str());
|
|
}
|
|
#endif
|
|
delete _vector[index];
|
|
_vector.erase(_vector.begin() + index);
|
|
}
|
|
|
|
inline bool Contains(const BorrowedPtr<ValueT>& value) const noexcept {
|
|
return std::find(_vector.begin(), _vector.end(), value) != _vector.end();
|
|
}
|
|
|
|
/// Find the index of the first occurrence of a value in the list, return -1 if none is found.
|
|
/// \param value The value we want the index for.
|
|
/// \return The index of the first occurrence of the value in the list, or -1 if none is found.
|
|
inline size_t IndexOf(const BorrowedPtr<ValueT>& value) const noexcept {
|
|
const auto& it = std::find(_vector.begin(), _vector.end(), value);
|
|
if (it == _vector.end())
|
|
return -1;
|
|
return std::distance(_vector.begin(), it);
|
|
}
|
|
|
|
inline void Append(ValueT* value) { _vector.push_back(value); }
|
|
inline BorrowedPtr<ValueT> operator[](size_t index) const { return At(index); }
|
|
|
|
inline size_t Count() const noexcept { return _vector.size(); }
|
|
|
|
iterator begin() noexcept { return _vector.begin(); }
|
|
iterator end() noexcept { return _vector.end(); }
|
|
const_iterator begin() const noexcept { return _vector.begin(); }
|
|
const_iterator end() const noexcept { return _vector.end(); }
|
|
|
|
ValueT* const* RawData() const noexcept { return _vector.data(); }
|
|
|
|
const std::vector<ValueT*>& GetStdList() const noexcept { return _vector; }
|
|
std::vector<ValueT*>& GetStdList() noexcept { return _vector; }
|
|
};
|
|
}
|
|
|
|
#endif // ARBUTILS_UNIQUEPTRLIST_HPP
|