Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove deprecated std::aligned_storage & std::aligned_union #331

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/rapidcheck/Gen.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cassert>
#include <exception>

#include "rapidcheck/detail/Any.h"
#include "rapidcheck/detail/ImplicitParam.h"
Expand Down
3 changes: 1 addition & 2 deletions include/rapidcheck/Maybe.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ class Maybe {
~Maybe();

private:
using Storage = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
Storage m_storage;
alignas(T) uint8_t m_storage[sizeof(T)];
bool m_initialized;
};

Expand Down
12 changes: 6 additions & 6 deletions include/rapidcheck/Maybe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ template <typename T>
template <typename... Args>
void Maybe<T>::init(Args &&... args) {
reset();
new (&m_storage) T(std::forward<Args>(args)...);
new (m_storage) T(std::forward<Args>(args)...);
m_initialized = true;
}

Expand All @@ -66,27 +66,27 @@ void Maybe<T>::reset() {

template <typename T>
T &Maybe<T>::operator*() & {
return *reinterpret_cast<T *>(&m_storage);
return *reinterpret_cast<T *>(m_storage);
}

template <typename T>
const T &Maybe<T>::operator*() const & {
return *reinterpret_cast<const T *>(&m_storage);
return *reinterpret_cast<const T *>(m_storage);
}

template <typename T>
T &&Maybe<T>::operator*() && {
return std::move(*reinterpret_cast<T *>(&m_storage));
return std::move(*reinterpret_cast<T *>(m_storage));
}

template <typename T>
T *Maybe<T>::operator->() {
return reinterpret_cast<T *>(&m_storage);
return reinterpret_cast<T *>(m_storage);
}

template <typename T>
const T *Maybe<T>::operator->() const {
return reinterpret_cast<const T *>(&m_storage);
return reinterpret_cast<const T *>(m_storage);
}

template <typename T>
Expand Down
6 changes: 0 additions & 6 deletions include/rapidcheck/detail/AlignedUnion.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,5 @@ struct MaxOf<V1, V2, Vs...>
std::size_t,
(V1 > MaxOf<V2, Vs...>::value ? V1 : MaxOf<V2, Vs...>::value)> {};

/// Replacement for std::aligned_union for compiles that do not have it.
template <typename... Ts>
using AlignedUnion =
typename std::aligned_storage<MaxOf<sizeof(Ts)...>::value,
MaxOf<alignof(Ts)...>::value>::type;

} // namespace detail
} // namespace rc
4 changes: 2 additions & 2 deletions include/rapidcheck/detail/Traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace detail {
#define RC_SFINAE_TRAIT(Name, expression) \
struct Name##Impl { \
template <typename T, typename = expression> \
static std::true_type test(const T &); \
static std::true_type test(const T *); \
static std::false_type test(...); \
}; \
\
template <typename T> \
using Name = decltype(Name##Impl::test(std::declval<T>()));
using Name = decltype(Name##Impl::test(static_cast<T *>(nullptr)));

RC_SFINAE_TRAIT(IsStreamInsertible, decltype(std::cout << std::declval<T>()))

Expand Down
4 changes: 2 additions & 2 deletions include/rapidcheck/detail/Variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ class Variant {
static constexpr bool isValidType();

std::size_t m_typeIndex;
using Storage = AlignedUnion<Type, Types...>;
Storage m_storage;
alignas(MaxOf<alignof(Type), alignof(Types)...>::value)
uint8_t m_storage[MaxOf<sizeof(Type), sizeof(Types)...>::value];
};

template <typename Type, typename... Types>
Expand Down
46 changes: 23 additions & 23 deletions include/rapidcheck/detail/Variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Variant<Type, Types...>::Variant(T &&value) noexcept(
static_assert(isValidType<Decay<T>>(),
"T is not a valid type of this variant");

new (&m_storage) Decay<T>(std::forward<T>(value));
new (m_storage) Decay<T>(std::forward<T>(value));
}

template <typename Type, typename... Types>
Expand All @@ -43,11 +43,11 @@ operator=(const T &value) noexcept {

const auto newIndex = indexOfType<T>();
if (newIndex == m_typeIndex) {
*reinterpret_cast<T *>(&m_storage) = value;
*reinterpret_cast<T *>(m_storage) = value;
} else {
destroy(m_typeIndex, &m_storage);
destroy(m_typeIndex, m_storage);
m_typeIndex = newIndex;
new (&m_storage) T(value);
new (m_storage) T(value);
}
return *this;
}
Expand All @@ -60,11 +60,11 @@ operator=(T &&value) noexcept {

const auto newIndex = indexOfType<T>();
if (newIndex == m_typeIndex) {
*reinterpret_cast<T *>(&m_storage) = std::move(value);
*reinterpret_cast<T *>(m_storage) = std::move(value);
} else {
destroy(m_typeIndex, &m_storage);
destroy(m_typeIndex, m_storage);
m_typeIndex = newIndex;
new (&m_storage) T(std::move(value));
new (m_storage) T(std::move(value));
}
return *this;
}
Expand All @@ -73,14 +73,14 @@ template <typename Type, typename... Types>
template <typename T>
T &Variant<Type, Types...>::get() & {
assert(indexOfType<T>() == m_typeIndex);
return *reinterpret_cast<T *>(&m_storage);
return *reinterpret_cast<T *>(m_storage);
}

template <typename Type, typename... Types>
template <typename T>
const T &Variant<Type, Types...>::get() const & {
assert(indexOfType<T>() == m_typeIndex);
return *reinterpret_cast<const T *>(&m_storage);
return *reinterpret_cast<const T *>(m_storage);
}

template <typename Type, typename... Types>
Expand All @@ -97,7 +97,7 @@ bool Variant<Type, Types...>::match(T &value) const {
return false;
}

value = *reinterpret_cast<const T *>(&m_storage);
value = *reinterpret_cast<const T *>(m_storage);
return true;
}

Expand All @@ -123,7 +123,7 @@ bool Variant<Type, Types...>::operator==(const Variant &rhs) const {
static bool (*const equalsFuncs[])(const void *, const void *) = {
&variantEqualsImpl<Type>, &variantEqualsImpl<Types>...};

return equalsFuncs[m_typeIndex](&m_storage, &rhs.m_storage);
return equalsFuncs[m_typeIndex](m_storage, rhs.m_storage);
}

template <typename T>
Expand All @@ -136,21 +136,21 @@ void Variant<Type, Types...>::printTo(std::ostream &os) const {
static void (*printToFuncs[])(std::ostream &, const void *) = {
&variantPrintToImpl<Type>, &variantPrintToImpl<Types>...};

printToFuncs[m_typeIndex](os, &m_storage);
printToFuncs[m_typeIndex](os, m_storage);
}

template <typename Type, typename... Types>
Variant<Type, Types...>::Variant(const Variant &other) noexcept(
AllIs<std::is_nothrow_copy_constructible, Type, Types...>::value)
: m_typeIndex(other.m_typeIndex) {
copy(m_typeIndex, &m_storage, &other.m_storage);
copy(m_typeIndex, m_storage, other.m_storage);
}

template <typename Type, typename... Types>
Variant<Type, Types...>::Variant(Variant &&other) noexcept(
AllIs<std::is_nothrow_move_constructible, Type, Types...>::value)
: m_typeIndex(other.m_typeIndex) {
move(m_typeIndex, &m_storage, &other.m_storage);
move(m_typeIndex, m_storage, other.m_storage);
}

template <typename Type, typename... Types>
Expand All @@ -163,12 +163,12 @@ operator=(const Variant &rhs) noexcept(
"AllIs types must be nothrow move-constructible to use copy assignment");

if (m_typeIndex == rhs.m_typeIndex) {
copyAssign(m_typeIndex, &m_storage, &rhs.m_storage);
copyAssign(m_typeIndex, m_storage, rhs.m_storage);
} else {
Storage tmp;
copy(rhs.m_typeIndex, &tmp, &rhs.m_storage);
destroy(m_typeIndex, &m_storage);
move(rhs.m_typeIndex, &m_storage, &tmp);
decltype(m_storage) tmp;
copy(rhs.m_typeIndex, tmp, rhs.m_storage);
destroy(m_typeIndex, m_storage);
move(rhs.m_typeIndex, m_storage, tmp);
m_typeIndex = rhs.m_typeIndex;
}

Expand All @@ -184,19 +184,19 @@ operator=(Variant &&rhs) noexcept(
"AllIs types must be nothrow move-constructible to use copy assignment");

if (m_typeIndex == rhs.m_typeIndex) {
moveAssign(m_typeIndex, &m_storage, &rhs.m_storage);
moveAssign(m_typeIndex, m_storage, rhs.m_storage);
} else {
destroy(m_typeIndex, &m_storage);
destroy(m_typeIndex, m_storage);
m_typeIndex = rhs.m_typeIndex;
move(m_typeIndex, &m_storage, &rhs.m_storage);
move(m_typeIndex, m_storage, rhs.m_storage);
}

return *this;
}

template <typename Type, typename... Types>
Variant<Type, Types...>::~Variant() noexcept {
destroy(m_typeIndex, &m_storage);
destroy(m_typeIndex, m_storage);
}

template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion test/detail/MulticastTestListenerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ TEST_CASE("MulticastTestListener") {
prop("passes on correct arguments",
[](const TestMetadata &metadata, const TestResult &result) {
MockTestListener mock;
mock.onTestFinishedCallback = [=](const TestMetadata &meta,
mock.onTestFinishedCallback = [&](const TestMetadata &meta,
const TestResult &res) {
RC_ASSERT(meta == metadata);
RC_ASSERT(res == result);
Expand Down