/src/serenity/Userland/Libraries/LibJS/Bytecode/ScopedOperand.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2024, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/RefCounted.h> |
10 | | #include <LibJS/Bytecode/Operand.h> |
11 | | |
12 | | namespace JS::Bytecode { |
13 | | |
14 | | class ScopedOperandImpl : public RefCounted<ScopedOperandImpl> { |
15 | | public: |
16 | | ScopedOperandImpl(Generator& generator, Operand operand) |
17 | 135k | : m_generator(generator) |
18 | 135k | , m_operand(operand) |
19 | 135k | { |
20 | 135k | } |
21 | | |
22 | | ~ScopedOperandImpl(); |
23 | | |
24 | 0 | [[nodiscard]] Operand const& operand() const { return m_operand; } |
25 | 801k | [[nodiscard]] Operand& operand() { return m_operand; } |
26 | | |
27 | | private: |
28 | | Generator& m_generator; |
29 | | Operand m_operand; |
30 | | }; |
31 | | |
32 | | class ScopedOperand { |
33 | | public: |
34 | | explicit ScopedOperand(Generator& generator, Operand operand) |
35 | 135k | : m_impl(adopt_ref(*new ScopedOperandImpl(generator, operand))) |
36 | 135k | { |
37 | 135k | } |
38 | | |
39 | 801k | [[nodiscard]] Operand const& operand() const { return m_impl->operand(); } |
40 | 15 | [[nodiscard]] Operand& operand() { return m_impl->operand(); } |
41 | 400k | operator Operand() const { return operand(); } |
42 | | |
43 | 0 | [[nodiscard]] bool operator==(ScopedOperand const& other) const { return operand() == other.operand(); } |
44 | | |
45 | 0 | [[nodiscard]] size_t ref_count() const { return m_impl->ref_count(); } |
46 | | |
47 | | private: |
48 | | NonnullRefPtr<ScopedOperandImpl> m_impl; |
49 | | }; |
50 | | |
51 | | } |