Skip to content

Truly independent web browser

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

#pragma once

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

namespace Web::CSS {

class WEB_API KeywordStyleValue : public StyleValueWithDefaultOperators<KeywordStyleValue> {
public:
    static ValueComparingNonnullRefPtr<KeywordStyleValue const> create(Keyword keyword)
    {
        // Keyword values are immutable and the keyword set is small, so every keyword is
        // interned: one instance per keyword for the lifetime of the process, for all of its
        // threads. Repeated creations are then allocation-free, and identical keywords are
        // pointer-identical.
        static auto const& instances = *[] {
            auto* instances = new (nothrow) Vector<NonnullRefPtr<KeywordStyleValue const>>();
            instances->ensure_capacity(number_of_keywords);
            for (size_t i = 0; i < number_of_keywords; ++i)
                instances->unchecked_append(*make_immortal(new (nothrow) KeywordStyleValue(static_cast<Keyword>(i))));
            return instances;
        }();
        return instances[to_underlying(keyword)];
    }
    virtual ~KeywordStyleValue() override = default;

    Keyword keyword() const { return static_cast<Keyword>(m_value->keyword.keyword); }

    static bool is_color(Keyword);
    bool has_color() const;

private:
    friend class StyleValue;

    explicit KeywordStyleValue(StyleValueFFI::StyleValueData const* data)
        : StyleValueWithDefaultOperators(Type::Keyword, data)
    {
    }

    explicit KeywordStyleValue(Keyword keyword)
        : StyleValueWithDefaultOperators(Type::Keyword, StyleValueFFI::rust_style_value_create_keyword(to_underlying(keyword)))
    {
    }
};

}