From dc17e01c9923b5af79b3ee1cea4435f6952c9a57 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 30 Jan 2021 13:49:03 +0100 Subject: [PATCH] AK: Allow Checked += Checked, and other such operations The overflow state from both Checkeds is OR'ed in the result. --- AK/Checked.h | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/AK/Checked.h b/AK/Checked.h index 9be8affdcb..efa6640344 100644 --- a/AK/Checked.h +++ b/AK/Checked.h @@ -1,6 +1,6 @@ /* * Copyright (C) 2011-2019 Apple Inc. All rights reserved. - * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020-2021, Andreas Kling * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -186,24 +186,52 @@ public: m_value /= other; } + constexpr Checked& operator+=(const Checked& other) + { + m_overflow |= other.m_overflow; + add(other.value()); + return *this; + } + constexpr Checked& operator+=(T other) { add(other); return *this; } + constexpr Checked& operator-=(const Checked& other) + { + m_overflow |= other.m_overflow; + sub(other.value()); + return *this; + } + constexpr Checked& operator-=(T other) { sub(other); return *this; } + constexpr Checked& operator*=(const Checked& other) + { + m_overflow |= other.m_overflow; + mul(other.value()); + return *this; + } + constexpr Checked& operator*=(T other) { mul(other); return *this; } + constexpr Checked& operator/=(const Checked& other) + { + m_overflow |= other.m_overflow; + div(other.value()); + return *this; + } + constexpr Checked& operator/=(T other) { div(other);