From f41ffe339c948a1ca6c531e07a88cb4a7cad37b2 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Sun, 3 Feb 2019 16:37:08 +1100 Subject: [PATCH] endian: be less clever with value operators --- endian.hpp | 53 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/endian.hpp b/endian.hpp index 40cb83f8..405bc557 100644 --- a/endian.hpp +++ b/endian.hpp @@ -3,14 +3,14 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * - * Copyright 2010-2014 Danny Robson + * Copyright 2010-2019 Danny Robson */ -#ifndef __UTIL_ENDIAN_HPP -#define __UTIL_ENDIAN_HPP +#pragma once #include "std.hpp" #include "types/bits.hpp" +#include "cast.hpp" #include #include @@ -206,25 +206,53 @@ namespace cruft { namespace endian { - template + template struct value { + constexpr RawT native (void) const { return convert (raw); } - operator ValueT () const + operator RawT () const { - return convert (raw); + return native (); } - ValueT native (void) const { return *this; } + decltype(auto) operator+ () const { return +native (); } - decltype(auto) operator+ () const { return +this->operator ValueT (); } + constexpr value operator>> (std::size_t s) const { + RawT const val = native () >> s; + return { .raw = convert (val) }; + } + constexpr value operator<< (std::size_t s) const { + RawT const val = native () << s; + return { .raw = convert (val) }; + } - ValueT operator<< (size_t s) const { return convert (raw) << s; } + value operator& (value rhs) const { return value { .raw = raw & rhs.raw }; } - auto& operator= (ValueT const &val) { raw = convert (val); return *this; } + template + value operator& (OperandT rhs) const { + RawT const val = native () & rhs; + return { .raw = convert (val) }; + } - ValueT raw; + auto& operator= (RawT const &val) { raw = convert (val); return *this; } + + bool operator== (value const &rhs) const { return raw == rhs.raw; } + + RawT raw; }; + template + constexpr decltype(auto) operator== (value const &a, OperandT const &b) + { + return a.native () == b; + } + + template + constexpr decltype(auto) operator== (OperandT const &a, value const &b) + { + return a == b.native (); + } + template using little = value; template using big = value; @@ -246,6 +274,3 @@ namespace cruft { using bi32 = endian::big; using bi64 = endian::big; } - - -#endif