/////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2015 Microsoft Corporation. All rights reserved. // // This code is licensed under the MIT License (MIT). // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // ///////////////////////////////////////////////////////////////////////////////
template <typenameEnum, typename T, size_t Length> class EnumeratedArray;
// Stuff from gsl_util
// narrow_cast(): a searchable way to do narrowing casts of values template <class T, class U> inline constexpr T narrow_cast(U&& u) { returnstatic_cast<T>(std::forward<U>(u));
}
// end gsl_util
// [views.constants], constants // This was -1 in gsl::span, but using size_t for sizes instead of ptrdiff_t // and reserving a magic value that realistically doesn't occur in // compile-time-constant Span sizes makes things a lot less messy in terms of // comparison between signed and unsigned.
constexpr const size_t dynamic_extent = std::numeric_limits<size_t>::max();
template <class ElementType, size_t Extent = dynamic_extent> class Span;
template <class T> struct is_std_array : public is_std_array_oracle<std::remove_cv_t<T>> {};
template <size_t From, size_t To> struct is_allowed_extent_conversion
: public std::integral_constant<bool, From == To ||
From == mozilla::dynamic_extent ||
To == mozilla::dynamic_extent> {};
template <class From, class To> struct is_allowed_element_type_conversion
: public std::integral_constant< bool, std::is_convertible_v<From (*)[], To (*)[]>> {};
struct SpanKnownBounds {};
template <class SpanT, bool IsConst> class span_iterator { using element_type_ = typename SpanT::element_type;
public: using iterator_category = std::random_access_iterator_tag; using value_type = std::remove_const_t<element_type_>; using difference_type = ptrdiff_t;
using reference =
std::conditional_t<IsConst, const element_type_, element_type_>&; using pointer = std::add_pointer_t<reference>;
private: // For whatever reason, the compiler doesn't like optimizing away the above // MOZ_RELEASE_ASSERT when `span_iterator` is constructed for // obviously-correct cases like `span.begin()` or `span.end()`. We provide // this private constructor for such cases.
constexpr span_iterator(const SpanT* span, typename SpanT::index_type index,
SpanKnownBounds)
: span_(span), index_(index) {}
public: // `other` is already correct by construction; we do not need to go through // the release assert above. Put differently, this constructor is effectively // a copy constructor and therefore needs no assertions. friendclass span_iterator<SpanT, true>;
constexpr MOZ_IMPLICIT span_iterator(const span_iterator<SpanT, false>& other)
: span_(other.span_), index_(other.index_) {}
constexpr friendbooloperator==(const span_iterator& lhs, const span_iterator& rhs) { // Iterators from different spans are uncomparable. A diagnostic assertion // should be enough to check this, though. To ensure that no iterators from // different spans are ever considered equal, still compare them in release // builds.
MOZ_DIAGNOSTIC_ASSERT(lhs.span_ == rhs.span_); return lhs.index_ == rhs.index_ && lhs.span_ == rhs.span_;
}
/** * Span - slices for C++ * * Span implements Rust's slice concept for C++. It's called "Span" instead of * "Slice" to follow the naming used in C++ Core Guidelines. * * A Span wraps a pointer and a length that identify a non-owning view to a * contiguous block of memory of objects of the same type. Various types, * including (pre-decay) C arrays, XPCOM strings, nsTArray, mozilla::Array, * mozilla::Range and contiguous standard-library containers, auto-convert * into Spans when attempting to pass them as arguments to methods that take * Spans. (Span itself autoconverts into mozilla::Range.) * * Like Rust's slices, Span provides safety against out-of-bounds access by * performing run-time bound checks. However, unlike Rust's slices, Span * cannot provide safety against use-after-free. * * (Note: Span is like Rust's slice only conceptually. Due to the lack of * ABI guarantees, you should still decompose spans/slices to raw pointer * and length parts when crossing the FFI. The Elements() and data() methods * are guaranteed to return a non-null pointer even for zero-length spans, * so the pointer can be used as a raw part of a Rust slice without further * checks.) * * In addition to having constructors (with the support of deduction guides) * that take various well-known types, a Span for an arbitrary type can be * constructed from a pointer and a length or a pointer and another pointer * pointing just past the last element. * * A Span<const char> or Span<const char16_t> can be obtained for const char* * or const char16_t pointing to a zero-terminated string using the * MakeStringSpan() function (which treats a nullptr argument equivalently * to the empty string). Corresponding implicit constructor does not exist * in order to avoid accidental construction in cases where const char* or * const char16_t* do not point to a zero-terminated string. * * Span has methods that follow the Mozilla naming style and methods that * don't. The methods that follow the Mozilla naming style are meant to be * used directly from Mozilla code. The methods that don't are meant for * integration with C++11 range-based loops and with meta-programming that * expects the same methods that are found on the standard-library * containers. For example, to decompose a Span into its parts in Mozilla * code, use Elements() and Length() (as with nsTArray) instead of data() * and size() (as with std::vector). * * The pointer and length wrapped by a Span cannot be changed after a Span has * been created. When new values are required, simply create a new Span. Span * has a method called Subspan() that works analogously to the Substring() * method of XPCOM strings taking a start index and an optional length. As a * Mozilla extension (relative to Microsoft's gsl::span that mozilla::Span is * based on), Span has methods From(start), To(end) and FromTo(start, end) * that correspond to Rust's &slice[start..], &slice[..end] and * &slice[start..end], respectively. (That is, the end index is the index of * the first element not to be included in the new subspan.) * * When indicating a Span that's only read from, const goes inside the type * parameter. Don't put const in front of Span. That is: * size_t ReadsFromOneSpanAndWritesToAnother(Span<const uint8_t> aReadFrom, * Span<uint8_t> aWrittenTo); * * Any Span<const T> can be viewed as Span<const uint8_t> using the function * AsBytes(). Any Span<T> can be viewed as Span<uint8_t> using the function * AsWritableBytes(). * * Note that iterators from different Span instances are uncomparable, even if * they refer to the same memory. This also applies to any spans derived via * Subspan etc.
*/ template <class ElementType, size_t Extent /* = dynamic_extent */> class Span { public: // constants and types using element_type = ElementType; using value_type = std::remove_cv_t<element_type>; using index_type = size_t; using pointer = element_type*; using reference = element_type&;
using iterator =
span_details::span_iterator<Span<ElementType, Extent>, false>; using const_iterator =
span_details::span_iterator<Span<ElementType, Extent>, true>; using reverse_iterator = std::reverse_iterator<iterator>; using const_reverse_iterator = std::reverse_iterator<const_iterator>;
constexpr staticconst index_type extent = Extent;
// [Span.cons], Span constructors, copy, assignment, and destructor // "Dependent" is needed to make "std::enable_if_t<(Dependent || // Extent == 0 || Extent == dynamic_extent)>" SFINAE, // since // "std::enable_if_t<(Extent == 0 || Extent == dynamic_extent)>" is // ill-formed when Extent is neither of the extreme values. /** * Constructor with no args.
*/ template <bool Dependent = false, class = std::enable_if_t<(Dependent || Extent == 0 ||
Extent == dynamic_extent)>>
constexpr Span() : storage_(nullptr, span_details::extent_type<0>()) {}
/** * Constructor for C array.
*/ template <size_t N>
constexpr MOZ_IMPLICIT Span(element_type (&aArr)[N])
: storage_(&aArr[0], span_details::extent_type<N>()) {}
// Implicit constructors for char* and char16_t* pointers are deleted in order // to avoid accidental construction in cases where a pointer does not point to // a zero-terminated string. A Span<const char> or Span<const char16_t> can be // obtained for const char* or const char16_t pointing to a zero-terminated // string using the MakeStringSpan() function. // (This must be a template because otherwise it will prevent the previous // array constructor to match because an array decays to a pointer. This only // exists to point to the above explanation, since there's no other // constructor that would match.) template < typename T, typename = std::enable_if_t<
std::is_pointer_v<T> &&
(std::is_same_v<std::remove_const_t<std::decay_t<T>>, char> ||
std::is_same_v<std::remove_const_t<std::decay_t<T>>, char16_t>)>>
Span(T& aStr) = delete;
/** * Constructor for std::array.
*/ template <size_t N, class ArrayElementType = std::remove_const_t<element_type>>
constexpr MOZ_IMPLICIT Span(std::array<ArrayElementType, N>& aArr)
: storage_(&aArr[0], span_details::extent_type<N>()) {}
/** * Constructor for mozilla::UniquePtr holding an array and length.
*/ template <class ArrayElementType = std::add_pointer<element_type>, class DeleterType>
constexpr Span(const mozilla::UniquePtr<ArrayElementType, DeleterType>& aPtr,
index_type aLength)
: storage_(aPtr.get(), aLength) {}
// NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the // requirement on Container to be a contiguous sequence container. /** * Constructor for standard-library containers.
*/ template < class Container, class Dummy = std::enable_if_t<
!std::is_const_v<Container> &&
!span_details::is_span<Container>::value &&
!span_details::is_std_array<Container>::value &&
std::is_convertible_v<typename Container::pointer, pointer> &&
std::is_convertible_v<typename Container::pointer,
decltype(std::declval<Container>().data())>,
Container>>
constexpr MOZ_IMPLICIT Span(Container& cont, Dummy* = nullptr)
: Span(cont.data(), ReleaseAssertedCast<index_type>(cont.size())) {}
// NB: the SFINAE here uses .Elements() as a incomplete/imperfect proxy for // the requirement on Container to be a contiguous sequence container. /** * Constructor for contiguous Mozilla containers.
*/ template < class Container, class = std::enable_if_t<
!std::is_const_v<Container> &&
!span_details::is_span<Container>::value &&
!span_details::is_std_array<Container>::value &&
std::is_convertible_v<typename Container::value_type*, pointer> &&
std::is_convertible_v< typename Container::value_type*,
decltype(std::declval<Container>().Elements())>>>
constexpr MOZ_IMPLICIT Span(Container& cont, void* = nullptr)
: Span(cont.Elements(), ReleaseAssertedCast<index_type>(cont.Length())) {}
// [Span.sub], Span subviews /** * Subspan with first N elements with compile-time N.
*/ template <size_t Count>
constexpr Span<element_type, Count> First() const {
MOZ_RELEASE_ASSERT(Count <= size()); return {data(), Count};
}
/** * Subspan with last N elements with compile-time N.
*/ template <size_t Count>
constexpr Span<element_type, Count> Last() const { const size_t len = size();
MOZ_RELEASE_ASSERT(Count <= len); return {data() + (len - Count), Count};
}
/** * Subspan with compile-time start index and length.
*/ template <size_t Offset, size_t Count = dynamic_extent>
constexpr Span<element_type, Count> Subspan() const { const size_t len = size();
MOZ_RELEASE_ASSERT(Offset <= len &&
(Count == dynamic_extent || (Offset + Count <= len))); return {data() + Offset, Count == dynamic_extent ? len - Offset : Count};
}
/** * Subspan with first N elements with run-time N.
*/
constexpr Span<element_type, dynamic_extent> First(index_type aCount) const {
MOZ_RELEASE_ASSERT(aCount <= size()); return {data(), aCount};
}
/** * Subspan with last N elements with run-time N.
*/
constexpr Span<element_type, dynamic_extent> Last(index_type aCount) const { const size_t len = size();
MOZ_RELEASE_ASSERT(aCount <= len); return {data() + (len - aCount), aCount};
}
/** * Subspan with run-time start index and length.
*/
constexpr Span<element_type, dynamic_extent> Subspan(
index_type aStart, index_type aLength = dynamic_extent) const { const size_t len = size();
MOZ_RELEASE_ASSERT(aStart <= len && (aLength == dynamic_extent ||
(aStart + aLength <= len))); return {data() + aStart,
aLength == dynamic_extent ? len - aStart : aLength};
}
/** * Pointer to the first element of the span. The return value is never * nullptr, not ever for zero-length spans, so it can be passed as-is * to std::slice::from_raw_parts() in Rust.
*/
constexpr pointer Elements() const { return data(); }
/** * Pointer to the first element of the span (standard-libray duck typing * version). The return value is never nullptr, not ever for zero-length * spans, so it can be passed as-is to std::slice::from_raw_parts() in Rust.
*/
constexpr pointer data() const { return storage_.data(); }
private: // this implementation detail class lets us take advantage of the // empty base class optimization to pay for only storage of a single // pointer in the case of fixed-size Spans template <class ExtentType> class storage_type : public ExtentType { public: template <class OtherExtentType>
constexpr storage_type(pointer elements, OtherExtentType ext)
: ExtentType(ext) // Replace nullptr with aligned bogus pointer for Rust slice // compatibility. See // https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html
,
data_(elements ? elements
: reinterpret_cast<pointer>(alignof(element_type))) { const size_t extentSize = ExtentType::size();
MOZ_RELEASE_ASSERT((!elements && extentSize == 0) ||
(elements && extentSize != dynamic_extent));
}
namespace span_details { // if we only supported compilers with good constexpr support then // this pair of classes could collapse down to a constexpr function
// we should use a narrow_cast<> to go to size_t, but older compilers may not // see it as constexpr and so will fail compilation of the template template <class ElementType, size_t Extent> struct calculate_byte_size
: std::integral_constant<size_t, static_cast<size_t>(sizeof(ElementType) * static_cast<size_t>(Extent))> {
};
/** * View a span of uint8_t as a span of char.
*/ inline Span<constchar> AsChars(Span<const uint8_t> s) { return {reinterpret_cast<constchar*>(s.data()), s.size()};
}
/** * View a writable span of uint8_t as a span of char.
*/ inline Span<char> AsWritableChars(Span<uint8_t> s) { return {reinterpret_cast<char*>(s.data()), s.size()};
}
/** * Create span from a zero-terminated C string. nullptr is * treated as the empty string.
*/
constexpr Span<constchar> MakeStringSpan(constchar* aZeroTerminated) { if (!aZeroTerminated) { return Span<constchar>();
} return Span<constchar>(aZeroTerminated,
std::char_traits<char>::length(aZeroTerminated));
}
/** * Create span from a zero-terminated UTF-16 C string. nullptr is * treated as the empty string.
*/
constexpr Span<const char16_t> MakeStringSpan(const char16_t* aZeroTerminated) { if (!aZeroTerminated) { return Span<const char16_t>();
} return Span<const char16_t>(
aZeroTerminated, std::char_traits<char16_t>::length(aZeroTerminated));
}
} // namespace mozilla
#endif// mozilla_Span_h
¤ Diese beiden folgenden Angebotsgruppen bietet das Unternehmen0.19Angebot
Wie Sie bei der Firma Beratungs- und Dienstleistungen beauftragen können
¤