NumericStyleValue.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #pragma once
  10. #include <LibWeb/CSS/StyleValue.h>
  11. namespace Web::CSS {
  12. class NumericStyleValue : public StyleValueWithDefaultOperators<NumericStyleValue> {
  13. public:
  14. static ErrorOr<ValueComparingNonnullRefPtr<NumericStyleValue>> create_float(float value)
  15. {
  16. return adopt_nonnull_ref_or_enomem(new (nothrow) NumericStyleValue(value));
  17. }
  18. static ErrorOr<ValueComparingNonnullRefPtr<NumericStyleValue>> create_integer(i64 value)
  19. {
  20. return adopt_nonnull_ref_or_enomem(new (nothrow) NumericStyleValue(value));
  21. }
  22. float number() const
  23. {
  24. return m_value.visit(
  25. [](float value) { return value; },
  26. [](i64 value) { return (float)value; });
  27. }
  28. bool has_integer() const { return m_value.has<i64>(); }
  29. float integer() const { return m_value.get<i64>(); }
  30. virtual ErrorOr<String> to_string() const override;
  31. bool properties_equal(NumericStyleValue const& other) const { return m_value == other.m_value; }
  32. private:
  33. explicit NumericStyleValue(Variant<float, i64> value)
  34. : StyleValueWithDefaultOperators(Type::Numeric)
  35. , m_value(move(value))
  36. {
  37. }
  38. Variant<float, i64> m_value { (i64)0 };
  39. };
  40. }