tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

AssertionsImpl.h (1413B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef DOM_QUOTA_ASSERTIONSIMPL_H_
      8 #define DOM_QUOTA_ASSERTIONSIMPL_H_
      9 
     10 #include <type_traits>
     11 
     12 #include "mozilla/Assertions.h"
     13 #include "mozilla/dom/quota/Assertions.h"
     14 
     15 namespace mozilla::dom::quota {
     16 
     17 namespace detail {
     18 
     19 template <typename T, bool = std::is_unsigned_v<T>>
     20 struct IntChecker {
     21  static void Assert(T aInt) {
     22    static_assert(std::is_integral_v<T>, "Not an integer!");
     23    MOZ_ASSERT(aInt >= 0);
     24  }
     25 };
     26 
     27 template <typename T>
     28 struct IntChecker<T, true> {
     29  static void Assert(T aInt) {
     30    static_assert(std::is_integral_v<T>, "Not an integer!");
     31  }
     32 };
     33 
     34 }  // namespace detail
     35 
     36 template <typename T>
     37 void AssertNoOverflow(uint64_t aDest, T aArg) {
     38  detail::IntChecker<T>::Assert(aDest);
     39  detail::IntChecker<T>::Assert(aArg);
     40  MOZ_ASSERT(UINT64_MAX - aDest >= uint64_t(aArg));
     41 }
     42 
     43 template <typename T, typename U>
     44 void AssertNoUnderflow(T aDest, U aArg) {
     45  detail::IntChecker<T>::Assert(aDest);
     46  detail::IntChecker<T>::Assert(aArg);
     47  MOZ_ASSERT(uint64_t(aDest) >= uint64_t(aArg));
     48 }
     49 
     50 }  // namespace mozilla::dom::quota
     51 
     52 #endif  // DOM_QUOTA_ASSERTIONSIMPL_H_