Wrappable.h 858 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/WeakPtr.h>
  8. #include <LibJS/Heap/Heap.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibWeb/Forward.h>
  11. namespace Web::Bindings {
  12. class Wrappable {
  13. public:
  14. virtual ~Wrappable();
  15. void set_wrapper(Wrapper&);
  16. Wrapper* wrapper() { return m_wrapper; }
  17. const Wrapper* wrapper() const { return m_wrapper; }
  18. private:
  19. WeakPtr<Wrapper> m_wrapper;
  20. };
  21. template<class NativeObject>
  22. inline Wrapper* wrap_impl(JS::GlobalObject& global_object, NativeObject& native_object)
  23. {
  24. if (!native_object.wrapper()) {
  25. native_object.set_wrapper(*global_object.heap().allocate<typename NativeObject::WrapperType>(global_object, global_object, native_object));
  26. }
  27. return native_object.wrapper();
  28. }
  29. }