Browse Source

base: improve assert messages in the array class.

undefined
Sam Hocevar 10 years ago
parent
commit
f075894e8e
1 changed files with 32 additions and 19 deletions
  1. +32
    -19
      src/lol/base/array.h

+ 32
- 19
src/lol/base/array.h View File

@@ -1,7 +1,7 @@
// //
// Lol Engine // Lol Engine
// //
// Copyright: (c) 2010-2014 Sam Hocevar <sam@hocevar.net>
// Copyright: (c) 2010-2015 Sam Hocevar <sam@hocevar.net>
// (c) 2013-2014 Benjamin "Touky" Huet <huet.benjamin@gmail.com> // (c) 2013-2014 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
// This program is free software; you can redistribute it and/or // This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To // modify it under the terms of the Do What The Fuck You Want To
@@ -133,15 +133,17 @@ public:
{ {
/* Allow array[0] even if size is zero so that people can /* Allow array[0] even if size is zero so that people can
* always use &array[0] to get a pointer to the data. */ * always use &array[0] to get a pointer to the data. */
ASSERT(n >= 0);
ASSERT(n < m_count || (!n && !m_count));
ASSERT(n >= 0 && (n < m_count || (!n && !m_count)),
"cannot access index %lld in array of size %lld",
(long long int)n, (long long int)m_count);
return m_data[n]; return m_data[n];
} }


inline element_t const& operator[](ptrdiff_t n) const inline element_t const& operator[](ptrdiff_t n) const
{ {
ASSERT(n >= 0);
ASSERT(n < m_count || (!n && !m_count));
ASSERT(n >= 0 && (n < m_count || (!n && !m_count)),
"cannot access index %lld in array of size %lld",
(long long int)n, (long long int)m_count);
return m_data[n]; return m_data[n];
} }


@@ -205,8 +207,9 @@ public:


inline void Insert(T const &x, ptrdiff_t pos) inline void Insert(T const &x, ptrdiff_t pos)
{ {
ASSERT(pos >= 0);
ASSERT(pos <= m_count);
ASSERT(pos >= 0 && pos <= m_count,
"cannot insert at index %lld in array of size %lld",
(long long int)pos, (long long int)m_count);


if (m_count >= m_reserved) if (m_count >= m_reserved)
Grow(); Grow();
@@ -222,8 +225,9 @@ public:


inline bool InsertUnique(T const &x, ptrdiff_t pos) inline bool InsertUnique(T const &x, ptrdiff_t pos)
{ {
ASSERT(pos >= 0);
ASSERT(pos <= m_count);
ASSERT(pos >= 0 && pos <= m_count,
"cannot insert at index %lld in array of size %lld",
(long long int)pos, (long long int)m_count);


if (Find(x) != INDEX_NONE) if (Find(x) != INDEX_NONE)
return false; return false;
@@ -284,10 +288,12 @@ public:


void Swap(ptrdiff_t pos1, ptrdiff_t pos2) void Swap(ptrdiff_t pos1, ptrdiff_t pos2)
{ {
ASSERT(pos1 >= 0);
ASSERT(pos2 >= 0);
ASSERT(pos1 < m_count);
ASSERT(pos2 < m_count);
ASSERT(pos1 >= 0 && pos1 <= m_count,
"cannot swap index %lld in array of size %lld",
(long long int)pos1, (long long int)m_count);
ASSERT(pos2 >= 0 && pos2 <= m_count,
"cannot swap index %lld in array of size %lld",
(long long int)pos2, (long long int)m_count);


if (pos1 != pos2) if (pos1 != pos2)
std::swap((*this)[pos1], (*this)[pos2]); std::swap((*this)[pos1], (*this)[pos2]);
@@ -296,8 +302,11 @@ public:
void Remove(ptrdiff_t pos, ptrdiff_t todelete = 1) void Remove(ptrdiff_t pos, ptrdiff_t todelete = 1)
{ {
ASSERT(todelete >= 0); ASSERT(todelete >= 0);
ASSERT(pos - todelete >= -m_count - 1);
ASSERT(pos + todelete <= m_count);
ASSERT(pos - todelete >= -m_count - 1 && pos + todelete <= m_count,
"cannot remove %lld elements at %lld in array of size %lld",
(long long int)todelete, (long long int)pos,
(long long int)m_count);

if (pos < 0) if (pos < 0)
pos = m_count + pos; pos = m_count + pos;


@@ -311,8 +320,11 @@ public:
void RemoveSwap(ptrdiff_t pos, ptrdiff_t todelete = 1) void RemoveSwap(ptrdiff_t pos, ptrdiff_t todelete = 1)
{ {
ASSERT(todelete >= 0); ASSERT(todelete >= 0);
ASSERT(pos - todelete >= -m_count - 1);
ASSERT(pos + todelete <= m_count);
ASSERT(pos - todelete >= -m_count - 1 && pos + todelete <= m_count,
"cannot remove %lld elements at %lld in array of size %lld",
(long long int)todelete, (long long int)pos,
(long long int)m_count);

if (pos < 0) if (pos < 0)
pos = m_count + pos; pos = m_count + pos;


@@ -485,8 +497,9 @@ public:


inline void Insert(ptrdiff_t pos, T... args) inline void Insert(ptrdiff_t pos, T... args)
{ {
ASSERT(pos >= 0);
ASSERT(pos <= this->m_count);
ASSERT(pos >= 0 && pos <= this->m_count,
"cannot insert at index %lld in array of size %lld",
(long long int)pos, (long long int)this->m_count);


if (this->m_count >= this->m_reserved) if (this->m_count >= this->m_reserved)
this->Grow(); this->Grow();


Loading…
Cancel
Save