From 414f10ae2837ed3066084e764d2b70fc53d53e5c Mon Sep 17 00:00:00 2001 From: Treb Connell Date: Thu, 8 Oct 2015 13:37:53 -0700 Subject: [PATCH] Move strings in Expects into comments so that Expects can be a macro --- include/array_view.h | 36 ++++++++++++++++++------------------ include/gsl_assert.h | 7 ++----- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/include/array_view.h b/include/array_view.h index bc75e53..2ad7d9a 100644 --- a/include/array_view.h +++ b/include/array_view.h @@ -108,7 +108,7 @@ namespace details constexpr coordinate_facade(std::initializer_list il) { static_assert(std::is_base_of::value, "ConcreteType must be derived from coordinate_facade."); - Expects(il.size() == rank, "The size of the initializer list must match the rank of the array"); + Expects(il.size() == rank); // The size of the initializer list must match the rank of the array for (size_t i = 0; i < rank; ++i) { elems[i] = begin(il)[i]; @@ -131,13 +131,13 @@ namespace details // Preconditions: component_idx < rank constexpr reference operator[](size_t component_idx) { - Expects(component_idx < rank, "Component index must be less than rank"); + Expects(component_idx < rank); // Component index must be less than rank return elems[component_idx]; } // Preconditions: component_idx < rank constexpr const_reference operator[](size_t component_idx) const { - Expects(component_idx < rank, "Component index must be less than rank"); + Expects(component_idx < rank); // Component index must be less than rank return elems[component_idx]; } constexpr bool operator==(const ConcreteType& rhs) const noexcept @@ -335,7 +335,7 @@ public: // Preconditions: il.size() == rank constexpr index(std::initializer_list il) { - Expects(il.size() == rank, "Size of the initializer list must match the rank of the array"); + Expects(il.size() == rank); // Size of the initializer list must match the rank of the array value = begin(il)[0]; } @@ -355,14 +355,14 @@ public: // Preconditions: component_idx < rank constexpr reference operator[](size_type component_idx) noexcept { - Expects(component_idx == 0, "Component index must be less than rank"); + Expects(component_idx == 0); // Component index must be less than rank (void)(component_idx); return value; } // Preconditions: component_idx < rank constexpr const_reference operator[](size_type component_idx) const noexcept { - Expects(component_idx == 0, "Component index must be less than rank"); + Expects(component_idx == 0); // Component index must be less than rank (void)(component_idx); return value; } @@ -661,7 +661,7 @@ namespace details template SizeType linearize(const T & arr) const { - Expects(arr[Dim] < CurrentRange, "Index is out of range"); + Expects(arr[Dim] < CurrentRange); // Index is out of range return static_cast(this->Base::totalSize()) * arr[Dim] + this->Base::template linearize(arr); } @@ -798,8 +798,8 @@ public: constexpr static_bounds(std::initializer_list il) : m_ranges(il.begin()) { - Expects(MyRanges::DynamicNum == il.size(), "Size of the initializer list must match the rank of the array"); - Expects(il.size() <= details::SizeTypeTraits::max_value, "Size of the range is larger than the max element of the size type"); + Expects(MyRanges::DynamicNum == il.size()); // Size of the initializer list must match the rank of the array + Expects(il.size() <= details::SizeTypeTraits::max_value); // Size of the range is larger than the max element of the size type } constexpr static_bounds() = default; @@ -1398,8 +1398,8 @@ public: template 1), typename Ret = std::enable_if_t> constexpr Ret operator[](size_type idx) const { - Expects(idx < m_bounds.size(), "index is out of bounds of the array"); - Expects(idx * m_bounds.stride() < m_bounds.total_size(), "index is out of bounds of the underlying data"); + Expects(idx < m_bounds.size()); // index is out of bounds of the array + Expects(idx * m_bounds.stride() < m_bounds.total_size()); // index is out of bounds of the underlying data return Ret {m_pdata + idx * m_bounds.stride(), m_bounds.slice()}; } @@ -1946,20 +1946,20 @@ public: template strided_array_view(value_type(&values)[N], bounds_type bounds) : Base(values, std::move(bounds)) { - Expects(this->bounds().total_size() <= N, "Bounds cross data boundaries"); + Expects(this->bounds().total_size() <= N); // Bounds cross data boundaries } // from raw data strided_array_view(pointer ptr, size_type size, bounds_type bounds): Base(ptr, std::move(bounds)) { - Expects(this->bounds().total_size() <= size, "Bounds cross data boundaries"); + Expects(this->bounds().total_size() <= size); // Bounds cross data boundaries } // from array view template > strided_array_view(array_view av, bounds_type bounds) : Base(av.data(), std::move(bounds)) { - Expects(this->bounds().total_size() <= av.bounds().total_size(), "Bounds cross data boundaries"); + Expects(this->bounds().total_size() <= av.bounds().total_size()); // Bounds cross data boundaries } // convertible @@ -2004,7 +2004,7 @@ public: private: static index_type resize_extent(const index_type& extent, size_t d) { - Expects(extent[rank - 1] >= d && (extent[rank-1] % d == 0), "The last dimension of the array needs to contain a multiple of new type elements"); + Expects(extent[rank - 1] >= d && (extent[rank-1] % d == 0)); // The last dimension of the array needs to contain a multiple of new type elements index_type ret = extent; ret[rank - 1] /= d; @@ -2015,7 +2015,7 @@ private: template > static index_type resize_stride(const index_type& strides, size_t , void * = 0) { - Expects(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized"); + Expects(strides[rank - 1] == 1); // Only strided arrays with regular strides can be resized return strides; } @@ -2023,8 +2023,8 @@ private: template 1), typename Dummy = std::enable_if_t> static index_type resize_stride(const index_type& strides, size_t d) { - Expects(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized"); - Expects(strides[rank - 2] >= d && (strides[rank - 2] % d == 0), "The strides must have contiguous chunks of memory that can contain a multiple of new type elements"); + Expects(strides[rank - 1] == 1); // Only strided arrays with regular strides can be resized + Expects(strides[rank - 2] >= d && (strides[rank - 2] % d == 0)); // The strides must have contiguous chunks of memory that can contain a multiple of new type elements for (size_t i = rank - 1; i > 0; --i) fail_fast_assert((strides[i-1] >= strides[i]) && (strides[i-1] % strides[i] == 0), "Only strided arrays with regular strides can be resized"); diff --git a/include/gsl_assert.h b/include/gsl_assert.h index fe8a166..e9a3699 100644 --- a/include/gsl_assert.h +++ b/include/gsl_assert.h @@ -27,7 +27,6 @@ namespace gsl { - // // Having "fail fast" result in an exception makes unit testing // the GSL classes that rely upon it much simpler. @@ -53,10 +52,8 @@ inline void fail_fast_assert(bool cond, const char* const) { if (!cond) std::ter // // GSL.assert: assertions // -inline void Expects(bool cond) { fail_fast_assert(cond); } -inline void Expects(bool cond, const char* msg) { fail_fast_assert(cond, msg); } -inline void Ensures(bool cond) { fail_fast_assert(cond); } -inline void Ensures(bool cond, const char* msg) { fail_fast_assert(cond, msg); } +#define Expects(x) gsl::fail_fast_assert((x)) +#define Ensures(x) gsl::fail_fast_assert((x)) } // namespace gsl