Began reimplementation of span. Basic constructors.

This commit is contained in:
Neil MacIntosh 2016-02-24 16:11:33 -08:00
parent cec26a23b9
commit d3929c59a0
3 changed files with 126 additions and 9 deletions

View File

@ -106,6 +106,7 @@ inline T narrow(U u)
T t = narrow_cast<T>(u); T t = narrow_cast<T>(u);
if (static_cast<U>(t) != u) if (static_cast<U>(t) != u)
throw narrowing_error(); 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{}))) if (!details::is_same_signedness<T, U>::value && ((t < T{}) != (u < U{})))
throw narrowing_error(); throw narrowing_error();
return t; return t;

View File

@ -21,16 +21,9 @@
#include "gsl_assert.h" #include "gsl_assert.h"
#include "gsl_util.h" #include "gsl_util.h"
#include <algorithm>
#include <array> #include <array>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <iterator>
#include <limits> #include <limits>
#include <new> #include <iterator>
#include <numeric>
#include <stdexcept> #include <stdexcept>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
@ -79,6 +72,129 @@
namespace gsl namespace gsl
{ {
// [views.constants], constants
constexpr const std::ptrdiff_t dynamic_extent = -1;
// [span], class template span
template <class ElementType, std::ptrdiff_t Extent = dynamic_extent>
class span {
public:
// constants and types
using element_type = ElementType;
using index_type = std::ptrdiff_t;
using pointer = element_type*;
using reference = element_type&;
#if 0 // TODO
using iterator = /*implementation-defined */;
using const_iterator = /* implementation-defined */;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
#endif
constexpr static const index_type extent = Extent;
// [span.cons], span constructors, copy, assignment, and destructor
constexpr span() noexcept : data_(nullptr), size_(0)
{ static_assert(extent == dynamic_extent || extent == 0, "Cannot default initialize a fixed-length span."); }
constexpr span(nullptr_t) noexcept : span() {}
constexpr span(pointer ptr, index_type count) : data_(ptr), size_(count)
{ Expects((!ptr && count == 0) || (ptr && count >= 0)); }
#if 0 // TODO
constexpr span(pointer firstElem, pointer lastElem);
template <size_t N>
constexpr span(element_type(&arr)[N]);
template <size_t N>
constexpr span(array<remove_const_t<element_type>, N>& arr);
template <size_t N>
constexpr span(const array<remove_const_t<element_type>, N>& arr);
template <class Container>
constexpr span(Container& cont);
template <class Container>
span(const Container&&) = delete;
constexpr span(const span& other) noexcept = default;
constexpr span(span&& other) noexcept = default;
template <class OtherElementType, ptrdiff_t OtherExtent>
constexpr span(const span<OtherElementType, OtherExtent>& other);
template <class OtherElementType, ptrdiff_t OtherExtent>
constexpr span(span<OtherElementType, OtherExtent>&& other);
~span() noexcept = default;
constexpr span& operator=(const span& other) noexcept = default;
constexpr span& operator=(span&& other) noexcept = default;
// [span.sub], span subviews
template <ptrdiff_t Count>
constexpr span<element_type, Count> first() const;
template <ptrdiff_t Count>
constexpr span<element_type, Count> last() const;
template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent>
constexpr span<element_type, Count> subspan() const;
constexpr span<element_type, dynamic_extent> first(index_type count) const;
constexpr span<element_type, dynamic_extent> last(index_type count) const;
constexpr span<element_type, dynamic_extent> subspan(index_type offset, index_type count = dynamic_extent) const;
#endif
// [span.obs], span observers
constexpr index_type length() const noexcept { return size(); }
constexpr index_type size() const noexcept { return 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; }
#if 0 // TODO
// [span.elem], span element access
constexpr reference operator[](index_type idx) const;
constexpr reference operator()(index_type idx) const;
#endif
constexpr pointer data() const noexcept { return data_; }
#if 0 // TODO
// [span.iter], span iterator support
iterator begin() const noexcept;
iterator end() const noexcept;
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
reverse_iterator rbegin() const noexcept;
reverse_iterator rend() const noexcept;
const_reverse_iterator crbegin() const noexcept;
const_reverse_iterator crend() const noexcept;
#endif
private:
pointer data_;
index_type size_;
};
#if 0 // TODO
// [span.comparison], span comparison operators
template <class ElementType, ptrdiff_t Extent>
constexpr bool operator==(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) const noexcept;
template <class ElementType, ptrdiff_t Extent>
constexpr bool operator!=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) const noexcept;
template <class ElementType, ptrdiff_t Extent>
constexpr bool operator<(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) const noexcept;
template <class ElementType, ptrdiff_t Extent>
constexpr bool operator<=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) const noexcept;
template <class ElementType, ptrdiff_t Extent>
constexpr bool operator>(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) const noexcept;
template <class ElementType, ptrdiff_t Extent>
constexpr bool operator>=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) const noexcept;
#endif
#if 0 // TODO
// [span.objectrep], views of object representation
template <class ElementType, ptrdiff_t Extent>
constexpr span<const char, ((Extent == dynamic_extent) ? dynamic_extent : (sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept;
template <class ElementType, ptrdiff_t Extent>
constexpr span<char, ((Extent == dynamic_extent) ? dynamic_extent : (sizeof(ElementType) * Extent))> as_writeable_bytes(span<ElementType, Extent>) noexcept;
#endif
} // namespace gsl } // namespace gsl

View File

@ -39,7 +39,6 @@ struct DerivedClass : BaseClass
SUITE(span_tests) SUITE(span_tests)
{ {
#if 0
TEST(default_constructor) TEST(default_constructor)
{ {
{ {
@ -116,6 +115,7 @@ SUITE(span_tests)
} }
} }
#if 0
TEST(from_nullptr_length_constructor) TEST(from_nullptr_length_constructor)
{ {
{ {