1/**
2 * Copyright (c) 2006-2023 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20
21#ifndef LOVE_REFERENCE_H
22#define LOVE_REFERENCE_H
23
24struct lua_State;
25
26namespace love
27{
28
29/**
30 * This class wraps the reference functionality built into
31 * Lua, which allows C++ code to refer to Lua variables.
32 **/
33class Reference
34{
35public:
36
37 /**
38 * Creates the reference object, but does not create
39 * the actual reference.
40 **/
41 Reference();
42
43 /**
44 * Creates the object and a reference to the value
45 * on the top of the stack.
46 **/
47 Reference(lua_State *L);
48
49 /**
50 * Deletes the reference, if any.
51 **/
52 virtual ~Reference();
53
54 /**
55 * Creates a reference to the value on the
56 * top of the stack.
57 **/
58 void ref(lua_State *L);
59
60 /**
61 * Unrefs the reference, if any.
62 **/
63 void unref();
64
65 /**
66 * Pushes the referred value onto the stack of the specified Lua coroutine.
67 * NOTE: The coroutine *must* belong to the same Lua state that was used for
68 * Reference::ref.
69 **/
70 void push(lua_State *L);
71
72private:
73
74 // A pinned coroutine (probably the main thread) belonging to the Lua state
75 // in which the reference resides.
76 lua_State *pinnedL;
77
78 // Index to the Lua reference.
79 int idx;
80};
81
82} // love
83
84#endif // LOVE_REFERENCE_H
85