Fix compilation errors.

This commit is contained in:
mutouyun 2023-09-03 13:54:52 +08:00
parent a50bc78678
commit 5fec1f80ff
2 changed files with 24 additions and 7 deletions

View File

@ -83,6 +83,21 @@ void destroy(ForwardIt first, ForwardIt last) noexcept {
#endif #endif
} }
/**
* \brief Destroys a number of objects in a range.
* \see https://en.cppreference.com/w/cpp/memory/destroy_n
*/
template <typename ForwardIt, typename Size>
ForwardIt destroy_n(ForwardIt first, Size n) noexcept {
#if defined(LIBIMP_CPP_17)
return std::destroy_n(first, n);
#else
for (; n > 0; (void) ++first, --n)
destroy(std::addressof(*first));
return first;
#endif
}
/** /**
* \brief Constructs objects by default-initialization * \brief Constructs objects by default-initialization
* in an uninitialized area of memory, defined by a start and a count. * in an uninitialized area of memory, defined by a start and a count.

View File

@ -207,6 +207,7 @@ struct holder_info {
std::size_t sizeof_type; std::size_t sizeof_type;
std::size_t count; std::size_t count;
void (*copy)(allocator const &, void const *s, void *d); void (*copy)(allocator const &, void const *s, void *d);
void (*dest)(void *p, std::size_t n) noexcept;
}; };
template <typename Value, typename Construct> template <typename Value, typename Construct>
@ -260,6 +261,9 @@ public:
if (dst.value_ptr_ == nullptr) return; if (dst.value_ptr_ == nullptr) return;
dst.info_ = src.info_; dst.info_ = src.info_;
}; };
info_.dest = [](void *p, std::size_t n) noexcept {
::LIBIMP::destroy_n(static_cast<Value *>(p), n);
};
} }
bool valid() const noexcept override { bool valid() const noexcept override {
@ -292,23 +296,21 @@ public:
void move_to(allocator const &, void *p) noexcept override { void move_to(allocator const &, void *p) noexcept override {
auto *des = ::LIBIMP::construct<holder>(p); auto *des = ::LIBIMP::construct<holder>(p);
if (value_ptr_ == nullptr) { if (!valid()) return;
return;
}
std::swap(value_ptr_, des->value_ptr_); std::swap(value_ptr_, des->value_ptr_);
std::swap(info_, des->info_); std::swap(info_, des->info_);
} }
void copy_to(allocator const &alloc, void *p) const noexcept(false) override { void copy_to(allocator const &alloc, void *p) const noexcept(false) override {
auto *des = ::LIBIMP::construct<holder>(p); auto *des = ::LIBIMP::construct<holder>(p);
if (value_ptr_ == nullptr) { if (!valid()) return;
return;
}
info_.copy(alloc, this, des); info_.copy(alloc, this, des);
} }
void destroy(allocator const &alloc) noexcept override { void destroy(allocator const &alloc) noexcept override {
alloc.deallocate(::LIBIMP::destroy(value_ptr_), info_.sizeof_type * info_.count); if (!valid()) return;
info_.dest(value_ptr_, count());
alloc.deallocate(value_ptr_, sizeof_heap());
} }
}; };