Partway through removing configurable SizeType.

This commit is contained in:
Neil MacIntosh 2015-10-15 14:29:35 -07:00
parent e0d10d1d1c
commit f45fedbec2
4 changed files with 411 additions and 462 deletions

File diff suppressed because it is too large Load Diff

View File

@ -34,16 +34,16 @@ namespace gsl
// type system for these types that will not either incur significant runtime costs or
// (sometimes needlessly) break existing programs when introduced.
//
template<size_t Max = dynamic_range>
template<std::ptrdiff_t Max = dynamic_range>
using czstring = const char*;
template<size_t Max = dynamic_range>
template<std::ptrdiff_t Max = dynamic_range>
using cwzstring = const wchar_t*;
template<size_t Max = dynamic_range>
template<std::ptrdiff_t Max = dynamic_range>
using zstring = char*;
template<size_t Max = dynamic_range>
template<std::ptrdiff_t Max = dynamic_range>
using wzstring = wchar_t*;
//
@ -54,19 +54,19 @@ using wzstring = wchar_t*;
//
// TODO (neilmac) once array_view regains configurable size_type, update these typedef's
//
template <class CharT, size_t Extent = dynamic_range>
template <class CharT, std::ptrdiff_t Extent = dynamic_range>
using basic_string_view = array_view<CharT, Extent>;
template<size_t Extent = dynamic_range>
template<std::ptrdiff_t Extent = dynamic_range>
using string_view = basic_string_view<char, Extent>;
template<size_t Extent = dynamic_range>
template<std::ptrdiff_t Extent = dynamic_range>
using cstring_view = basic_string_view<const char, Extent>;
template<size_t Extent = dynamic_range>
template<std::ptrdiff_t Extent = dynamic_range>
using wstring_view = basic_string_view<wchar_t, Extent>;
template<size_t Extent = dynamic_range>
template<std::ptrdiff_t Extent = dynamic_range>
using cwstring_view = basic_string_view<const wchar_t, Extent>;
@ -94,38 +94,39 @@ array_view<T, dynamic_range> ensure_sentinel(const T* seq, SizeType max = std::n
// the limit of size_type.
//
template<class T>
inline basic_string_view<T, dynamic_range> ensure_z(T* const & sz, size_t max = std::numeric_limits<size_t>::max())
inline basic_string_view<T, dynamic_range> ensure_z(T* const & sz, std::ptrdiff_t max = PTRDIFF_MAX)
{
return ensure_sentinel<0>(sz, max);
}
// TODO (neilmac) there is probably a better template-magic way to get the const and non-const overloads to share an implementation
inline basic_string_view<char, dynamic_range> ensure_z(char* const & sz, size_t max)
inline basic_string_view<char, dynamic_range> ensure_z(char* const& sz, std::ptrdiff_t max)
{
auto len = strnlen(sz, max);
fail_fast_assert(sz[len] == 0); return{ sz, len };
fail_fast_assert(sz[len] == 0);
return{ sz, static_cast<std::ptrdiff_t>(len) };
}
inline basic_string_view<const char, dynamic_range> ensure_z(const char* const& sz, size_t max)
inline basic_string_view<const char, dynamic_range> ensure_z(const char* const& sz, std::ptrdiff_t max)
{
auto len = strnlen(sz, max);
fail_fast_assert(sz[len] == 0); return{ sz, len };
fail_fast_assert(sz[len] == 0); return{ sz, static_cast<std::ptrdiff_t>(len) };
}
inline basic_string_view<wchar_t, dynamic_range> ensure_z(wchar_t* const & sz, size_t max)
inline basic_string_view<wchar_t, dynamic_range> ensure_z(wchar_t* const& sz, std::ptrdiff_t max)
{
auto len = wcsnlen(sz, max);
fail_fast_assert(sz[len] == 0); return{ sz, len };
fail_fast_assert(sz[len] == 0); return{ sz, static_cast<std::ptrdiff_t>(len) };
}
inline basic_string_view<const wchar_t, dynamic_range> ensure_z(const wchar_t* const & sz, size_t max)
inline basic_string_view<const wchar_t, dynamic_range> ensure_z(const wchar_t* const& sz, std::ptrdiff_t max)
{
auto len = wcsnlen(sz, max);
fail_fast_assert(sz[len] == 0); return{ sz, len };
fail_fast_assert(sz[len] == 0); return{ sz, static_cast<std::ptrdiff_t>(len) };
}
template<class T, size_t N>
basic_string_view<T, dynamic_range> ensure_z(T(&sz)[N]) { return ensure_z(&sz[0], N); }
basic_string_view<T, dynamic_range> ensure_z(T(&sz)[N]) { return ensure_z(&sz[0], static_cast<std::ptrdiff_t>(N)); }
template<class Cont>
basic_string_view<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_range> ensure_z(Cont& cont)

View File

@ -50,7 +50,7 @@ SUITE(array_view_tests)
delete[] ptr.data();
static_bounds<size_t, 4, dynamic_range, 2> bounds{ 3 };
static_bounds<4, dynamic_range, 2> bounds{ 3 };
#ifdef CONFIRM_COMPILATION_ERRORS
array_view<int, 4, dynamic_range, 2> av(nullptr, bounds);
@ -151,9 +151,9 @@ SUITE(array_view_tests)
TEST(md_access)
{
unsigned int width = 5, height = 20;
auto width = 5, height = 20;
unsigned int imgSize = width * height;
auto imgSize = width * height;
auto image_ptr = new int[imgSize][3];
// size check will be done
@ -162,9 +162,9 @@ SUITE(array_view_tests)
iota(image_view.begin(), image_view.end(), 1);
int expected = 0;
for (unsigned int i = 0; i < height; i++)
for (auto i = 0; i < height; i++)
{
for (unsigned int j = 0; j < width; j++)
for (auto j = 0; j < width; j++)
{
CHECK(expected + 1 == image_view[i][j][0]);
CHECK(expected + 2 == image_view[i][j][1]);
@ -244,7 +244,7 @@ SUITE(array_view_tests)
auto av8 = av7.as_array_view<int>();
CHECK(av8.size() == av6.size());
for (size_t i = 0; i < av8.size(); i++)
for (auto i = 0; i < av8.size(); i++)
{
CHECK(av8[i] == 1);
}

View File

@ -23,16 +23,16 @@ using namespace gsl;;
namespace
{
void use(unsigned int&) {}
void use(std::ptrdiff_t&) {}
}
SUITE(bounds_test)
{
TEST(basic_bounds)
{
for (auto point : static_bounds <unsigned int, dynamic_range, 3, 4 > { 2 })
for (auto point : static_bounds<dynamic_range, 3, 4 > { 2 })
{
for (unsigned int j = 0; j < decltype(point)::rank; j++)
for (decltype(point)::size_type j = 0; j < decltype(point)::rank; j++)
{
use(j);
use(point[j]);
@ -42,24 +42,24 @@ SUITE(bounds_test)
TEST(bounds_basic)
{
static_bounds<size_t, 3, 4, 5> b;
static_bounds<3, 4, 5> b;
auto a = b.slice();
static_bounds<size_t, 4, dynamic_range, 2> x{ 4 };
static_bounds<4, dynamic_range, 2> x{ 4 };
x.slice().slice();
}
TEST (arrayview_iterator)
{
static_bounds<size_t, 4, dynamic_range, 2> bounds{ 3 };
static_bounds<4, dynamic_range, 2> bounds{ 3 };
auto itr = bounds.begin();
#ifdef CONFIRM_COMPILATION_ERRORS
array_view< int, 4, dynamic_range, 2> av(nullptr, bounds);
array_view<int, 4, dynamic_range, 2> av(nullptr, bounds);
auto itr2 = av.cbegin();
for (auto & v : av) {
for (auto& v : av) {
v = 4;
}
fill(av.begin(), av.end(), 0);
@ -68,24 +68,24 @@ SUITE(bounds_test)
TEST (bounds_convertible)
{
static_bounds<size_t, 7, 4, 2> b1;
static_bounds<size_t, 7, dynamic_range, 2> b2 = b1;
static_bounds<7, 4, 2> b1;
static_bounds<7, dynamic_range, 2> b2 = b1;
#ifdef CONFIRM_COMPILATION_ERRORS
static_bounds<size_t, 7, dynamic_range, 1> b4 = b2;
static_bounds<7, dynamic_range, 1> b4 = b2;
#endif
static_bounds<size_t, dynamic_range, dynamic_range, dynamic_range> b3 = b1;
static_bounds<int, 7, 4, 2> b4 = b3;
static_bounds<dynamic_range, dynamic_range, dynamic_range> b3 = b1;
static_bounds<7, 4, 2> b4 = b3;
static_bounds<size_t, dynamic_range> b11;
static_bounds<dynamic_range> b11;
static_bounds<size_t, dynamic_range> b5;
static_bounds<size_t, 34> b6;
static_bounds<dynamic_range> b5;
static_bounds<34> b6;
b5 = static_bounds<size_t, 20>();
b5 = static_bounds<20>();
CHECK_THROW(b6 = b5, fail_fast);
b5 = static_bounds<size_t, 34>();
b5 = static_bounds<34>();
b6 = b5;
CHECK(b5 == b6);