ScopedOperand.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefCounted.h>
  8. #include <LibJS/Bytecode/Operand.h>
  9. namespace JS::Bytecode {
  10. class ScopedOperandImpl : public RefCounted<ScopedOperandImpl> {
  11. public:
  12. ScopedOperandImpl(Generator& generator, Operand operand)
  13. : m_generator(generator)
  14. , m_operand(operand)
  15. {
  16. }
  17. ~ScopedOperandImpl();
  18. [[nodiscard]] Operand const& operand() const { return m_operand; }
  19. [[nodiscard]] Operand& operand() { return m_operand; }
  20. private:
  21. Generator& m_generator;
  22. Operand m_operand;
  23. };
  24. class ScopedOperand {
  25. public:
  26. explicit ScopedOperand(Generator& generator, Operand operand)
  27. : m_impl(adopt_ref(*new ScopedOperandImpl(generator, operand)))
  28. {
  29. }
  30. [[nodiscard]] Operand const& operand() const { return m_impl->operand(); }
  31. [[nodiscard]] Operand& operand() { return m_impl->operand(); }
  32. operator Operand() const { return operand(); }
  33. [[nodiscard]] bool operator==(ScopedOperand const& other) const { return operand() == other.operand(); }
  34. [[nodiscard]] size_t ref_count() const { return m_impl->ref_count(); }
  35. private:
  36. NonnullRefPtr<ScopedOperandImpl> m_impl;
  37. };
  38. }