GSL/include/span.h

777 lines
24 KiB
C
Raw Normal View History

2016-02-24 14:26:28 -05:00
///////////////////////////////////////////////////////////////////////////////
//
// 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.
//
///////////////////////////////////////////////////////////////////////////////
#pragma once
#ifndef GSL_SPAN_H
#define GSL_SPAN_H
#include "gsl_assert.h"
#include "gsl_byte.h"
2016-07-20 16:17:47 -04:00
#include "gsl_util.h"
2016-02-24 14:26:28 -05:00
#include <array>
#include <iterator>
2016-07-20 16:17:47 -04:00
#include <limits>
2016-02-24 14:26:28 -05:00
#include <stdexcept>
#include <type_traits>
#include <utility>
#ifdef _MSC_VER
#pragma warning(push)
2016-03-31 15:01:07 -04:00
// turn off some warnings that are noisy about our Expects statements
2016-02-24 14:26:28 -05:00
#pragma warning(disable : 4127) // conditional expression is constant
2016-03-31 15:01:07 -04:00
// blanket turn off warnings from CppCoreCheck for now
// so people aren't annoyed by them when running the tool.
// more targeted suppressions will be added in a future update to the GSL
2016-07-20 16:17:47 -04:00
#pragma warning(disable : 26481 26482 26483 26485 26490 26491 26492 26493 26495)
2016-03-31 15:01:07 -04:00
2016-02-24 14:26:28 -05:00
// No MSVC does constexpr fully yet
#pragma push_macro("constexpr")
#define constexpr
// VS 2013 workarounds
#if _MSC_VER <= 1800
#define GSL_MSVC_HAS_VARIADIC_CTOR_BUG
#define GSL_MSVC_NO_SUPPORT_FOR_MOVE_CTOR_DEFAULT
2016-07-20 16:17:47 -04:00
// noexcept is not understood
2016-02-24 14:26:28 -05:00
#ifndef GSL_THROW_ON_CONTRACT_VIOLATION
#pragma push_macro("noexcept")
#define noexcept /* nothing */
#endif
// turn off some misguided warnings
#pragma warning(push)
#pragma warning(disable : 4351) // warns about newly introduced aggregate initializer behavior
#pragma warning(disable : 4512) // warns that assignment op could not be generated
#endif // _MSC_VER <= 1800
#endif // _MSC_VER
#ifdef GSL_THROW_ON_CONTRACT_VIOLATION
#ifdef _MSC_VER
#pragma push_macro("noexcept")
#endif
#define noexcept /* nothing */
#endif // GSL_THROW_ON_CONTRACT_VIOLATION
namespace gsl
{
2016-07-20 16:17:47 -04:00
// [views.constants], constants
constexpr const std::ptrdiff_t dynamic_extent = -1;
template <class ElementType, std::ptrdiff_t Extent = dynamic_extent>
class span;
// implementation details
2016-03-01 15:11:41 -05:00
namespace details
{
2016-07-20 16:17:47 -04:00
template <class T>
struct is_span_oracle : std::false_type
{
};
2016-03-01 15:11:41 -05:00
2016-07-20 16:17:47 -04:00
template <class ElementType, std::ptrdiff_t Extent>
struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type
{
};
2016-03-16 22:39:55 -04:00
2016-07-20 16:17:47 -04:00
template <class T>
struct is_span : public is_span_oracle<std::remove_cv_t<T>>
{
};
template <class T>
struct is_std_array_oracle : std::false_type
{
};
template <class ElementType, size_t Extent>
struct is_std_array_oracle<std::array<ElementType, Extent>> : std::true_type
{
};
template <class T>
struct is_std_array : public is_std_array_oracle<std::remove_cv_t<T>>
2016-07-20 16:17:47 -04:00
{
};
2016-03-16 22:39:55 -04:00
2016-07-20 16:17:47 -04:00
template <class From, class To>
struct is_allowed_pointer_conversion
2016-07-26 22:19:47 -04:00
: public std::integral_constant<bool, std::is_pointer<From>::value &&
std::is_pointer<To>::value &&
std::is_convertible<From, To>::value>
2016-07-20 16:17:47 -04:00
{
};
2016-07-20 16:17:47 -04:00
template <class From, class To>
struct is_allowed_integral_conversion
2016-07-26 22:19:47 -04:00
: public std::integral_constant<
bool, std::is_integral<From>::value && std::is_integral<To>::value &&
sizeof(From) == sizeof(To) && alignof(From) == alignof(To) &&
std::is_convertible<From, To>::value>
2016-07-20 16:17:47 -04:00
{
};
2016-03-16 22:39:55 -04:00
2016-07-20 16:17:47 -04:00
template <std::ptrdiff_t From, std::ptrdiff_t To>
struct is_allowed_extent_conversion
2016-07-26 22:19:47 -04:00
: public std::integral_constant<bool, From == To || From == gsl::dynamic_extent ||
To == gsl::dynamic_extent>
2016-07-20 16:17:47 -04:00
{
};
2016-03-16 22:39:55 -04:00
2016-07-20 16:17:47 -04:00
template <class From, class To>
struct is_allowed_element_type_conversion
: public std::integral_constant<bool, std::is_same<From, std::remove_cv_t<To>>::value ||
2016-07-26 22:19:47 -04:00
is_allowed_pointer_conversion<From, To>::value ||
is_allowed_integral_conversion<From, To>::value>
2016-07-20 16:17:47 -04:00
{
};
2016-03-16 22:39:55 -04:00
2016-07-20 16:17:47 -04:00
template <class From>
struct is_allowed_element_type_conversion<From, byte>
: public std::integral_constant<bool, !std::is_const<From>::value>
2016-07-20 16:17:47 -04:00
{
};
2016-03-16 22:39:55 -04:00
2016-07-20 16:17:47 -04:00
template <class From>
struct is_allowed_element_type_conversion<From, const byte> : public std::true_type
2016-07-20 16:17:47 -04:00
{
};
2016-07-20 16:17:47 -04:00
template <class Span>
class const_span_iterator
{
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = typename Span::element_type;
using difference_type = std::ptrdiff_t;
2016-07-20 16:17:47 -04:00
using const_pointer = std::add_const_t<value_type*>;
using pointer = const_pointer;
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
using const_reference = std::add_const_t<value_type&>;
using reference = const_reference;
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
constexpr const_span_iterator() : const_span_iterator(nullptr, 0) {}
constexpr const_span_iterator(const Span* span, typename Span::index_type index)
: span_(span), index_(index)
{
Expects(span == nullptr || (index_ >= 0 && index <= span_->length()));
}
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
constexpr reference operator*() const
{
Expects(span_);
return (*span_)[index_];
}
constexpr pointer operator->() const
{
Expects(span_);
return &((*span_)[index_]);
}
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
constexpr const_span_iterator& operator++() noexcept
{
Expects(span_ && index_ >= 0 && index_ < span_->length());
++index_;
return *this;
}
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
constexpr const_span_iterator operator++(int) noexcept
{
auto ret = *this;
++(*this);
return ret;
}
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
constexpr const_span_iterator& operator--() noexcept
{
Expects(span_ && index_ > 0 && index_ <= span_->length());
--index_;
return *this;
}
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
constexpr const_span_iterator operator--(int) noexcept
{
auto ret = *this;
--(*this);
return ret;
}
2016-07-20 16:17:47 -04:00
constexpr const_span_iterator operator+(difference_type n) const noexcept
{
2016-07-26 21:44:13 -04:00
auto ret = *this;
2016-07-20 16:17:47 -04:00
return ret += n;
}
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
constexpr const_span_iterator& operator+=(difference_type n) noexcept
{
Expects(span_ && (index_ + n) >= 0 && (index_ + n) <= span_->length());
index_ += n;
return *this;
}
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
constexpr const_span_iterator operator-(difference_type n) const noexcept
{
2016-07-26 21:44:13 -04:00
auto ret = *this;
2016-07-20 16:17:47 -04:00
return ret -= n;
}
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
constexpr const_span_iterator& operator-=(difference_type n) noexcept
{
return *this += -n;
}
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
constexpr difference_type operator-(const const_span_iterator& rhs) const noexcept
{
Expects(span_ == rhs.span_);
return index_ - rhs.index_;
}
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
constexpr reference operator[](difference_type n) const noexcept { return *(*this + n); }
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
constexpr bool operator==(const const_span_iterator& rhs) const noexcept
{
return span_ == rhs.span_ && index_ == rhs.index_;
}
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
constexpr bool operator!=(const const_span_iterator& rhs) const noexcept
{
return !(*this == rhs);
}
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
constexpr bool operator<(const const_span_iterator& rhs) const noexcept
{
Expects(span_ == rhs.span_);
return index_ < rhs.index_;
}
2016-07-20 16:17:47 -04:00
constexpr bool operator<=(const const_span_iterator& rhs) const noexcept
{
return !(rhs < *this);
}
2016-07-20 16:17:47 -04:00
constexpr bool operator>(const const_span_iterator& rhs) const noexcept
{
return rhs < *this;
}
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
constexpr bool operator>=(const const_span_iterator& rhs) const noexcept
{
return !(rhs > *this);
}
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
void swap(const_span_iterator& rhs) noexcept
{
std::swap(index_, rhs.index_);
std::swap(span_, rhs.span_);
2016-07-20 16:17:47 -04:00
}
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
private:
const Span* span_;
std::ptrdiff_t index_;
2016-07-20 16:17:47 -04:00
};
2016-07-20 16:17:47 -04:00
template <class Span>
class span_iterator : public const_span_iterator<Span>
{
using base_type = const_span_iterator<Span>;
2016-07-20 16:17:47 -04:00
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = typename Span::element_type;
using difference_type = std::ptrdiff_t;
2016-07-20 16:17:47 -04:00
using pointer = value_type*;
using reference = value_type&;
2016-07-20 16:17:47 -04:00
constexpr span_iterator() : base_type() {}
constexpr span_iterator(const Span* span, typename Span::index_type index)
: base_type(span, index)
{
}
2016-07-20 16:17:47 -04:00
constexpr reference operator*() const
{
return const_cast<reference>(base_type::operator*());
}
constexpr pointer operator->() const
{
return const_cast<pointer>(base_type::operator->());
}
2016-07-20 16:17:47 -04:00
constexpr span_iterator& operator++() noexcept
{
base_type::operator++();
return *this;
}
2016-07-20 16:17:47 -04:00
constexpr span_iterator operator++(int) noexcept { return base_type::operator++(1); }
2016-07-20 16:17:47 -04:00
constexpr span_iterator& operator--() noexcept
{
base_type::operator--();
return *this;
}
2016-07-20 16:17:47 -04:00
constexpr span_iterator operator--(int) noexcept { return base_type::operator--(1); }
2016-07-20 16:17:47 -04:00
constexpr span_iterator operator+(difference_type n) const noexcept
{
return {base_type::operator+(n)};
2016-07-20 16:17:47 -04:00
}
2016-07-20 16:17:47 -04:00
constexpr span_iterator& operator+=(difference_type n) noexcept
{
return {base_type::operator+=(n)};
2016-07-20 16:17:47 -04:00
}
2016-07-20 16:17:47 -04:00
constexpr span_iterator operator-(difference_type n) const noexcept
{
return base_type::operator-(n);
}
2016-07-20 16:17:47 -04:00
constexpr span_iterator& operator-=(difference_type n) noexcept
{
return base_type::operator-=(n);
}
2016-07-20 16:17:47 -04:00
constexpr difference_type operator-(const span_iterator& rhs) const noexcept
{
return base_type::operator-(rhs);
}
2016-07-20 16:17:47 -04:00
constexpr reference operator[](difference_type n) const noexcept { return *(*this + n); }
2016-07-20 16:17:47 -04:00
constexpr bool operator==(const span_iterator& rhs) const noexcept
{
return base_type::operator==(rhs);
}
2016-07-20 16:17:47 -04:00
constexpr bool operator!=(const span_iterator& rhs) const noexcept
{
return !(*this == rhs);
}
2016-07-20 16:17:47 -04:00
constexpr bool operator<(const span_iterator& rhs) const noexcept
{
return base_type::operator<(rhs);
}
2016-07-20 16:17:47 -04:00
constexpr bool operator<=(const span_iterator& rhs) const noexcept
{
return !(rhs < *this);
}
2016-07-20 16:17:47 -04:00
constexpr bool operator>(const span_iterator& rhs) const noexcept { return rhs < *this; }
2016-07-20 16:17:47 -04:00
constexpr bool operator>=(const span_iterator& rhs) const noexcept
{
return !(rhs > *this);
}
2016-07-20 16:17:47 -04:00
void swap(span_iterator& rhs) noexcept { base_type::swap(rhs); }
private:
2016-07-26 22:19:47 -04:00
constexpr span_iterator(const base_type& base) : base_type(base) {}
2016-07-20 16:17:47 -04:00
};
2016-07-20 16:17:47 -04:00
template <typename Span>
constexpr const_span_iterator<Span>
operator+(typename const_span_iterator<Span>::difference_type n,
const const_span_iterator<Span>& rhs) noexcept
{
return rhs + n;
}
2016-07-20 16:17:47 -04:00
template <typename Span>
constexpr const_span_iterator<Span>
operator-(typename const_span_iterator<Span>::difference_type n,
const const_span_iterator<Span>& rhs) noexcept
{
return rhs - n;
}
2016-07-20 16:17:47 -04:00
template <typename Span>
constexpr span_iterator<Span> operator+(typename span_iterator<Span>::difference_type n,
const span_iterator<Span>& rhs) noexcept
{
return rhs + n;
}
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
template <typename Span>
constexpr span_iterator<Span> operator-(typename span_iterator<Span>::difference_type n,
const span_iterator<Span>& rhs) noexcept
{
return rhs - n;
}
2016-03-16 22:39:55 -04:00
template <std::ptrdiff_t Ext>
class extent_type
{
public:
2016-07-26 22:19:47 -04:00
using index_type = std::ptrdiff_t;
static_assert(Ext >= 0, "A fixed-size span must be >= 0 in size.");
constexpr extent_type() noexcept {}
template <index_type Other>
constexpr extent_type(extent_type<Other> ext) noexcept
{
static_assert(Other == Ext || Other == dynamic_extent,
"Mismatch between fixed-size extent and size of initializing data.");
Expects(ext.size() == Ext);
}
constexpr extent_type(index_type size) { Expects(size == Ext); }
constexpr inline index_type size() const noexcept { return Ext; }
};
template <>
class extent_type<dynamic_extent>
{
public:
2016-07-26 22:19:47 -04:00
using index_type = std::ptrdiff_t;
template <index_type Other>
explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size())
{
}
explicit constexpr extent_type(index_type size) : size_(size) { Expects(size >= 0); }
constexpr inline index_type size() const noexcept { return size_; }
private:
index_type size_;
};
2016-03-01 15:11:41 -05:00
} // namespace details
2016-07-20 16:17:47 -04:00
// [span], class template span
2016-03-01 15:11:41 -05:00
template <class ElementType, std::ptrdiff_t Extent>
2016-07-20 16:17:47 -04:00
class span
{
public:
2016-07-20 16:17:47 -04:00
// constants and types
using element_type = ElementType;
using index_type = std::ptrdiff_t;
using pointer = element_type*;
using reference = element_type&;
2016-05-29 16:52:28 -04:00
using iterator = details::span_iterator<span<ElementType, Extent>>;
using const_iterator = details::const_span_iterator<span>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
2016-05-29 16:52:28 -04:00
constexpr static const index_type extent = Extent;
2016-07-20 16:17:47 -04:00
// [span.cons], span constructors, copy, assignment, and destructor
constexpr span() noexcept : storage_(nullptr, details::extent_type<0>()) {}
2016-02-25 14:42:26 -05:00
constexpr span(std::nullptr_t) noexcept : span() {}
2016-02-25 14:42:26 -05:00
2016-07-20 16:17:47 -04:00
constexpr span(pointer ptr, index_type count) : storage_(ptr, count) {}
2016-02-25 14:42:26 -05:00
constexpr span(pointer firstElem, pointer lastElem)
: storage_(firstElem, std::distance(firstElem, lastElem))
2016-07-20 16:17:47 -04:00
{
}
template <size_t N>
constexpr span(element_type (&arr)[N]) noexcept : storage_(&arr[0], details::extent_type<N>())
2016-07-20 16:17:47 -04:00
{
}
2016-07-26 22:19:47 -04:00
template <size_t N, class ArrayElementType = std::remove_const_t<element_type>>
constexpr span(std::array<ArrayElementType, N>& arr) noexcept
: storage_(&arr[0], details::extent_type<N>())
2016-07-20 16:17:47 -04:00
{
}
template <size_t N>
2016-07-20 12:24:49 -04:00
constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) noexcept
: storage_(&arr[0], details::extent_type<N>())
2016-07-20 16:17:47 -04:00
{
}
2016-02-29 16:16:48 -05:00
2016-03-01 15:11:41 -05:00
// NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
// on Container to be a contiguous sequence container.
template <class Container,
2016-07-20 16:17:47 -04:00
class = std::enable_if_t<
2016-07-26 22:19:47 -04:00
!details::is_span<Container>::value && !details::is_std_array<Container>::value &&
std::is_convertible<typename Container::pointer, pointer>::value &&
std::is_convertible<typename Container::pointer,
2016-07-20 16:17:47 -04:00
decltype(std::declval<Container>().data())>::value>>
constexpr span(Container& cont) : span(cont.data(), cont.size())
{
}
2016-03-01 15:11:41 -05:00
template <class Container,
2016-07-20 16:17:47 -04:00
class = std::enable_if_t<
std::is_const<element_type>::value && !details::is_span<Container>::value &&
std::is_convertible<typename Container::pointer, pointer>::value &&
std::is_convertible<typename Container::pointer,
2016-07-20 16:17:47 -04:00
decltype(std::declval<Container>().data())>::value>>
constexpr span(const Container& cont) : span(cont.data(), cont.size())
{
}
constexpr span(const span& other) noexcept = default;
constexpr span(span&& other) noexcept = default;
2016-03-16 22:39:55 -04:00
2016-07-20 16:17:47 -04:00
template <
class OtherElementType, std::ptrdiff_t OtherExtent,
class = std::enable_if_t<
2016-07-20 16:17:47 -04:00
details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>>
2016-03-16 22:39:55 -04:00
constexpr span(const span<OtherElementType, OtherExtent>& other)
2016-07-26 22:19:47 -04:00
: storage_(reinterpret_cast<pointer>(other.data()),
details::extent_type<OtherExtent>(other.size()))
2016-07-20 16:17:47 -04:00
{
}
2016-03-16 22:39:55 -04:00
2016-07-20 16:17:47 -04:00
template <
class OtherElementType, std::ptrdiff_t OtherExtent,
class = std::enable_if_t<
details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
2016-07-20 16:17:47 -04:00
details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>>
2016-03-16 22:39:55 -04:00
constexpr span(span<OtherElementType, OtherExtent>&& other)
2016-07-26 22:19:47 -04:00
: storage_(reinterpret_cast<pointer>(other.data()),
details::extent_type<OtherExtent>(other.size()))
2016-07-20 16:17:47 -04:00
{
}
2016-03-16 22:39:55 -04:00
~span() noexcept = default;
constexpr span& operator=(const span& other) noexcept = default;
constexpr span& operator=(span&& other) noexcept = default;
2016-07-20 16:17:47 -04:00
// [span.sub], span subviews
template <std::ptrdiff_t Count>
2016-03-18 19:49:29 -04:00
constexpr span<element_type, Count> first() const
{
Expects(Count >= 0 && Count <= size());
2016-07-20 16:17:47 -04:00
return {data(), Count};
2016-03-18 19:49:29 -04:00
}
template <std::ptrdiff_t Count>
2016-03-18 19:49:29 -04:00
constexpr span<element_type, Count> last() const
{
Expects(Count >= 0 && Count <= size());
2016-07-20 16:17:47 -04:00
return {data() + (size() - Count), Count};
2016-03-18 19:49:29 -04:00
}
template <std::ptrdiff_t Offset, std::ptrdiff_t Count = dynamic_extent>
2016-03-18 19:49:29 -04:00
constexpr span<element_type, Count> subspan() const
{
Expects((Offset == 0 || (Offset > 0 && Offset <= size())) &&
(Count == dynamic_extent || (Count >= 0 && Offset + Count <= size())));
2016-07-20 16:17:47 -04:00
return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
2016-03-18 19:49:29 -04:00
}
constexpr span<element_type, dynamic_extent> first(index_type count) const
{
Expects(count >= 0 && count <= size());
2016-07-20 16:17:47 -04:00
return {data(), count};
2016-03-18 19:49:29 -04:00
}
constexpr span<element_type, dynamic_extent> last(index_type count) const
{
Expects(count >= 0 && count <= size());
2016-07-20 16:17:47 -04:00
return {data() + (size() - count), count};
2016-03-18 19:49:29 -04:00
}
constexpr span<element_type, dynamic_extent> subspan(index_type offset,
2016-07-20 16:17:47 -04:00
index_type count = dynamic_extent) const
2016-03-18 19:49:29 -04:00
{
Expects((offset == 0 || (offset > 0 && offset <= size())) &&
(count == dynamic_extent || (count >= 0 && offset + count <= size())));
2016-07-20 16:17:47 -04:00
return {data() + offset, count == dynamic_extent ? size() - offset : count};
2016-03-18 19:49:29 -04:00
}
2016-07-20 16:17:47 -04:00
// [span.obs], span observers
constexpr index_type length() const noexcept { return size(); }
2016-07-20 16:17:47 -04:00
constexpr index_type size() const noexcept { return storage_.size(); }
constexpr index_type length_bytes() const noexcept { return size_bytes(); }
constexpr index_type size_bytes() const noexcept { return size() * sizeof(element_type); }
constexpr bool empty() const noexcept { return size() == 0; }
2016-07-20 16:17:47 -04:00
// [span.elem], span element access
2016-02-25 14:42:26 -05:00
constexpr reference operator[](index_type idx) const
{
Expects(idx >= 0 && idx < storage_.size());
2016-07-20 12:24:49 -04:00
return data()[idx];
2016-02-25 14:42:26 -05:00
}
constexpr reference operator()(index_type idx) const { return this->operator[](idx); }
constexpr pointer data() const noexcept { return storage_.data(); }
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
// [span.iter], span iterator support
2016-05-29 16:52:28 -04:00
iterator begin() const noexcept { return {this, 0}; }
iterator end() const noexcept { return {this, length()}; }
const_iterator cbegin() const noexcept { return {this, 0}; }
const_iterator cend() const noexcept { return {this, length()}; }
2016-07-20 16:17:47 -04:00
reverse_iterator rbegin() const noexcept { return reverse_iterator{{this, length()}}; }
reverse_iterator rend() const noexcept { return reverse_iterator{{this, 0}}; }
const_reverse_iterator crbegin() const noexcept { return reverse_iterator{{this, length()}}; }
const_reverse_iterator crend() const noexcept { return reverse_iterator{{this, 0}}; }
private:
2016-07-20 16:17:47 -04:00
// 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>
2016-07-20 16:17:47 -04:00
constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data)
{
2016-07-26 21:44:13 -04:00
Expects((!data && ExtentType::size() == 0) || (data && ExtentType::size() >= 0));
2016-07-20 16:17:47 -04:00
}
2016-07-20 16:17:47 -04:00
constexpr inline pointer data() const noexcept { return data_; }
private:
pointer data_;
};
storage_type<details::extent_type<Extent>> storage_;
};
2016-07-20 16:17:47 -04:00
// [span.comparison], span comparison operators
template <class ElementType, std::ptrdiff_t FirstExtent, std::ptrdiff_t SecondExtent>
2016-07-26 22:19:47 -04:00
constexpr bool operator==(const span<ElementType, FirstExtent>& l,
const span<ElementType, SecondExtent>& r)
2016-07-20 16:17:47 -04:00
{
return std::equal(l.begin(), l.end(), r.begin(), r.end());
}
template <class ElementType, std::ptrdiff_t Extent>
2016-07-20 16:17:47 -04:00
constexpr bool operator!=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
{
return !(l == r);
}
template <class ElementType, std::ptrdiff_t Extent>
2016-07-20 16:17:47 -04:00
constexpr bool operator<(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
{
return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
}
template <class ElementType, std::ptrdiff_t Extent>
2016-07-20 16:17:47 -04:00
constexpr bool operator<=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
{
return !(l > r);
}
template <class ElementType, std::ptrdiff_t Extent>
2016-07-20 16:17:47 -04:00
constexpr bool operator>(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
{
return r < l;
}
template <class ElementType, std::ptrdiff_t Extent>
2016-07-20 16:17:47 -04:00
constexpr bool operator>=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
{
return !(l < r);
}
2016-05-29 20:06:29 -04:00
namespace details
{
// if we only supported compilers with good constexpr support then
// this pair of classes could collapse down to a constexpr function
2016-07-20 16:17:47 -04:00
// we should use a narrow_cast<> to go to size_t, but older compilers may not see it as
// constexpr
2016-05-29 20:06:29 -04:00
// and so will fail compilation of the template
template <class ElementType, std::ptrdiff_t Extent>
2016-07-20 16:17:47 -04:00
struct calculate_byte_size
: std::integral_constant<std::ptrdiff_t,
static_cast<std::ptrdiff_t>(sizeof(ElementType) *
2016-07-26 22:19:47 -04:00
static_cast<std::size_t>(Extent))>
2016-07-20 16:17:47 -04:00
{
};
2016-05-29 20:06:29 -04:00
template <class ElementType>
2016-07-20 16:17:47 -04:00
struct calculate_byte_size<ElementType, dynamic_extent>
: std::integral_constant<std::ptrdiff_t, dynamic_extent>
{
};
2016-05-29 20:06:29 -04:00
}
2016-07-20 16:17:47 -04:00
// [span.objectrep], views of object representation
template <class ElementType, std::ptrdiff_t Extent>
2016-07-20 16:17:47 -04:00
span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
as_bytes(span<ElementType, Extent> s) noexcept
{
return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
}
template <class ElementType, std::ptrdiff_t Extent,
2016-07-20 16:17:47 -04:00
class = std::enable_if_t<!std::is_const<ElementType>::value>>
span<byte, details::calculate_byte_size<ElementType, Extent>::value>
as_writeable_bytes(span<ElementType, Extent> s) noexcept
{
return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
}
2016-02-24 14:26:28 -05:00
} // namespace gsl
#ifdef _MSC_VER
#undef constexpr
#pragma pop_macro("constexpr")
#if _MSC_VER <= 1800
#pragma warning(pop)
#ifndef GSL_THROW_ON_CONTRACT_VIOLATION
#undef noexcept
#pragma pop_macro("noexcept")
#endif // GSL_THROW_ON_CONTRACT_VIOLATION
#undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
#endif // _MSC_VER <= 1800
#endif // _MSC_VER
#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
#undef noexcept
#ifdef _MSC_VER
#pragma warning(pop)
#pragma pop_macro("noexcept")
#endif
#endif // GSL_THROW_ON_CONTRACT_VIOLATION
#endif // GSL_SPAN_H