Etterna 0.74.4
Loading...
Searching...
No Matches
LuaReference.h
1#ifndef LUA_REFERENCE_H
2#define LUA_REFERENCE_H
3
4#include "Etterna/Singletons/LuaManager.h"
5
6struct lua_State;
7using Lua = lua_State;
10{
11 public:
13 virtual ~LuaReference();
14
15 /* Copying a reference makes a new reference pointing to the same object. */
16 LuaReference(const LuaReference& cpy);
17 /* Moving a reference does not make a new reference and leaves the
18 * moved-from value with LUA_NOREF. */
20 auto operator=(const LuaReference& cpy) -> LuaReference&;
21
22 // Convenience constructor.
23 LuaReference(Lua* L)
24 : m_iReference(LUA_NOREF)
25 {
26 SetFromStack(L);
27 }
28
29 void swap(LuaReference& other)
30 {
31 LuaReference temp = *this;
32 *this = other;
33 other = temp;
34 }
35
36 /* Create a reference pointing to the item at the top of the stack, and pop
37 * the stack. */
38 void SetFromStack(Lua* L);
39 void SetFromNil();
40
41 /* Evaluate an expression that returns an object; store the object in a
42 * reference. For example, evaluating "{ 1, 2, 3 }" will result in a
43 * reference to a table. On success, return true. On error, set to nil and
44 * return false. */
45 auto SetFromExpression(const std::string& sExpression) -> bool;
46
48 void DeepCopy();
49
50 /* Push the referenced object onto the stack. If not set (or set to nil),
51 * push nil. */
52 virtual void PushSelf(Lua* L) const;
53
59 [[nodiscard]] auto IsSet() const -> bool;
63 [[nodiscard]] auto IsNil() const -> bool;
64 void Unset() { Unregister(); }
65
66 /* Return the referenced type, or LUA_TNONE if not set. */
67 [[nodiscard]] auto GetLuaType() const -> int;
68
69 auto GetIdentifier() -> int { return m_iReference; }
70
71 [[nodiscard]] auto Serialize() const -> std::string;
72
73 template<typename T>
74 static auto Create(const T& val) -> LuaReference
75 {
76 Lua* L = LUA->Get();
77 LuaReference ref;
78 LuaHelpers::Push(L, val);
79 ref.SetFromStack(L);
80 LUA->Release(L);
81
82 return ref;
83 }
84
85 template<class T>
86 static auto CreateFromPush(T& obj) -> LuaReference
87 {
88 Lua* L = LUA->Get();
89 LuaReference ref;
90 obj.PushSelf(L);
91 ref.SetFromStack(L);
92 LUA->Release(L);
93
94 return ref;
95 }
96
97 private:
98 void Unregister();
99 int m_iReference;
100};
101
102using apActorCommands = std::shared_ptr<LuaReference>;
103
104class LuaTable : public LuaReference
105{
106 public:
107 LuaTable();
108
109 /* Get the key with the given name, and push it on the stack. */
110 void Get(Lua* L, const std::string& sKey);
111
112 /* Set a key by the given name to a value on the stack, and pop the value
113 * off the stack. */
114 void Set(Lua* L, const std::string& sKey);
115};
116
117#endif
A self-cleaning Lua reference.
Definition LuaReference.h:10
auto IsSet() const -> bool
Determine if the reference is set.
Definition LuaReference.cpp:97
auto IsNil() const -> bool
Determine if the reference is nil.
Definition LuaReference.cpp:103
void DeepCopy()
Deep-copy tables, detaching this reference from any others.
Definition LuaReference.cpp:70
Definition LuaReference.h:105