[wpiutil] Add ArrayRef/std::span/wpi::span implicit conversions

This commit is contained in:
Peter Johnson
2021-05-23 10:37:30 -07:00
parent bc15b953b4
commit 4426216725
3 changed files with 141 additions and 0 deletions

View File

@@ -14,6 +14,7 @@
#include "wpi/SmallVector.h"
#include "wpi/STLExtras.h"
#include "wpi/Compiler.h"
#include "wpi/span.h"
#include <algorithm>
#include <array>
#include <cassert>
@@ -24,6 +25,9 @@
#include <optional>
#include <type_traits>
#include <vector>
#if __has_include(<span>)
#include <span>
#endif
namespace wpi {
@@ -126,6 +130,24 @@ namespace wpi {
std::is_convertible<U *const *, T const *>::value>::type* = 0)
: Data(Vec.data()), Length(Vec.size()) {}
#ifdef __cpp_lib_span
/// Construct an ArrayRef from std::span
/*implicit*/ ArrayRef(std::span<T> Span)
: Data(Span.data()), Length(Span.size()) {}
/// Construct an ArrayRef from std::span
/*implicit*/ ArrayRef(std::span<const T> Span)
: Data(Span.data()), Length(Span.size()) {}
#endif
/// Construct an ArrayRef from wpi::span
/*implicit*/ ArrayRef(span<T> Span)
: Data(Span.data()), Length(Span.size()) {}
/// Construct an ArrayRef from wpi::span
/*implicit*/ ArrayRef(span<const T> Span)
: Data(Span.data()), Length(Span.size()) {}
/// @}
/// @name Simple Operations
/// @{
@@ -268,6 +290,15 @@ namespace wpi {
return std::vector<T>(Data, Data+Length);
}
#ifdef __cpp_lib_span
operator std::span<const T>() const {
return std::span<const T>(Data, Length);
}
#endif
operator span<const T>() const {
return span<const T>(Data, Length);
}
/// @}
};
@@ -322,6 +353,14 @@ namespace wpi {
template <size_t N>
/*implicit*/ constexpr MutableArrayRef(T (&Arr)[N]) : ArrayRef<T>(Arr) {}
#ifdef __cpp_lib_span
/// Construct an ArrayRef from std::span
/*implicit*/ MutableArrayRef(std::span<T> Span) : ArrayRef<T>(Span) {}
#endif
/// Construct an ArrayRef from wpi::span
/*implicit*/ MutableArrayRef(span<T> Span) : ArrayRef<T>(Span) {}
T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
iterator begin() const { return data(); }
@@ -414,6 +453,16 @@ namespace wpi {
assert(Index < this->size() && "Invalid index!");
return data()[Index];
}
#ifdef __cpp_lib_span
operator std::span<T>() const {
return std::span<T>(data(), this->size());
}
#endif
operator span<T>() const {
return span<T>(data(), this->size());
}
};
/// This is a MutableArrayRef that owns its array.

View File

@@ -17,6 +17,9 @@ http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/n4820.pdf
#include <cstddef>
#include <cstdint>
#include <type_traits>
#if __has_include(<span>)
#include <span>
#endif
namespace wpi {
@@ -219,6 +222,12 @@ public:
: storage_(other.data(), other.size())
{}
#ifdef __cpp_lib_span
constexpr span(std::span<ElementType> other) noexcept
: storage_(other.data(), other.size())
{}
#endif
~span() noexcept = default;
constexpr span&
@@ -327,6 +336,15 @@ public:
return reverse_iterator(begin());
}
#ifdef __cpp_lib_span
constexpr operator auto() const {
return std::span < ElementType,
(Extent == dynamic_extent)
? std::dynamic_extent
: Extent > (storage_.ptr, storage_.size);
}
#endif
private:
storage_type storage_{};
};