[ox/std] Replace reinterpret_cast with bit_cast in Vector

This commit is contained in:
Gary Talent 2020-10-20 19:28:46 -05:00
parent 84dfb17f7f
commit 985b2b57ba
2 changed files with 17 additions and 16 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2015 - 2018 gtalent2@gmail.com * Copyright 2015 - 2020 gary@drinkingtea.net
* *
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
@ -16,7 +16,7 @@ namespace ox {
template<typename To, typename From> template<typename To, typename From>
typename enable_if<sizeof(To) == sizeof(From), To>::type bit_cast(From src) noexcept { typename enable_if<sizeof(To) == sizeof(From), To>::type bit_cast(From src) noexcept {
To dst = {}; To dst;
memcpy(&dst, &src, sizeof(src)); memcpy(&dst, &src, sizeof(src));
return dst; return dst;
} }

View File

@ -8,6 +8,7 @@
#pragma once #pragma once
#include "bit.hpp"
#include "new.hpp" #include "new.hpp"
#include "types.hpp" #include "types.hpp"
#include "utility.hpp" #include "utility.hpp"
@ -37,17 +38,17 @@ class Vector {
Vector &operator=(Vector &&other); Vector &operator=(Vector &&other);
T &operator[](std::size_t i) noexcept; [[nodiscard]] T &operator[](std::size_t i) noexcept;
const T &operator[](std::size_t i) const noexcept; [[nodiscard]] const T &operator[](std::size_t i) const noexcept;
T &front() noexcept; [[nodiscard]] T &front() noexcept;
const T &front() const noexcept; [[nodiscard]] const T &front() const noexcept;
T &back() noexcept; [[nodiscard]] T &back() noexcept;
const T &back() const noexcept; [[nodiscard]] const T &back() const noexcept;
std::size_t size() const noexcept; std::size_t size() const noexcept;
@ -57,9 +58,9 @@ class Vector {
void resize(std::size_t size); void resize(std::size_t size);
T *data() noexcept; [[nodiscard]] T *data() noexcept;
const T *data() const noexcept; [[nodiscard]] const T *data() const noexcept;
[[nodiscard]] bool contains(T) const; [[nodiscard]] bool contains(T) const;
@ -94,7 +95,7 @@ template<typename T>
Vector<T>::Vector(std::size_t size) { Vector<T>::Vector(std::size_t size) {
m_size = size; m_size = size;
m_cap = m_size; m_cap = m_size;
m_items = reinterpret_cast<T*>(new AllocAlias<T>[m_cap]); m_items = bit_cast<T*>(new AllocAlias<T>[m_cap]);
for (std::size_t i = 0; i < size; i++) { for (std::size_t i = 0; i < size; i++) {
m_items[i] = {}; m_items[i] = {};
} }
@ -104,7 +105,7 @@ template<typename T>
Vector<T>::Vector(const Vector<T> &other) { Vector<T>::Vector(const Vector<T> &other) {
m_size = other.m_size; m_size = other.m_size;
m_cap = other.m_cap; m_cap = other.m_cap;
m_items = reinterpret_cast<T*>(new AllocAlias<T>[m_cap]); m_items = bit_cast<T*>(new AllocAlias<T>[m_cap]);
for (std::size_t i = 0; i < m_size; i++) { for (std::size_t i = 0; i < m_size; i++) {
m_items[i] = ox::move(other.m_items[i]); m_items[i] = ox::move(other.m_items[i]);
} }
@ -127,7 +128,7 @@ Vector<T>::~Vector() {
m_items[i].~T(); m_items[i].~T();
} }
} }
delete[] reinterpret_cast<AllocAlias<T>*>(m_items); delete[] bit_cast<AllocAlias<T>*>(m_items);
m_items = nullptr; m_items = nullptr;
} }
@ -136,7 +137,7 @@ Vector<T> &Vector<T>::operator=(const Vector<T> &other) {
this->~Vector<T>(); this->~Vector<T>();
m_size = other.m_size; m_size = other.m_size;
m_cap = other.m_cap; m_cap = other.m_cap;
m_items = reinterpret_cast<T*>(new AllocAlias<T>[m_cap]); m_items = bit_cast<T*>(new AllocAlias<T>[m_cap]);
for (std::size_t i = 0; i < m_size; i++) { for (std::size_t i = 0; i < m_size; i++) {
m_items[i] = other.m_items[i]; m_items[i] = other.m_items[i];
} }
@ -293,7 +294,7 @@ template<typename T>
void Vector<T>::expandCap(std::size_t cap) { void Vector<T>::expandCap(std::size_t cap) {
auto oldItems = m_items; auto oldItems = m_items;
m_cap = cap; m_cap = cap;
m_items = reinterpret_cast<T*>(new AllocAlias<T>[m_cap]); m_items = bit_cast<T*>(new AllocAlias<T>[m_cap]);
if (oldItems) { // move over old items if (oldItems) { // move over old items
const auto itRange = cap > m_size ? m_size : cap; const auto itRange = cap > m_size ? m_size : cap;
for (std::size_t i = 0; i < itRange; i++) { for (std::size_t i = 0; i < itRange; i++) {
@ -302,7 +303,7 @@ void Vector<T>::expandCap(std::size_t cap) {
for (std::size_t i = itRange; i < m_cap; i++) { for (std::size_t i = itRange; i < m_cap; i++) {
new (&m_items[i]) T; new (&m_items[i]) T;
} }
delete[] reinterpret_cast<AllocAlias<T>*>(oldItems); delete[] bit_cast<AllocAlias<T>*>(oldItems);
} }
} }