From c812c92d1a1835f0bda783e709481188c8d92225 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Sat, 15 Mar 2014 19:34:59 -0700 Subject: Clean up header organization --- src/generic/enum.hpp | 170 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 src/generic/enum.hpp (limited to 'src/generic/enum.hpp') diff --git a/src/generic/enum.hpp b/src/generic/enum.hpp new file mode 100644 index 0000000..6f29981 --- /dev/null +++ b/src/generic/enum.hpp @@ -0,0 +1,170 @@ +#ifndef TMWA_GENERIC_ENUM_HPP +#define TMWA_GENERIC_ENUM_HPP + +# include "../sanity.hpp" + +# include + +# include "../compat/iter.hpp" + +template +struct earray +{ + // no ctor/dtor and one public member variable for easy initialization + T _data[size_t(max)]; + + T& operator[](E v) + { + return _data[size_t(v)]; + } + + const T& operator[](E v) const + { + return _data[size_t(v)]; + } + + T *begin() + { + return _data; + } + + T *end() + { + return _data + size_t(max); + } + + const T *begin() const + { + return _data; + } + + const T *end() const + { + return _data + size_t(max); + } + + 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); + } +}; + +template +class eptr +{ + T *_data; +public: + eptr(std::nullptr_t=nullptr) + : _data(nullptr) + {} + + template + eptr(earray& arr) + : _data(arr._data) + {} + + T& operator [](E v) + { + return _data[size_t(v)]; + } + + explicit operator bool() + { + return _data; + } + + bool operator not() + { + return not _data; + } +}; + +// std::underlying_type isn't supported until gcc 4.7 +// this is a poor man's emulation +template +struct underlying_type +{ + static_assert(std::is_enum::value, "Only enums have underlying type!"); + typedef typename std::conditional< + std::is_signed::value, + typename std::make_signed::type, + typename std::make_unsigned::type + >::type type; +}; + +template::value> +struct remove_enum +{ + typedef E type; +}; +template +struct remove_enum +{ + typedef typename underlying_type::type type; +}; + + +# define ENUM_BITWISE_OPERATORS(E) \ +inline \ +E operator & (E l, E r) \ +{ \ + typedef underlying_type::type U; \ + return E(U(l) & U(r)); \ +} \ +inline \ +E operator | (E l, E r) \ +{ \ + typedef underlying_type::type U; \ + return E(U(l) | U(r)); \ +} \ +inline \ +E operator ^ (E l, E r) \ +{ \ + typedef underlying_type::type U; \ + return E(U(l) ^ U(r)); \ +} \ +inline \ +E& operator &= (E& l, E r) \ +{ \ + return l = l & r; \ +} \ +inline \ +E& operator |= (E& l, E r) \ +{ \ + return l = l | r; \ +} \ +inline \ +E& operator ^= (E& l, E r) \ +{ \ + return l = l ^ r; \ +} \ +inline \ +E operator ~ (E r) \ +{ \ + return E(-1) ^ r; \ +} + +template +class EnumMath +{ + typedef typename underlying_type::type U; +public: + static + E inced(E v) + { + return E(U(v) + 1); + } +}; + +template +IteratorPair>> erange(E b, E e) +{ + return {b, e}; +} + +#endif // TMWA_GENERIC_ENUM_HPP -- cgit v1.2.3-70-g09d2