summaryrefslogtreecommitdiffhomepage
path: root/src/script.hpp
blob: 82f164842cee9ddd38f7a361ef16bc46b2431806 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#pragma once
#include "script-enums.hpp"
#include "compat/defs.hpp"
#include "compat/borrowed-ptr.hpp"
#include "src/object-type.hpp"

namespace floormat
{
struct object;
struct Ns;

struct base_script
{
    fm_DECLARE_DELETED_COPY_MOVE_ASSIGNMENTS(base_script);

    virtual StringView name() const = 0;
    virtual const void* id() const = 0;
    virtual object_type type() const = 0;

    constexpr base_script() noexcept = default;
    virtual ~base_script() noexcept;

    static StringView state_name(script_lifecycle x);
    static void _assert_state(script_lifecycle old_state, script_lifecycle s, const char* file, int line);
};

template<typename S, typename Obj>
class Script final
{
    S* ptr;
    script_lifecycle _state;
    static S* make_empty();

public:
    fm_DECLARE_DELETED_COPY_MOVE_ASSIGNMENTS(Script);

    Script();
    ~Script() noexcept;

    // [no-init]        -> do_create()              -> [initializing]
    // [initializing]   -> do_initialize()          -> [created]
    // [created]        -> call()                   -> [created]
    // [created]        -> do_reassign()            -> [no-init]
    // [created]        -> do_destroy_pre()         -> [destroying]
    // [destroying]     -> do_finish_destroy()      -> [torn-down]
    // [torn-down]      -> do_ensure_torn_down()    -> [torn-down]
    // *                -> do_error_unwind()        -> [torn-down]
    // [torn-down]      -> ~script()
    // [no-init]        -> ~script() // for tests

    script_lifecycle state() const;
    S* operator->();
    explicit operator bool() const;

    void do_create(S* ptr);
    void do_create(Pointer<S> ptr);
    void do_initialize(const bptr<Obj>& obj);
    void do_reassign(S* ptr, const bptr<Obj>& obj);
    void do_reassign(Pointer<S> ptr, const bptr<Obj>& obj);
    void do_clear(const bptr<Obj>& obj);
    void do_destroy_pre(const bptr<Obj>& obj, script_destroy_reason r);
    void do_finish_destroy();
    void do_ensure_torn_down();
    void do_error_unwind();
};

} // namespace floormat