summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Bindings/Wrappable.h
blob: 36a581f3998f0eaace950dce8a56c8d1837d6555 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/WeakPtr.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibWeb/Forward.h>

namespace Web::Bindings {

class Wrappable {
public:
    virtual ~Wrappable() = default;

    void set_wrapper(Wrapper&);
    Wrapper* wrapper() { return m_wrapper; }
    const Wrapper* wrapper() const { return m_wrapper; }

private:
    WeakPtr<Wrapper> m_wrapper;
};

template<class NativeObject>
inline Wrapper* wrap_impl(JS::GlobalObject& global_object, NativeObject& native_object)
{
    if (!native_object.wrapper()) {
        native_object.set_wrapper(*global_object.heap().allocate<typename NativeObject::WrapperType>(global_object, global_object, native_object));
    }
    return native_object.wrapper();
}

}