diff options
Diffstat (limited to 'src/ints')
-rw-r--r-- | src/ints/cmp.hpp | 2 | ||||
-rw-r--r-- | src/ints/fwd.hpp | 26 | ||||
-rw-r--r-- | src/ints/udl.hpp | 12 | ||||
-rw-r--r-- | src/ints/udl_test.cpp | 7 | ||||
-rw-r--r-- | src/ints/wrap.cpp | 21 | ||||
-rw-r--r-- | src/ints/wrap.hpp | 109 |
6 files changed, 169 insertions, 8 deletions
diff --git a/src/ints/cmp.hpp b/src/ints/cmp.hpp index b979c46..e0e819b 100644 --- a/src/ints/cmp.hpp +++ b/src/ints/cmp.hpp @@ -19,7 +19,7 @@ // 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 <limits> diff --git a/src/ints/fwd.hpp b/src/ints/fwd.hpp new file mode 100644 index 0000000..7685da5 --- /dev/null +++ b/src/ints/fwd.hpp @@ -0,0 +1,26 @@ +#ifndef TMWA_INTS_FWD_HPP +#define TMWA_INTS_FWD_HPP +// ints/fwd.hpp - list of type names for ints library +// +// Copyright © 2014 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" + +// meh, add more when I feel like it + +#endif // TMWA_INTS_FWD_HPP diff --git a/src/ints/udl.hpp b/src/ints/udl.hpp index e3e5fcc..ecb5478 100644 --- a/src/ints/udl.hpp +++ b/src/ints/udl.hpp @@ -19,7 +19,7 @@ // 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 <cstdint> @@ -121,14 +121,17 @@ namespace ints ullong magnitude = V; template<class T> + constexpr operator T() { typedef typename std::make_unsigned<T>::type U; - - constexpr bool is_signed = T(-1) < T(0); + // boo, body of constexpr function can't use variables +# define is_signed bool(T(-1) < T(0)) static_assert(is_signed >= (sign && magnitude), "signed"); - constexpr ullong max = ullong(U(-1) >> is_signed); +# define max ullong(ullong(U(-1) >> is_signed)) static_assert(magnitude <= max || (sign && magnitude == max + 1), "magna"); +# undef is_signed +# undef max return sign ? T(ullong(-magnitude)) : T(magnitude); } }; @@ -162,6 +165,7 @@ namespace ints struct nint64 { int64_t value; int64_t operator -() { return value; } }; template<char... C> + constexpr SignedMagnitudeConstant<false, IntParser<C...>::value> operator "" _const() { return {}; } diff --git a/src/ints/udl_test.cpp b/src/ints/udl_test.cpp index 26ea7c3..3bcbaad 100644 --- a/src/ints/udl_test.cpp +++ b/src/ints/udl_test.cpp @@ -475,9 +475,10 @@ TEST(ints, smc) TEST(ints, constant) { - EXPECT_EQ(0_const, (ints::SignedMagnitudeConstant<false, 0>{})); - EXPECT_EQ(1_const, (ints::SignedMagnitudeConstant<false, 1>{})); - EXPECT_EQ(1_const, (ints::SignedMagnitudeConstant<false, 1>{})); + // gtest is funny with conversions + assert(0_const == (ints::SignedMagnitudeConstant<false, 0>{})); + assert(1_const == (ints::SignedMagnitudeConstant<false, 1>{})); + assert(1_const == (ints::SignedMagnitudeConstant<false, 1>{})); } TEST(ints, udl8) diff --git a/src/ints/wrap.cpp b/src/ints/wrap.cpp new file mode 100644 index 0000000..a80bd9f --- /dev/null +++ b/src/ints/wrap.cpp @@ -0,0 +1,21 @@ +#include "wrap.hpp" +// wrap.cpp - basic integer wrapper classes +// +// Copyright © 2014 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 "../poison.hpp" diff --git a/src/ints/wrap.hpp b/src/ints/wrap.hpp new file mode 100644 index 0000000..b25a1ad --- /dev/null +++ b/src/ints/wrap.hpp @@ -0,0 +1,109 @@ +#ifndef TMWA_INTS_WRAP_HPP +#define TMWA_INTS_WRAP_HPP +// wrap.hpp - basic integer wrapper classes +// +// Copyright © 2014 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 "fwd.hpp" + +# include <cstdint> + +# include <type_traits> + +namespace ints +{ + namespace wrapped + { + template<class R> + struct Wrapped + { + typedef R wrapped_type; + R _value; + protected: + constexpr + Wrapped(uint32_t v=0) + : _value(v) + {} + public: + explicit + operator bool () const { return _value; } + bool operator !() const { return !_value; } + }; + + template<class W> + bool operator == (W l, W r) + { + return l._value == r._value; + } + template<class W> + bool operator != (W l, W r) + { + return l._value != r._value; + } + template<class W> + bool operator < (W l, W r) + { + return l._value < r._value; + } + + template<class T> + constexpr + typename T::wrapped_type unwrap(typename std::enable_if<true, T>::type w) + { + return w._value; + } + template<class T> + constexpr + T wrap(typename T::wrapped_type v) + { + struct Sub : T + { + constexpr + Sub(typename T::wrapped_type v) + : T(v) + {} + }; + return Sub(v); + } + + template<class W> + constexpr + W next(W w) + { + return wrap<W>(unwrap<W>(w) + 1); + } + template<class W> + constexpr + W prev(W w) + { + return wrap<W>(unwrap<W>(w) - 1); + } + + template<class R> + R convert_for_printf(Wrapped<R> w) + { + return w._value; + } + } // namespace wrapped +} // namespace ints + +using ints::wrapped::Wrapped; +using ints::wrapped::unwrap; +using ints::wrapped::wrap; + +#endif // TMWA_INTS_WRAP_HPP |