diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2014-03-30 23:14:12 -0700 |
---|---|---|
committer | Ben Longbons <b.r.longbons@gmail.com> | 2014-03-31 10:18:49 -0700 |
commit | 1a651243bb2c8e18baa9aac30ac52a62185074e7 (patch) | |
tree | dd2c0bfc448faef129fb64edec9f64d2ab12bfe5 /src/generic | |
parent | 769e8ac9c17779a15492d7fcfc1931c014670c2d (diff) | |
download | tmwa-1a651243bb2c8e18baa9aac30ac52a62185074e7.tar.gz tmwa-1a651243bb2c8e18baa9aac30ac52a62185074e7.tar.bz2 tmwa-1a651243bb2c8e18baa9aac30ac52a62185074e7.tar.xz tmwa-1a651243bb2c8e18baa9aac30ac52a62185074e7.zip |
Be stricter about most arrays
Diffstat (limited to 'src/generic')
-rw-r--r-- | src/generic/const_array.cpp | 3 | ||||
-rw-r--r-- | src/generic/const_array.hpp | 132 | ||||
-rw-r--r-- | src/generic/enum.hpp | 39 | ||||
-rw-r--r-- | src/generic/intern-pool.hpp | 1 | ||||
-rw-r--r-- | src/generic/matrix.hpp | 6 |
5 files changed, 37 insertions, 144 deletions
diff --git a/src/generic/const_array.cpp b/src/generic/const_array.cpp deleted file mode 100644 index 0c09333..0000000 --- a/src/generic/const_array.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include "const_array.hpp" - -#include "../poison.hpp" diff --git a/src/generic/const_array.hpp b/src/generic/const_array.hpp deleted file mode 100644 index 1c70f5d..0000000 --- a/src/generic/const_array.hpp +++ /dev/null @@ -1,132 +0,0 @@ -#ifndef TMWA_GENERIC_CONST_ARRAY_HPP -#define TMWA_GENERIC_CONST_ARRAY_HPP -// const_array.hpp - just a pointer-to-const and a length -// -// Copyright © 2011-2012 Ben Longbons <b.r.longbons@gmail.com> -// -// This file is part of The Mana World (Athena server) -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see <http://www.gnu.org/licenses/>. - -# include "../sanity.hpp" - -# include <cstring> - -# include <iterator> -# include <ostream> -# include <vector> - -# ifdef WORKAROUND_GCC46_COMPILER -// constexpr is buggy with templates in this version -// Is this still needed now that const_string is removed? -# define constexpr /* nothing */ -# endif - -// TODO see if I ever actually use this, and not the subclass -template<class T> -class const_array -{ - const T *d; - size_t n; -public: - typedef const T *iterator; - typedef std::reverse_iterator<iterator> reverse_iterator; - - constexpr - const_array(std::nullptr_t) - : d(nullptr), n(0) - {} - - constexpr - const_array(const T *p, size_t z) - : d(p), n(z) - {} - - constexpr - const_array(const T *b, const T *e) - : d(b), n(e - b) - {} - - const_array(std::initializer_list<T> list) - : d(list.begin()), n(list.size()) - {} - - // Implicit conversion from std::vector - const_array(const std::vector<T>& v) - : d(v.data()), n(v.size()) - {} - - // but disallow conversion from a temporary - const_array(std::vector<T>&&) = delete; - - // Oops. see src/warnings.hpp - constexpr - const T *data() const { return d; } - constexpr - size_t size() const { return n; } - constexpr - bool empty() const { return not n; } - constexpr explicit - operator bool() const { return n; } - - constexpr - std::pair<const_array, const_array> cut(size_t o) const - { - return {const_array(d, o), const_array(d + o, n - o)}; - } - - constexpr - const_array first(size_t o) const - { - return cut(o).first; - } - - constexpr - const_array last(size_t l) const - { - return cut(size() - l).second; - } - - constexpr - const_array after(size_t o) const - { - return cut(o).second; - } - - constexpr - iterator begin() const { return d; } - constexpr - iterator end() const { return d + n; } - constexpr - reverse_iterator rbegin() const { return reverse_iterator(end()); } - constexpr - reverse_iterator rend() const { return reverse_iterator(begin()); } - - constexpr - const T& front() const { return *begin(); } - constexpr - const T& back() const { return *rbegin(); } - - // This probably shouldn't be used, but I'm adding it for porting. - T& operator[](size_t i) - { - return const_cast<T&>(d[i]); - } -}; - -# ifdef WORKAROUND_GCC46_COMPILER -# undef constexpr -# endif - -#endif // TMWA_GENERIC_CONST_ARRAY_HPP diff --git a/src/generic/enum.hpp b/src/generic/enum.hpp index 6f29981..29e5c36 100644 --- a/src/generic/enum.hpp +++ b/src/generic/enum.hpp @@ -3,6 +3,8 @@ # include "../sanity.hpp" +# include <cassert> + # include <type_traits> # include "../compat/iter.hpp" @@ -10,17 +12,27 @@ template<class T, class E, E max> struct earray { + constexpr static + size_t size() + { + return static_cast<size_t>(max); + } + // no ctor/dtor and one public member variable for easy initialization - T _data[size_t(max)]; + T _data[size()]; T& operator[](E v) { - return _data[size_t(v)]; + auto i = static_cast<size_t>(v); + assert (i < size()); + return _data[i]; } const T& operator[](E v) const { - return _data[size_t(v)]; + auto i = static_cast<size_t>(v); + assert (i < size()); + return _data[i]; } T *begin() @@ -30,7 +42,7 @@ struct earray T *end() { - return _data + size_t(max); + return _data + size(); } const T *begin() const @@ -40,7 +52,7 @@ struct earray const T *end() const { - return _data + size_t(max); + return _data + size(); } friend bool operator == (const earray& l, const earray& r) @@ -54,23 +66,30 @@ struct earray } }; -template<class T, class E> +template<class T, class E, E max> class eptr { + constexpr static + size_t size() + { + return static_cast<size_t>(max); + } + T *_data; public: eptr(std::nullptr_t=nullptr) : _data(nullptr) {} - template<E max> eptr(earray<T, E, max>& arr) : _data(arr._data) {} - T& operator [](E v) + T& operator [](E v) const { - return _data[size_t(v)]; + auto i = static_cast<size_t>(v); + assert (i < size()); + return _data[i]; } explicit operator bool() @@ -109,6 +128,8 @@ struct remove_enum<E, true> }; +// This really should just go in a namespace +// that's how I use it anyway ... # define ENUM_BITWISE_OPERATORS(E) \ inline \ E operator & (E l, E r) \ diff --git a/src/generic/intern-pool.hpp b/src/generic/intern-pool.hpp index 69f20ef..f9c1e8f 100644 --- a/src/generic/intern-pool.hpp +++ b/src/generic/intern-pool.hpp @@ -29,6 +29,7 @@ public: ZString outtern(size_t sz) const { + assert (sz < names.size()); return names[sz]; } diff --git a/src/generic/matrix.hpp b/src/generic/matrix.hpp index 3530ba7..b337249 100644 --- a/src/generic/matrix.hpp +++ b/src/generic/matrix.hpp @@ -3,6 +3,8 @@ # include "../sanity.hpp" +# include <cassert> + # include "../compat/memory.hpp" template<class T> @@ -34,10 +36,14 @@ public: T& ref(size_t x, size_t y) { + assert (x < _xs); + assert (y < _ys); return _data[x + y * _xs]; } const T& ref(size_t x, size_t y) const { + assert (x < _xs); + assert (y < _ys); return _data[x + y * _xs]; } |