ScopedOperand.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2024, Andreas Kling <kling@serenityos.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 operand() const { return m_operand; }
  19. private:
  20. Generator& m_generator;
  21. Operand m_operand;
  22. };
  23. class ScopedOperand {
  24. public:
  25. explicit ScopedOperand(Generator& generator, Operand operand)
  26. : m_impl(adopt_ref(*new ScopedOperandImpl(generator, operand)))
  27. {
  28. }
  29. [[nodiscard]] Operand operand() const { return m_impl->operand(); }
  30. operator Operand() const { return operand(); }
  31. [[nodiscard]] bool operator==(ScopedOperand const& other) const { return operand() == other.operand(); }
  32. [[nodiscard]] size_t ref_count() const { return m_impl->ref_count(); }
  33. private:
  34. NonnullRefPtr<ScopedOperandImpl> m_impl;
  35. };
  36. }