2016-08-01 21:49:48 -04:00
|
|
|
|
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
|
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
#include <gsl/gsl_assert>
|
|
|
|
#include <gsl/gsl_byte>
|
|
|
|
#include <gsl/gsl_util>
|
2017-04-20 10:51:37 -04:00
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
#include <array>
|
2016-02-24 19:11:33 -05:00
|
|
|
#include <iterator>
|
2016-07-20 16:17:47 -04:00
|
|
|
#include <limits>
|
2017-04-20 10:51:37 -04:00
|
|
|
#include <memory>
|
2016-02-24 14:26:28 -05:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2017-04-20 10:51:37 -04:00
|
|
|
#pragma warning(push)
|
|
|
|
|
|
|
|
// turn off some warnings that are noisy about our Expects statements
|
|
|
|
#pragma warning(disable : 4127) // conditional expression is constant
|
|
|
|
|
|
|
|
// 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
|
|
|
|
#pragma warning(disable : 26481 26482 26483 26485 26490 26491 26492 26493 26495)
|
|
|
|
|
|
|
|
#if _MSC_VER < 1910
|
|
|
|
#pragma push_macro("constexpr")
|
|
|
|
#define constexpr /*constexpr*/
|
|
|
|
|
|
|
|
// VS 2013 workarounds
|
|
|
|
#if _MSC_VER <= 1800
|
|
|
|
#define GSL_MSVC_HAS_VARIADIC_CTOR_BUG
|
|
|
|
#define GSL_MSVC_NO_DEFAULT_MOVE_CTOR
|
|
|
|
#define GSL_MSVC_NO_CPP14_STD_EQUAL
|
|
|
|
|
|
|
|
// noexcept is not understood
|
|
|
|
#pragma push_macro("noexcept")
|
|
|
|
#define noexcept /*noexcept*/
|
|
|
|
|
|
|
|
#pragma push_macro("alignof")
|
|
|
|
#define alignof __alignof
|
|
|
|
|
|
|
|
// turn off some misguided warnings
|
|
|
|
#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 < 1910
|
|
|
|
#endif // _MSC_VER
|
2016-02-24 14:26:28 -05:00
|
|
|
|
|
|
|
#ifdef GSL_THROW_ON_CONTRACT_VIOLATION
|
2017-04-20 10:51:37 -04:00
|
|
|
#define GSL_NOEXCEPT /*noexcept*/
|
2017-02-07 18:59:37 -05:00
|
|
|
#else
|
2017-04-20 10:51:37 -04:00
|
|
|
#define GSL_NOEXCEPT noexcept
|
2016-02-24 14:26:28 -05:00
|
|
|
#endif // GSL_THROW_ON_CONTRACT_VIOLATION
|
|
|
|
|
|
|
|
namespace gsl
|
|
|
|
{
|
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
// [views.constants], constants
|
2016-06-12 21:28:19 -04:00
|
|
|
constexpr const std::ptrdiff_t dynamic_extent = -1;
|
|
|
|
|
2016-07-26 21:34:27 -04:00
|
|
|
template <class ElementType, std::ptrdiff_t Extent = dynamic_extent>
|
|
|
|
class span;
|
|
|
|
|
2016-06-12 21:28:19 -04:00
|
|
|
// 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>
|
2016-07-26 21:34:27 -04:00
|
|
|
struct is_span : public is_span_oracle<std::remove_cv_t<T>>
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct is_std_array_oracle : std::false_type
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2017-02-13 15:11:45 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
2016-07-26 21:34:27 -04:00
|
|
|
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 <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
|
2016-09-15 01:01:02 -04:00
|
|
|
: public std::integral_constant<bool, std::is_convertible<From (*)[], To (*)[]>::value>
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
};
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2016-08-08 15:06:47 -04:00
|
|
|
template <class Span, bool IsConst>
|
2016-08-02 00:41:20 -04:00
|
|
|
class span_iterator
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2016-12-09 23:19:50 -05:00
|
|
|
using element_type_ = typename Span::element_type;
|
2017-04-20 10:51:37 -04:00
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
public:
|
|
|
|
using iterator_category = std::random_access_iterator_tag;
|
2017-04-13 16:55:20 -04:00
|
|
|
using value_type = std::remove_cv_t<element_type_>;
|
2016-08-08 15:06:47 -04:00
|
|
|
using difference_type = typename Span::index_type;
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
using reference = std::conditional_t<IsConst, const element_type_, element_type_>&;
|
2016-12-09 23:19:50 -05:00
|
|
|
using pointer = std::add_pointer_t<reference>;
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr span_iterator() GSL_NOEXCEPT : span_iterator(nullptr, 0) {}
|
2016-08-08 16:33:02 -04:00
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
constexpr span_iterator(const Span* span, typename Span::index_type index)
|
2016-08-01 21:49:48 -04:00
|
|
|
: span_(span), index_(index)
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2016-08-01 21:49:48 -04:00
|
|
|
Expects(span == nullptr || (index_ >= 0 && index <= span_->length()));
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2016-08-08 15:06:47 -04:00
|
|
|
friend class span_iterator<Span, true>;
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr span_iterator(const span_iterator<Span, false>& other) GSL_NOEXCEPT
|
2016-08-08 16:33:02 -04:00
|
|
|
: span_iterator(other.span_, other.index_)
|
|
|
|
{
|
|
|
|
}
|
2016-08-08 15:06:47 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
constexpr span_iterator<Span, IsConst>&
|
|
|
|
operator=(const span_iterator<Span, IsConst>&) GSL_NOEXCEPT = default;
|
2016-10-09 20:01:46 -04:00
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
constexpr reference operator*() const
|
|
|
|
{
|
2016-08-01 21:49:48 -04:00
|
|
|
Expects(span_);
|
|
|
|
return (*span_)[index_];
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-08-01 21:49:48 -04:00
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
constexpr pointer operator->() const
|
|
|
|
{
|
2016-08-01 21:49:48 -04:00
|
|
|
Expects(span_);
|
|
|
|
return &((*span_)[index_]);
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2016-11-03 16:15:33 -04:00
|
|
|
constexpr span_iterator& operator++()
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2016-08-01 21:49:48 -04:00
|
|
|
Expects(span_ && index_ >= 0 && index_ < span_->length());
|
|
|
|
++index_;
|
2016-07-20 16:17:47 -04:00
|
|
|
return *this;
|
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr span_iterator operator++(int) GSL_NOEXCEPT
|
2016-08-01 21:49:48 -04:00
|
|
|
{
|
|
|
|
auto ret = *this;
|
|
|
|
++(*this);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2016-11-03 16:15:33 -04:00
|
|
|
constexpr span_iterator& operator--()
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2016-08-01 21:49:48 -04:00
|
|
|
Expects(span_ && index_ > 0 && index_ <= span_->length());
|
|
|
|
--index_;
|
2016-07-20 16:17:47 -04:00
|
|
|
return *this;
|
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr span_iterator operator--(int) GSL_NOEXCEPT
|
2016-08-01 21:49:48 -04:00
|
|
|
{
|
|
|
|
auto ret = *this;
|
|
|
|
--(*this);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr span_iterator operator+(difference_type n) const GSL_NOEXCEPT
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2016-08-01 21:49:48 -04:00
|
|
|
auto ret = *this;
|
|
|
|
return ret += n;
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2016-11-03 16:15:33 -04:00
|
|
|
constexpr span_iterator& operator+=(difference_type n)
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2016-08-01 21:49:48 -04:00
|
|
|
Expects(span_ && (index_ + n) >= 0 && (index_ + n) <= span_->length());
|
|
|
|
index_ += n;
|
|
|
|
return *this;
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr span_iterator operator-(difference_type n) const GSL_NOEXCEPT
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2016-08-01 21:49:48 -04:00
|
|
|
auto ret = *this;
|
|
|
|
return ret -= n;
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr span_iterator& operator-=(difference_type n) GSL_NOEXCEPT { return *this += -n; }
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2016-11-03 16:15:33 -04:00
|
|
|
constexpr difference_type operator-(const span_iterator& rhs) const
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2016-08-01 21:49:48 -04:00
|
|
|
Expects(span_ == rhs.span_);
|
|
|
|
return index_ - rhs.index_;
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
constexpr reference operator[](difference_type n) const GSL_NOEXCEPT
|
|
|
|
{
|
|
|
|
return *(*this + n);
|
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2016-08-08 16:33:02 -04:00
|
|
|
constexpr friend bool operator==(const span_iterator& lhs,
|
2017-02-07 18:59:37 -05:00
|
|
|
const span_iterator& rhs) GSL_NOEXCEPT
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2016-08-08 15:06:47 -04:00
|
|
|
return lhs.span_ == rhs.span_ && lhs.index_ == rhs.index_;
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2016-08-08 16:33:02 -04:00
|
|
|
constexpr friend bool operator!=(const span_iterator& lhs,
|
2017-02-07 18:59:37 -05:00
|
|
|
const span_iterator& rhs) GSL_NOEXCEPT
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2016-08-08 15:06:47 -04:00
|
|
|
return !(lhs == rhs);
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2016-11-03 16:15:33 -04:00
|
|
|
constexpr friend bool operator<(const span_iterator& lhs, const span_iterator& rhs)
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2016-08-08 15:06:47 -04:00
|
|
|
Expects(lhs.span_ == rhs.span_);
|
|
|
|
return lhs.index_ < rhs.index_;
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2016-08-08 16:33:02 -04:00
|
|
|
constexpr friend bool operator<=(const span_iterator& lhs,
|
2017-02-07 18:59:37 -05:00
|
|
|
const span_iterator& rhs) GSL_NOEXCEPT
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2016-08-08 15:06:47 -04:00
|
|
|
return !(rhs < lhs);
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
constexpr friend bool operator>(const span_iterator& lhs,
|
|
|
|
const span_iterator& rhs) GSL_NOEXCEPT
|
2016-08-08 15:06:47 -04:00
|
|
|
{
|
|
|
|
return rhs < lhs;
|
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2016-08-08 16:33:02 -04:00
|
|
|
constexpr friend bool operator>=(const span_iterator& lhs,
|
2017-02-07 18:59:37 -05:00
|
|
|
const span_iterator& rhs) GSL_NOEXCEPT
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2016-08-08 15:06:47 -04:00
|
|
|
return !(rhs > lhs);
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
void swap(span_iterator& rhs) GSL_NOEXCEPT
|
2016-08-01 21:49:48 -04:00
|
|
|
{
|
|
|
|
std::swap(index_, rhs.index_);
|
|
|
|
std::swap(span_, rhs.span_);
|
|
|
|
}
|
|
|
|
|
2016-08-08 15:06:47 -04:00
|
|
|
protected:
|
2016-08-01 21:49:48 -04:00
|
|
|
const Span* span_;
|
|
|
|
std::ptrdiff_t index_;
|
2016-07-20 16:17:47 -04:00
|
|
|
};
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2016-08-08 15:06:47 -04:00
|
|
|
template <class Span, bool IsConst>
|
2017-02-07 18:59:37 -05:00
|
|
|
inline constexpr span_iterator<Span, IsConst>
|
2016-08-08 15:06:47 -04:00
|
|
|
operator+(typename span_iterator<Span, IsConst>::difference_type n,
|
2017-02-07 18:59:37 -05:00
|
|
|
const span_iterator<Span, IsConst>& rhs) GSL_NOEXCEPT
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
return rhs + n;
|
|
|
|
}
|
2016-05-29 16:52:28 -04:00
|
|
|
|
2016-08-08 15:06:47 -04:00
|
|
|
template <class Span, bool IsConst>
|
2017-02-07 18:59:37 -05:00
|
|
|
inline constexpr span_iterator<Span, IsConst>
|
2016-08-08 15:06:47 -04:00
|
|
|
operator-(typename span_iterator<Span, IsConst>::difference_type n,
|
2017-02-07 18:59:37 -05:00
|
|
|
const span_iterator<Span, IsConst>& rhs) GSL_NOEXCEPT
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
return rhs - n;
|
|
|
|
}
|
2016-03-16 22:39:55 -04:00
|
|
|
|
2016-07-26 21:34:27 -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;
|
2016-07-26 21:34:27 -04:00
|
|
|
|
|
|
|
static_assert(Ext >= 0, "A fixed-size span must be >= 0 in size.");
|
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr extent_type() GSL_NOEXCEPT {}
|
2016-07-26 21:34:27 -04:00
|
|
|
|
|
|
|
template <index_type Other>
|
2017-01-28 03:08:48 -05:00
|
|
|
constexpr extent_type(extent_type<Other> ext)
|
2016-07-26 21:34:27 -04:00
|
|
|
{
|
|
|
|
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); }
|
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr index_type size() const GSL_NOEXCEPT { return Ext; }
|
2016-07-26 21:34:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class extent_type<dynamic_extent>
|
|
|
|
{
|
|
|
|
public:
|
2016-07-26 22:19:47 -04:00
|
|
|
using index_type = std::ptrdiff_t;
|
2016-07-26 21:34:27 -04:00
|
|
|
|
|
|
|
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); }
|
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr index_type size() const GSL_NOEXCEPT { return size_; }
|
2016-07-26 21:34:27 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2016-02-24 19:11:33 -05:00
|
|
|
public:
|
2016-07-20 16:17:47 -04:00
|
|
|
// constants and types
|
2016-02-24 19:11:33 -05:00
|
|
|
using element_type = ElementType;
|
2017-04-20 10:51:37 -04:00
|
|
|
using value_type = std::remove_cv_t<ElementType>;
|
2016-02-24 19:11:33 -05:00
|
|
|
using index_type = std::ptrdiff_t;
|
|
|
|
using pointer = element_type*;
|
|
|
|
using reference = element_type&;
|
2016-05-29 16:52:28 -04:00
|
|
|
|
2016-08-08 15:06:47 -04:00
|
|
|
using iterator = details::span_iterator<span<ElementType, Extent>, false>;
|
|
|
|
using const_iterator = details::span_iterator<span<ElementType, Extent>, true>;
|
2016-02-24 19:11:33 -05:00
|
|
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
2016-06-26 10:00:56 -04:00
|
|
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
2016-05-29 16:52:28 -04:00
|
|
|
|
2017-04-13 16:55:20 -04:00
|
|
|
using size_type = index_type;
|
|
|
|
|
2016-02-24 19:11:33 -05:00
|
|
|
constexpr static const index_type extent = Extent;
|
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
// [span.cons], span constructors, copy, assignment, and destructor
|
2017-01-28 03:08:48 -05:00
|
|
|
template <bool Dependent = false,
|
2017-04-20 10:51:37 -04:00
|
|
|
// "Dependent" is needed to make "std::enable_if_t<Dependent || Extent <= 0>" SFINAE,
|
|
|
|
// since "std::enable_if_t<Extent <= 0>" is ill-formed when Extent is greater than 0.
|
|
|
|
class = std::enable_if_t<(Dependent || Extent <= 0)>>
|
|
|
|
constexpr span() GSL_NOEXCEPT : storage_(nullptr, details::extent_type<0>())
|
|
|
|
{
|
|
|
|
}
|
2016-02-25 14:42:26 -05:00
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr span(std::nullptr_t) GSL_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
|
|
|
|
2016-02-28 03:50:53 -05:00
|
|
|
constexpr span(pointer firstElem, pointer lastElem)
|
|
|
|
: storage_(firstElem, std::distance(firstElem, lastElem))
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-13 15:11:45 -05:00
|
|
|
template <std::size_t N>
|
2017-04-20 10:51:37 -04:00
|
|
|
constexpr span(element_type (&arr)[N]) GSL_NOEXCEPT
|
|
|
|
: storage_(&arr[0], details::extent_type<N>())
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
}
|
2016-07-26 22:19:47 -04:00
|
|
|
|
2017-02-13 15:11:45 -05:00
|
|
|
template <std::size_t N, class ArrayElementType = std::remove_const_t<element_type>>
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr span(std::array<ArrayElementType, N>& arr) GSL_NOEXCEPT
|
2016-07-26 21:34:27 -04:00
|
|
|
: storage_(&arr[0], details::extent_type<N>())
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
}
|
2016-06-14 23:14:17 -04:00
|
|
|
|
2017-02-13 15:11:45 -05:00
|
|
|
template <std::size_t N>
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) GSL_NOEXCEPT
|
2016-07-26 21:34:27 -04:00
|
|
|
: storage_(&arr[0], details::extent_type<N>())
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
}
|
2016-02-29 16:16:48 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
template <class ArrayElementType = std::add_pointer<element_type>>
|
|
|
|
constexpr span(const std::unique_ptr<ArrayElementType>& ptr, index_type count)
|
|
|
|
: storage_(ptr.get(), count)
|
|
|
|
{
|
|
|
|
}
|
2016-11-03 21:55:41 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
constexpr span(const std::unique_ptr<ElementType>& ptr) : storage_(ptr.get(), ptr.get() ? 1 : 0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
constexpr span(const std::shared_ptr<ElementType>& ptr) : storage_(ptr.get(), ptr.get() ? 1 : 0)
|
|
|
|
{
|
|
|
|
}
|
2016-11-03 21:55:41 -04: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 &&
|
2016-07-26 21:34:27 -04:00
|
|
|
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>>
|
2016-10-04 23:13:18 -04:00
|
|
|
constexpr span(Container& cont) : span(cont.data(), narrow<index_type>(cont.size()))
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
}
|
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 &&
|
2016-07-26 21:34:27 -04:00
|
|
|
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>>
|
2016-10-04 23:13:18 -04:00
|
|
|
constexpr span(const Container& cont) : span(cont.data(), narrow<index_type>(cont.size()))
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
}
|
2016-03-17 20:20:33 -04:00
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr span(const span& other) GSL_NOEXCEPT = default;
|
2016-07-29 14:16:06 -04:00
|
|
|
#ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr span(span&& other) GSL_NOEXCEPT = default;
|
2016-07-29 14:16:06 -04:00
|
|
|
#else
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr span(span&& other) GSL_NOEXCEPT : storage_(std::move(other.storage_)) {}
|
2016-07-29 14:16:06 -04:00
|
|
|
#endif
|
2016-03-16 22:39:55 -04:00
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
template <
|
|
|
|
class OtherElementType, std::ptrdiff_t OtherExtent,
|
2016-06-12 21:28:19 -04:00
|
|
|
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-09-15 01:01:02 -04:00
|
|
|
: storage_(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,
|
2016-06-12 21:28:19 -04:00
|
|
|
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-09-15 01:01:02 -04:00
|
|
|
: storage_(other.data(), details::extent_type<OtherExtent>(other.size()))
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
}
|
2016-03-16 22:39:55 -04:00
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
~span() GSL_NOEXCEPT = default;
|
|
|
|
constexpr span& operator=(const span& other) GSL_NOEXCEPT = default;
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2016-07-29 14:16:06 -04:00
|
|
|
#ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr span& operator=(span&& other) GSL_NOEXCEPT = default;
|
2016-07-29 14:16:06 -04:00
|
|
|
#else
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr span& operator=(span&& other) GSL_NOEXCEPT
|
2016-08-01 16:10:02 -04:00
|
|
|
{
|
|
|
|
storage_ = std::move(other.storage_);
|
|
|
|
return *this;
|
|
|
|
}
|
2016-07-29 14:16:06 -04:00
|
|
|
#endif
|
2016-07-20 16:17:47 -04:00
|
|
|
// [span.sub], span subviews
|
2016-07-26 21:34:27 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-07-26 21:34:27 -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
|
|
|
}
|
|
|
|
|
2016-07-26 21:34:27 -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
|
|
|
|
{
|
2016-07-26 21:34:27 -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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2016-07-26 21:34:27 -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
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr index_type length() const GSL_NOEXCEPT { return size(); }
|
|
|
|
constexpr index_type size() const GSL_NOEXCEPT { return storage_.size(); }
|
|
|
|
constexpr index_type length_bytes() const GSL_NOEXCEPT { return size_bytes(); }
|
2017-04-20 10:51:37 -04:00
|
|
|
constexpr index_type size_bytes() const GSL_NOEXCEPT
|
|
|
|
{
|
|
|
|
return size() * narrow_cast<index_type>(sizeof(element_type));
|
|
|
|
}
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr bool empty() const GSL_NOEXCEPT { return size() == 0; }
|
2016-02-24 19:11:33 -05:00
|
|
|
|
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
|
|
|
|
{
|
2016-02-28 03:50:53 -05:00
|
|
|
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
|
|
|
}
|
2016-10-26 17:11:24 -04:00
|
|
|
|
|
|
|
constexpr reference at(index_type idx) const { return this->operator[](idx); }
|
2016-02-25 14:42:26 -05:00
|
|
|
constexpr reference operator()(index_type idx) const { return this->operator[](idx); }
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr pointer data() const GSL_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
|
2017-02-07 18:59:37 -05:00
|
|
|
iterator begin() const GSL_NOEXCEPT { return {this, 0}; }
|
|
|
|
iterator end() const GSL_NOEXCEPT { return {this, length()}; }
|
2016-07-18 14:38:01 -04:00
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
const_iterator cbegin() const GSL_NOEXCEPT { return {this, 0}; }
|
|
|
|
const_iterator cend() const GSL_NOEXCEPT { return {this, length()}; }
|
2016-07-20 16:17:47 -04:00
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
reverse_iterator rbegin() const GSL_NOEXCEPT { return reverse_iterator{end()}; }
|
|
|
|
reverse_iterator rend() const GSL_NOEXCEPT { return reverse_iterator{begin()}; }
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
const_reverse_iterator crbegin() const GSL_NOEXCEPT { return const_reverse_iterator{cend()}; }
|
|
|
|
const_reverse_iterator crend() const GSL_NOEXCEPT { return const_reverse_iterator{cbegin()}; }
|
2016-06-12 21:28:19 -04:00
|
|
|
|
2016-07-18 14:38:01 -04:00
|
|
|
private:
|
2016-07-20 16:17:47 -04:00
|
|
|
// this implementation detail class lets us take advantage of the
|
2016-02-28 03:50:53 -05:00
|
|
|
// 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-02-28 03:50:53 -05:00
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
constexpr pointer data() const GSL_NOEXCEPT { return data_; }
|
2016-02-28 03:50:53 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
pointer data_;
|
|
|
|
};
|
|
|
|
|
2016-07-26 21:34:27 -04:00
|
|
|
storage_type<details::extent_type<Extent>> storage_;
|
2016-02-24 19:11:33 -05:00
|
|
|
};
|
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
// [span.comparison], span comparison operators
|
2016-07-26 21:34:27 -04:00
|
|
|
template <class ElementType, std::ptrdiff_t FirstExtent, std::ptrdiff_t SecondExtent>
|
2017-02-07 18:59:37 -05:00
|
|
|
inline constexpr bool operator==(const span<ElementType, FirstExtent>& l,
|
2017-04-20 10:51:37 -04:00
|
|
|
const span<ElementType, SecondExtent>& r)
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2016-07-29 14:16:06 -04:00
|
|
|
#ifdef GSL_MSVC_NO_CPP14_STD_EQUAL
|
|
|
|
return (l.size() == r.size()) && std::equal(l.begin(), l.end(), r.begin());
|
|
|
|
#else
|
2016-07-20 16:17:47 -04:00
|
|
|
return std::equal(l.begin(), l.end(), r.begin(), r.end());
|
2016-07-29 14:16:06 -04:00
|
|
|
#endif
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2016-07-26 21:34:27 -04:00
|
|
|
template <class ElementType, std::ptrdiff_t Extent>
|
2017-04-20 10:51:37 -04:00
|
|
|
inline constexpr bool operator!=(const span<ElementType, Extent>& l,
|
|
|
|
const span<ElementType, Extent>& r)
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
return !(l == r);
|
|
|
|
}
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2016-07-26 21:34:27 -04:00
|
|
|
template <class ElementType, std::ptrdiff_t Extent>
|
2017-04-20 10:51:37 -04:00
|
|
|
inline constexpr bool operator<(const span<ElementType, Extent>& l,
|
|
|
|
const span<ElementType, Extent>& r)
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
|
|
|
|
}
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2016-07-26 21:34:27 -04:00
|
|
|
template <class ElementType, std::ptrdiff_t Extent>
|
2017-04-20 10:51:37 -04:00
|
|
|
inline constexpr bool operator<=(const span<ElementType, Extent>& l,
|
|
|
|
const span<ElementType, Extent>& r)
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
return !(l > r);
|
|
|
|
}
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2016-07-26 21:34:27 -04:00
|
|
|
template <class ElementType, std::ptrdiff_t Extent>
|
2017-04-20 10:51:37 -04:00
|
|
|
inline constexpr bool operator>(const span<ElementType, Extent>& l,
|
|
|
|
const span<ElementType, Extent>& r)
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
return r < l;
|
|
|
|
}
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2016-07-26 21:34:27 -04:00
|
|
|
template <class ElementType, std::ptrdiff_t Extent>
|
2017-04-20 10:51:37 -04:00
|
|
|
inline constexpr bool operator>=(const span<ElementType, Extent>& l,
|
|
|
|
const span<ElementType, Extent>& r)
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
return !(l < r);
|
|
|
|
}
|
2016-02-24 19:11:33 -05:00
|
|
|
|
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
|
|
|
|
|
2017-02-13 15:11:45 -05:00
|
|
|
// we should use a narrow_cast<> to go to std::size_t, but older compilers may not see it as
|
2016-07-20 16:17:47 -04:00
|
|
|
// constexpr
|
2016-05-29 20:06:29 -04:00
|
|
|
// and so will fail compilation of the template
|
2016-07-26 21:34:27 -04:00
|
|
|
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,
|
2016-07-26 21:34:27 -04:00
|
|
|
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
|
2016-07-26 21:34:27 -04:00
|
|
|
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>
|
2017-02-07 18:59:37 -05:00
|
|
|
as_bytes(span<ElementType, Extent> s) GSL_NOEXCEPT
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
|
|
|
|
}
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2016-07-26 21:34:27 -04:00
|
|
|
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>
|
2017-02-07 18:59:37 -05:00
|
|
|
as_writeable_bytes(span<ElementType, Extent> s) GSL_NOEXCEPT
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2016-11-16 13:17:04 -05:00
|
|
|
//
|
|
|
|
// make_span() - Utility functions for creating spans
|
|
|
|
//
|
|
|
|
template <class ElementType>
|
2017-04-20 10:51:37 -04:00
|
|
|
span<ElementType> make_span(ElementType* ptr, typename span<ElementType>::index_type count)
|
|
|
|
{
|
|
|
|
return span<ElementType>(ptr, count);
|
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
|
|
|
template <class ElementType>
|
2017-04-20 10:51:37 -04:00
|
|
|
span<ElementType> make_span(ElementType* firstElem, ElementType* lastElem)
|
|
|
|
{
|
|
|
|
return span<ElementType>(firstElem, lastElem);
|
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
2017-02-13 15:11:45 -05:00
|
|
|
template <class ElementType, std::size_t N>
|
2017-04-20 10:51:37 -04:00
|
|
|
span<ElementType> make_span(ElementType (&arr)[N])
|
|
|
|
{
|
|
|
|
return span<ElementType>(arr);
|
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
|
|
|
template <class Container>
|
2017-04-20 10:51:37 -04:00
|
|
|
span<typename Container::value_type> make_span(Container& cont)
|
|
|
|
{
|
|
|
|
return span<typename Container::value_type>(cont);
|
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
|
|
|
template <class Container>
|
2017-04-20 10:51:37 -04:00
|
|
|
span<const typename Container::value_type> make_span(const Container& cont)
|
|
|
|
{
|
|
|
|
return span<const typename Container::value_type>(cont);
|
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
|
|
|
template <class Ptr>
|
2017-04-20 10:51:37 -04:00
|
|
|
span<typename Ptr::element_type> make_span(Ptr& cont, std::ptrdiff_t count)
|
|
|
|
{
|
|
|
|
return span<typename Ptr::element_type>(cont, count);
|
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
|
|
|
template <class Ptr>
|
2017-04-20 10:51:37 -04:00
|
|
|
span<typename Ptr::element_type> make_span(Ptr& cont)
|
|
|
|
{
|
|
|
|
return span<typename Ptr::element_type>(cont);
|
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
2016-08-23 04:30:06 -04:00
|
|
|
// Specialization of gsl::at for span
|
|
|
|
template <class ElementType, std::ptrdiff_t Extent>
|
2017-02-07 18:59:37 -05:00
|
|
|
inline constexpr ElementType& at(const span<ElementType, Extent>& s, std::ptrdiff_t index)
|
2016-08-23 04:30:06 -04:00
|
|
|
{
|
|
|
|
// No bounds checking here because it is done in span::operator[] called below
|
|
|
|
return s[index];
|
|
|
|
}
|
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
} // namespace gsl
|
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
#undef GSL_NOEXCEPT
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-02-07 18:59:37 -05:00
|
|
|
#ifdef _MSC_VER
|
2017-04-20 10:51:37 -04:00
|
|
|
#if _MSC_VER < 1910
|
|
|
|
#undef constexpr
|
|
|
|
#pragma pop_macro("constexpr")
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
#if _MSC_VER <= 1800
|
|
|
|
#undef noexcept
|
|
|
|
#pragma pop_macro("noexcept")
|
2016-07-29 14:16:06 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
#undef alignof
|
|
|
|
#pragma pop_macro("alignof")
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
#undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
|
|
|
|
#undef GSL_MSVC_NO_DEFAULT_MOVE_CTOR
|
|
|
|
#undef GSL_MSVC_NO_CPP14_STD_EQUAL
|
|
|
|
#endif // _MSC_VER <= 1800
|
|
|
|
#endif // _MSC_VER < 1910
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
#pragma warning(pop)
|
2016-02-24 14:26:28 -05:00
|
|
|
#endif // _MSC_VER
|
|
|
|
|
|
|
|
#endif // GSL_SPAN_H
|