mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 10:31:50 +00:00
Fix exception safety in util::stack_vector::push_back
This commit is contained in:
parent
16935fb5b9
commit
273682355a
1 changed files with 13 additions and 9 deletions
|
|
@ -48,21 +48,25 @@ namespace util {
|
|||
}
|
||||
|
||||
void push_back(const T& value) {
|
||||
if (size_ < capacity) {
|
||||
auto data = reinterpret_cast<char*>(data_.ptr() + (size_++));
|
||||
new (data) T(value);
|
||||
} else {
|
||||
if (size_ >= capacity) {
|
||||
throw std::overflow_error("stack vector capacity exceeded");
|
||||
}
|
||||
|
||||
void* p = data_.data + size_ * sizeof(T);
|
||||
::new (p) T(value);
|
||||
|
||||
++size_;
|
||||
}
|
||||
|
||||
void push_back(T&& value) {
|
||||
if (size_ < capacity) {
|
||||
auto data = reinterpret_cast<char*>(data_.ptr() + (size_++));
|
||||
new (data) T(std::move(value));
|
||||
} else {
|
||||
if (size_ >= capacity) {
|
||||
throw std::overflow_error("stack vector capacity exceeded");
|
||||
}
|
||||
|
||||
void* p = data_.data + size_ * sizeof(T);
|
||||
::new (p) T(std::move(value));
|
||||
|
||||
++size_;
|
||||
}
|
||||
|
||||
void pop_back() {
|
||||
|
|
@ -76,7 +80,7 @@ namespace util {
|
|||
|
||||
void clear() {
|
||||
for (int i = 0; i < size_; ++i) {
|
||||
data_.ptr()[i].~T();
|
||||
data_.ptr()[i].~T();
|
||||
}
|
||||
size_ = 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue