[ox] Fix for C++20

This commit is contained in:
Gary Talent 2021-11-02 00:45:02 -05:00
parent e91d2653a3
commit f61efbafaf
6 changed files with 28 additions and 38 deletions

View File

@ -48,13 +48,10 @@ class FieldCounter {
}; };
template<typename T> template<typename T>
#if __cplusplus >= 202002L constexpr int countFields() noexcept {
consteval T t;
#endif
int countFields() noexcept {
AllocAlias<T> a = {};
FieldCounter<T> c; FieldCounter<T> c;
oxIgnoreError(model(&c, std::bit_cast<T*>(&a))); oxIgnoreError(model(&c, &t));
return c.fields; return c.fields;
} }

View File

@ -44,20 +44,17 @@ struct TypeNameCatcher {
}; };
template<typename T> template<typename T>
#if __cplusplus >= 202002L constexpr const char *getModelTypeName() noexcept {
consteval T t;
#endif
const char *getModelTypeName() noexcept {
AllocAlias<T> a = {};
TypeNameCatcher nc; TypeNameCatcher nc;
oxIgnoreError(model(&nc, std::bit_cast<T*>(&a))); oxIgnoreError(model(&nc, &t));
return nc.name; return nc.name;
} }
template<typename T> template<typename T>
constexpr const char *getModelTypeName(T *val) noexcept { constexpr const char *getModelTypeName(T *val) noexcept {
TypeNameCatcher nc; TypeNameCatcher nc;
oxIgnoreError(model(&nc, std::bit_cast<T*>(&val))); oxIgnoreError(model(&nc, val));
return nc.name; return nc.name;
} }

View File

@ -61,8 +61,8 @@ namespace std {
#if __cplusplus >= 202002L && !defined(OX_USE_STDLIB) #if __cplusplus >= 202002L && !defined(OX_USE_STDLIB)
template<typename To, typename From> template<typename To, typename From>
constexpr typename ox::enable_if<sizeof(To) == sizeof(From), To>::type bit_cast(From src) noexcept { constexpr typename ox::enable_if<sizeof(To) == sizeof(From), To>::type bit_cast(const From &src) noexcept {
return __builtin_bitcast(src); return __builtin_bit_cast(To, src);
} }
#elif __cplusplus < 202002L #elif __cplusplus < 202002L

View File

@ -172,7 +172,7 @@ constexpr Error toError(const Result<T> &ve) noexcept {
constexpr void oxIgnoreError(const ox::Error&) noexcept {} constexpr void oxIgnoreError(const ox::Error&) noexcept {}
#if __cplusplus >= 202002L #if __cplusplus >= 202002L
#define oxReturnError(x) if (const auto _ox_error = ox::detail::toError(x)) [[unlikely]] return _ox_error #define oxReturnError(x) if (const auto _ox_error = ox::detail::toError(x)) [[unlikely]] return _ox_error
#define oxThrowError(x) if (const auto _ox_error = ox::detail::toError(x)) [[unlikely]] ox::Exception(_ox_error) #define oxThrowError(x) if (const auto _ox_error = ox::detail::toError(x)) [[unlikely]] throw ox::Exception(_ox_error)
#else #else
#define oxReturnError(err) if (const auto _ox_error = ox::detail::toError(err)) return _ox_error #define oxReturnError(err) if (const auto _ox_error = ox::detail::toError(err)) return _ox_error
#define oxThrowError(err) if (const auto _ox_error = ox::detail::toError(err)) throw ox::Exception(_ox_error) #define oxThrowError(err) if (const auto _ox_error = ox::detail::toError(err)) throw ox::Exception(_ox_error)

View File

@ -42,11 +42,7 @@ constexpr const char *toCString(const BasicString<size> &s) noexcept {
} }
#if __has_include(<string>) #if __has_include(<string>)
#if __cplusplus >= 202002L
constexpr
#else
inline inline
#endif
const char *toCString(const std::string &s) noexcept { const char *toCString(const std::string &s) noexcept {
return s.c_str(); return s.c_str();
} }

View File

@ -32,31 +32,31 @@ 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 = std::bit_cast<T*>(m_data); *items = reinterpret_cast<T*>(m_data);
} else { } else {
*items = std::bit_cast<T*>(new AllocAlias<T>[cap]); *items = reinterpret_cast<T*>(new AllocAlias<T>[cap]);
} }
} }
constexpr void moveConstructItemsFrom(T **items, SmallVector &src, const std::size_t count, const std::size_t cap) noexcept { constexpr void moveConstructItemsFrom(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 = reinterpret_cast<T*>(m_data);
const auto srcItems = bit_cast<T*>(src.m_data); const auto srcItems = reinterpret_cast<T*>(src.m_data);
for (auto i = 0u; i < count; ++i) { for (auto i = 0u; i < count; ++i) {
new (&dstItems[i]) T(move(srcItems[i])); new (&dstItems[i]) T(move(srcItems[i]));
} }
*items = bit_cast<T*>(m_data); *items = reinterpret_cast<T*>(m_data);
} }
} }
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 = std::bit_cast<T*>(m_data); const auto dstItems = reinterpret_cast<T*>(m_data);
const auto srcItems = std::bit_cast<T*>(src.m_data); const auto srcItems = reinterpret_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 = std::bit_cast<T*>(m_data); *items = reinterpret_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 = std::bit_cast<T*>(new AllocAlias<T>[cap]); *items = reinterpret_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(std::bit_cast<AllocAlias<T>*>(m_items)); this->clearItems(reinterpret_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(std::bit_cast<AllocAlias<T>*>(m_items)); this->clearItems(reinterpret_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(std::bit_cast<AllocAlias<T>*>(m_items)); this->clearItems(reinterpret_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 {*std::bit_cast<T*>(&v), OxError(1)}; return {*reinterpret_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 {*std::bit_cast<T*>(&v), OxError(1)}; return {*reinterpret_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 {*std::bit_cast<T*>(&v), OxError(1)}; return {*reinterpret_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 {*std::bit_cast<T*>(&v), OxError(1)}; return {*reinterpret_cast<T*>(&v), OxError(1)};
} }
return m_items[m_size - 1]; return m_items[m_size - 1];
} }
@ -604,7 +604,7 @@ void Vector<T, SmallVectorSize>::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;
} }
this->clearItems(std::bit_cast<AllocAlias<T>*>(oldItems)); this->clearItems(reinterpret_cast<AllocAlias<T>*>(oldItems));
} }
} }