[ox/std] Specify std::bit_cast in Vector

This commit is contained in:
Gary Talent 2021-08-02 20:58:10 -05:00
parent 90b2e7a43c
commit b61f7a95ad

View File

@ -32,9 +32,9 @@ struct SmallVector {
protected: protected:
constexpr void initItems(T **items, std::size_t cap) noexcept { constexpr void initItems(T **items, std::size_t cap) noexcept {
if (cap <= Size) { if (cap <= Size) {
*items = bit_cast<T*>(m_data); *items = std::bit_cast<T*>(m_data);
} else { } else {
*items = bit_cast<T*>(new AllocAlias<T>[cap]); *items = std::bit_cast<T*>(new AllocAlias<T>[cap]);
} }
} }
@ -51,12 +51,12 @@ struct SmallVector {
constexpr void moveItemsFrom(T **items, SmallVector &src, const std::size_t count, const std::size_t cap) noexcept { constexpr void moveItemsFrom(T **items, SmallVector &src, const std::size_t count, const std::size_t cap) noexcept {
if (cap <= Size) { if (cap <= Size) {
const auto dstItems = bit_cast<T*>(m_data); const auto dstItems = std::bit_cast<T*>(m_data);
const auto srcItems = bit_cast<T*>(src.m_data); const auto srcItems = std::bit_cast<T*>(src.m_data);
for (auto i = 0u; i < count; ++i) { for (auto i = 0u; i < count; ++i) {
dstItems[i] = move(srcItems[i]); dstItems[i] = move(srcItems[i]);
} }
*items = bit_cast<T*>(m_data); *items = std::bit_cast<T*>(m_data);
} }
} }
@ -76,7 +76,7 @@ struct SmallVector<T, 0> {
SmallVector(SmallVector&&) noexcept = default; SmallVector(SmallVector&&) noexcept = default;
protected: protected:
constexpr void initItems(T **items, std::size_t cap) noexcept { constexpr void initItems(T **items, std::size_t cap) noexcept {
*items = bit_cast<T*>(new AllocAlias<T>[cap]); *items = std::bit_cast<T*>(new AllocAlias<T>[cap]);
} }
[[maybe_unused]] [[maybe_unused]]
@ -382,7 +382,7 @@ Vector<T, SmallVectorSize>::Vector(Vector &&other) noexcept {
template<typename T, std::size_t SmallVectorSize> template<typename T, std::size_t SmallVectorSize>
Vector<T, SmallVectorSize>::~Vector() { Vector<T, SmallVectorSize>::~Vector() {
clear(); clear();
this->clearItems(bit_cast<AllocAlias<T>*>(m_items)); this->clearItems(std::bit_cast<AllocAlias<T>*>(m_items));
m_items = nullptr; m_items = nullptr;
} }
@ -403,7 +403,7 @@ template<typename T, std::size_t SmallVectorSize>
constexpr Vector<T, SmallVectorSize> &Vector<T, SmallVectorSize>::operator=(const Vector &other) { constexpr Vector<T, SmallVectorSize> &Vector<T, SmallVectorSize>::operator=(const Vector &other) {
if (this != &other) { if (this != &other) {
clear(); clear();
this->clearItems(bit_cast<AllocAlias<T>*>(m_items)); this->clearItems(std::bit_cast<AllocAlias<T>*>(m_items));
m_items = nullptr; m_items = nullptr;
m_size = other.m_size; m_size = other.m_size;
m_cap = other.m_cap; m_cap = other.m_cap;
@ -419,7 +419,7 @@ template<typename T, std::size_t SmallVectorSize>
constexpr Vector<T, SmallVectorSize> &Vector<T, SmallVectorSize>::operator=(Vector &&other) noexcept { constexpr Vector<T, SmallVectorSize> &Vector<T, SmallVectorSize>::operator=(Vector &&other) noexcept {
if (this != &other) { if (this != &other) {
clear(); clear();
this->clearItems(bit_cast<AllocAlias<T>*>(m_items)); this->clearItems(std::bit_cast<AllocAlias<T>*>(m_items));
m_size = other.m_size; m_size = other.m_size;
m_cap = other.m_cap; m_cap = other.m_cap;
m_items = other.m_items; m_items = other.m_items;
@ -445,7 +445,7 @@ template<typename T, std::size_t SmallVectorSize>
Result<T&> Vector<T, SmallVectorSize>::front() noexcept { Result<T&> Vector<T, SmallVectorSize>::front() noexcept {
if (!m_size) { if (!m_size) {
AllocAlias<T> v; AllocAlias<T> v;
return {*bit_cast<T*>(&v), OxError(1)}; return {*std::bit_cast<T*>(&v), OxError(1)};
} }
return m_items[0]; return m_items[0];
} }
@ -454,7 +454,7 @@ template<typename T, std::size_t SmallVectorSize>
Result<const T&> Vector<T, SmallVectorSize>::front() const noexcept { Result<const T&> Vector<T, SmallVectorSize>::front() const noexcept {
if (!m_size) { if (!m_size) {
AllocAlias<T> v; AllocAlias<T> v;
return {*bit_cast<T*>(&v), OxError(1)}; return {*std::bit_cast<T*>(&v), OxError(1)};
} }
return m_items[0]; return m_items[0];
} }
@ -463,7 +463,7 @@ template<typename T, std::size_t SmallVectorSize>
Result<T&> Vector<T, SmallVectorSize>::back() noexcept { Result<T&> Vector<T, SmallVectorSize>::back() noexcept {
if (!m_size) { if (!m_size) {
AllocAlias<T> v; AllocAlias<T> v;
return {*bit_cast<T*>(&v), OxError(1)}; return {*std::bit_cast<T*>(&v), OxError(1)};
} }
return m_items[m_size - 1]; return m_items[m_size - 1];
} }
@ -472,7 +472,7 @@ template<typename T, std::size_t SmallVectorSize>
Result<const T&> Vector<T, SmallVectorSize>::back() const noexcept { Result<const T&> Vector<T, SmallVectorSize>::back() const noexcept {
if (!m_size) { if (!m_size) {
AllocAlias<T> v; AllocAlias<T> v;
return {*bit_cast<T*>(&v), OxError(1)}; return {*std::bit_cast<T*>(&v), OxError(1)};
} }
return m_items[m_size - 1]; return m_items[m_size - 1];
} }
@ -601,7 +601,10 @@ void Vector<T, SmallVectorSize>::expandCap(std::size_t cap) {
for (std::size_t i = 0; i < itRange; i++) { for (std::size_t i = 0; i < itRange; i++) {
new (&m_items[i]) T(move(oldItems[i])); new (&m_items[i]) T(move(oldItems[i]));
} }
this->clearItems(bit_cast<AllocAlias<T>*>(oldItems)); for (std::size_t i = itRange; i < m_cap; i++) {
new (&m_items[i]) T;
}
this->clearItems(std::bit_cast<AllocAlias<T>*>(oldItems));
} }
} }