2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
#ifndef LUAOBJECT_H
|
|
|
|
#define LUAOBJECT_H
|
|
|
|
|
2011-08-15 16:06:15 +02:00
|
|
|
#include "declarations.h"
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
/// LuaObject, all script-able classes have it as base
|
|
|
|
class LuaObject : public std::enable_shared_from_this<LuaObject>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LuaObject();
|
2011-08-15 16:06:15 +02:00
|
|
|
virtual ~LuaObject();
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
/// Calls a function or table of functions stored in a lua field, results are pushed onto the stack,
|
|
|
|
/// if any lua error occurs, it will be reported to stdout and return 0 results
|
|
|
|
/// @return the number of results
|
|
|
|
template<typename... T>
|
|
|
|
int callLuaField(const std::string& field, const T&... args);
|
2011-08-20 22:30:41 +02:00
|
|
|
template<typename R, typename... T>
|
|
|
|
R callLuaField(const std::string& field, const T&... args);
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2012-03-25 16:10:15 +02:00
|
|
|
/// Returns true if the lua field exists
|
2012-02-07 05:55:20 +01:00
|
|
|
bool hasLuaField(const std::string& field);
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Sets a field in this lua object
|
|
|
|
template<typename T>
|
|
|
|
void setLuaField(const std::string& key, const T& value);
|
|
|
|
|
|
|
|
/// Gets a field from this lua object
|
|
|
|
template<typename T>
|
|
|
|
T getLuaField(const std::string& key);
|
|
|
|
|
|
|
|
/// Release fields table reference
|
2011-08-20 22:30:41 +02:00
|
|
|
void releaseLuaFieldsTable();
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
/// Sets a field from this lua object, the value must be on the stack
|
|
|
|
void luaSetField(const std::string& key);
|
|
|
|
|
|
|
|
/// Gets a field from this lua object, the result is pushed onto the stack
|
|
|
|
void luaGetField(const std::string& key);
|
|
|
|
|
2012-02-07 01:41:53 +01:00
|
|
|
/// Gets the table containing all stored fields of this lua object, the result is pushed onto the stack
|
|
|
|
void luaGetFieldsTable();
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Returns the number of references of this object
|
|
|
|
/// @note each userdata of this object on lua counts as a reference
|
|
|
|
int getUseCount();
|
|
|
|
|
2012-02-01 16:20:13 +01:00
|
|
|
/// Returns the derived class name, its the same name used in Lua
|
|
|
|
virtual std::string getClassName() const {
|
2012-01-07 18:36:58 +01:00
|
|
|
// TODO: this could be cached for more performance
|
2012-05-28 15:06:26 +02:00
|
|
|
return stdext::demangle_name(typeid(*this).name());
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LuaObjectPtr asLuaObject() { return shared_from_this(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_fieldsTableRef;
|
|
|
|
};
|
|
|
|
|
|
|
|
#include "luainterface.h"
|
|
|
|
|
|
|
|
template<typename... T>
|
|
|
|
int LuaObject::callLuaField(const std::string& field, const T&... args) {
|
2011-11-13 05:49:32 +01:00
|
|
|
// note that the field must be retrieved from this object lua value
|
|
|
|
// to force using the __index metamethod of it's metatable
|
|
|
|
// so cannot use LuaObject::getField here
|
|
|
|
// push field
|
|
|
|
g_lua.pushObject(asLuaObject());
|
|
|
|
g_lua.getField(field);
|
|
|
|
|
|
|
|
if(!g_lua.isNil()) {
|
|
|
|
// the first argument is always this object (self)
|
|
|
|
g_lua.insert(-2);
|
|
|
|
g_lua.polymorphicPush(args...);
|
|
|
|
return g_lua.protectedCall(1 + sizeof...(args));
|
|
|
|
} else {
|
|
|
|
g_lua.pop(2);
|
2011-08-20 22:30:41 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename R, typename... T>
|
|
|
|
R LuaObject::callLuaField(const std::string& field, const T&... args) {
|
|
|
|
R result;
|
|
|
|
int rets = callLuaField(field, args...);
|
|
|
|
if(rets > 0) {
|
|
|
|
assert(rets == 1);
|
|
|
|
result = g_lua.polymorphicPop<R>();
|
2011-08-14 16:09:26 +02:00
|
|
|
} else
|
2011-08-20 22:30:41 +02:00
|
|
|
result = R();
|
|
|
|
return result;
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void LuaObject::setLuaField(const std::string& key, const T& value) {
|
|
|
|
g_lua.polymorphicPush(value);
|
|
|
|
luaSetField(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T LuaObject::getLuaField(const std::string& key) {
|
|
|
|
luaGetField(key);
|
|
|
|
return g_lua.polymorphicPop<T>();
|
|
|
|
}
|
|
|
|
|
2012-01-19 05:12:53 +01:00
|
|
|
#endif
|