LNXPWRBN/button/input0
     ˆÄ�þDá^YÀ67d óÃƒ}÷ÂÁÀ¿ &è    /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * 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/. */

#ifndef ProportionValue_h
#define ProportionValue_h

#include "mozilla/Attributes.h"

#include <algorithm>
#include <limits>

namespace mozilla {

// Class storing a proportion value between 0 and 1, effectively 0% to 100%.
// The public interface deals with doubles, but internally the value is encoded
// in an integral type, so arithmetic operations are fast.
// It also supports an invalid value: Use MakeInvalid() to construct, it infects
// any operation, and gets converted to a signaling NaN.
class ProportionValue {
 public:
  using UnderlyingType = uint32_t;

  // Default-construct at 0%.
  constexpr ProportionValue()
      // This `noexcept` is necessary to avoid a build error when encapsulating
      // `ProportionValue` in `std::Atomic`:
      // "use of deleted function
      // 'constexpr std::atomic<mozilla::ProportionValue>::atomic()"
      // because the default `std::atomic<T>::atomic()` constructor is marked:
      // `noexcept(std::is_nothrow_default_constructible_v<T>)`
      // and therefore this default constructor here must be explicitly marked
      // `noexcept` as well.
      noexcept
      : mIntegralValue(0u) {}

  // Construct a ProportionValue with the given value, clamped to 0..1.
  // Note that it's constexpr, so construction from literal numbers should incur
  // no runtime costs.
  // If `aValue` is NaN, behavior is undefined! Use `MakeInvalid()` instead.
  constexpr explicit ProportionValue(double aValue)
      : mIntegralValue(UnderlyingType(std::clamp(aValue, 0.0, 1.0) * scMaxD)) {}

  [[nodiscard]] static constexpr ProportionValue MakeInvalid() {
    return ProportionValue(scInvalidU, Internal{});
  }

  [[nodiscard]] constexpr double ToDouble() const {
    return IsInvalid() ? std::numeric_limits<double>::signaling_NaN()
                       : (double(mIntegralValue) * scInvMaxD);
  }

  // Retrieve the underlying integral value, for storage or testing purposes.
  [[nodiscard]] constexpr UnderlyingType ToUnderlyingType() const {
    return mIntegralValue;
  };

  // Re-construct a ProportionValue from an underlying integral value.
  [[nodiscard]] static constexpr ProportionValue FromUnderlyingType(
      UnderlyingType aUnderlyingType) {
    return ProportionValue(
        (aUnderlyingType <= scMaxU) ? aUnderlyingType : scInvalidU, Internal{});
  }

  [[nodiscard]] constexpr bool IsExactlyZero() const {
    return mIntegralValue == 0u;
  }

  [[nodiscard]] constexpr bool IsExactlyOne() const {
    return mIntegralValue == scMaxU;
  }

  [[nodiscard]] constexpr bool IsValid() const {
    // Compare to the maximum value, not just exactly scInvalidU, to catch any
    // kind of invalid state.
    return mIntegralValue <= scMaxU;
  }
  [[nodiscard]] constexpr bool IsInvalid() const {
    // Compare to the maximum value, not just exactly scInvalidU, to catch any
    // kind of invalid state.
    return mIntegralValue > scMaxU;
  }

  // Strict comparisons based on the underlying integral value. Use
  // `CompareWithin` instead to make fuzzy comparisons.
  // `ProportionValue::MakeInvalid()`s are equal, and greater than anything
  // else; Best to avoid comparisons, and first use IsInvalid() instead.
#define OPERATOR_COMPARISON(CMP)                                  \
  [[nodiscard]] constexpr friend bool operator CMP(               \
      const ProportionValue& aLHS, const ProportionValue& aRHS) { \
    return aLHS.mIntegralValue CMP aRHS.mIntegralValue;           \
  }
  OPERATOR_COMPARISON(==)
  OPERATOR_COMPARISON(!=)
  OPERATOR_COMPARISON(<)
  OPERATOR_COMPARISON(<=)
  OPERATOR_COMPARISON(>)
  OPERATOR_COMPARISON(>=)
#undef OPERATOR_COMPARISON

  // Arithmetic operations + - *, all working on t