/////////////////////////////////////////////////////////////////////////////// // // 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_util.h" #include "gsl_byte.h" #include #include #include #include #include #include #ifdef _MSC_VER // turn off some warnings that are noisy about our Expects statements #pragma warning(push) #pragma warning(disable : 4127) // conditional expression is constant // 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 // noexcept is not understood #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 { template class span; // [views.constants], constants constexpr const std::ptrdiff_t dynamic_extent = -1; // implementation details namespace details { template struct is_span_oracle : std::false_type { }; template struct is_span_oracle> : std::true_type { }; template struct is_span : is_span_oracle> { }; template struct is_allowed_pointer_conversion : std::bool_constant< std::is_pointer::value && std::is_pointer::value && std::is_convertible::value > { }; template struct is_allowed_integral_conversion : std::bool_constant< std::is_integral::value && std::is_integral::value && sizeof(From) == sizeof(To) && alignof(From) == alignof(To) && std::is_convertible::value > { }; template struct is_allowed_extent_conversion : std::bool_constant< From == To || From == gsl::dynamic_extent || To == gsl::dynamic_extent > { }; template struct is_allowed_element_type_conversion : std::bool_constant< std::is_same>::value || is_allowed_pointer_conversion::value || is_allowed_integral_conversion::value > { }; template struct is_allowed_element_type_conversion : std::bool_constant::value> { }; template struct is_allowed_element_type_conversion : std::true_type { }; template 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; using const_pointer = std::add_const_t; using pointer = const_pointer; using const_reference = std::add_const_t; using reference = const_reference; 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())); } constexpr reference operator*() const { Expects(span_); return (*span_)[index_]; } constexpr pointer operator->() const { Expects(span_); return &((*span_)[index_]); } constexpr const_span_iterator& operator++() noexcept { Expects(span_ && index_ >= 0 && index_ < span_->length()); ++index_; return *this; } constexpr const_span_iterator operator++(int) noexcept { auto ret = *this; ++(*this); return ret; } constexpr const_span_iterator& operator--() noexcept { Expects(span_ && index > 0 && index_ <= span_->length()); --index_; return *this; } constexpr const_span_iterator operator--(int) noexcept { auto ret = *this; --(*this); return ret; } constexpr const_span_iterator operator+(difference_type n) const noexcept { auto ret{*this}; return ret += n; } constexpr const_span_iterator& operator+=(difference_type n) noexcept { index_ += n; Expects(span_ && index_ >= 0 && index_ <= span_->length()); return *this; } constexpr const_span_iterator operator-(difference_type n) const noexcept { auto ret{*this}; return ret -= n; } constexpr const_span_iterator& operator-=(difference_type n) noexcept { return *this += -n; } constexpr difference_type operator-(const const_span_iterator& rhs) const noexcept { Expects(span_ == rhs.span_); return index_ - rhs.index_; } constexpr reference operator[](difference_type n) const noexcept { return *(*this + n); } constexpr bool operator==(const const_span_iterator& rhs) const noexcept { return span_ == rhs.span_ && index_ == rhs.index_; } constexpr bool operator!=(const const_span_iterator& rhs) const noexcept { return !(*this == rhs); } constexpr bool operator<(const const_span_iterator& rhs) const noexcept { Expects(span_ == rhs.span_); return index_ < rhs.index_; } constexpr bool operator<=(const const_span_iterator& rhs) const noexcept { return !(rhs < *this); } constexpr bool operator>(const const_span_iterator& rhs) const noexcept { return rhs < *this; } constexpr bool operator>=(const const_span_iterator& rhs) const noexcept { return !(rhs > *this); } void swap(const_span_iterator& rhs) noexcept { std::swap(index_, rhs.index_); std::swap(m_span, rhs.m_span); } private: const Span* span_; ptrdiff_t index_; }; template class span_iterator : public const_span_iterator { using base_type = 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; using pointer = value_type*; using reference = value_type&; constexpr span_iterator() : base_type() {} constexpr span_iterator(const Span* span, typename Span::index_type index) : base_type(span, index) {} constexpr reference operator*() const { return reinterpret_cast(base_type::operator*()); } constexpr pointer operator->() const { return reinterpret_cast(base_type::operator->()); } constexpr span_iterator& operator++() noexcept { base_type::operator++(); return *this; } constexpr span_iterator operator++(int) noexcept { return base_type::operator++(1); } constexpr span_iterator& operator--() noexcept { base_type::operator--(); return *this; } constexpr span_iterator operator--(int) noexcept { return base_type::operator--(1); } constexpr span_iterator operator+(difference_type n) const noexcept { return base_type::operator+(n); } constexpr span_iterator& operator+=(difference_type n) noexcept { return base_type::operator+=(n); } constexpr span_iterator operator-(difference_type n) const noexcept { return base_type::operator-(n); } constexpr span_iterator& operator-=(difference_type n) noexcept { return base_type::operator-=(n); } constexpr difference_type operator-(const span_iterator& rhs) const noexcept { return base_type::operator-(rhs); } constexpr reference operator[](difference_type n) const noexcept { return *(*this + n); } constexpr bool operator==(const span_iterator& rhs) const noexcept { return base_type::operator==(rhs); } constexpr bool operator!=(const span_iterator& rhs) const noexcept { return !(*this == rhs); } constexpr bool operator<(const span_iterator& rhs) const noexcept { return base_type::operator<(rhs); } constexpr bool operator<=(const span_iterator& rhs) const noexcept { return !(rhs < *this); } constexpr bool operator>(const span_iterator& rhs) const noexcept { return rhs < *this; } constexpr bool operator>=(const span_iterator& rhs) const noexcept { return !(rhs > *this); } void swap(span_iterator& rhs) noexcept { base_type::swap(rhs); } }; template constexpr const_span_iterator operator+(typename const_span_iterator::difference_type n, const const_span_iterator& rhs) noexcept { return rhs + n; } template constexpr const_span_iterator operator-(typename const_span_iterator::difference_type n, const const_span_iterator& rhs) noexcept { return rhs - n; } template constexpr span_iterator operator+(typename span_iterator::difference_type n, const span_iterator& rhs) noexcept { return rhs + n; } template constexpr span_iterator operator-(typename span_iterator::difference_type n, const span_iterator& rhs) noexcept { return rhs - n; } } // namespace details // [span], class template span template class span { public: // constants and types using element_type = ElementType; using index_type = std::ptrdiff_t; using pointer = element_type*; using reference = element_type&; using iterator = details::span_iterator>; using const_iterator = details::span_iterator; using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; constexpr static const index_type extent = Extent; // [span.cons], span constructors, copy, assignment, and destructor constexpr span() noexcept : storage_(nullptr, extent_type<0>()) {} constexpr span(nullptr_t) noexcept : span() {} constexpr span(pointer ptr, index_type count) : storage_(ptr, count) { Expects((!ptr && count == 0) || (ptr && count >= 0)); } constexpr span(pointer firstElem, pointer lastElem) : storage_(firstElem, std::distance(firstElem, lastElem)) {} template constexpr span(element_type(&arr)[N]) noexcept : storage_(&arr[0], extent_type()) {} template constexpr span(std::array& arr) : storage_(&arr[0], extent_type()) {} template ::value>> constexpr span(std::array, N>& arr) : storage_(&arr[0], extent_type()) {} template ::value>> constexpr span(const std::array, N>& arr) : storage_(&arr[0], extent_type()) {} // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement // on Container to be a contiguous sequence container. template ::value && std::is_convertible::value && std::is_convertible().data())>::value> > constexpr span(Container& cont) : span(cont.data(), cont.size()) {} template ::value && !details::is_span::value && std::is_convertible::value && std::is_convertible().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; template ::value && details::is_allowed_element_type_conversion::value > > constexpr span(const span& other) : storage_(reinterpret_cast(other.data()), extent_type(other.size())) {} template ::value && details::is_allowed_element_type_conversion::value > > constexpr span(span&& other) : storage_(reinterpret_cast(other.data()), extent_type(other.size())) {} ~span() noexcept = default; constexpr span& operator=(const span& other) noexcept = default; constexpr span& operator=(span&& other) noexcept = default; // [span.sub], span subviews template constexpr span first() const { Expects(Count >= 0 && Count <= size()); return { data(), Count }; } template constexpr span last() const { Expects(Count >= 0 && Count <= size()); return{ data() + (size() - Count), Count }; } template constexpr span subspan() const { Expects((Offset == 0 || Offset > 0 && Offset <= size()) && (Count == dynamic_extent || Count >= 0 && Offset + Count <= size())); return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count }; } constexpr span first(index_type count) const { Expects(count >= 0 && count <= size()); return { data(), count }; } constexpr span last(index_type count) const { Expects(count >= 0 && count <= size()); return { data() + (size() - count), count }; } constexpr span subspan(index_type offset, index_type count = dynamic_extent) const { Expects((offset == 0 || offset > 0 && offset <= size()) && (count == dynamic_extent || count >= 0 && offset + count <= size())); return { data() + offset, count == dynamic_extent ? size() - offset : count }; } // [span.obs], span observers constexpr index_type length() const noexcept { return size(); } 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; } // [span.elem], span element access constexpr reference operator[](index_type idx) const { Expects(idx >= 0 && idx < storage_.size()); return storage_.data()[idx]; } constexpr reference operator()(index_type idx) const { return this->operator[](idx); } constexpr pointer data() const noexcept { return storage_.data(); } // [span.iter], span iterator support iterator begin() const noexcept { return {this, 0}; } iterator end() const noexcept { return {this, length()}; } reverse_iterator rbegin() const noexcept { return {this, length()}; } reverse_iterator rend() const noexcept { return {this, 0}; } private: constexpr static const bool is_span_type = true; template class extent_type; template class extent_type { public: static_assert(Extent >= 0, "A fixed-size span must be >= 0 in size."); constexpr extent_type() noexcept {} template constexpr extent_type(extent_type ext) noexcept { static_assert(Other == Extent || Other == dynamic_extent, "Mismatch between fixed-size extent and size of initializing data."); Expects(ext.size() == Extent); } constexpr extent_type(index_type size) { Expects(size == Extent); } constexpr inline index_type size() const noexcept { return Extent; } }; template <> class extent_type { public: template explicit constexpr extent_type(extent_type 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_; }; // 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 storage_type : public ExtentType { public: template constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data) {} constexpr inline pointer data() const noexcept { return data_; } private: pointer data_; }; storage_type> storage_; }; // [span.comparison], span comparison operators template constexpr bool operator==(const span& l, const span& r) { return std::equal(l.begin(), l.end(), r.begin(), r.end()); } template constexpr bool operator!=(const span& l, const span& r) { return !(l == r); } template constexpr bool operator<(const span& l, const span& r) { return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); } template constexpr bool operator<=(const span& l, const span& r) { return !(l > r); } template constexpr bool operator>(const span& l, const span& r) { return r < l; } template constexpr bool operator>=(const span& l, const span& r) { return !(l < r); } namespace 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 struct calculate_byte_size : std::integral_constant(sizeof(ElementType) * static_cast(Extent))> {}; template struct calculate_byte_size : std::integral_constant {}; } // [span.objectrep], views of object representation template span::value> as_bytes(span s) noexcept { return {reinterpret_cast(s.data()), s.size_bytes()}; } template ::value>> span::value> as_writeable_bytes(span s) noexcept { return {reinterpret_cast(s.data()), s.size_bytes()}; } } // 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