1/*
2 * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#ifndef LUAOBJECT_H
24#define LUAOBJECT_H
25
26#include <framework/util/stats.h>
27#include "declarations.h"
28
29/// LuaObject, all script-able classes have it as base
30// @bindclass
31class LuaObject : public stdext::shared_object
32{
33public:
34 LuaObject();
35 virtual ~LuaObject();
36
37 template<typename T>
38 void connectLuaField(const std::string& field, const std::function<T>& f, bool pushFront = false);
39
40 /// Calls a function or table of functions stored in a lua field, results are pushed onto the stack,
41 /// if any lua error occurs, it will be reported to stdout and return 0 results
42 /// @return the number of results
43 template<typename... T>
44 int luaCallLuaField(const std::string& field, const T&... args);
45
46 template<typename R, typename... T>
47 R callLuaField(const std::string& field, const T&... args);
48 template<typename... T>
49 void callLuaField(const std::string& field, const T&... args);
50
51 /// Returns true if the lua field exists
52 bool hasLuaField(const std::string& field);
53
54 /// Sets a field in this lua object
55 template<typename T>
56 void setLuaField(const std::string& key, const T& value);
57
58 /// Gets a field from this lua object
59 template<typename T>
60 T getLuaField(const std::string& key);
61
62 /// Release fields table reference
63 void releaseLuaFieldsTable();
64
65 /// Sets a field from this lua object, the value must be on the stack
66 void luaSetField(const std::string& key);
67
68 /// Gets a field from this lua object, the result is pushed onto the stack
69 void luaGetField(const std::string& key);
70
71 /// Get object's metatable
72 void luaGetMetatable();
73
74 /// Gets the table containing all stored fields of this lua object, the result is pushed onto the stack
75 void luaGetFieldsTable();
76
77 /// Returns the number of references of this object
78 /// @note each userdata of this object on lua counts as a reference
79 int getUseCount();
80
81 /// Returns the derived class name, its the same name used in Lua
82 std::string getClassName();
83
84 LuaObjectPtr asLuaObject() { return static_self_cast<LuaObject>(); }
85
86 void operator=(const LuaObject& other) { }
87
88private:
89 int m_fieldsTableRef;
90};
91
92template<typename F>
93void connect(const LuaObjectPtr& obj, const std::string& field, const std::function<F>& f, bool pushFront = false);
94
95template<typename Lambda>
96typename std::enable_if<std::is_constructible<decltype(&Lambda::operator())>::value, void>::type
97connect(const LuaObjectPtr& obj, const std::string& field, const Lambda& f, bool pushFront = false);
98
99#include "luainterface.h"
100
101template<typename T>
102void LuaObject::connectLuaField(const std::string& field, const std::function<T>& f, bool pushFront)
103{
104 luaGetField(field);
105 if(g_lua.isTable()) {
106 if(pushFront)
107 g_lua.pushInteger(1);
108 push_luavalue(f);
109 g_lua.callGlobalField("table","insert");
110 } else {
111 if(g_lua.isNil()) {
112 push_luavalue(f);
113 luaSetField(field);
114 g_lua.pop();
115 } else if(g_lua.isFunction()) {
116 g_lua.newTable();
117 g_lua.insert(-2);
118 g_lua.rawSeti(1);
119 push_luavalue(f);
120 g_lua.rawSeti(2);
121 luaSetField(field);
122 }
123 }
124}
125
126// connect for std::function
127template<typename F>
128void connect(const LuaObjectPtr& obj, const std::string& field, const std::function<F>& f, bool pushFront) {
129 obj->connectLuaField<F>(field, f, pushFront);
130}
131
132namespace luabinder {
133 template<typename F>
134 struct connect_lambda;
135
136 template<typename Lambda, typename Ret, typename... Args>
137 struct connect_lambda<Ret(Lambda::*)(Args...) const> {
138 static void call(const LuaObjectPtr& obj, const std::string& field, const Lambda& f, bool pushFront) {
139 connect(obj, field, std::function<Ret(Args...)>(f), pushFront);
140 }
141 };
142};
143
144// connect for lambdas
145template<typename Lambda>
146typename std::enable_if<std::is_constructible<decltype(&Lambda::operator())>::value, void>::type
147connect(const LuaObjectPtr& obj, const std::string& field, const Lambda& f, bool pushFront) {
148 typedef decltype(&Lambda::operator()) F;
149 luabinder::connect_lambda<F>::call(obj, field, f, pushFront);
150}
151
152template<typename... T>
153int LuaObject::luaCallLuaField(const std::string& field, const T&... args) {
154 AutoStat s(STATS_LUA, getClassName() + ":" + field);
155
156 // note that the field must be retrieved from this object lua value
157 // to force using the __index metamethod of it's metatable
158 // so cannot use LuaObject::getField here
159 // push field
160 //AutoStat s(STATS_LUA, field);
161 g_lua.pushObject(asLuaObject());
162 g_lua.getField(field);
163
164 int ret = 0;
165 if(!g_lua.isNil()) {
166 // the first argument is always this object (self)
167 g_lua.insert(-2);
168 int numArgs = g_lua.polymorphicPush(args...);
169 ret = g_lua.signalCall(1 + numArgs);
170 } else {
171 g_lua.pop(2);
172 }
173
174 return ret;
175}
176
177template<typename R, typename... T>
178R LuaObject::callLuaField(const std::string& field, const T&... args) {
179 R result;
180 int rets = luaCallLuaField(field, args...);
181 if(rets > 0) {
182 VALIDATE(rets == 1);
183 result = g_lua.polymorphicPop<R>();
184 } else
185 result = R();
186 return result;
187}
188
189template<typename... T>
190void LuaObject::callLuaField(const std::string& field, const T&... args) {
191 int rets = luaCallLuaField(field, args...);
192 if(rets > 0)
193 g_lua.pop(rets);
194}
195
196template<typename T>
197void LuaObject::setLuaField(const std::string& key, const T& value) {
198 g_lua.polymorphicPush(value);
199 luaSetField(key);
200}
201
202template<typename T>
203T LuaObject::getLuaField(const std::string& key) {
204 luaGetField(key);
205 return g_lua.polymorphicPop<T>();
206}
207
208#endif
209