From ad049a15b43b7ddba3fe7d0a898652fc8022629d Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Tue, 22 Apr 2014 11:46:23 -0700 Subject: Use strict ID types Possibly some missing for the far side of the network. AccountId and BlockId are still terribly entangled. --- src/ints/cmp.hpp | 2 +- src/ints/fwd.hpp | 26 ++++++++++++ src/ints/udl.hpp | 12 ++++-- src/ints/udl_test.cpp | 7 ++-- src/ints/wrap.cpp | 21 ++++++++++ src/ints/wrap.hpp | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 169 insertions(+), 8 deletions(-) create mode 100644 src/ints/fwd.hpp create mode 100644 src/ints/wrap.cpp create mode 100644 src/ints/wrap.hpp (limited to 'src/ints') 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 . -# include "../sanity.hpp" +# include "fwd.hpp" # include 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 +// +// 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 . + +# 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 . -# include "../sanity.hpp" +# include "fwd.hpp" # include @@ -121,14 +121,17 @@ namespace ints ullong magnitude = V; template + constexpr operator T() { typedef typename std::make_unsigned::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 + constexpr SignedMagnitudeConstant::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{})); - EXPECT_EQ(1_const, (ints::SignedMagnitudeConstant{})); - EXPECT_EQ(1_const, (ints::SignedMagnitudeConstant{})); + // gtest is funny with conversions + assert(0_const == (ints::SignedMagnitudeConstant{})); + assert(1_const == (ints::SignedMagnitudeConstant{})); + assert(1_const == (ints::SignedMagnitudeConstant{})); } 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 +// +// 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 . + +#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 +// +// 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 . + +# include "fwd.hpp" + +# include + +# include + +namespace ints +{ + namespace wrapped + { + template + 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 + bool operator == (W l, W r) + { + return l._value == r._value; + } + template + bool operator != (W l, W r) + { + return l._value != r._value; + } + template + bool operator < (W l, W r) + { + return l._value < r._value; + } + + template + constexpr + typename T::wrapped_type unwrap(typename std::enable_if::type w) + { + return w._value; + } + template + constexpr + T wrap(typename T::wrapped_type v) + { + struct Sub : T + { + constexpr + Sub(typename T::wrapped_type v) + : T(v) + {} + }; + return Sub(v); + } + + template + constexpr + W next(W w) + { + return wrap(unwrap(w) + 1); + } + template + constexpr + W prev(W w) + { + return wrap(unwrap(w) - 1); + } + + template + R convert_for_printf(Wrapped 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 -- cgit v1.2.3-60-g2f50