Skip to content

Truly independent web browser

1.5 KB
Raw Blame
/*
 * Copyright (c) 2018-2020, Andreas Kling <[email protected]>
 * Copyright (c) 2021, Tobias Christiansen <[email protected]>
 * Copyright (c) 2021-2025, Sam Atkins <[email protected]>
 * Copyright (c) 2022-2023, MacDue <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibWeb/CSS/StyleValues/StyleValue.h>
#include <LibWeb/Export.h>

namespace Web::CSS {

class WEB_API NumberStyleValue final : public StyleValue {
public:
    static ValueComparingNonnullRefPtr<NumberStyleValue const> create(double value)
    {
        // Zero and one dominate real-world number values (opacity, alpha, flex factors), so
        // those two are interned.
        if (value == 0 && !signbit(value)) {
            static auto const& zero_instance = *make_immortal(new (nothrow) NumberStyleValue(0.0));
            return zero_instance;
        }
        if (value == 1) {
            static auto const& one_instance = *make_immortal(new (nothrow) NumberStyleValue(1));
            return one_instance;
        }
        return adopt_ref(*new (nothrow) NumberStyleValue(value));
    }

    double number() const { return m_value->number.value; }

private:
    friend class StyleValue;

    explicit NumberStyleValue(StyleValueFFI::StyleValueData const* data)
        : StyleValue(Type::Number, data)
    {
    }

    explicit NumberStyleValue(double value)
        : StyleValue(Type::Number, StyleValueFFI::rust_style_value_create_number(value))
    {
    }
};

}