diff options
Diffstat (limited to 'src/generic/enum.hpp')
-rw-r--r-- | src/generic/enum.hpp | 82 |
1 files changed, 17 insertions, 65 deletions
diff --git a/src/generic/enum.hpp b/src/generic/enum.hpp index 8b3509f..81c9b12 100644 --- a/src/generic/enum.hpp +++ b/src/generic/enum.hpp @@ -1,5 +1,4 @@ -#ifndef TMWA_GENERIC_ENUM_HPP -#define TMWA_GENERIC_ENUM_HPP +#pragma once // enum.hpp - Safe building blocks for enumerated types. // // Copyright © 2012-2014 Ben Longbons <b.r.longbons@gmail.com> @@ -19,70 +18,23 @@ // 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 "fwd.hpp" -# include <cassert> +#include <cassert> +#include <cstddef> -# include <type_traits> +#include <algorithm> +#include <type_traits> -# include "../compat/iter.hpp" +#include "../compat/iter.hpp" -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& operator[](E v) - { - auto i = static_cast<size_t>(v); - assert (i < size()); - return _data[i]; - } - - const T& operator[](E v) const - { - auto i = static_cast<size_t>(v); - assert (i < size()); - return _data[i]; - } - - T *begin() - { - return _data; - } +#include "array.hpp" - T *end() - { - return _data + size(); - } - const T *begin() const - { - return _data; - } - - const T *end() const - { - return _data + size(); - } - - friend bool operator == (const earray& l, const earray& r) - { - return std::equal(l.begin(), l.end(), r.begin()); - } - - friend bool operator != (const earray& l, const earray& r) - { - return !(l == r); - } -}; +namespace tmwa +{ +template<class T, class E, E max> +using earray = GenericArray<T, EnumIndexing<E, max>>; template<class T, class E, E max> class eptr @@ -100,7 +52,7 @@ public: {} eptr(earray<T, E, max>& arr) - : _data(arr._data) + : _data(arr.data) {} T& operator [](E v) const @@ -123,6 +75,7 @@ public: // std::underlying_type isn't supported until gcc 4.7 // this is a poor man's emulation +// TODO I'm depending on GCC 4.7 now, this can go away template<class E> struct underlying_type { @@ -148,7 +101,7 @@ 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) \ +#define ENUM_BITWISE_OPERATORS(E) \ inline \ E operator & (E l, E r) \ { \ @@ -196,7 +149,7 @@ public: static E inced(E v) { - return E(U(v) + 1); + return static_cast<E>(static_cast<U>(v) + 1); } }; @@ -205,5 +158,4 @@ IteratorPair<ValueIterator<E, EnumMath<E>>> erange(E b, E e) { return {b, e}; } - -#endif // TMWA_GENERIC_ENUM_HPP +} // namespace tmwa |