mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
Merge branch 'master' into thread_classes
This commit is contained in:
commit
7d608055d8
@ -2,6 +2,49 @@ cmake_minimum_required(VERSION 2.8.7)
|
|||||||
|
|
||||||
project(GSL CXX)
|
project(GSL CXX)
|
||||||
|
|
||||||
|
# creates a library GSL which is an interface (header files only)
|
||||||
|
add_library(GSL INTERFACE)
|
||||||
|
|
||||||
|
# when minimum version required is 3.8.0 remove if below
|
||||||
|
# both branches do exactly the same thing
|
||||||
|
if ( CMAKE_MAJOR_VERSION VERSION_LESS 3.7.9)
|
||||||
|
if (NOT MSVC)
|
||||||
|
include(CheckCXXCompilerFlag)
|
||||||
|
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)
|
||||||
|
if(COMPILER_SUPPORTS_CXX14)
|
||||||
|
target_compile_options(GSL INTERFACE "-std=c++14")
|
||||||
|
else()
|
||||||
|
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++14 support. Please use a different C++ compiler.")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
endif()
|
||||||
|
else ()
|
||||||
|
# set the GSL library to be compiled only with c++14
|
||||||
|
target_compile_features(GSL INTERFACE cxx_std_14)
|
||||||
|
# on *nix systems force the use of -std=c++XX instead of -std=gnu++XX (default)
|
||||||
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# add definitions to the library and targets that consume it
|
||||||
|
target_compile_definitions(GSL INTERFACE
|
||||||
|
$<$<CXX_COMPILER_ID:MSVC>:
|
||||||
|
# remove unnecessary warnings about unchecked iterators
|
||||||
|
_SCL_SECURE_NO_WARNINGS
|
||||||
|
>
|
||||||
|
)
|
||||||
|
|
||||||
|
# add include folders to the library and targets that consume it
|
||||||
|
target_include_directories(GSL INTERFACE
|
||||||
|
$<BUILD_INTERFACE:
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||||
|
>
|
||||||
|
)
|
||||||
|
|
||||||
|
# add natvis file to the library so it will automatically be loaded into Visual Studio
|
||||||
|
target_sources(GSL INTERFACE
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/GSL.natvis
|
||||||
|
)
|
||||||
|
|
||||||
install(
|
install(
|
||||||
DIRECTORY include/gsl
|
DIRECTORY include/gsl
|
||||||
DESTINATION include
|
DESTINATION include
|
||||||
|
@ -29,6 +29,7 @@ The test suite that exercises GSL has been built and passes successfully on the
|
|||||||
* GNU/Linux using GCC 5.1
|
* GNU/Linux using GCC 5.1
|
||||||
* OS X Yosemite using Xcode with Apple Clang 7.0.0.7000072
|
* OS X Yosemite using Xcode with Apple Clang 7.0.0.7000072
|
||||||
* OS X Yosemite using GCC-5.2.0
|
* OS X Yosemite using GCC-5.2.0
|
||||||
|
* OS X Sierra 10.12.4 using Apple LLVM version 8.1.0 (Clang-802.0.42)
|
||||||
* FreeBSD 10.x with Clang/LLVM 3.6
|
* FreeBSD 10.x with Clang/LLVM 3.6
|
||||||
|
|
||||||
> If you successfully port GSL to another platform, we would love to hear from you. Please submit an issue to let us know. Also please consider
|
> If you successfully port GSL to another platform, we would love to hear from you. Please submit an issue to let us know. Also please consider
|
||||||
|
@ -25,8 +25,10 @@
|
|||||||
#include <gsl/span> // span
|
#include <gsl/span> // span
|
||||||
#include <gsl/string_span> // zstring, string_span, zstring_builder...
|
#include <gsl/string_span> // zstring, string_span, zstring_builder...
|
||||||
#include <gsl/gsl_thread> // raii_thread, detached_thread...
|
#include <gsl/gsl_thread> // raii_thread, detached_thread...
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#if defined(_MSC_VER) && _MSC_VER < 1910
|
#if defined(_MSC_VER) && _MSC_VER < 1910
|
||||||
#pragma push_macro("constexpr")
|
#pragma push_macro("constexpr")
|
||||||
@ -77,10 +79,15 @@ public:
|
|||||||
static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr.");
|
static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr.");
|
||||||
|
|
||||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||||
constexpr not_null(U&& u) : ptr_(std::forward<U>(u)) { Expects(ptr_ != nullptr); }
|
constexpr not_null(U&& u) : ptr_(std::forward<U>(u))
|
||||||
|
{
|
||||||
|
Expects(ptr_ != nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||||
constexpr not_null(const not_null<U>& other) : not_null(other.get()) {}
|
constexpr not_null(const not_null<U>& other) : not_null(other.get())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
not_null(const not_null& other) = default;
|
not_null(const not_null& other) = default;
|
||||||
not_null& operator=(const not_null& other) = default;
|
not_null& operator=(const not_null& other) = default;
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
#define GSL_ALGORITHM_H
|
#define GSL_ALGORITHM_H
|
||||||
|
|
||||||
#include <gsl/span>
|
#include <gsl/span>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
@ -37,13 +38,14 @@
|
|||||||
namespace gsl
|
namespace gsl
|
||||||
{
|
{
|
||||||
|
|
||||||
template <class SrcElementType, std::ptrdiff_t SrcExtent,
|
template <class SrcElementType, std::ptrdiff_t SrcExtent, class DestElementType,
|
||||||
class DestElementType, std::ptrdiff_t DestExtent>
|
std::ptrdiff_t DestExtent>
|
||||||
void copy(span<SrcElementType, SrcExtent> src, span<DestElementType, DestExtent> dest)
|
void copy(span<SrcElementType, SrcExtent> src, span<DestElementType, DestExtent> dest)
|
||||||
{
|
{
|
||||||
static_assert(std::is_assignable<decltype(*dest.data()), decltype(*src.data())>::value,
|
static_assert(std::is_assignable<decltype(*dest.data()), decltype(*src.data())>::value,
|
||||||
"Elements of source span can not be assigned to elements of destination span");
|
"Elements of source span can not be assigned to elements of destination span");
|
||||||
static_assert(SrcExtent == dynamic_extent || DestExtent == dynamic_extent || (SrcExtent <= DestExtent),
|
static_assert(SrcExtent == dynamic_extent || DestExtent == dynamic_extent ||
|
||||||
|
(SrcExtent <= DestExtent),
|
||||||
"Source range is longer than target range");
|
"Source range is longer than target range");
|
||||||
|
|
||||||
Expects(dest.size() >= src.size());
|
Expects(dest.size() >= src.size());
|
||||||
|
@ -77,13 +77,13 @@ struct fail_fast : public std::logic_error
|
|||||||
#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
|
#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
|
||||||
|
|
||||||
#define GSL_CONTRACT_CHECK(type, cond) \
|
#define GSL_CONTRACT_CHECK(type, cond) \
|
||||||
(GSL_LIKELY(cond) ? static_cast<void>(0) : \
|
(GSL_LIKELY(cond) ? static_cast<void>(0) \
|
||||||
throw gsl::fail_fast("GSL: " type " failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__)))
|
: throw gsl::fail_fast("GSL: " type " failure at " __FILE__ \
|
||||||
|
": " GSL_STRINGIFY(__LINE__)))
|
||||||
|
|
||||||
#elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)
|
#elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)
|
||||||
|
|
||||||
#define GSL_CONTRACT_CHECK(type, cond) \
|
#define GSL_CONTRACT_CHECK(type, cond) (GSL_LIKELY(cond) ? static_cast<void>(0) : std::terminate())
|
||||||
(GSL_LIKELY(cond) ? static_cast<void>(0) : std::terminate())
|
|
||||||
|
|
||||||
#elif defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION)
|
#elif defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION)
|
||||||
|
|
||||||
|
@ -113,10 +113,8 @@ template<bool E, typename T>
|
|||||||
inline constexpr byte to_byte_impl(T t) noexcept
|
inline constexpr byte to_byte_impl(T t) noexcept
|
||||||
{
|
{
|
||||||
static_assert(
|
static_assert(
|
||||||
E,
|
E, "gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. "
|
||||||
"gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. "
|
"If you are calling to_byte with an integer contant use: gsl::to_byte<t>() version.");
|
||||||
"If you are calling to_byte with an integer contant use: gsl::to_byte<t>() version."
|
|
||||||
);
|
|
||||||
return static_cast<byte>(t);
|
return static_cast<byte>(t);
|
||||||
}
|
}
|
||||||
template <>
|
template <>
|
||||||
@ -134,7 +132,8 @@ inline constexpr byte to_byte(T t) noexcept
|
|||||||
template <int I>
|
template <int I>
|
||||||
inline constexpr byte to_byte() noexcept
|
inline constexpr byte to_byte() noexcept
|
||||||
{
|
{
|
||||||
static_assert(I >= 0 && I <= 255, "gsl::byte only has 8 bits of storage, values must be in range 0-255");
|
static_assert(I >= 0 && I <= 255,
|
||||||
|
"gsl::byte only has 8 bits of storage, values must be in range 0-255");
|
||||||
return static_cast<byte>(I);
|
return static_cast<byte>(I);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
#define GSL_UTIL_H
|
#define GSL_UTIL_H
|
||||||
|
|
||||||
#include <gsl/gsl_assert> // Ensures/Expects
|
#include <gsl/gsl_assert> // Ensures/Expects
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
#include <gsl/gsl_assert>
|
#include <gsl/gsl_assert>
|
||||||
#include <gsl/gsl_byte>
|
#include <gsl/gsl_byte>
|
||||||
#include <gsl/gsl_util>
|
#include <gsl/gsl_util>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
@ -207,7 +208,10 @@ public:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend constexpr index operator*(value_type v, const index& rhs) GSL_NOEXCEPT { return rhs * v; }
|
friend constexpr index operator*(value_type v, const index& rhs) GSL_NOEXCEPT
|
||||||
|
{
|
||||||
|
return rhs * v;
|
||||||
|
}
|
||||||
|
|
||||||
constexpr index& operator*=(value_type v) GSL_NOEXCEPT
|
constexpr index& operator*=(value_type v) GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
@ -344,10 +348,11 @@ namespace details
|
|||||||
static const std::size_t DynamicNum = Base::DynamicNum + 1;
|
static const std::size_t DynamicNum = Base::DynamicNum + 1;
|
||||||
static const size_type CurrentRange = dynamic_range;
|
static const size_type CurrentRange = dynamic_range;
|
||||||
static const size_type TotalSize = dynamic_range;
|
static const size_type TotalSize = dynamic_range;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_type m_bound;
|
size_type m_bound;
|
||||||
public:
|
|
||||||
|
|
||||||
|
public:
|
||||||
BoundsRanges(const std::ptrdiff_t* const arr)
|
BoundsRanges(const std::ptrdiff_t* const arr)
|
||||||
: Base(arr + 1), m_bound(*arr * this->Base::totalSize())
|
: Base(arr + 1), m_bound(*arr * this->Base::totalSize())
|
||||||
{
|
{
|
||||||
@ -492,7 +497,8 @@ namespace details
|
|||||||
return obj_;
|
return obj_;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <std::size_t N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base>
|
template <std::size_t N, typename MyChain = TypeChain,
|
||||||
|
typename MyBase = typename MyChain::Base>
|
||||||
auto getObj(std::false_type)
|
auto getObj(std::false_type)
|
||||||
-> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase&>(obj_)).template get<N>())
|
-> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase&>(obj_)).template get<N>())
|
||||||
{
|
{
|
||||||
@ -690,7 +696,10 @@ public:
|
|||||||
return !(*this == rhs);
|
return !(*this == rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr const_iterator begin() const GSL_NOEXCEPT { return const_iterator(*this, index_type{}); }
|
constexpr const_iterator begin() const GSL_NOEXCEPT
|
||||||
|
{
|
||||||
|
return const_iterator(*this, index_type{});
|
||||||
|
}
|
||||||
|
|
||||||
constexpr const_iterator end() const GSL_NOEXCEPT
|
constexpr const_iterator end() const GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
@ -790,9 +799,15 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
constexpr index_type index_bounds() const GSL_NOEXCEPT { return m_extents; }
|
constexpr index_type index_bounds() const GSL_NOEXCEPT { return m_extents; }
|
||||||
constexpr const_iterator begin() const GSL_NOEXCEPT { return const_iterator{*this, index_type{}}; }
|
constexpr const_iterator begin() const GSL_NOEXCEPT
|
||||||
|
{
|
||||||
|
return const_iterator{*this, index_type{}};
|
||||||
|
}
|
||||||
|
|
||||||
constexpr const_iterator end() const GSL_NOEXCEPT { return const_iterator{*this, index_bounds()}; }
|
constexpr const_iterator end() const GSL_NOEXCEPT
|
||||||
|
{
|
||||||
|
return const_iterator{*this, index_bounds()};
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
index_type m_extents;
|
index_type m_extents;
|
||||||
@ -931,18 +946,27 @@ public:
|
|||||||
return curr_ == rhs.curr_;
|
return curr_ == rhs.curr_;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr bool operator!=(const bounds_iterator& rhs) const GSL_NOEXCEPT { return !(*this == rhs); }
|
constexpr bool operator!=(const bounds_iterator& rhs) const GSL_NOEXCEPT
|
||||||
|
{
|
||||||
|
return !(*this == rhs);
|
||||||
|
}
|
||||||
|
|
||||||
constexpr bool operator<(const bounds_iterator& rhs) const GSL_NOEXCEPT
|
constexpr bool operator<(const bounds_iterator& rhs) const GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
return less(curr_, rhs.curr_);
|
return less(curr_, rhs.curr_);
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr bool operator<=(const bounds_iterator& rhs) const GSL_NOEXCEPT { return !(rhs < *this); }
|
constexpr bool operator<=(const bounds_iterator& rhs) const GSL_NOEXCEPT
|
||||||
|
{
|
||||||
|
return !(rhs < *this);
|
||||||
|
}
|
||||||
|
|
||||||
constexpr bool operator>(const bounds_iterator& rhs) const GSL_NOEXCEPT { return rhs < *this; }
|
constexpr bool operator>(const bounds_iterator& rhs) const GSL_NOEXCEPT { return rhs < *this; }
|
||||||
|
|
||||||
constexpr bool operator>=(const bounds_iterator& rhs) const GSL_NOEXCEPT { return !(rhs > *this); }
|
constexpr bool operator>=(const bounds_iterator& rhs) const GSL_NOEXCEPT
|
||||||
|
{
|
||||||
|
return !(rhs > *this);
|
||||||
|
}
|
||||||
|
|
||||||
void swap(bounds_iterator& rhs) GSL_NOEXCEPT
|
void swap(bounds_iterator& rhs) GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
@ -1227,7 +1251,8 @@ public:
|
|||||||
|
|
||||||
// construct from nullptr with size of 0 (helps with template function calls)
|
// construct from nullptr with size of 0 (helps with template function calls)
|
||||||
template <class IntType, typename = std::enable_if_t<std::is_integral<IntType>::value>>
|
template <class IntType, typename = std::enable_if_t<std::is_integral<IntType>::value>>
|
||||||
constexpr multi_span(std::nullptr_t, IntType size) GSL_NOEXCEPT : multi_span(nullptr, bounds_type{})
|
constexpr multi_span(std::nullptr_t, IntType size) GSL_NOEXCEPT
|
||||||
|
: multi_span(nullptr, bounds_type{})
|
||||||
{
|
{
|
||||||
static_assert(bounds_type::dynamic_rank != 0 ||
|
static_assert(bounds_type::dynamic_rank != 0 ||
|
||||||
(bounds_type::dynamic_rank == 0 && bounds_type::static_size == 0),
|
(bounds_type::dynamic_rank == 0 && bounds_type::static_size == 0),
|
||||||
@ -1249,7 +1274,8 @@ public:
|
|||||||
constexpr multi_span(value_type&&) = delete;
|
constexpr multi_span(value_type&&) = delete;
|
||||||
|
|
||||||
// construct from pointer + length
|
// construct from pointer + length
|
||||||
constexpr multi_span(pointer ptr, size_type size) GSL_NOEXCEPT : multi_span(ptr, bounds_type{size})
|
constexpr multi_span(pointer ptr, size_type size) GSL_NOEXCEPT
|
||||||
|
: multi_span(ptr, bounds_type{size})
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1426,9 +1452,8 @@ public:
|
|||||||
|
|
||||||
// subspan() - create a subview of count elements starting at offset
|
// subspan() - create a subview of count elements starting at offset
|
||||||
// supplying dynamic_range for count will consume all available elements from offset
|
// supplying dynamic_range for count will consume all available elements from offset
|
||||||
constexpr multi_span<ValueType, dynamic_range> subspan(size_type offset,
|
constexpr multi_span<ValueType, dynamic_range>
|
||||||
size_type count = dynamic_range) const
|
subspan(size_type offset, size_type count = dynamic_range) const GSL_NOEXCEPT
|
||||||
GSL_NOEXCEPT
|
|
||||||
{
|
{
|
||||||
Expects((offset >= 0 && offset <= this->size()) &&
|
Expects((offset >= 0 && offset <= this->size()) &&
|
||||||
(count == dynamic_range || (count <= this->size() - offset)));
|
(count == dynamic_range || (count <= this->size() - offset)));
|
||||||
@ -1436,8 +1461,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// section - creates a non-contiguous, strided multi_span from a contiguous one
|
// section - creates a non-contiguous, strided multi_span from a contiguous one
|
||||||
constexpr strided_span<ValueType, Rank> section(index_type origin, index_type extents) const
|
constexpr strided_span<ValueType, Rank> section(index_type origin,
|
||||||
GSL_NOEXCEPT
|
index_type extents) const GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
|
size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
|
||||||
return {&this->operator[](origin), size,
|
return {&this->operator[](origin), size,
|
||||||
@ -1451,7 +1476,10 @@ public:
|
|||||||
constexpr size_type length() const GSL_NOEXCEPT { return this->size(); }
|
constexpr size_type length() const GSL_NOEXCEPT { return this->size(); }
|
||||||
|
|
||||||
// length of the multi_span in bytes
|
// length of the multi_span in bytes
|
||||||
constexpr size_type size_bytes() const GSL_NOEXCEPT { return narrow_cast<size_type>(sizeof(value_type)) * this->size(); }
|
constexpr size_type size_bytes() const GSL_NOEXCEPT
|
||||||
|
{
|
||||||
|
return narrow_cast<size_type>(sizeof(value_type)) * this->size();
|
||||||
|
}
|
||||||
|
|
||||||
// length of the multi_span in bytes
|
// length of the multi_span in bytes
|
||||||
constexpr size_type length_bytes() const GSL_NOEXCEPT { return this->size_bytes(); }
|
constexpr size_type length_bytes() const GSL_NOEXCEPT { return this->size_bytes(); }
|
||||||
@ -1539,8 +1567,8 @@ public:
|
|||||||
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
|
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
|
||||||
typename Dummy = std::enable_if_t<std::is_same<
|
typename Dummy = std::enable_if_t<std::is_same<
|
||||||
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
||||||
constexpr bool operator==(const multi_span<OtherValueType, OtherDimensions...>& other) const
|
constexpr bool
|
||||||
GSL_NOEXCEPT
|
operator==(const multi_span<OtherValueType, OtherDimensions...>& other) const GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
return bounds_.size() == other.bounds_.size() &&
|
return bounds_.size() == other.bounds_.size() &&
|
||||||
(data_ == other.data_ || std::equal(this->begin(), this->end(), other.begin()));
|
(data_ == other.data_ || std::equal(this->begin(), this->end(), other.begin()));
|
||||||
@ -1549,8 +1577,8 @@ public:
|
|||||||
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
|
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
|
||||||
typename Dummy = std::enable_if_t<std::is_same<
|
typename Dummy = std::enable_if_t<std::is_same<
|
||||||
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
||||||
constexpr bool operator!=(const multi_span<OtherValueType, OtherDimensions...>& other) const
|
constexpr bool
|
||||||
GSL_NOEXCEPT
|
operator!=(const multi_span<OtherValueType, OtherDimensions...>& other) const GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
return !(*this == other);
|
return !(*this == other);
|
||||||
}
|
}
|
||||||
@ -1558,8 +1586,8 @@ public:
|
|||||||
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
|
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
|
||||||
typename Dummy = std::enable_if_t<std::is_same<
|
typename Dummy = std::enable_if_t<std::is_same<
|
||||||
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
||||||
constexpr bool operator<(const multi_span<OtherValueType, OtherDimensions...>& other) const
|
constexpr bool
|
||||||
GSL_NOEXCEPT
|
operator<(const multi_span<OtherValueType, OtherDimensions...>& other) const GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
|
return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
|
||||||
}
|
}
|
||||||
@ -1567,8 +1595,8 @@ public:
|
|||||||
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
|
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
|
||||||
typename Dummy = std::enable_if_t<std::is_same<
|
typename Dummy = std::enable_if_t<std::is_same<
|
||||||
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
||||||
constexpr bool operator<=(const multi_span<OtherValueType, OtherDimensions...>& other) const
|
constexpr bool
|
||||||
GSL_NOEXCEPT
|
operator<=(const multi_span<OtherValueType, OtherDimensions...>& other) const GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
return !(other < *this);
|
return !(other < *this);
|
||||||
}
|
}
|
||||||
@ -1576,8 +1604,8 @@ public:
|
|||||||
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
|
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
|
||||||
typename Dummy = std::enable_if_t<std::is_same<
|
typename Dummy = std::enable_if_t<std::is_same<
|
||||||
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
||||||
constexpr bool operator>(const multi_span<OtherValueType, OtherDimensions...>& other) const
|
constexpr bool
|
||||||
GSL_NOEXCEPT
|
operator>(const multi_span<OtherValueType, OtherDimensions...>& other) const GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
return (other < *this);
|
return (other < *this);
|
||||||
}
|
}
|
||||||
@ -1585,8 +1613,8 @@ public:
|
|||||||
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
|
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
|
||||||
typename Dummy = std::enable_if_t<std::is_same<
|
typename Dummy = std::enable_if_t<std::is_same<
|
||||||
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
||||||
constexpr bool operator>=(const multi_span<OtherValueType, OtherDimensions...>& other) const
|
constexpr bool
|
||||||
GSL_NOEXCEPT
|
operator>=(const multi_span<OtherValueType, OtherDimensions...>& other) const GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
return !(*this < other);
|
return !(*this < other);
|
||||||
}
|
}
|
||||||
@ -1638,7 +1666,8 @@ multi_span<byte> as_writeable_bytes(multi_span<U, Dimensions...> s) GSL_NOEXCEPT
|
|||||||
// on all implementations. It should be considered an experimental extension
|
// on all implementations. It should be considered an experimental extension
|
||||||
// to the standard GSL interface.
|
// to the standard GSL interface.
|
||||||
template <typename U, std::ptrdiff_t... Dimensions>
|
template <typename U, std::ptrdiff_t... Dimensions>
|
||||||
inline constexpr auto as_multi_span(multi_span<const byte, Dimensions...> s) GSL_NOEXCEPT -> multi_span<
|
inline constexpr auto
|
||||||
|
as_multi_span(multi_span<const byte, Dimensions...> s) GSL_NOEXCEPT -> multi_span<
|
||||||
const U, static_cast<std::ptrdiff_t>(
|
const U, static_cast<std::ptrdiff_t>(
|
||||||
multi_span<const byte, Dimensions...>::bounds_type::static_size != dynamic_range
|
multi_span<const byte, Dimensions...>::bounds_type::static_size != dynamic_range
|
||||||
? (static_cast<std::size_t>(
|
? (static_cast<std::size_t>(
|
||||||
@ -1830,7 +1859,8 @@ public:
|
|||||||
|
|
||||||
size_type size = this->bounds().total_size() / d;
|
size_type size = this->bounds().total_size() / d;
|
||||||
return {const_cast<OtherValueType*>(reinterpret_cast<const OtherValueType*>(this->data())),
|
return {const_cast<OtherValueType*>(reinterpret_cast<const OtherValueType*>(this->data())),
|
||||||
size, bounds_type{resize_extent(this->bounds().index_bounds(), d),
|
size,
|
||||||
|
bounds_type{resize_extent(this->bounds().index_bounds(), d),
|
||||||
resize_stride(this->bounds().strides(), d)}};
|
resize_stride(this->bounds().strides(), d)}};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1898,7 +1928,8 @@ public:
|
|||||||
template <typename OtherValueType, std::ptrdiff_t OtherRank,
|
template <typename OtherValueType, std::ptrdiff_t OtherRank,
|
||||||
typename Dummy = std::enable_if_t<std::is_same<
|
typename Dummy = std::enable_if_t<std::is_same<
|
||||||
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
||||||
constexpr bool operator==(const strided_span<OtherValueType, OtherRank>& other) const GSL_NOEXCEPT
|
constexpr bool
|
||||||
|
operator==(const strided_span<OtherValueType, OtherRank>& other) const GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
return bounds_.size() == other.bounds_.size() &&
|
return bounds_.size() == other.bounds_.size() &&
|
||||||
(data_ == other.data_ || std::equal(this->begin(), this->end(), other.begin()));
|
(data_ == other.data_ || std::equal(this->begin(), this->end(), other.begin()));
|
||||||
@ -1907,7 +1938,8 @@ public:
|
|||||||
template <typename OtherValueType, std::ptrdiff_t OtherRank,
|
template <typename OtherValueType, std::ptrdiff_t OtherRank,
|
||||||
typename Dummy = std::enable_if_t<std::is_same<
|
typename Dummy = std::enable_if_t<std::is_same<
|
||||||
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
||||||
constexpr bool operator!=(const strided_span<OtherValueType, OtherRank>& other) const GSL_NOEXCEPT
|
constexpr bool
|
||||||
|
operator!=(const strided_span<OtherValueType, OtherRank>& other) const GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
return !(*this == other);
|
return !(*this == other);
|
||||||
}
|
}
|
||||||
@ -1915,7 +1947,8 @@ public:
|
|||||||
template <typename OtherValueType, std::ptrdiff_t OtherRank,
|
template <typename OtherValueType, std::ptrdiff_t OtherRank,
|
||||||
typename Dummy = std::enable_if_t<std::is_same<
|
typename Dummy = std::enable_if_t<std::is_same<
|
||||||
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
||||||
constexpr bool operator<(const strided_span<OtherValueType, OtherRank>& other) const GSL_NOEXCEPT
|
constexpr bool
|
||||||
|
operator<(const strided_span<OtherValueType, OtherRank>& other) const GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
|
return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
|
||||||
}
|
}
|
||||||
@ -1923,7 +1956,8 @@ public:
|
|||||||
template <typename OtherValueType, std::ptrdiff_t OtherRank,
|
template <typename OtherValueType, std::ptrdiff_t OtherRank,
|
||||||
typename Dummy = std::enable_if_t<std::is_same<
|
typename Dummy = std::enable_if_t<std::is_same<
|
||||||
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
||||||
constexpr bool operator<=(const strided_span<OtherValueType, OtherRank>& other) const GSL_NOEXCEPT
|
constexpr bool
|
||||||
|
operator<=(const strided_span<OtherValueType, OtherRank>& other) const GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
return !(other < *this);
|
return !(other < *this);
|
||||||
}
|
}
|
||||||
@ -1931,7 +1965,8 @@ public:
|
|||||||
template <typename OtherValueType, std::ptrdiff_t OtherRank,
|
template <typename OtherValueType, std::ptrdiff_t OtherRank,
|
||||||
typename Dummy = std::enable_if_t<std::is_same<
|
typename Dummy = std::enable_if_t<std::is_same<
|
||||||
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
||||||
constexpr bool operator>(const strided_span<OtherValueType, OtherRank>& other) const GSL_NOEXCEPT
|
constexpr bool
|
||||||
|
operator>(const strided_span<OtherValueType, OtherRank>& other) const GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
return (other < *this);
|
return (other < *this);
|
||||||
}
|
}
|
||||||
@ -1939,7 +1974,8 @@ public:
|
|||||||
template <typename OtherValueType, std::ptrdiff_t OtherRank,
|
template <typename OtherValueType, std::ptrdiff_t OtherRank,
|
||||||
typename Dummy = std::enable_if_t<std::is_same<
|
typename Dummy = std::enable_if_t<std::is_same<
|
||||||
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
|
||||||
constexpr bool operator>=(const strided_span<OtherValueType, OtherRank>& other) const GSL_NOEXCEPT
|
constexpr bool
|
||||||
|
operator>=(const strided_span<OtherValueType, OtherRank>& other) const GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
return !(*this < other);
|
return !(*this < other);
|
||||||
}
|
}
|
||||||
@ -2074,15 +2110,24 @@ public:
|
|||||||
Expects(m_validator == rhs.m_validator);
|
Expects(m_validator == rhs.m_validator);
|
||||||
return data_ == rhs.data_;
|
return data_ == rhs.data_;
|
||||||
}
|
}
|
||||||
bool operator!=(const contiguous_span_iterator& rhs) const GSL_NOEXCEPT { return !(*this == rhs); }
|
bool operator!=(const contiguous_span_iterator& rhs) const GSL_NOEXCEPT
|
||||||
|
{
|
||||||
|
return !(*this == rhs);
|
||||||
|
}
|
||||||
bool operator<(const contiguous_span_iterator& rhs) const GSL_NOEXCEPT
|
bool operator<(const contiguous_span_iterator& rhs) const GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
Expects(m_validator == rhs.m_validator);
|
Expects(m_validator == rhs.m_validator);
|
||||||
return data_ < rhs.data_;
|
return data_ < rhs.data_;
|
||||||
}
|
}
|
||||||
bool operator<=(const contiguous_span_iterator& rhs) const GSL_NOEXCEPT { return !(rhs < *this); }
|
bool operator<=(const contiguous_span_iterator& rhs) const GSL_NOEXCEPT
|
||||||
|
{
|
||||||
|
return !(rhs < *this);
|
||||||
|
}
|
||||||
bool operator>(const contiguous_span_iterator& rhs) const GSL_NOEXCEPT { return rhs < *this; }
|
bool operator>(const contiguous_span_iterator& rhs) const GSL_NOEXCEPT { return rhs < *this; }
|
||||||
bool operator>=(const contiguous_span_iterator& rhs) const GSL_NOEXCEPT { return !(rhs > *this); }
|
bool operator>=(const contiguous_span_iterator& rhs) const GSL_NOEXCEPT
|
||||||
|
{
|
||||||
|
return !(rhs > *this);
|
||||||
|
}
|
||||||
void swap(contiguous_span_iterator& rhs) GSL_NOEXCEPT
|
void swap(contiguous_span_iterator& rhs) GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
std::swap(data_, rhs.data_);
|
std::swap(data_, rhs.data_);
|
||||||
|
108
include/gsl/span
108
include/gsl/span
@ -23,13 +23,14 @@
|
|||||||
#include <gsl/gsl_assert>
|
#include <gsl/gsl_assert>
|
||||||
#include <gsl/gsl_byte>
|
#include <gsl/gsl_byte>
|
||||||
#include <gsl/gsl_util>
|
#include <gsl/gsl_util>
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
#include <memory>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma warning(push)
|
#pragma warning(push)
|
||||||
@ -131,13 +132,13 @@ namespace details
|
|||||||
class span_iterator
|
class span_iterator
|
||||||
{
|
{
|
||||||
using element_type_ = typename Span::element_type;
|
using element_type_ = typename Span::element_type;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using iterator_category = std::random_access_iterator_tag;
|
using iterator_category = std::random_access_iterator_tag;
|
||||||
using value_type = std::remove_cv_t<element_type_>;
|
using value_type = std::remove_cv_t<element_type_>;
|
||||||
using difference_type = typename Span::index_type;
|
using difference_type = typename Span::index_type;
|
||||||
|
|
||||||
using reference =
|
using reference = std::conditional_t<IsConst, const element_type_, element_type_>&;
|
||||||
std::conditional_t<IsConst, const element_type_, element_type_> &;
|
|
||||||
using pointer = std::add_pointer_t<reference>;
|
using pointer = std::add_pointer_t<reference>;
|
||||||
|
|
||||||
constexpr span_iterator() GSL_NOEXCEPT : span_iterator(nullptr, 0) {}
|
constexpr span_iterator() GSL_NOEXCEPT : span_iterator(nullptr, 0) {}
|
||||||
@ -154,7 +155,8 @@ namespace details
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr span_iterator<Span, IsConst>& operator=(const span_iterator<Span, IsConst>&) GSL_NOEXCEPT = default;
|
constexpr span_iterator<Span, IsConst>&
|
||||||
|
operator=(const span_iterator<Span, IsConst>&) GSL_NOEXCEPT = default;
|
||||||
|
|
||||||
constexpr reference operator*() const
|
constexpr reference operator*() const
|
||||||
{
|
{
|
||||||
@ -223,7 +225,10 @@ namespace details
|
|||||||
return index_ - rhs.index_;
|
return index_ - rhs.index_;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr reference operator[](difference_type n) const GSL_NOEXCEPT { return *(*this + n); }
|
constexpr reference operator[](difference_type n) const GSL_NOEXCEPT
|
||||||
|
{
|
||||||
|
return *(*this + n);
|
||||||
|
}
|
||||||
|
|
||||||
constexpr friend bool operator==(const span_iterator& lhs,
|
constexpr friend bool operator==(const span_iterator& lhs,
|
||||||
const span_iterator& rhs) GSL_NOEXCEPT
|
const span_iterator& rhs) GSL_NOEXCEPT
|
||||||
@ -249,7 +254,8 @@ namespace details
|
|||||||
return !(rhs < lhs);
|
return !(rhs < lhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr friend bool operator>(const span_iterator& lhs, const span_iterator& rhs) GSL_NOEXCEPT
|
constexpr friend bool operator>(const span_iterator& lhs,
|
||||||
|
const span_iterator& rhs) GSL_NOEXCEPT
|
||||||
{
|
{
|
||||||
return rhs < lhs;
|
return rhs < lhs;
|
||||||
}
|
}
|
||||||
@ -356,7 +362,9 @@ public:
|
|||||||
// "Dependent" is needed to make "std::enable_if_t<Dependent || Extent <= 0>" SFINAE,
|
// "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.
|
// since "std::enable_if_t<Extent <= 0>" is ill-formed when Extent is greater than 0.
|
||||||
class = std::enable_if_t<(Dependent || Extent <= 0)>>
|
class = std::enable_if_t<(Dependent || Extent <= 0)>>
|
||||||
constexpr span() GSL_NOEXCEPT : storage_(nullptr, details::extent_type<0>()) {}
|
constexpr span() GSL_NOEXCEPT : storage_(nullptr, details::extent_type<0>())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
constexpr span(std::nullptr_t) GSL_NOEXCEPT : span() {}
|
constexpr span(std::nullptr_t) GSL_NOEXCEPT : span() {}
|
||||||
|
|
||||||
@ -368,7 +376,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <std::size_t N>
|
template <std::size_t N>
|
||||||
constexpr span(element_type (&arr)[N]) GSL_NOEXCEPT : storage_(&arr[0], details::extent_type<N>())
|
constexpr span(element_type (&arr)[N]) GSL_NOEXCEPT
|
||||||
|
: storage_(&arr[0], details::extent_type<N>())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -385,10 +394,17 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class ArrayElementType = std::add_pointer<element_type>>
|
template <class ArrayElementType = std::add_pointer<element_type>>
|
||||||
constexpr span(const std::unique_ptr<ArrayElementType>& ptr, index_type count) : storage_(ptr.get(), count) {}
|
constexpr span(const std::unique_ptr<ArrayElementType>& ptr, index_type count)
|
||||||
|
: storage_(ptr.get(), count)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
constexpr span(const std::unique_ptr<ElementType>& ptr) : storage_(ptr.get(), ptr.get() ? 1 : 0) {}
|
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) {}
|
{
|
||||||
|
}
|
||||||
|
constexpr span(const std::shared_ptr<ElementType>& ptr) : storage_(ptr.get(), ptr.get() ? 1 : 0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
// NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
|
// NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
|
||||||
// on Container to be a contiguous sequence container.
|
// on Container to be a contiguous sequence container.
|
||||||
@ -498,7 +514,10 @@ public:
|
|||||||
constexpr index_type length() const GSL_NOEXCEPT { return size(); }
|
constexpr index_type length() const GSL_NOEXCEPT { return size(); }
|
||||||
constexpr index_type size() const GSL_NOEXCEPT { return storage_.size(); }
|
constexpr index_type size() const GSL_NOEXCEPT { return storage_.size(); }
|
||||||
constexpr index_type length_bytes() const GSL_NOEXCEPT { return size_bytes(); }
|
constexpr index_type length_bytes() const GSL_NOEXCEPT { return size_bytes(); }
|
||||||
constexpr index_type size_bytes() const GSL_NOEXCEPT { return size() * narrow_cast<index_type>(sizeof(element_type)); }
|
constexpr index_type size_bytes() const GSL_NOEXCEPT
|
||||||
|
{
|
||||||
|
return size() * narrow_cast<index_type>(sizeof(element_type));
|
||||||
|
}
|
||||||
constexpr bool empty() const GSL_NOEXCEPT { return size() == 0; }
|
constexpr bool empty() const GSL_NOEXCEPT { return size() == 0; }
|
||||||
|
|
||||||
// [span.elem], span element access
|
// [span.elem], span element access
|
||||||
@ -561,31 +580,36 @@ inline constexpr bool operator==(const span<ElementType, FirstExtent>& l,
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class ElementType, std::ptrdiff_t Extent>
|
template <class ElementType, std::ptrdiff_t Extent>
|
||||||
inline constexpr bool operator!=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
|
inline constexpr bool operator!=(const span<ElementType, Extent>& l,
|
||||||
|
const span<ElementType, Extent>& r)
|
||||||
{
|
{
|
||||||
return !(l == r);
|
return !(l == r);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class ElementType, std::ptrdiff_t Extent>
|
template <class ElementType, std::ptrdiff_t Extent>
|
||||||
inline constexpr bool operator<(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
|
inline 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());
|
return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class ElementType, std::ptrdiff_t Extent>
|
template <class ElementType, std::ptrdiff_t Extent>
|
||||||
inline constexpr bool operator<=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
|
inline constexpr bool operator<=(const span<ElementType, Extent>& l,
|
||||||
|
const span<ElementType, Extent>& r)
|
||||||
{
|
{
|
||||||
return !(l > r);
|
return !(l > r);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class ElementType, std::ptrdiff_t Extent>
|
template <class ElementType, std::ptrdiff_t Extent>
|
||||||
inline constexpr bool operator>(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
|
inline constexpr bool operator>(const span<ElementType, Extent>& l,
|
||||||
|
const span<ElementType, Extent>& r)
|
||||||
{
|
{
|
||||||
return r < l;
|
return r < l;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class ElementType, std::ptrdiff_t Extent>
|
template <class ElementType, std::ptrdiff_t Extent>
|
||||||
inline constexpr bool operator>=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
|
inline constexpr bool operator>=(const span<ElementType, Extent>& l,
|
||||||
|
const span<ElementType, Extent>& r)
|
||||||
{
|
{
|
||||||
return !(l < r);
|
return !(l < r);
|
||||||
}
|
}
|
||||||
@ -633,40 +657,46 @@ as_writeable_bytes(span<ElementType, Extent> s) GSL_NOEXCEPT
|
|||||||
// make_span() - Utility functions for creating spans
|
// make_span() - Utility functions for creating spans
|
||||||
//
|
//
|
||||||
template <class ElementType>
|
template <class ElementType>
|
||||||
span<ElementType>
|
span<ElementType> make_span(ElementType* ptr, typename span<ElementType>::index_type count)
|
||||||
make_span(ElementType* ptr, typename span<ElementType>::index_type count)
|
{
|
||||||
{ return span<ElementType>(ptr, count); }
|
return span<ElementType>(ptr, count);
|
||||||
|
}
|
||||||
|
|
||||||
template <class ElementType>
|
template <class ElementType>
|
||||||
span<ElementType>
|
span<ElementType> make_span(ElementType* firstElem, ElementType* lastElem)
|
||||||
make_span(ElementType* firstElem, ElementType* lastElem)
|
{
|
||||||
{ return span<ElementType>(firstElem, lastElem); }
|
return span<ElementType>(firstElem, lastElem);
|
||||||
|
}
|
||||||
|
|
||||||
template <class ElementType, std::size_t N>
|
template <class ElementType, std::size_t N>
|
||||||
span<ElementType>
|
span<ElementType, N> make_span(ElementType (&arr)[N])
|
||||||
make_span(ElementType (&arr)[N])
|
{
|
||||||
{ return span<ElementType>(arr); }
|
return span<ElementType, N>(arr);
|
||||||
|
}
|
||||||
|
|
||||||
template <class Container>
|
template <class Container>
|
||||||
span<typename Container::value_type>
|
span<typename Container::value_type> make_span(Container& cont)
|
||||||
make_span(Container &cont)
|
{
|
||||||
{ return span<typename Container::value_type>(cont); }
|
return span<typename Container::value_type>(cont);
|
||||||
|
}
|
||||||
|
|
||||||
template <class Container>
|
template <class Container>
|
||||||
span<const typename Container::value_type>
|
span<const typename Container::value_type> make_span(const Container& cont)
|
||||||
make_span(const Container &cont)
|
{
|
||||||
{ return span<const typename Container::value_type>(cont); }
|
return span<const typename Container::value_type>(cont);
|
||||||
|
}
|
||||||
|
|
||||||
template <class Ptr>
|
template <class Ptr>
|
||||||
span<typename Ptr::element_type>
|
span<typename Ptr::element_type> make_span(Ptr& cont, std::ptrdiff_t count)
|
||||||
make_span(Ptr& cont, std::ptrdiff_t count)
|
{
|
||||||
{ return span<typename Ptr::element_type>(cont, count); }
|
return span<typename Ptr::element_type>(cont, count);
|
||||||
|
}
|
||||||
|
|
||||||
template <class Ptr>
|
template <class Ptr>
|
||||||
span<typename Ptr::element_type>
|
span<typename Ptr::element_type> make_span(Ptr& cont)
|
||||||
make_span(Ptr& cont)
|
{
|
||||||
{ return span<typename Ptr::element_type>(cont); }
|
return span<typename Ptr::element_type>(cont);
|
||||||
|
}
|
||||||
|
|
||||||
// Specialization of gsl::at for span
|
// Specialization of gsl::at for span
|
||||||
template <class ElementType, std::ptrdiff_t Extent>
|
template <class ElementType, std::ptrdiff_t Extent>
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
#include <gsl/gsl_assert>
|
#include <gsl/gsl_assert>
|
||||||
#include <gsl/gsl_util>
|
#include <gsl/gsl_util>
|
||||||
#include <gsl/span>
|
#include <gsl/span>
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -91,28 +92,24 @@ namespace details
|
|||||||
{
|
{
|
||||||
inline std::ptrdiff_t string_length(const char* str, std::ptrdiff_t n)
|
inline std::ptrdiff_t string_length(const char* str, std::ptrdiff_t n)
|
||||||
{
|
{
|
||||||
if (str == nullptr || n <= 0)
|
if (str == nullptr || n <= 0) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
span<const char> str_span{str, n};
|
span<const char> str_span{str, n};
|
||||||
|
|
||||||
std::ptrdiff_t len = 0;
|
std::ptrdiff_t len = 0;
|
||||||
while (len < n && str_span[len])
|
while (len < n && str_span[len]) len++;
|
||||||
len++;
|
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::ptrdiff_t wstring_length(const wchar_t* str, std::ptrdiff_t n)
|
inline std::ptrdiff_t wstring_length(const wchar_t* str, std::ptrdiff_t n)
|
||||||
{
|
{
|
||||||
if (str == nullptr || n <= 0)
|
if (str == nullptr || n <= 0) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
span<const wchar_t> str_span{str, n};
|
span<const wchar_t> str_span{str, n};
|
||||||
|
|
||||||
std::ptrdiff_t len = 0;
|
std::ptrdiff_t len = 0;
|
||||||
while (len < n && str_span[len])
|
while (len < n && str_span[len]) len++;
|
||||||
len++;
|
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
@ -316,7 +313,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <std::size_t N, class ArrayElementType = std::remove_const_t<element_type>>
|
template <std::size_t N, class ArrayElementType = std::remove_const_t<element_type>>
|
||||||
constexpr basic_string_span(const std::array<ArrayElementType, N>& arr) GSL_NOEXCEPT : span_(arr)
|
constexpr basic_string_span(const std::array<ArrayElementType, N>& arr) GSL_NOEXCEPT
|
||||||
|
: span_(arr)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -484,11 +482,8 @@ inline std::wstring to_string(wstring_span<> view)
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template <typename CharT,
|
template <typename CharT, typename Traits = typename std::char_traits<CharT>,
|
||||||
typename Traits = typename std::char_traits<CharT>,
|
typename Allocator = std::allocator<CharT>, typename gCharT, std::ptrdiff_t Extent>
|
||||||
typename Allocator = std::allocator<CharT>,
|
|
||||||
typename gCharT,
|
|
||||||
std::ptrdiff_t Extent>
|
|
||||||
std::basic_string<CharT, Traits, Allocator> to_basic_string(basic_string_span<gCharT, Extent> view)
|
std::basic_string<CharT, Traits, Allocator> to_basic_string(basic_string_span<gCharT, Extent> view)
|
||||||
{
|
{
|
||||||
return {view.data(), static_cast<std::size_t>(view.length())};
|
return {view.data(), static_cast<std::size_t>(view.length())};
|
||||||
|
@ -2,43 +2,70 @@ cmake_minimum_required(VERSION 2.8.7)
|
|||||||
|
|
||||||
project(GSLTests CXX)
|
project(GSLTests CXX)
|
||||||
|
|
||||||
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/unittest-cpp/tests)
|
# will make visual studio generated project group files
|
||||||
execute_process(COMMAND git submodule update --init WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
|
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
||||||
|
|
||||||
|
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/unittest-cpp/CMakeLists.txt)
|
||||||
|
find_package(Git)
|
||||||
|
execute_process(
|
||||||
|
COMMAND ${GIT_EXECUTABLE} submodule update --init
|
||||||
|
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||||
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_subdirectory(unittest-cpp)
|
add_subdirectory(unittest-cpp)
|
||||||
|
|
||||||
include_directories(
|
# this interface adds compile options to how the tests are run
|
||||||
../include
|
# please try to keep entries ordered =)
|
||||||
|
add_library(gsl_tests_config INTERFACE)
|
||||||
|
target_compile_options(gsl_tests_config INTERFACE
|
||||||
|
$<$<CXX_COMPILER_ID:MSVC>:
|
||||||
|
/EHsc
|
||||||
|
/W4
|
||||||
|
/WX
|
||||||
|
>
|
||||||
|
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:
|
||||||
|
-fno-strict-aliasing
|
||||||
|
-Wall
|
||||||
|
-Wcast-align
|
||||||
|
-Wconversion
|
||||||
|
-Wctor-dtor-privacy
|
||||||
|
-Werror
|
||||||
|
-Wextra
|
||||||
|
-Wno-missing-braces
|
||||||
|
-Wnon-virtual-dtor
|
||||||
|
-Wold-style-cast
|
||||||
|
-Woverloaded-virtual
|
||||||
|
-Wpedantic
|
||||||
|
-Wshadow
|
||||||
|
-Wsign-conversion
|
||||||
|
>
|
||||||
|
)
|
||||||
|
|
||||||
|
# set test to include the unittest-cpp headers
|
||||||
|
# this shiuld be removed when UnitTest++ has the proper headers
|
||||||
|
target_include_directories(gsl_tests_config INTERFACE
|
||||||
./unittest-cpp
|
./unittest-cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
add_definitions(-DGSL_THROW_ON_CONTRACT_VIOLATION)
|
# set definitions for tests
|
||||||
|
target_compile_definitions(gsl_tests_config INTERFACE
|
||||||
if(MSVC) # has the support we need
|
GSL_THROW_ON_CONTRACT_VIOLATION
|
||||||
# remove unnecessary warnings about unchecked iterators
|
)
|
||||||
add_definitions(-D_SCL_SECURE_NO_WARNINGS)
|
|
||||||
add_compile_options(/EHsc /W4 /WX)
|
|
||||||
else()
|
|
||||||
include(CheckCXXCompilerFlag)
|
|
||||||
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)
|
|
||||||
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
|
|
||||||
if(COMPILER_SUPPORTS_CXX14)
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -fno-strict-aliasing -std=c++14 -Wall -Wextra -Wpedantic -Wno-missing-braces -Wconversion -Wsign-conversion -Wctor-dtor-privacy -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Woverloaded-virtual")
|
|
||||||
elseif(COMPILER_SUPPORTS_CXX11)
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -fno-strict-aliasing -std=c++11 -Wall -Wextra -Wpedantic -Wno-missing-braces -Wconversion -Wsign-conversion -Wctor-dtor-privacy -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Woverloaded-virtual")
|
|
||||||
else()
|
|
||||||
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
|
|
||||||
endif()
|
|
||||||
endif()
|
|
||||||
|
|
||||||
function(add_gsl_test name)
|
function(add_gsl_test name)
|
||||||
add_executable(${name} ${name}.cpp)
|
add_executable(${name} ${name}.cpp)
|
||||||
target_link_libraries(${name} UnitTest++)
|
target_link_libraries(${name}
|
||||||
|
UnitTest++
|
||||||
|
GSL
|
||||||
|
gsl_tests_config
|
||||||
|
)
|
||||||
add_test(
|
add_test(
|
||||||
${name}
|
${name}
|
||||||
${name}
|
${name}
|
||||||
)
|
)
|
||||||
|
# group all tests under GSL_tests
|
||||||
|
set_property(TARGET ${name} PROPERTY FOLDER "GSL_tests")
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
add_gsl_test(span_tests)
|
add_gsl_test(span_tests)
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <UnitTest++/UnitTest++.h>
|
#include <UnitTest++/UnitTest++.h>
|
||||||
|
|
||||||
#include <gsl/gsl_algorithm>
|
#include <gsl/gsl_algorithm>
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <UnitTest++/UnitTest++.h>
|
#include <UnitTest++/UnitTest++.h>
|
||||||
|
|
||||||
#include <gsl/gsl>
|
#include <gsl/gsl>
|
||||||
|
|
||||||
using namespace gsl;
|
using namespace gsl;
|
||||||
@ -47,7 +48,4 @@ SUITE(assertion_tests)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int, const char *[])
|
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||||
{
|
|
||||||
return UnitTest::RunAllTests();
|
|
||||||
}
|
|
||||||
|
@ -15,9 +15,11 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <UnitTest++/UnitTest++.h>
|
#include <UnitTest++/UnitTest++.h>
|
||||||
|
|
||||||
#include <gsl/gsl>
|
#include <gsl/gsl>
|
||||||
#include <vector>
|
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
using gsl::fail_fast;
|
using gsl::fail_fast;
|
||||||
|
|
||||||
@ -110,7 +112,4 @@ static constexpr bool test_constexpr()
|
|||||||
static_assert(test_constexpr(), "FAIL");
|
static_assert(test_constexpr(), "FAIL");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int main()
|
int main() { return UnitTest::RunAllTests(); }
|
||||||
{
|
|
||||||
return UnitTest::RunAllTests();
|
|
||||||
}
|
|
||||||
|
@ -15,7 +15,9 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <UnitTest++/UnitTest++.h>
|
#include <UnitTest++/UnitTest++.h>
|
||||||
|
|
||||||
#include <gsl/multi_span>
|
#include <gsl/multi_span>
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -30,11 +32,9 @@ SUITE(bounds_test)
|
|||||||
{
|
{
|
||||||
TEST(basic_bounds)
|
TEST(basic_bounds)
|
||||||
{
|
{
|
||||||
for (auto point : static_bounds<dynamic_range, 3, 4 > { 2 })
|
for (auto point : static_bounds<dynamic_range, 3, 4>{2}) {
|
||||||
{
|
|
||||||
for (decltype(point)::size_type j = 0;
|
for (decltype(point)::size_type j = 0;
|
||||||
j < static_cast<decltype(point)::size_type>(decltype(point)::rank);
|
j < static_cast<decltype(point)::size_type>(decltype(point)::rank); j++)
|
||||||
j++)
|
|
||||||
{
|
{
|
||||||
use(j);
|
use(j);
|
||||||
use(point[static_cast<std::size_t>(j)]);
|
use(point[static_cast<std::size_t>(j)]);
|
||||||
@ -97,7 +97,4 @@ SUITE(bounds_test)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int, const char *[])
|
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||||
{
|
|
||||||
return UnitTest::RunAllTests();
|
|
||||||
}
|
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <UnitTest++/UnitTest++.h>
|
#include <UnitTest++/UnitTest++.h>
|
||||||
|
|
||||||
#include <gsl/gsl_byte>
|
#include <gsl/gsl_byte>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -129,7 +130,6 @@ SUITE(byte_tests)
|
|||||||
CHECK(res == i);
|
CHECK(res == i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <UnitTest++/UnitTest++.h>
|
#include <UnitTest++/UnitTest++.h>
|
||||||
|
|
||||||
#include <gsl/multi_span>
|
#include <gsl/multi_span>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -668,7 +669,9 @@ SUITE(multi_span_tests)
|
|||||||
CHECK(s2.empty());
|
CHECK(s2.empty());
|
||||||
|
|
||||||
auto get_temp_span = [&]() -> multi_span<int> { return {&arr[1], 2}; };
|
auto get_temp_span = [&]() -> multi_span<int> { return {&arr[1], 2}; };
|
||||||
auto use_span = [&](multi_span<const int> s) { CHECK(s.length() == 2 && s.data() == &arr[1]); };
|
auto use_span = [&](multi_span<const int> s) {
|
||||||
|
CHECK(s.length() == 2 && s.data() == &arr[1]);
|
||||||
|
};
|
||||||
use_span(get_temp_span());
|
use_span(get_temp_span());
|
||||||
|
|
||||||
s1 = get_temp_span();
|
s1 = get_temp_span();
|
||||||
@ -1283,10 +1286,7 @@ SUITE(multi_span_tests)
|
|||||||
delete[] arr;
|
delete[] arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(index_constructors)
|
TEST(index_constructors){{// components of the same type
|
||||||
{
|
|
||||||
{
|
|
||||||
// components of the same type
|
|
||||||
index<3> i1(0, 1, 2);
|
index<3> i1(0, 1, 2);
|
||||||
CHECK(i1[0] == 0);
|
CHECK(i1[0] == 0);
|
||||||
|
|
||||||
@ -1515,7 +1515,8 @@ SUITE(multi_span_tests)
|
|||||||
}
|
}
|
||||||
// both bounds are dynamic
|
// both bounds are dynamic
|
||||||
{
|
{
|
||||||
multi_span<int, dynamic_range, dynamic_range> av2 = as_multi_span(av, dim(height), dim(width));
|
multi_span<int, dynamic_range, dynamic_range> av2 =
|
||||||
|
as_multi_span(av, dim(height), dim(width));
|
||||||
iterate_second_column(av2);
|
iterate_second_column(av2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1536,7 +1537,8 @@ SUITE(multi_span_tests)
|
|||||||
|
|
||||||
CHECK_THROW(av1[10][3][4], fail_fast);
|
CHECK_THROW(av1[10][3][4], fail_fast);
|
||||||
|
|
||||||
multi_span<const double, dynamic_range, 6, 4> av2 = as_multi_span(av1, dim(5), dim<6>(), dim<4>());
|
multi_span<const double, dynamic_range, 6, 4> av2 =
|
||||||
|
as_multi_span(av1, dim(5), dim<6>(), dim<4>());
|
||||||
(void) av2;
|
(void) av2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,16 +15,24 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <UnitTest++/UnitTest++.h>
|
#include <UnitTest++/UnitTest++.h>
|
||||||
|
|
||||||
#include <gsl/gsl>
|
#include <gsl/gsl>
|
||||||
#include <vector>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
using namespace gsl;
|
using namespace gsl;
|
||||||
|
|
||||||
struct MyBase {};
|
struct MyBase
|
||||||
struct MyDerived : public MyBase {};
|
{
|
||||||
struct Unrelated {};
|
};
|
||||||
|
struct MyDerived : public MyBase
|
||||||
|
{
|
||||||
|
};
|
||||||
|
struct Unrelated
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
// stand-in for a user-defined ref-counted class
|
// stand-in for a user-defined ref-counted class
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -48,48 +56,49 @@ struct CustomPtr
|
|||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
std::string operator==(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
std::string operator==(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||||
{
|
{
|
||||||
return reinterpret_cast<const void*>(lhs.p_) == reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
|
return reinterpret_cast<const void*>(lhs.p_) == reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||||
|
: "false";
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
std::string operator!=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
std::string operator!=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||||
{
|
{
|
||||||
return reinterpret_cast<const void*>(lhs.p_) != reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
|
return reinterpret_cast<const void*>(lhs.p_) != reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||||
|
: "false";
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
std::string operator<(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
std::string operator<(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||||
{
|
{
|
||||||
return reinterpret_cast<const void*>(lhs.p_) < reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
|
return reinterpret_cast<const void*>(lhs.p_) < reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||||
|
: "false";
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
std::string operator>(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
std::string operator>(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||||
{
|
{
|
||||||
return reinterpret_cast<const void*>(lhs.p_) > reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
|
return reinterpret_cast<const void*>(lhs.p_) > reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||||
|
: "false";
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
std::string operator<=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
std::string operator<=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||||
{
|
{
|
||||||
return reinterpret_cast<const void*>(lhs.p_) <= reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
|
return reinterpret_cast<const void*>(lhs.p_) <= reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||||
|
: "false";
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
std::string operator>=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
std::string operator>=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||||
{
|
{
|
||||||
return reinterpret_cast<const void*>(lhs.p_) >= reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
|
return reinterpret_cast<const void*>(lhs.p_) >= reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||||
|
: "false";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SUITE(NotNullTests)
|
SUITE(NotNullTests)
|
||||||
{
|
{
|
||||||
|
|
||||||
bool helper(not_null<int*> p)
|
bool helper(not_null<int*> p) { return *p == 12; }
|
||||||
{
|
|
||||||
return *p == 12;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(TestNotNullConstructors)
|
TEST(TestNotNullConstructors)
|
||||||
{
|
{
|
||||||
@ -110,7 +119,8 @@ SUITE(NotNullTests)
|
|||||||
not_null<int*> p(rp);
|
not_null<int*> p(rp);
|
||||||
CHECK(p.get() == &i);
|
CHECK(p.get() == &i);
|
||||||
|
|
||||||
not_null<std::shared_ptr<int>> x(std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
|
not_null<std::shared_ptr<int>> x(
|
||||||
|
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(TestNotNullCasting)
|
TEST(TestNotNullCasting)
|
||||||
@ -242,7 +252,4 @@ SUITE(NotNullTests)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int, const char *[])
|
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||||
{
|
|
||||||
return UnitTest::RunAllTests();
|
|
||||||
}
|
|
||||||
|
@ -15,17 +15,16 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <UnitTest++/UnitTest++.h>
|
#include <UnitTest++/UnitTest++.h>
|
||||||
|
|
||||||
#include <gsl/gsl>
|
#include <gsl/gsl>
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
using namespace gsl;
|
using namespace gsl;
|
||||||
|
|
||||||
SUITE(owner_tests)
|
SUITE(owner_tests)
|
||||||
{
|
{
|
||||||
void f(int* i)
|
void f(int* i) { *i += 1; }
|
||||||
{
|
|
||||||
*i += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(basic_test)
|
TEST(basic_test)
|
||||||
{
|
{
|
||||||
@ -37,7 +36,4 @@ SUITE(owner_tests)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int, const char *[])
|
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||||
{
|
|
||||||
return UnitTest::RunAllTests();
|
|
||||||
}
|
|
||||||
|
@ -15,15 +15,16 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <UnitTest++/UnitTest++.h>
|
#include <UnitTest++/UnitTest++.h>
|
||||||
|
|
||||||
#include <gsl/span>
|
#include <gsl/span>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <regex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <regex>
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace gsl;
|
using namespace gsl;
|
||||||
@ -148,7 +149,9 @@ SUITE(span_tests)
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto workaround_macro = []() { span<int, 1> s{ nullptr, static_cast<span<int>::index_type>(0) }; };
|
auto workaround_macro = []() {
|
||||||
|
span<int, 1> s{nullptr, static_cast<span<int>::index_type>(0)};
|
||||||
|
};
|
||||||
CHECK_THROW(workaround_macro(), fail_fast);
|
CHECK_THROW(workaround_macro(), fail_fast);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -588,8 +591,7 @@ SUITE(span_tests)
|
|||||||
{
|
{
|
||||||
auto arr = std::make_unique<int[]>(4);
|
auto arr = std::make_unique<int[]>(4);
|
||||||
|
|
||||||
for (auto i = 0U; i < 4; i++)
|
for (auto i = 0U; i < 4; i++) arr[i] = gsl::narrow_cast<int>(i + 1);
|
||||||
arr[i] = gsl::narrow_cast<int>(i + 1);
|
|
||||||
|
|
||||||
{
|
{
|
||||||
span<int> s{arr, 4};
|
span<int> s{arr, 4};
|
||||||
@ -744,10 +746,7 @@ SUITE(span_tests)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(from_convertible_span_constructor)
|
TEST(from_convertible_span_constructor){{span<DerivedClass> avd;
|
||||||
{
|
|
||||||
{
|
|
||||||
span<DerivedClass> avd;
|
|
||||||
span<const DerivedClass> avcd = avd;
|
span<const DerivedClass> avcd = avd;
|
||||||
static_cast<void>(avcd);
|
static_cast<void>(avcd);
|
||||||
}
|
}
|
||||||
@ -1108,8 +1107,7 @@ SUITE(span_tests)
|
|||||||
|
|
||||||
it = first;
|
it = first;
|
||||||
CHECK(it == first);
|
CHECK(it == first);
|
||||||
while (it != s.end())
|
while (it != s.end()) {
|
||||||
{
|
|
||||||
*it = 5;
|
*it = 5;
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
@ -1117,8 +1115,7 @@ SUITE(span_tests)
|
|||||||
CHECK(it == beyond);
|
CHECK(it == beyond);
|
||||||
CHECK(it - beyond == 0);
|
CHECK(it - beyond == 0);
|
||||||
|
|
||||||
for (const auto& n : s)
|
for (const auto& n : s) {
|
||||||
{
|
|
||||||
CHECK(n == 5);
|
CHECK(n == 5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1164,8 +1161,7 @@ SUITE(span_tests)
|
|||||||
int last = 0;
|
int last = 0;
|
||||||
it = first;
|
it = first;
|
||||||
CHECK(it == first);
|
CHECK(it == first);
|
||||||
while (it != s.cend())
|
while (it != s.cend()) {
|
||||||
{
|
|
||||||
CHECK(*it == last + 1);
|
CHECK(*it == last + 1);
|
||||||
|
|
||||||
last = *it;
|
last = *it;
|
||||||
@ -1205,8 +1201,7 @@ SUITE(span_tests)
|
|||||||
|
|
||||||
it = first;
|
it = first;
|
||||||
CHECK(it == first);
|
CHECK(it == first);
|
||||||
while (it != s.rend())
|
while (it != s.rend()) {
|
||||||
{
|
|
||||||
*it = 5;
|
*it = 5;
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
@ -1214,8 +1209,7 @@ SUITE(span_tests)
|
|||||||
CHECK(it == beyond);
|
CHECK(it == beyond);
|
||||||
CHECK(it - beyond == 0);
|
CHECK(it - beyond == 0);
|
||||||
|
|
||||||
for (const auto& n : s)
|
for (const auto& n : s) {
|
||||||
{
|
|
||||||
CHECK(n == 5);
|
CHECK(n == 5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1248,8 +1242,7 @@ SUITE(span_tests)
|
|||||||
it = first;
|
it = first;
|
||||||
CHECK(it == first);
|
CHECK(it == first);
|
||||||
int last = 5;
|
int last = 5;
|
||||||
while (it != s.crend())
|
while (it != s.crend()) {
|
||||||
{
|
|
||||||
CHECK(*it == last - 1);
|
CHECK(*it == last - 1);
|
||||||
last = *it;
|
last = *it;
|
||||||
|
|
||||||
|
@ -15,22 +15,27 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <UnitTest++/UnitTest++.h>
|
#include <UnitTest++/UnitTest++.h>
|
||||||
|
|
||||||
#include <gsl/multi_span>
|
#include <gsl/multi_span>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <list>
|
||||||
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <list>
|
|
||||||
#include <iostream>
|
|
||||||
#include <memory>
|
|
||||||
#include <map>
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace gsl;
|
using namespace gsl;
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
struct BaseClass {};
|
struct BaseClass
|
||||||
struct DerivedClass : BaseClass {};
|
{
|
||||||
|
};
|
||||||
|
struct DerivedClass : BaseClass
|
||||||
|
{
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
SUITE(strided_span_tests)
|
SUITE(strided_span_tests)
|
||||||
@ -75,7 +80,6 @@ SUITE(strided_span_tests)
|
|||||||
CHECK(sav1.bounds().stride() == 1);
|
CHECK(sav1.bounds().stride() == 1);
|
||||||
CHECK(sav1[0] == 1 && sav1[8] == 9);
|
CHECK(sav1[0] == 1 && sav1[8] == 9);
|
||||||
|
|
||||||
|
|
||||||
strided_span<const int, 1> sav2{carr, {{4}, {2}}}; // const T -> const T
|
strided_span<const int, 1> sav2{carr, {{4}, {2}}}; // const T -> const T
|
||||||
CHECK(sav2.bounds().index_bounds() == index<1>{4});
|
CHECK(sav2.bounds().index_bounds() == index<1>{4});
|
||||||
CHECK(sav2.bounds().strides() == index<1>{2});
|
CHECK(sav2.bounds().strides() == index<1>{2});
|
||||||
@ -102,9 +106,11 @@ SUITE(strided_span_tests)
|
|||||||
|
|
||||||
#if _MSC_VER > 1800
|
#if _MSC_VER > 1800
|
||||||
// strided_span<const int, 1> sav_c{ {src}, {2, 1} };
|
// strided_span<const int, 1> sav_c{ {src}, {2, 1} };
|
||||||
strided_span<const int, 1> sav_c{ multi_span<const int>{src}, strided_bounds<1>{2, 1} };
|
strided_span<const int, 1> sav_c{multi_span<const int>{src},
|
||||||
|
strided_bounds<1>{2, 1}};
|
||||||
#else
|
#else
|
||||||
strided_span<const int, 1> sav_c{ multi_span<const int>{src}, strided_bounds<1>{2, 1} };
|
strided_span<const int, 1> sav_c{multi_span<const int>{src},
|
||||||
|
strided_bounds<1>{2, 1}};
|
||||||
#endif
|
#endif
|
||||||
CHECK(sav_c.bounds().index_bounds() == index<1>{2});
|
CHECK(sav_c.bounds().index_bounds() == index<1>{2});
|
||||||
CHECK(sav_c.bounds().strides() == index<1>{1});
|
CHECK(sav_c.bounds().strides() == index<1>{1});
|
||||||
@ -113,7 +119,8 @@ SUITE(strided_span_tests)
|
|||||||
#if _MSC_VER > 1800
|
#if _MSC_VER > 1800
|
||||||
strided_span<volatile int, 1> sav_v{src, {2, 1}};
|
strided_span<volatile int, 1> sav_v{src, {2, 1}};
|
||||||
#else
|
#else
|
||||||
strided_span<volatile int, 1> sav_v{ multi_span<volatile int>{src}, strided_bounds<1>{2, 1} };
|
strided_span<volatile int, 1> sav_v{multi_span<volatile int>{src},
|
||||||
|
strided_bounds<1>{2, 1}};
|
||||||
#endif
|
#endif
|
||||||
CHECK(sav_v.bounds().index_bounds() == index<1>{2});
|
CHECK(sav_v.bounds().index_bounds() == index<1>{2});
|
||||||
CHECK(sav_v.bounds().strides() == index<1>{1});
|
CHECK(sav_v.bounds().strides() == index<1>{1});
|
||||||
@ -122,7 +129,8 @@ SUITE(strided_span_tests)
|
|||||||
#if _MSC_VER > 1800
|
#if _MSC_VER > 1800
|
||||||
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
|
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
|
||||||
#else
|
#else
|
||||||
strided_span<const volatile int, 1> sav_cv{ multi_span<const volatile int>{src}, strided_bounds<1>{2, 1} };
|
strided_span<const volatile int, 1> sav_cv{multi_span<const volatile int>{src},
|
||||||
|
strided_bounds<1>{2, 1}};
|
||||||
#endif
|
#endif
|
||||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{2});
|
CHECK(sav_cv.bounds().index_bounds() == index<1>{2});
|
||||||
CHECK(sav_cv.bounds().strides() == index<1>{1});
|
CHECK(sav_cv.bounds().strides() == index<1>{1});
|
||||||
@ -141,7 +149,8 @@ SUITE(strided_span_tests)
|
|||||||
#if _MSC_VER > 1800
|
#if _MSC_VER > 1800
|
||||||
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
|
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
|
||||||
#else
|
#else
|
||||||
strided_span<const volatile int, 1> sav_cv{ multi_span<const volatile int>{src}, strided_bounds<1>{2, 1} };
|
strided_span<const volatile int, 1> sav_cv{multi_span<const volatile int>{src},
|
||||||
|
strided_bounds<1>{2, 1}};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{2});
|
CHECK(sav_cv.bounds().index_bounds() == index<1>{2});
|
||||||
@ -161,7 +170,8 @@ SUITE(strided_span_tests)
|
|||||||
#if _MSC_VER > 1800
|
#if _MSC_VER > 1800
|
||||||
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
|
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
|
||||||
#else
|
#else
|
||||||
strided_span<const volatile int, 1> sav_cv{ multi_span<const volatile int>{src}, strided_bounds<1>{2, 1} };
|
strided_span<const volatile int, 1> sav_cv{multi_span<const volatile int>{src},
|
||||||
|
strided_bounds<1>{2, 1}};
|
||||||
#endif
|
#endif
|
||||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{2});
|
CHECK(sav_cv.bounds().index_bounds() == index<1>{2});
|
||||||
CHECK(sav_cv.bounds().strides() == index<1>{1});
|
CHECK(sav_cv.bounds().strides() == index<1>{1});
|
||||||
@ -187,7 +197,9 @@ SUITE(strided_span_tests)
|
|||||||
multi_span<const int, 2> av2{av};
|
multi_span<const int, 2> av2{av};
|
||||||
CHECK(av2[1] == 5);
|
CHECK(av2[1] == 5);
|
||||||
|
|
||||||
static_assert(std::is_convertible<const multi_span<int, 2>, multi_span<const int, 2>>::value, "ctor is not implicit!");
|
static_assert(
|
||||||
|
std::is_convertible<const multi_span<int, 2>, multi_span<const int, 2>>::value,
|
||||||
|
"ctor is not implicit!");
|
||||||
|
|
||||||
const strided_span<int, 1> src{arr, {2, 1}};
|
const strided_span<int, 1> src{arr, {2, 1}};
|
||||||
strided_span<const int, 1> sav{src};
|
strided_span<const int, 1> sav{src};
|
||||||
@ -195,7 +207,9 @@ SUITE(strided_span_tests)
|
|||||||
CHECK(sav.bounds().stride() == 1);
|
CHECK(sav.bounds().stride() == 1);
|
||||||
CHECK(sav[1] == 5);
|
CHECK(sav[1] == 5);
|
||||||
|
|
||||||
static_assert(std::is_convertible<const strided_span<int, 1>, strided_span<const int, 1>>::value, "ctor is not implicit!");
|
static_assert(
|
||||||
|
std::is_convertible<const strided_span<int, 1>, strided_span<const int, 1>>::value,
|
||||||
|
"ctor is not implicit!");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check copy constructor
|
// Check copy constructor
|
||||||
@ -258,13 +272,15 @@ SUITE(strided_span_tests)
|
|||||||
{
|
{
|
||||||
std::vector<int> data(5 * 10);
|
std::vector<int> data(5 * 10);
|
||||||
std::iota(begin(data), end(data), 0);
|
std::iota(begin(data), end(data), 0);
|
||||||
const multi_span<int, 5, 10> src = as_multi_span(multi_span<int>{data}, dim<5>(), dim<10>());
|
const multi_span<int, 5, 10> src =
|
||||||
|
as_multi_span(multi_span<int>{data}, dim<5>(), dim<10>());
|
||||||
|
|
||||||
const strided_span<int, 2> sav{src, {{5, 10}, {10, 1}}};
|
const strided_span<int, 2> sav{src, {{5, 10}, {10, 1}}};
|
||||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||||
const strided_span<const int, 2> csav{{src}, {{5, 10}, {10, 1}}};
|
const strided_span<const int, 2> csav{{src}, {{5, 10}, {10, 1}}};
|
||||||
#endif
|
#endif
|
||||||
const strided_span<const int, 2> csav{ multi_span<const int, 5, 10>{ src }, { { 5, 10 },{ 10, 1 } } };
|
const strided_span<const int, 2> csav{multi_span<const int, 5, 10>{src},
|
||||||
|
{{5, 10}, {10, 1}}};
|
||||||
|
|
||||||
strided_span<int, 1> sav_sl = sav[2];
|
strided_span<int, 1> sav_sl = sav[2];
|
||||||
CHECK(sav_sl[0] == 20);
|
CHECK(sav_sl[0] == 20);
|
||||||
@ -284,11 +300,7 @@ SUITE(strided_span_tests)
|
|||||||
// use cases, such as column-major multidimensional array
|
// use cases, such as column-major multidimensional array
|
||||||
// (aka. "FORTRAN" layout).
|
// (aka. "FORTRAN" layout).
|
||||||
|
|
||||||
int cm_array[3 * 5] = {
|
int cm_array[3 * 5] = {1, 4, 7, 10, 13, 2, 5, 8, 11, 14, 3, 6, 9, 12, 15};
|
||||||
1, 4, 7, 10, 13,
|
|
||||||
2, 5, 8, 11, 14,
|
|
||||||
3, 6, 9, 12, 15
|
|
||||||
};
|
|
||||||
strided_span<int, 2> cm_sav{cm_array, {{5, 3}, {1, 5}}};
|
strided_span<int, 2> cm_sav{cm_array, {{5, 3}, {1, 5}}};
|
||||||
|
|
||||||
// Accessing elements
|
// Accessing elements
|
||||||
@ -415,7 +427,8 @@ SUITE(strided_span_tests)
|
|||||||
strided_span<int, 1> sav4{av, {1, 1, 1}};
|
strided_span<int, 1> sav4{av, {1, 1, 1}};
|
||||||
strided_span<int, 2> sav5{av.as_multi_span(dim<2>(), dim<2>()), {1}};
|
strided_span<int, 2> sav5{av.as_multi_span(dim<2>(), dim<2>()), {1}};
|
||||||
strided_span<int, 2> sav6{av.as_multi_span(dim<2>(), dim<2>()), {1, 1, 1}};
|
strided_span<int, 2> sav6{av.as_multi_span(dim<2>(), dim<2>()), {1, 1, 1}};
|
||||||
strided_span<int, 2> sav7{ av.as_multi_span(dim<2>(), dim<2>()), { { 1,1 },{ 1,1 },{ 1,1 } } };
|
strided_span<int, 2> sav7{av.as_multi_span(dim<2>(), dim<2>()),
|
||||||
|
{{1, 1}, {1, 1}, {1, 1}}};
|
||||||
|
|
||||||
index<1> index{0, 1};
|
index<1> index{0, 1};
|
||||||
strided_span<int, 1> sav8{arr, {1, {1, 1}}};
|
strided_span<int, 1> sav8{arr, {1, {1, 1}}};
|
||||||
@ -463,7 +476,8 @@ SUITE(strided_span_tests)
|
|||||||
// retype strided array with regular strides - from multi_span
|
// retype strided array with regular strides - from multi_span
|
||||||
{
|
{
|
||||||
strided_bounds<2> bounds{{2, bytes.size() / 4}, {bytes.size() / 2, 1}};
|
strided_bounds<2> bounds{{2, bytes.size() / 4}, {bytes.size() / 2, 1}};
|
||||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
multi_span<const byte, 2, dynamic_range> bytes2 =
|
||||||
|
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||||
strided_span<const byte, 2> sav2{bytes2, bounds};
|
strided_span<const byte, 2> sav2{bytes2, bounds};
|
||||||
strided_span<int, 2> sav3 = sav2.as_strided_span<int>();
|
strided_span<int, 2> sav3 = sav2.as_strided_span<int>();
|
||||||
CHECK(sav3[0][0] == 0);
|
CHECK(sav3[0][0] == 0);
|
||||||
@ -475,7 +489,8 @@ SUITE(strided_span_tests)
|
|||||||
// retype strided array with not enough elements - last dimension of the array is too small
|
// retype strided array with not enough elements - last dimension of the array is too small
|
||||||
{
|
{
|
||||||
strided_bounds<2> bounds{{4, 2}, {4, 1}};
|
strided_bounds<2> bounds{{4, 2}, {4, 1}};
|
||||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
multi_span<const byte, 2, dynamic_range> bytes2 =
|
||||||
|
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||||
strided_span<const byte, 2> sav2{bytes2, bounds};
|
strided_span<const byte, 2> sav2{bytes2, bounds};
|
||||||
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
||||||
}
|
}
|
||||||
@ -483,23 +498,28 @@ SUITE(strided_span_tests)
|
|||||||
// retype strided array with not enough elements - strides are too small
|
// retype strided array with not enough elements - strides are too small
|
||||||
{
|
{
|
||||||
strided_bounds<2> bounds{{4, 2}, {2, 1}};
|
strided_bounds<2> bounds{{4, 2}, {2, 1}};
|
||||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
multi_span<const byte, 2, dynamic_range> bytes2 =
|
||||||
|
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||||
strided_span<const byte, 2> sav2{bytes2, bounds};
|
strided_span<const byte, 2> sav2{bytes2, bounds};
|
||||||
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
||||||
}
|
}
|
||||||
|
|
||||||
// retype strided array with not enough elements - last dimension does not divide by the new typesize
|
// retype strided array with not enough elements - last dimension does not divide by the new
|
||||||
|
// typesize
|
||||||
{
|
{
|
||||||
strided_bounds<2> bounds{{2, 6}, {4, 1}};
|
strided_bounds<2> bounds{{2, 6}, {4, 1}};
|
||||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
multi_span<const byte, 2, dynamic_range> bytes2 =
|
||||||
|
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||||
strided_span<const byte, 2> sav2{bytes2, bounds};
|
strided_span<const byte, 2> sav2{bytes2, bounds};
|
||||||
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
||||||
}
|
}
|
||||||
|
|
||||||
// retype strided array with not enough elements - strides does not divide by the new typesize
|
// retype strided array with not enough elements - strides does not divide by the new
|
||||||
|
// typesize
|
||||||
{
|
{
|
||||||
strided_bounds<2> bounds{{2, 1}, {6, 1}};
|
strided_bounds<2> bounds{{2, 1}, {6, 1}};
|
||||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
multi_span<const byte, 2, dynamic_range> bytes2 =
|
||||||
|
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||||
strided_span<const byte, 2> sav2{bytes2, bounds};
|
strided_span<const byte, 2> sav2{bytes2, bounds};
|
||||||
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
||||||
}
|
}
|
||||||
@ -530,8 +550,7 @@ SUITE(strided_span_tests)
|
|||||||
CHECK_THROW(empty_sav.begin()[0], fail_fast);
|
CHECK_THROW(empty_sav.begin()[0], fail_fast);
|
||||||
CHECK_THROW(empty_sav.cbegin()[0], fail_fast);
|
CHECK_THROW(empty_sav.cbegin()[0], fail_fast);
|
||||||
|
|
||||||
for (const auto& v : empty_sav)
|
for (const auto& v : empty_sav) {
|
||||||
{
|
|
||||||
(void) v;
|
(void) v;
|
||||||
CHECK(false);
|
CHECK(false);
|
||||||
}
|
}
|
||||||
@ -545,8 +564,7 @@ SUITE(strided_span_tests)
|
|||||||
CHECK_THROW(empty_sav.begin()[0], fail_fast);
|
CHECK_THROW(empty_sav.begin()[0], fail_fast);
|
||||||
CHECK_THROW(empty_sav.cbegin()[0], fail_fast);
|
CHECK_THROW(empty_sav.cbegin()[0], fail_fast);
|
||||||
|
|
||||||
for (const auto& v : empty_sav)
|
for (const auto& v : empty_sav) {
|
||||||
{
|
|
||||||
(void) v;
|
(void) v;
|
||||||
CHECK(false);
|
CHECK(false);
|
||||||
}
|
}
|
||||||
@ -567,14 +585,12 @@ SUITE(strided_span_tests)
|
|||||||
|
|
||||||
CHECK(strided.size() == length);
|
CHECK(strided.size() == length);
|
||||||
CHECK(strided.bounds().index_bounds()[0] == length);
|
CHECK(strided.bounds().index_bounds()[0] == length);
|
||||||
for (auto i = 0; i < strided.size(); ++i)
|
for (auto i = 0; i < strided.size(); ++i) {
|
||||||
{
|
|
||||||
CHECK(strided[i] == av[2 * i + 1]);
|
CHECK(strided[i] == av[2 * i + 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
for (auto num : strided)
|
for (auto num : strided) {
|
||||||
{
|
|
||||||
CHECK(num == av[2 * idx + 1]);
|
CHECK(num == av[2 * idx + 1]);
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
@ -600,8 +616,7 @@ SUITE(strided_span_tests)
|
|||||||
TEST(dynamic_strided_span_section_iteration)
|
TEST(dynamic_strided_span_section_iteration)
|
||||||
{
|
{
|
||||||
auto arr = new int[8];
|
auto arr = new int[8];
|
||||||
for (int i = 0; i < 4; ++i)
|
for (int i = 0; i < 4; ++i) {
|
||||||
{
|
|
||||||
arr[2 * i] = 4 + i;
|
arr[2 * i] = 4 + i;
|
||||||
arr[2 * i + 1] = i;
|
arr[2 * i + 1] = i;
|
||||||
}
|
}
|
||||||
@ -617,26 +632,22 @@ SUITE(strided_span_tests)
|
|||||||
const int expected[6] = {2, 3, 10, 11, 18, 19};
|
const int expected[6] = {2, 3, 10, 11, 18, 19};
|
||||||
auto section = av.section({0, 1, 0}, {3, 1, 2});
|
auto section = av.section({0, 1, 0}, {3, 1, 2});
|
||||||
|
|
||||||
for (auto i = 0; i < section.extent<0>(); ++i)
|
for (auto i = 0; i < section.extent<0>(); ++i) {
|
||||||
{
|
|
||||||
for (auto j = 0; j < section.extent<1>(); ++j)
|
for (auto j = 0; j < section.extent<1>(); ++j)
|
||||||
for (auto k = 0; k < section.extent<2>(); ++k)
|
for (auto k = 0; k < section.extent<2>(); ++k) {
|
||||||
{
|
|
||||||
auto idx = index<3>{i, j, k}; // avoid braces in the CHECK macro
|
auto idx = index<3>{i, j, k}; // avoid braces in the CHECK macro
|
||||||
CHECK(section[idx] == expected[2 * i + 2 * j + k]);
|
CHECK(section[idx] == expected[2 * i + 2 * j + k]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto i = 0; i < section.extent<0>(); ++i)
|
for (auto i = 0; i < section.extent<0>(); ++i) {
|
||||||
{
|
|
||||||
for (auto j = 0; j < section.extent<1>(); ++j)
|
for (auto j = 0; j < section.extent<1>(); ++j)
|
||||||
for (auto k = 0; k < section.extent<2>(); ++k)
|
for (auto k = 0; k < section.extent<2>(); ++k)
|
||||||
CHECK(section[i][j][k] == expected[2 * i + 2 * j + k]);
|
CHECK(section[i][j][k] == expected[2 * i + 2 * j + k]);
|
||||||
}
|
}
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (const auto num : section)
|
for (const auto num : section) {
|
||||||
{
|
|
||||||
CHECK(num == expected[i]);
|
CHECK(num == expected[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
@ -645,11 +656,9 @@ SUITE(strided_span_tests)
|
|||||||
TEST(strided_span_section_iteration_3d)
|
TEST(strided_span_section_iteration_3d)
|
||||||
{
|
{
|
||||||
int arr[3][4][2]{};
|
int arr[3][4][2]{};
|
||||||
for (auto i = 0; i < 3; ++i)
|
for (auto i = 0; i < 3; ++i) {
|
||||||
{
|
|
||||||
for (auto j = 0; j < 4; ++j)
|
for (auto j = 0; j < 4; ++j)
|
||||||
for (auto k = 0; k < 2; ++k)
|
for (auto k = 0; k < 2; ++k) arr[i][j][k] = 8 * i + 2 * j + k;
|
||||||
arr[i][j][k] = 8 * i + 2 * j + k;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -664,8 +673,7 @@ SUITE(strided_span_tests)
|
|||||||
const auto size = height * width;
|
const auto size = height * width;
|
||||||
|
|
||||||
auto arr = new int[static_cast<std::size_t>(size)];
|
auto arr = new int[static_cast<std::size_t>(size)];
|
||||||
for (auto i = 0; i < size; ++i)
|
for (auto i = 0; i < size; ++i) {
|
||||||
{
|
|
||||||
arr[i] = i;
|
arr[i] = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -695,7 +703,12 @@ SUITE(strided_span_tests)
|
|||||||
{
|
{
|
||||||
// get an multi_span of 'c' values from the list of X's
|
// get an multi_span of 'c' values from the list of X's
|
||||||
|
|
||||||
struct X { int a; int b; int c; };
|
struct X
|
||||||
|
{
|
||||||
|
int a;
|
||||||
|
int b;
|
||||||
|
int c;
|
||||||
|
};
|
||||||
|
|
||||||
X arr[4] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}};
|
X arr[4] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}};
|
||||||
|
|
||||||
@ -710,7 +723,9 @@ SUITE(strided_span_tests)
|
|||||||
CHECK(av.bounds().index_bounds()[1] == 12);
|
CHECK(av.bounds().index_bounds()[1] == 12);
|
||||||
|
|
||||||
// get the last 4 columns
|
// get the last 4 columns
|
||||||
auto section = av.section({0, 2 * s}, {4, s}); // { { arr[0].c[0], arr[0].c[1], arr[0].c[2], arr[0].c[3] } , { arr[1].c[0], ... } , ... }
|
auto section = av.section({0, 2 * s}, {4, s}); // { { arr[0].c[0], arr[0].c[1], arr[0].c[2],
|
||||||
|
// arr[0].c[3] } , { arr[1].c[0], ... } , ...
|
||||||
|
// }
|
||||||
|
|
||||||
// convert to array 4x1 array of integers
|
// convert to array 4x1 array of integers
|
||||||
auto cs = section.as_strided_span<int>(); // { { arr[0].c }, {arr[1].c } , ... }
|
auto cs = section.as_strided_span<int>(); // { { arr[0].c }, {arr[1].c } , ... }
|
||||||
@ -721,8 +736,7 @@ SUITE(strided_span_tests)
|
|||||||
// transpose to 1x4 array
|
// transpose to 1x4 array
|
||||||
strided_bounds<2> reverse_bounds{
|
strided_bounds<2> reverse_bounds{
|
||||||
{cs.bounds().index_bounds()[1], cs.bounds().index_bounds()[0]},
|
{cs.bounds().index_bounds()[1], cs.bounds().index_bounds()[0]},
|
||||||
{cs.bounds().strides()[1], cs.bounds().strides()[0]}
|
{cs.bounds().strides()[1], cs.bounds().strides()[0]}};
|
||||||
};
|
|
||||||
|
|
||||||
strided_span<int, 2> transposed{cs.data(), cs.bounds().total_size(), reverse_bounds};
|
strided_span<int, 2> transposed{cs.data(), cs.bounds().total_size(), reverse_bounds};
|
||||||
|
|
||||||
@ -733,16 +747,11 @@ SUITE(strided_span_tests)
|
|||||||
CHECK_THROW(result.bounds().index_bounds()[1], fail_fast);
|
CHECK_THROW(result.bounds().index_bounds()[1], fail_fast);
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (auto& num : result)
|
for (auto& num : result) {
|
||||||
{
|
|
||||||
CHECK(num == arr[i].c);
|
CHECK(num == arr[i].c);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int, const char *[])
|
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||||
{
|
|
||||||
return UnitTest::RunAllTests();
|
|
||||||
}
|
|
||||||
|
@ -15,16 +15,17 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <UnitTest++/UnitTest++.h>
|
#include <UnitTest++/UnitTest++.h>
|
||||||
#include <cstdlib>
|
|
||||||
#include <gsl/string_span>
|
|
||||||
#include <gsl/gsl> //owner
|
#include <gsl/gsl> //owner
|
||||||
#include <vector>
|
#include <gsl/string_span>
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace gsl;
|
using namespace gsl;
|
||||||
|
|
||||||
|
|
||||||
SUITE(string_span_tests)
|
SUITE(string_span_tests)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -117,7 +118,8 @@ SUITE(string_span_tests)
|
|||||||
|
|
||||||
TEST(TestToBasicString)
|
TEST(TestToBasicString)
|
||||||
{
|
{
|
||||||
auto s = gsl::to_basic_string<char,std::char_traits<char>,::std::allocator<char>>(cstring_span<>{});
|
auto s = gsl::to_basic_string<char, std::char_traits<char>, ::std::allocator<char>>(
|
||||||
|
cstring_span<>{});
|
||||||
CHECK(s.length() == 0);
|
CHECK(s.length() == 0);
|
||||||
|
|
||||||
char stack_string[] = "Hello";
|
char stack_string[] = "Hello";
|
||||||
@ -223,7 +225,6 @@ SUITE(string_span_tests)
|
|||||||
CHECK(span == span);
|
CHECK(span == span);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
{
|
{
|
||||||
const char ar[] = {'H', 'e', 'l', 'l', 'o'};
|
const char ar[] = {'H', 'e', 'l', 'l', 'o'};
|
||||||
const char ar1[] = "Hello";
|
const char ar1[] = "Hello";
|
||||||
@ -403,7 +404,6 @@ SUITE(string_span_tests)
|
|||||||
std::string str = "Hello";
|
std::string str = "Hello";
|
||||||
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||||
|
|
||||||
|
|
||||||
// comparison to static array with no null termination
|
// comparison to static array with no null termination
|
||||||
CHECK(span <= string_span<>(ar));
|
CHECK(span <= string_span<>(ar));
|
||||||
CHECK(span < string_span<>(rarr));
|
CHECK(span < string_span<>(rarr));
|
||||||
@ -757,10 +757,15 @@ SUITE(string_span_tests)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
T create() { return T{}; }
|
T create()
|
||||||
|
{
|
||||||
|
return T{};
|
||||||
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
void use(basic_string_span<T, gsl::dynamic_extent>) {}
|
void use(basic_string_span<T, gsl::dynamic_extent>)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
TEST(MoveConstructors)
|
TEST(MoveConstructors)
|
||||||
{
|
{
|
||||||
@ -850,8 +855,7 @@ SUITE(string_span_tests)
|
|||||||
Expects(span.size() > 1);
|
Expects(span.size() > 1);
|
||||||
|
|
||||||
int last = 0;
|
int last = 0;
|
||||||
if (span.size() > 4)
|
if (span.size() > 4) {
|
||||||
{
|
|
||||||
span[0] = 't';
|
span[0] = 't';
|
||||||
span[1] = 'm';
|
span[1] = 'm';
|
||||||
span[2] = 'p';
|
span[2] = 'p';
|
||||||
@ -892,14 +896,12 @@ SUITE(string_span_tests)
|
|||||||
char buf[10];
|
char buf[10];
|
||||||
|
|
||||||
auto name = CreateTempName({buf, 10});
|
auto name = CreateTempName({buf, 10});
|
||||||
if (!name.empty())
|
if (!name.empty()) {
|
||||||
{
|
|
||||||
czstring<> str = name.assume_z();
|
czstring<> str = name.assume_z();
|
||||||
CHECK(strlen(str) == 3);
|
CHECK(strlen(str) == 3);
|
||||||
CHECK(*(str + 3) == '\0');
|
CHECK(*(str + 3) == '\0');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cwzstring_span<> CreateTempNameW(wstring_span<> span)
|
cwzstring_span<> CreateTempNameW(wstring_span<> span)
|
||||||
@ -907,8 +909,7 @@ SUITE(string_span_tests)
|
|||||||
Expects(span.size() > 1);
|
Expects(span.size() > 1);
|
||||||
|
|
||||||
int last = 0;
|
int last = 0;
|
||||||
if (span.size() > 4)
|
if (span.size() > 4) {
|
||||||
{
|
|
||||||
span[0] = L't';
|
span[0] = L't';
|
||||||
span[1] = L'm';
|
span[1] = L'm';
|
||||||
span[2] = L'p';
|
span[2] = L'p';
|
||||||
@ -949,8 +950,7 @@ SUITE(string_span_tests)
|
|||||||
wchar_t buf[10];
|
wchar_t buf[10];
|
||||||
|
|
||||||
const auto name = CreateTempNameW({buf, 10});
|
const auto name = CreateTempNameW({buf, 10});
|
||||||
if (!name.empty())
|
if (!name.empty()) {
|
||||||
{
|
|
||||||
cwzstring<> str = name.assume_z();
|
cwzstring<> str = name.assume_z();
|
||||||
CHECK(wcsnlen(str, 10) == 3);
|
CHECK(wcsnlen(str, 10) == 3);
|
||||||
CHECK(*(str + 3) == L'\0');
|
CHECK(*(str + 3) == L'\0');
|
||||||
@ -966,7 +966,4 @@ SUITE(string_span_tests)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int, const char *[])
|
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||||
{
|
|
||||||
return UnitTest::RunAllTests();
|
|
||||||
}
|
|
||||||
|
@ -15,17 +15,16 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <UnitTest++/UnitTest++.h>
|
#include <UnitTest++/UnitTest++.h>
|
||||||
|
|
||||||
#include <gsl/gsl>
|
#include <gsl/gsl>
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
using namespace gsl;
|
using namespace gsl;
|
||||||
|
|
||||||
SUITE(utils_tests)
|
SUITE(utils_tests)
|
||||||
{
|
{
|
||||||
void f(int& i)
|
void f(int& i) { i += 1; }
|
||||||
{
|
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(finally_lambda)
|
TEST(finally_lambda)
|
||||||
{
|
{
|
||||||
@ -113,7 +112,4 @@ SUITE(utils_tests)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int, const char *[])
|
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||||
{
|
|
||||||
return UnitTest::RunAllTests();
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user