mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
Fixed compilation issues for GCC on Linux.
This commit is contained in:
parent
b03b04bfcd
commit
c366f4415d
@ -118,7 +118,6 @@ inline T narrow(U u)
|
||||
{
|
||||
T t = narrow_cast<T>(u);
|
||||
if (static_cast<U>(t) != u) throw narrowing_error();
|
||||
#pragma warning(suppress : 4127) // suppress warning from MSVC compiler about constant in if-test
|
||||
if (!details::is_same_signedness<T, U>::value && ((t < T{}) != (u < U{})))
|
||||
throw narrowing_error();
|
||||
return t;
|
||||
|
207
include/span.h
207
include/span.h
@ -79,12 +79,12 @@
|
||||
namespace gsl
|
||||
{
|
||||
|
||||
template <class ElementType, std::ptrdiff_t Extent = dynamic_extent>
|
||||
class span;
|
||||
|
||||
// [views.constants], constants
|
||||
constexpr const std::ptrdiff_t dynamic_extent = -1;
|
||||
|
||||
template <class ElementType, std::ptrdiff_t Extent = dynamic_extent>
|
||||
class span;
|
||||
|
||||
// implementation details
|
||||
namespace details
|
||||
{
|
||||
@ -99,20 +99,35 @@ namespace details
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct is_span : is_span_oracle<std::remove_cv_t<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>>
|
||||
{
|
||||
};
|
||||
|
||||
template <class From, class To>
|
||||
struct is_allowed_pointer_conversion
|
||||
: std::bool_constant<std::is_pointer<From>::value && std::is_pointer<To>::value &&
|
||||
: public std::integral_constant<bool, std::is_pointer<From>::value && std::is_pointer<To>::value &&
|
||||
std::is_convertible<From, To>::value>
|
||||
{
|
||||
};
|
||||
|
||||
template <class From, class To>
|
||||
struct is_allowed_integral_conversion
|
||||
: std::bool_constant<std::is_integral<From>::value && std::is_integral<To>::value &&
|
||||
: 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>
|
||||
{
|
||||
@ -120,13 +135,13 @@ namespace details
|
||||
|
||||
template <std::ptrdiff_t From, std::ptrdiff_t To>
|
||||
struct is_allowed_extent_conversion
|
||||
: std::bool_constant<From == To || From == gsl::dynamic_extent || To == gsl::dynamic_extent>
|
||||
: public std::integral_constant<bool, From == To || From == gsl::dynamic_extent || To == gsl::dynamic_extent>
|
||||
{
|
||||
};
|
||||
|
||||
template <class From, class To>
|
||||
struct is_allowed_element_type_conversion
|
||||
: std::bool_constant<std::is_same<From, std::remove_cv_t<To>>::value ||
|
||||
: public std::integral_constant<bool, std::is_same<From, std::remove_cv_t<To>>::value ||
|
||||
is_allowed_pointer_conversion<From, To>::value ||
|
||||
is_allowed_integral_conversion<From, To>::value>
|
||||
{
|
||||
@ -134,12 +149,12 @@ namespace details
|
||||
|
||||
template <class From>
|
||||
struct is_allowed_element_type_conversion<From, byte>
|
||||
: std::bool_constant<!std::is_const<From>::value>
|
||||
: public std::integral_constant<bool, !std::is_const<From>::value>
|
||||
{
|
||||
};
|
||||
|
||||
template <class From>
|
||||
struct is_allowed_element_type_conversion<From, const byte> : std::true_type
|
||||
struct is_allowed_element_type_conversion<From, const byte> : public std::true_type
|
||||
{
|
||||
};
|
||||
|
||||
@ -269,12 +284,12 @@ namespace details
|
||||
void swap(const_span_iterator& rhs) noexcept
|
||||
{
|
||||
std::swap(index_, rhs.index_);
|
||||
std::swap(m_span, rhs.m_span);
|
||||
std::swap(span_, rhs.span_);
|
||||
}
|
||||
|
||||
private:
|
||||
const Span* span_;
|
||||
ptrdiff_t index_;
|
||||
std::ptrdiff_t index_;
|
||||
};
|
||||
|
||||
template <class Span>
|
||||
@ -323,12 +338,12 @@ namespace details
|
||||
|
||||
constexpr span_iterator operator+(difference_type n) const noexcept
|
||||
{
|
||||
return base_type::operator+(n);
|
||||
return {base_type::operator+(n)};
|
||||
}
|
||||
|
||||
constexpr span_iterator& operator+=(difference_type n) noexcept
|
||||
{
|
||||
return base_type::operator+=(n);
|
||||
return {base_type::operator+=(n)};
|
||||
}
|
||||
|
||||
constexpr span_iterator operator-(difference_type n) const noexcept
|
||||
@ -376,6 +391,10 @@ namespace details
|
||||
}
|
||||
|
||||
void swap(span_iterator& rhs) noexcept { base_type::swap(rhs); }
|
||||
private:
|
||||
constexpr span_iterator(const base_type& base) : base_type(base)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Span>
|
||||
@ -408,6 +427,47 @@ namespace details
|
||||
return rhs - n;
|
||||
}
|
||||
|
||||
template <std::ptrdiff_t Ext>
|
||||
class extent_type
|
||||
{
|
||||
public:
|
||||
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:
|
||||
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_;
|
||||
};
|
||||
} // namespace details
|
||||
|
||||
// [span], class template span
|
||||
@ -429,9 +489,9 @@ public:
|
||||
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() noexcept : storage_(nullptr, details::extent_type<0>()) {}
|
||||
|
||||
constexpr span(nullptr_t) noexcept : span() {}
|
||||
constexpr span(std::nullptr_t) noexcept : span() {}
|
||||
|
||||
constexpr span(pointer ptr, index_type count) : storage_(ptr, count) {}
|
||||
|
||||
@ -441,24 +501,19 @@ public:
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
constexpr span(element_type (&arr)[N]) noexcept : storage_(&arr[0], extent_type<N>())
|
||||
constexpr span(element_type (&arr)[N]) noexcept : storage_(&arr[0], details::extent_type<N>())
|
||||
{
|
||||
}
|
||||
|
||||
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>())
|
||||
{
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
constexpr span(std::array<element_type, N>& arr) noexcept : storage_(&arr[0], extent_type<N>())
|
||||
{
|
||||
}
|
||||
|
||||
template <size_t N, class = std::enable_if_t<is_const<element_type>::value>>
|
||||
constexpr span(std::array<std::remove_const_t<element_type>, N>& arr) noexcept
|
||||
: storage_(&arr[0], extent_type<N>())
|
||||
{
|
||||
}
|
||||
|
||||
template <size_t N, class = std::enable_if_t<is_const<element_type>::value>>
|
||||
constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) noexcept
|
||||
: storage_(&arr[0], extent_type<N>())
|
||||
: storage_(&arr[0], details::extent_type<N>())
|
||||
{
|
||||
}
|
||||
|
||||
@ -467,8 +522,9 @@ public:
|
||||
template <class Container,
|
||||
class = std::enable_if_t<
|
||||
!details::is_span<Container>::value &&
|
||||
std::is_convertible<Container::pointer, pointer>::value &&
|
||||
std::is_convertible<Container::pointer,
|
||||
!details::is_std_array<Container>::value &&
|
||||
std::is_convertible<typename Container::pointer, pointer>::value &&
|
||||
std::is_convertible<typename Container::pointer,
|
||||
decltype(std::declval<Container>().data())>::value>>
|
||||
constexpr span(Container& cont) : span(cont.data(), cont.size())
|
||||
{
|
||||
@ -477,8 +533,8 @@ public:
|
||||
template <class Container,
|
||||
class = std::enable_if_t<
|
||||
std::is_const<element_type>::value && !details::is_span<Container>::value &&
|
||||
std::is_convertible<Container::pointer, pointer>::value &&
|
||||
std::is_convertible<Container::pointer,
|
||||
std::is_convertible<typename Container::pointer, pointer>::value &&
|
||||
std::is_convertible<typename Container::pointer,
|
||||
decltype(std::declval<Container>().data())>::value>>
|
||||
constexpr span(const Container& cont) : span(cont.data(), cont.size())
|
||||
{
|
||||
@ -493,7 +549,7 @@ public:
|
||||
details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
|
||||
details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>>
|
||||
constexpr span(const span<OtherElementType, OtherExtent>& other)
|
||||
: storage_(reinterpret_cast<pointer>(other.data()), extent_type<OtherExtent>(other.size()))
|
||||
: storage_(reinterpret_cast<pointer>(other.data()), details::extent_type<OtherExtent>(other.size()))
|
||||
{
|
||||
}
|
||||
|
||||
@ -503,7 +559,7 @@ public:
|
||||
details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
|
||||
details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>>
|
||||
constexpr span(span<OtherElementType, OtherExtent>&& other)
|
||||
: storage_(reinterpret_cast<pointer>(other.data()), extent_type<OtherExtent>(other.size()))
|
||||
: storage_(reinterpret_cast<pointer>(other.data()), details::extent_type<OtherExtent>(other.size()))
|
||||
{
|
||||
}
|
||||
|
||||
@ -512,25 +568,25 @@ public:
|
||||
constexpr span& operator=(span&& other) noexcept = default;
|
||||
|
||||
// [span.sub], span subviews
|
||||
template <ptrdiff_t Count>
|
||||
template <std::ptrdiff_t Count>
|
||||
constexpr span<element_type, Count> first() const
|
||||
{
|
||||
Expects(Count >= 0 && Count <= size());
|
||||
return {data(), Count};
|
||||
}
|
||||
|
||||
template <ptrdiff_t Count>
|
||||
template <std::ptrdiff_t Count>
|
||||
constexpr span<element_type, Count> last() const
|
||||
{
|
||||
Expects(Count >= 0 && Count <= size());
|
||||
return {data() + (size() - Count), Count};
|
||||
}
|
||||
|
||||
template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent>
|
||||
template <std::ptrdiff_t Offset, std::ptrdiff_t Count = dynamic_extent>
|
||||
constexpr span<element_type, Count> subspan() const
|
||||
{
|
||||
Expects((Offset == 0 || Offset > 0 && Offset <= size()) &&
|
||||
(Count == dynamic_extent || Count >= 0 && Offset + Count <= size()));
|
||||
Expects((Offset == 0 || (Offset > 0 && Offset <= size())) &&
|
||||
(Count == dynamic_extent || (Count >= 0 && Offset + Count <= size())));
|
||||
return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
|
||||
}
|
||||
|
||||
@ -549,8 +605,8 @@ public:
|
||||
constexpr span<element_type, dynamic_extent> 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()));
|
||||
Expects((offset == 0 || (offset > 0 && offset <= size())) &&
|
||||
(count == dynamic_extent || (count >= 0 && offset + count <= size())));
|
||||
return {data() + offset, count == dynamic_extent ? size() - offset : count};
|
||||
}
|
||||
|
||||
@ -584,47 +640,6 @@ public:
|
||||
const_reverse_iterator crend() const noexcept { return reverse_iterator{{this, 0}}; }
|
||||
|
||||
private:
|
||||
template <index_type Extent>
|
||||
class extent_type;
|
||||
|
||||
template <index_type Extent>
|
||||
class extent_type
|
||||
{
|
||||
public:
|
||||
static_assert(Extent >= 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 == 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<dynamic_extent>
|
||||
{
|
||||
public:
|
||||
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_;
|
||||
};
|
||||
|
||||
// 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
|
||||
@ -644,41 +659,41 @@ private:
|
||||
pointer data_;
|
||||
};
|
||||
|
||||
storage_type<extent_type<Extent>> storage_;
|
||||
storage_type<details::extent_type<Extent>> storage_;
|
||||
};
|
||||
|
||||
// [span.comparison], span comparison operators
|
||||
template <class ElementType, ptrdiff_t Extent>
|
||||
constexpr bool operator==(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
|
||||
template <class ElementType, std::ptrdiff_t FirstExtent, std::ptrdiff_t SecondExtent>
|
||||
constexpr bool operator==(const span<ElementType, FirstExtent>& l, const span<ElementType, SecondExtent>& r)
|
||||
{
|
||||
return std::equal(l.begin(), l.end(), r.begin(), r.end());
|
||||
}
|
||||
|
||||
template <class ElementType, ptrdiff_t Extent>
|
||||
template <class ElementType, std::ptrdiff_t Extent>
|
||||
constexpr bool operator!=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
|
||||
{
|
||||
return !(l == r);
|
||||
}
|
||||
|
||||
template <class ElementType, ptrdiff_t Extent>
|
||||
template <class ElementType, std::ptrdiff_t Extent>
|
||||
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, ptrdiff_t Extent>
|
||||
template <class ElementType, std::ptrdiff_t Extent>
|
||||
constexpr bool operator<=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
|
||||
{
|
||||
return !(l > r);
|
||||
}
|
||||
|
||||
template <class ElementType, ptrdiff_t Extent>
|
||||
template <class ElementType, std::ptrdiff_t Extent>
|
||||
constexpr bool operator>(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
|
||||
{
|
||||
return r < l;
|
||||
}
|
||||
|
||||
template <class ElementType, ptrdiff_t Extent>
|
||||
template <class ElementType, std::ptrdiff_t Extent>
|
||||
constexpr bool operator>=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
|
||||
{
|
||||
return !(l < r);
|
||||
@ -692,11 +707,11 @@ namespace details
|
||||
// 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, ptrdiff_t Extent>
|
||||
template <class ElementType, std::ptrdiff_t Extent>
|
||||
struct calculate_byte_size
|
||||
: std::integral_constant<std::ptrdiff_t,
|
||||
static_cast<ptrdiff_t>(sizeof(ElementType) *
|
||||
static_cast<size_t>(Extent))>
|
||||
static_cast<std::ptrdiff_t>(sizeof(ElementType) *
|
||||
static_cast<std::size_t>(Extent))>
|
||||
{
|
||||
};
|
||||
|
||||
@ -708,14 +723,14 @@ namespace details
|
||||
}
|
||||
|
||||
// [span.objectrep], views of object representation
|
||||
template <class ElementType, ptrdiff_t Extent>
|
||||
template <class ElementType, std::ptrdiff_t Extent>
|
||||
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, ptrdiff_t Extent,
|
||||
template <class ElementType, std::ptrdiff_t Extent,
|
||||
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
|
||||
|
@ -234,40 +234,39 @@ template <typename CharT, std::ptrdiff_t Extent = dynamic_extent>
|
||||
class basic_string_span
|
||||
{
|
||||
public:
|
||||
using value_type = CharT;
|
||||
using const_value_type = std::add_const_t<value_type>;
|
||||
using pointer = std::add_pointer_t<value_type>;
|
||||
using reference = std::add_lvalue_reference_t<value_type>;
|
||||
using const_reference = std::add_lvalue_reference_t<const_value_type>;
|
||||
using impl_type = span<value_type, Extent>;
|
||||
using element_type = CharT;
|
||||
using pointer = std::add_pointer_t<element_type>;
|
||||
using reference = std::add_lvalue_reference_t<element_type>;
|
||||
using const_reference = std::add_lvalue_reference_t<std::add_const_t<element_type>>;
|
||||
using impl_type = span<element_type, Extent>;
|
||||
|
||||
using size_type = ptrdiff_t;
|
||||
using index_type = typename impl_type::index_type;
|
||||
using iterator = typename impl_type::iterator;
|
||||
using const_iterator = typename impl_type::const_iterator;
|
||||
using reverse_iterator = typename impl_type::reverse_iterator;
|
||||
using const_reverse_iterator = typename impl_type::const_reverse_iterator;
|
||||
|
||||
// default (empty)
|
||||
constexpr basic_string_span() = default;
|
||||
constexpr basic_string_span() noexcept = default;
|
||||
|
||||
// copy
|
||||
constexpr basic_string_span(const basic_string_span& other) = default;
|
||||
constexpr basic_string_span(const basic_string_span& other) noexcept = default;
|
||||
|
||||
// move
|
||||
#ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR
|
||||
constexpr basic_string_span(basic_string_span&& other) = default;
|
||||
constexpr basic_string_span(basic_string_span&& other) noexcept = default;
|
||||
#else
|
||||
constexpr basic_string_span(basic_string_span&& other) : span_(std::move(other.span_)) {}
|
||||
#endif
|
||||
|
||||
// assign
|
||||
constexpr basic_string_span& operator=(const basic_string_span& other) = default;
|
||||
constexpr basic_string_span& operator=(const basic_string_span& other) noexcept = default;
|
||||
|
||||
// move assign
|
||||
#ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR
|
||||
constexpr basic_string_span& operator=(basic_string_span&& other) = default;
|
||||
constexpr basic_string_span& operator=(basic_string_span&& other) noexcept = default;
|
||||
#else
|
||||
constexpr basic_string_span& operator=(basic_string_span&& other)
|
||||
constexpr basic_string_span& operator=(basic_string_span&& other) noexcept
|
||||
{
|
||||
span_ = std::move(other.span_);
|
||||
return *this;
|
||||
@ -277,47 +276,55 @@ public:
|
||||
// from nullptr
|
||||
constexpr basic_string_span(std::nullptr_t ptr) noexcept : span_(ptr) {}
|
||||
|
||||
// from nullptr and length
|
||||
constexpr basic_string_span(std::nullptr_t ptr, size_type length) noexcept : span_(ptr, length)
|
||||
{
|
||||
}
|
||||
constexpr basic_string_span(pointer ptr, index_type length) : span_(ptr, length) {}
|
||||
constexpr basic_string_span(pointer firstElem, pointer lastElem) : span_(firstElem, lastElem) {}
|
||||
|
||||
// From static arrays - if 0-terminated, remove 0 from the view
|
||||
|
||||
// from static arrays and string literals
|
||||
// All other containers allow 0s within the length, so we do not remove them
|
||||
template <size_t N>
|
||||
constexpr basic_string_span(value_type (&arr)[N]) noexcept : span_(remove_z(arr))
|
||||
constexpr basic_string_span(element_type (&arr)[N]) : span_(remove_z(arr))
|
||||
{
|
||||
}
|
||||
|
||||
template <size_t N, class ArrayElementType = std::remove_const_t<element_type>>
|
||||
constexpr basic_string_span(std::array<ArrayElementType, N>& arr) noexcept
|
||||
: span_(arr) {}
|
||||
|
||||
template <size_t N, class ArrayElementType = std::remove_const_t<element_type>>
|
||||
constexpr basic_string_span(const std::array<ArrayElementType, N>& arr) noexcept
|
||||
: span_(arr) {}
|
||||
|
||||
// Container signature should work for basic_string after C++17 version exists
|
||||
template <class Traits, class Allocator>
|
||||
constexpr basic_string_span(std::basic_string<element_type, Traits, Allocator>& str)
|
||||
: span_(&str[0], str.length())
|
||||
{
|
||||
}
|
||||
|
||||
// Those allow 0s within the length, so we do not remove them
|
||||
|
||||
// from raw data and length
|
||||
constexpr basic_string_span(pointer ptr, size_type length) noexcept : span_(ptr, length) {}
|
||||
|
||||
// from string
|
||||
constexpr basic_string_span(std::string& s) noexcept
|
||||
: span_(const_cast<pointer>(s.data()), narrow_cast<std::ptrdiff_t>(s.length()))
|
||||
template <class Traits, class Allocator>
|
||||
constexpr basic_string_span(const std::basic_string<element_type, Traits, Allocator>& str)
|
||||
: span_(&str[0], str.length())
|
||||
{
|
||||
}
|
||||
|
||||
// from containers. Containers must have .size() and .data() function signatures
|
||||
template <typename Cont,
|
||||
typename = std::enable_if_t<
|
||||
!details::is_basic_string_span<Cont>::value && !details::is_span<Cont>::value &&
|
||||
std::is_convertible<Cont::pointer, pointer>::value &&
|
||||
std::is_convertible<Cont::pointer, decltype(std::declval<Cont>().data())>::value>>
|
||||
constexpr basic_string_span(Cont& cont) : span_(cont.data(), cont.size())
|
||||
// from containers. Containers must have a pointer type and data() function signatures
|
||||
template <class Container,
|
||||
class = std::enable_if_t<
|
||||
!details::is_basic_string_span<Container>::value &&
|
||||
!details::is_span<Container>::value &&
|
||||
std::is_convertible<typename Container::pointer, pointer>::value &&
|
||||
std::is_convertible<typename Container::pointer, decltype(std::declval<Container>().data())>::value>>
|
||||
constexpr basic_string_span(Container& cont) : span_(cont)
|
||||
{
|
||||
}
|
||||
|
||||
// disallow creation from temporary containers and strings
|
||||
template <typename Cont,
|
||||
typename = std::enable_if_t<
|
||||
!details::is_basic_string_span<Cont>::value && !details::is_span<Cont>::value &&
|
||||
std::is_convertible<Cont::pointer, pointer>::value &&
|
||||
std::is_convertible<Cont::pointer, decltype(std::declval<Cont>().data())>::value>>
|
||||
constexpr basic_string_span(const Cont& cont) : span_(cont.data(), cont.size())
|
||||
template <class Container,
|
||||
class = std::enable_if_t<
|
||||
!details::is_basic_string_span<Container>::value &&
|
||||
!details::is_span<Container>::value &&
|
||||
std::is_convertible<typename Container::pointer, pointer>::value &&
|
||||
std::is_convertible<typename Container::pointer, decltype(std::declval<Container>().data())>::value>>
|
||||
constexpr basic_string_span(const Container& cont) : span_(cont)
|
||||
{
|
||||
}
|
||||
|
||||
@ -326,109 +333,97 @@ public:
|
||||
template <typename OtherValueType, std::ptrdiff_t OtherExtent,
|
||||
typename Dummy = std::enable_if_t<
|
||||
std::is_convertible<span<OtherValueType, OtherExtent>, impl_type>::value>>
|
||||
constexpr basic_string_span(span<OtherValueType, OtherExtent> other) noexcept : span_(other)
|
||||
constexpr basic_string_span(const span<OtherValueType, OtherExtent>& other) : span_(other)
|
||||
{
|
||||
}
|
||||
#else
|
||||
// from span
|
||||
constexpr basic_string_span(span<value_type, Extent> other) noexcept : span_(other) {}
|
||||
constexpr basic_string_span(span<element_type, Extent> other) : span_(other) {}
|
||||
|
||||
template <typename = std::enable_if_t<
|
||||
!std::is_same<std::remove_const_t<value_type>, value_type>::value>>
|
||||
constexpr basic_string_span(span<std::remove_const_t<value_type>, Extent> other) noexcept
|
||||
!std::is_same<std::remove_const_t<element_type>, value_type>::value>>
|
||||
constexpr basic_string_span(const span<std::remove_const_t<element_type>, Extent>& other)
|
||||
: span_(other)
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
// from string_span
|
||||
template <typename OtherValueType, std::ptrdiff_t OtherExtent,
|
||||
typename = std::enable_if_t<std::is_convertible<
|
||||
basic_string_span<OtherValueType, OtherExtent>::impl_type, impl_type>::value>>
|
||||
constexpr basic_string_span(basic_string_span<OtherValueType, OtherExtent> other) noexcept
|
||||
template <class OtherValueType, std::ptrdiff_t OtherExtent,
|
||||
class = std::enable_if_t<std::is_convertible<
|
||||
typename basic_string_span<OtherValueType, OtherExtent>::impl_type, impl_type>::value>>
|
||||
constexpr basic_string_span(basic_string_span<OtherValueType, OtherExtent> other)
|
||||
: span_(other.data(), other.length())
|
||||
{
|
||||
}
|
||||
|
||||
constexpr bool empty() const noexcept { return length() == 0; }
|
||||
|
||||
// first Count elements
|
||||
template <size_type Count>
|
||||
constexpr basic_string_span<value_type, Count> first() const noexcept
|
||||
template <index_type Count>
|
||||
constexpr basic_string_span<element_type, Count> first() const
|
||||
{
|
||||
return {span_.template first<Count>()};
|
||||
}
|
||||
|
||||
constexpr basic_string_span<value_type, dynamic_extent> first(size_type count) const noexcept
|
||||
constexpr basic_string_span<index_type, dynamic_extent> first(index_type count) const
|
||||
{
|
||||
return {span_.first(count)};
|
||||
}
|
||||
|
||||
// last Count elements
|
||||
template <size_type Count>
|
||||
constexpr basic_string_span<value_type, Count> last() const noexcept
|
||||
template <index_type Count>
|
||||
constexpr basic_string_span<index_type, Count> last() const
|
||||
{
|
||||
return {span_.template last<Count>()};
|
||||
}
|
||||
|
||||
constexpr basic_string_span<value_type, dynamic_extent> last(size_type count) const noexcept
|
||||
constexpr basic_string_span<element_type, dynamic_extent> last(index_type count) const
|
||||
{
|
||||
return {span_.last(count)};
|
||||
}
|
||||
|
||||
// create a subview of Count elements starting from Offset
|
||||
template <size_type Offset, size_type Count>
|
||||
constexpr basic_string_span<value_type, Count> subspan() const noexcept
|
||||
template <index_type Offset, index_type Count>
|
||||
constexpr basic_string_span<element_type, Count> subspan() const
|
||||
{
|
||||
return {span_.template subspan<Offset, Count>()};
|
||||
}
|
||||
|
||||
constexpr basic_string_span<value_type, dynamic_extent>
|
||||
subspan(size_type offset, size_type count = dynamic_extent) const noexcept
|
||||
constexpr basic_string_span<element_type, dynamic_extent>
|
||||
subspan(index_type offset, index_type count = dynamic_extent) const
|
||||
{
|
||||
return {span_.subspan(offset, count)};
|
||||
}
|
||||
|
||||
constexpr reference operator[](size_type idx) const noexcept { return span_[idx]; }
|
||||
constexpr reference operator[](index_type idx) const { return span_[idx]; }
|
||||
constexpr reference operator()(index_type idx) const { return span_[idx]; }
|
||||
|
||||
constexpr pointer data() const noexcept { return span_.data(); }
|
||||
constexpr pointer data() const { return span_.data(); }
|
||||
|
||||
// length of the span in elements
|
||||
constexpr size_type length() const noexcept { return span_.size(); }
|
||||
|
||||
// length of the span in elements
|
||||
constexpr size_type size() const noexcept { return span_.size(); }
|
||||
|
||||
// length of the span in bytes
|
||||
constexpr size_type size_bytes() const noexcept { return span_.size_bytes(); }
|
||||
|
||||
// length of the span in bytes
|
||||
constexpr size_type length_bytes() const noexcept { return span_.length_bytes(); }
|
||||
constexpr index_type length() const noexcept { return span_.size(); }
|
||||
constexpr index_type size() const noexcept { return span_.size(); }
|
||||
constexpr index_type size_bytes() const noexcept { return span_.size_bytes(); }
|
||||
constexpr index_type length_bytes() const noexcept { return span_.length_bytes(); }
|
||||
constexpr bool empty() const noexcept { return size() == 0; }
|
||||
|
||||
constexpr iterator begin() const noexcept { return span_.begin(); }
|
||||
|
||||
constexpr iterator end() const noexcept { return span_.end(); }
|
||||
|
||||
|
||||
constexpr const_iterator cbegin() const noexcept { return span_.cbegin(); }
|
||||
|
||||
constexpr const_iterator cend() const noexcept { return span_.cend(); }
|
||||
|
||||
|
||||
constexpr reverse_iterator rbegin() const noexcept { return span_.rbegin(); }
|
||||
|
||||
constexpr reverse_iterator rend() const noexcept { return span_.rend(); }
|
||||
|
||||
constexpr const_reverse_iterator crbegin() const noexcept { return span_.crbegin(); }
|
||||
|
||||
constexpr const_reverse_iterator crend() const noexcept { return span_.crend(); }
|
||||
|
||||
private:
|
||||
static impl_type remove_z(pointer const& sz, std::ptrdiff_t max) noexcept
|
||||
static impl_type remove_z(pointer const& sz, std::ptrdiff_t max)
|
||||
{
|
||||
return {sz, details::length_func<value_type>()(sz, max)};
|
||||
return {sz, details::length_func<element_type>()(sz, max)};
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
static impl_type remove_z(value_type (&sz)[N]) noexcept
|
||||
static impl_type remove_z(element_type (&sz)[N])
|
||||
{
|
||||
return remove_z(&sz[0], narrow_cast<std::ptrdiff_t>(N));
|
||||
}
|
||||
@ -453,7 +448,7 @@ using cwstring_span = basic_string_span<const wchar_t, Extent>;
|
||||
//
|
||||
#ifndef GSL_MSVC_HAS_TYPE_DEDUCTION_BUG
|
||||
|
||||
template <typename CharT, ptrdiff_t Extent>
|
||||
template <typename CharT, std::ptrdiff_t Extent>
|
||||
std::basic_string<typename std::remove_const<CharT>::type>
|
||||
to_string(basic_string_span<CharT, Extent> view)
|
||||
{
|
||||
@ -559,15 +554,13 @@ using czstring_span = basic_zstring_span<const char, Max>;
|
||||
template <std::ptrdiff_t Max = dynamic_extent>
|
||||
using cwzstring_span = basic_zstring_span<const wchar_t, Max>;
|
||||
|
||||
} // namespace GSL
|
||||
|
||||
// operator ==
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename = std::enable_if_t<std::is_convertible<
|
||||
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
|
||||
bool operator==(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
|
||||
template <class CharT, std::ptrdiff_t Extent, class T,
|
||||
class = std::enable_if_t<details::is_basic_string_span<T>::value || std::is_convertible<
|
||||
T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>>
|
||||
bool operator==(const gsl::basic_string_span<CharT, Extent>& one, const T& other) noexcept
|
||||
{
|
||||
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
|
||||
gsl::basic_string_span<std::add_const_t<CharT>> tmp(other);
|
||||
#ifdef GSL_MSVC_NO_CPP14_STD_EQUAL
|
||||
return (one.size() == tmp.size()) && std::equal(one.begin(), one.end(), tmp.begin());
|
||||
#else
|
||||
@ -576,13 +569,13 @@ bool operator==(gsl::basic_string_span<CharT, Extent> one, const T& other) noexc
|
||||
}
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename Dummy = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
|
||||
!gsl::details::is_basic_string_span<T>::value>>
|
||||
bool operator==(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
|
||||
class CharT, std::ptrdiff_t Extent, class T,
|
||||
class = std::enable_if_t<!details::is_basic_string_span<T>::value &&
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value
|
||||
>>
|
||||
bool operator==(const T& one, const gsl::basic_string_span<CharT, Extent>& other) noexcept
|
||||
{
|
||||
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one);
|
||||
gsl::basic_string_span<std::add_const_t<CharT>> tmp(one);
|
||||
#ifdef GSL_MSVC_NO_CPP14_STD_EQUAL
|
||||
return (tmp.size() == other.size()) && std::equal(tmp.begin(), tmp.end(), other.begin());
|
||||
#else
|
||||
@ -590,40 +583,6 @@ bool operator==(const T& one, gsl::basic_string_span<CharT, Extent> other) noexc
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef _MSC_VER
|
||||
|
||||
// VS treats temp and const containers as convertible to basic_string_span,
|
||||
// so the cases below are already covered by the previous operators
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename DataType = typename T::value_type,
|
||||
typename Dummy = std::enable_if_t<
|
||||
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
|
||||
std::is_convertible<DataType*, CharT*>::value &&
|
||||
std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
|
||||
DataType>::value>>
|
||||
bool operator==(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
|
||||
{
|
||||
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
|
||||
return std::equal(one.begin(), one.end(), tmp.begin(), tmp.end());
|
||||
}
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename DataType = typename T::value_type,
|
||||
typename Dummy = std::enable_if_t<
|
||||
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
|
||||
std::is_convertible<DataType*, CharT*>::value &&
|
||||
std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
|
||||
DataType>::value>>
|
||||
bool operator==(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
|
||||
{
|
||||
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one);
|
||||
return std::equal(tmp.begin(), tmp.end(), other.begin(), other.end());
|
||||
}
|
||||
#endif
|
||||
|
||||
// operator !=
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename = std::enable_if_t<std::is_convertible<
|
||||
@ -643,38 +602,6 @@ bool operator!=(const T& one, gsl::basic_string_span<CharT, Extent> other) noexc
|
||||
return !(one == other);
|
||||
}
|
||||
|
||||
#ifndef _MSC_VER
|
||||
|
||||
// VS treats temp and const containers as convertible to basic_string_span,
|
||||
// so the cases below are already covered by the previous operators
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename DataType = typename T::value_type,
|
||||
typename Dummy = std::enable_if_t<
|
||||
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
|
||||
std::is_convertible<DataType*, CharT*>::value &&
|
||||
std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
|
||||
DataType>::value>>
|
||||
bool operator!=(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
|
||||
{
|
||||
return !(one == other);
|
||||
}
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename DataType = typename T::value_type,
|
||||
typename Dummy = std::enable_if_t<
|
||||
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
|
||||
std::is_convertible<DataType*, CharT*>::value &&
|
||||
std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
|
||||
DataType>::value>>
|
||||
bool operator!=(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
|
||||
{
|
||||
return !(one == other);
|
||||
}
|
||||
#endif
|
||||
|
||||
// operator<
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename = std::enable_if_t<std::is_convertible<
|
||||
@ -882,6 +809,7 @@ bool operator>=(const T& one, gsl::basic_string_span<CharT, Extent> other) noexc
|
||||
return !(one < other);
|
||||
}
|
||||
#endif
|
||||
} // namespace GSL
|
||||
|
||||
#ifdef _MSC_VER
|
||||
|
||||
|
@ -40,14 +40,14 @@ SUITE(string_span_tests)
|
||||
{
|
||||
std::string s = "Hello there world";
|
||||
cstring_span<> v = s;
|
||||
CHECK(v.length() == static_cast<cstring_span<>::size_type>(s.length()));
|
||||
CHECK(v.length() == static_cast<cstring_span<>::index_type>(s.length()));
|
||||
}
|
||||
|
||||
TEST(TestConstructFromStdVector)
|
||||
{
|
||||
std::vector<char> vec(5, 'h');
|
||||
string_span<> v {vec};
|
||||
CHECK(v.length() == static_cast<string_span<>::size_type>(vec.size()));
|
||||
CHECK(v.length() == static_cast<string_span<>::index_type>(vec.size()));
|
||||
}
|
||||
|
||||
TEST(TestStackArrayConstruction)
|
||||
@ -109,7 +109,7 @@ SUITE(string_span_tests)
|
||||
char stack_string[] = "Hello";
|
||||
cstring_span<> v = ensure_z(stack_string);
|
||||
auto s2 = gsl::to_string(v);
|
||||
CHECK(static_cast<cstring_span<>::size_type>(s2.length()) == v.length());
|
||||
CHECK(static_cast<cstring_span<>::index_type>(s2.length()) == v.length());
|
||||
CHECK(s2.length() == 5);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user