mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
Update for C++17 and fix MSVC warnings (#1694)
* Update MSVC arguments * Fix json allocator * Fix simulation diamond * Bump gtest * Remove empty varargs in unit tests * Replace test case with test suite * Remove deprecation warning in optional * Remove need for NOMIXMAX to be defined in wpilib headers
This commit is contained in:
committed by
Peter Johnson
parent
fb1239a2ad
commit
221011494d
@@ -10,6 +10,10 @@
|
||||
# ifndef WPIUTIL_WPI_OPTIONAL_H
|
||||
# define WPIUTIL_WPI_OPTIONAL_H
|
||||
|
||||
#ifdef _WIN32
|
||||
#define _SILENCE_CXX17_ADAPTOR_TYPEDEFS_DEPRECATION_WARNING
|
||||
#endif
|
||||
|
||||
# include <utility>
|
||||
# include <type_traits>
|
||||
# include <initializer_list>
|
||||
@@ -77,7 +81,7 @@ struct has_overloaded_addressof
|
||||
{
|
||||
template <class X>
|
||||
constexpr static bool has_overload(...) { return false; }
|
||||
|
||||
|
||||
template <class X, size_t S = sizeof(std::declval<X&>().operator&()) >
|
||||
constexpr static bool has_overload(bool) { return true; }
|
||||
|
||||
@@ -97,15 +101,15 @@ T* static_addressof(T& ref)
|
||||
}
|
||||
|
||||
|
||||
// the call to convert<A>(b) has return type A and converts b to type A iff b decltype(b) is implicitly convertible to A
|
||||
// the call to convert<A>(b) has return type A and converts b to type A iff b decltype(b) is implicitly convertible to A
|
||||
template <class U>
|
||||
constexpr U convert(U v) { return v; }
|
||||
|
||||
|
||||
|
||||
namespace swap_ns
|
||||
{
|
||||
using std::swap;
|
||||
|
||||
|
||||
template <class T>
|
||||
void adl_swap(T& t, T& u) noexcept(noexcept(swap(t, u)))
|
||||
{
|
||||
@@ -230,12 +234,12 @@ class optional : private OptionalBase<T>
|
||||
{
|
||||
static_assert( !std::is_same<typename std::decay<T>::type, nullopt_t>::value, "bad T" );
|
||||
static_assert( !std::is_same<typename std::decay<T>::type, in_place_t>::value, "bad T" );
|
||||
|
||||
|
||||
|
||||
constexpr bool initialized() const noexcept { return OptionalBase<T>::init_; }
|
||||
typename std::remove_const<T>::type* dataptr() { return std::addressof(OptionalBase<T>::storage_.value_); }
|
||||
constexpr const T* dataptr() const { return detail_::static_addressof(OptionalBase<T>::storage_.value_); }
|
||||
|
||||
|
||||
constexpr const T& contained_val() const& { return OptionalBase<T>::storage_.value_; }
|
||||
# if OPTIONAL_HAS_MOVE_ACCESSORS == 1
|
||||
constexpr T&& contained_val() && { return std::move(OptionalBase<T>::storage_.value_); }
|
||||
@@ -249,7 +253,7 @@ class optional : private OptionalBase<T>
|
||||
if (initialized()) dataptr()->T::~T();
|
||||
OptionalBase<T>::init_ = false;
|
||||
}
|
||||
|
||||
|
||||
template <class... Args>
|
||||
void initialize(Args&&... args) noexcept(noexcept(T(std::forward<Args>(args)...)))
|
||||
{
|
||||
@@ -312,7 +316,7 @@ public:
|
||||
clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
optional& operator=(const optional& rhs)
|
||||
{
|
||||
if (initialized() == true && rhs.initialized() == false) clear();
|
||||
@@ -320,7 +324,7 @@ public:
|
||||
else if (initialized() == true && rhs.initialized() == true) contained_val() = *rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
optional& operator=(optional&& rhs)
|
||||
noexcept(std::is_nothrow_move_assignable<T>::value && std::is_nothrow_move_constructible<T>::value)
|
||||
{
|
||||
@@ -342,22 +346,22 @@ public:
|
||||
else { initialize(std::forward<U>(v)); }
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
template <class... Args>
|
||||
void emplace(Args&&... args)
|
||||
{
|
||||
clear();
|
||||
initialize(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
|
||||
template <class U, class... Args>
|
||||
void emplace(std::initializer_list<U> il, Args&&... args)
|
||||
{
|
||||
clear();
|
||||
initialize<U, Args...>(il, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
|
||||
// 20.5.4.4, Swap
|
||||
void swap(optional<T>& rhs) noexcept(std::is_nothrow_move_constructible<T>::value
|
||||
&& noexcept(detail_::swap_ns::adl_swap(std::declval<T&>(), std::declval<T&>())))
|
||||
@@ -368,30 +372,30 @@ public:
|
||||
}
|
||||
|
||||
// 20.5.4.5, Observers
|
||||
|
||||
|
||||
explicit constexpr operator bool() const noexcept { return initialized(); }
|
||||
constexpr bool has_value() const noexcept { return initialized(); }
|
||||
|
||||
|
||||
constexpr T const* operator ->() const {
|
||||
return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), dataptr());
|
||||
}
|
||||
|
||||
|
||||
# if OPTIONAL_HAS_MOVE_ACCESSORS == 1
|
||||
|
||||
constexpr T* operator ->() {
|
||||
assert (initialized());
|
||||
return dataptr();
|
||||
}
|
||||
|
||||
|
||||
constexpr T const& operator *() const& {
|
||||
return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), contained_val());
|
||||
}
|
||||
|
||||
|
||||
constexpr T& operator *() & {
|
||||
assert (initialized());
|
||||
return contained_val();
|
||||
}
|
||||
|
||||
|
||||
constexpr T&& operator *() && {
|
||||
assert (initialized());
|
||||
return constexpr_move(contained_val());
|
||||
@@ -400,48 +404,48 @@ public:
|
||||
constexpr T const& value() const& {
|
||||
return initialized() ? contained_val() : (throw bad_optional_access("bad optional access"), contained_val());
|
||||
}
|
||||
|
||||
|
||||
constexpr T& value() & {
|
||||
return initialized() ? contained_val() : (throw bad_optional_access("bad optional access"), contained_val());
|
||||
}
|
||||
|
||||
|
||||
constexpr T&& value() && {
|
||||
if (!initialized()) throw bad_optional_access("bad optional access");
|
||||
return std::move(contained_val());
|
||||
}
|
||||
|
||||
|
||||
# else
|
||||
|
||||
T* operator ->() {
|
||||
assert (initialized());
|
||||
return dataptr();
|
||||
}
|
||||
|
||||
|
||||
constexpr T const& operator *() const {
|
||||
return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), contained_val());
|
||||
}
|
||||
|
||||
|
||||
T& operator *() {
|
||||
assert (initialized());
|
||||
return contained_val();
|
||||
}
|
||||
|
||||
|
||||
constexpr T const& value() const {
|
||||
return initialized() ? contained_val() : (throw bad_optional_access("bad optional access"), contained_val());
|
||||
}
|
||||
|
||||
|
||||
T& value() {
|
||||
return initialized() ? contained_val() : (throw bad_optional_access("bad optional access"), contained_val());
|
||||
}
|
||||
|
||||
|
||||
# endif
|
||||
|
||||
|
||||
template <class V>
|
||||
constexpr T value_or(V&& v) const&
|
||||
{
|
||||
return *this ? **this : detail_::convert<T>(constexpr_forward<V>(v));
|
||||
}
|
||||
|
||||
|
||||
# if OPTIONAL_HAS_MOVE_ACCESSORS == 1
|
||||
|
||||
template <class V>
|
||||
@@ -451,15 +455,15 @@ public:
|
||||
}
|
||||
|
||||
# else
|
||||
|
||||
|
||||
template <class V>
|
||||
T value_or(V&& v) &&
|
||||
{
|
||||
return *this ? constexpr_move(const_cast<optional<T>&>(*this).contained_val()) : detail_::convert<T>(constexpr_forward<V>(v));
|
||||
}
|
||||
|
||||
|
||||
# endif
|
||||
|
||||
|
||||
// 20.6.3.6, modifiers
|
||||
void reset() noexcept { clear(); }
|
||||
};
|
||||
@@ -471,42 +475,42 @@ class optional<T&>
|
||||
static_assert( !std::is_same<T, nullopt_t>::value, "bad T" );
|
||||
static_assert( !std::is_same<T, in_place_t>::value, "bad T" );
|
||||
T* ref;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
// 20.5.5.1, construction/destruction
|
||||
constexpr optional() noexcept : ref(nullptr) {}
|
||||
|
||||
|
||||
constexpr optional(nullopt_t) noexcept : ref(nullptr) {}
|
||||
|
||||
|
||||
constexpr optional(T& v) noexcept : ref(detail_::static_addressof(v)) {}
|
||||
|
||||
|
||||
optional(T&&) = delete;
|
||||
|
||||
|
||||
constexpr optional(const optional& rhs) noexcept : ref(rhs.ref) {}
|
||||
|
||||
|
||||
explicit constexpr optional(in_place_t, T& v) noexcept : ref(detail_::static_addressof(v)) {}
|
||||
|
||||
|
||||
explicit optional(in_place_t, T&&) = delete;
|
||||
|
||||
|
||||
~optional() = default;
|
||||
|
||||
|
||||
// 20.5.5.2, mutation
|
||||
optional& operator=(nullopt_t) noexcept {
|
||||
ref = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
// optional& operator=(const optional& rhs) noexcept {
|
||||
// ref = rhs.ref;
|
||||
// return *this;
|
||||
// }
|
||||
|
||||
|
||||
// optional& operator=(optional&& rhs) noexcept {
|
||||
// ref = rhs.ref;
|
||||
// return *this;
|
||||
// }
|
||||
|
||||
|
||||
template <typename U>
|
||||
auto operator=(U&& rhs) noexcept
|
||||
-> typename std::enable_if
|
||||
@@ -518,7 +522,7 @@ public:
|
||||
ref = rhs.ref;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template <typename U>
|
||||
auto operator=(U&& rhs) noexcept
|
||||
-> typename std::enable_if
|
||||
@@ -527,40 +531,40 @@ public:
|
||||
optional&
|
||||
>::type
|
||||
= delete;
|
||||
|
||||
|
||||
void emplace(T& v) noexcept {
|
||||
ref = detail_::static_addressof(v);
|
||||
}
|
||||
|
||||
|
||||
void emplace(T&&) = delete;
|
||||
|
||||
|
||||
|
||||
|
||||
void swap(optional<T&>& rhs) noexcept
|
||||
{
|
||||
std::swap(ref, rhs.ref);
|
||||
}
|
||||
|
||||
|
||||
// 20.5.5.3, observers
|
||||
constexpr T* operator->() const {
|
||||
return TR2_OPTIONAL_ASSERTED_EXPRESSION(ref, ref);
|
||||
}
|
||||
|
||||
|
||||
constexpr T& operator*() const {
|
||||
return TR2_OPTIONAL_ASSERTED_EXPRESSION(ref, *ref);
|
||||
}
|
||||
|
||||
|
||||
constexpr T& value() const {
|
||||
return ref ? *ref : (throw bad_optional_access("bad optional access"), *ref);
|
||||
}
|
||||
|
||||
|
||||
explicit constexpr operator bool() const noexcept {
|
||||
return ref != nullptr;
|
||||
}
|
||||
|
||||
|
||||
constexpr bool has_value() const noexcept {
|
||||
return ref != nullptr;
|
||||
}
|
||||
|
||||
|
||||
template <class V>
|
||||
constexpr typename std::decay<T>::type value_or(V&& v) const
|
||||
{
|
||||
@@ -889,18 +893,18 @@ namespace std
|
||||
{
|
||||
typedef typename hash<T>::result_type result_type;
|
||||
typedef wpi::optional<T> argument_type;
|
||||
|
||||
|
||||
constexpr result_type operator()(argument_type const& arg) const {
|
||||
return arg ? std::hash<T>{}(*arg) : result_type{};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename T>
|
||||
struct hash<wpi::optional<T&>>
|
||||
{
|
||||
typedef typename hash<T>::result_type result_type;
|
||||
typedef wpi::optional<T&> argument_type;
|
||||
|
||||
|
||||
constexpr result_type operator()(argument_type const& arg) const {
|
||||
return arg ? std::hash<T>{}(*arg) : result_type{};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user