2009-01-12 17:11:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-06-01 12:56:18 +00:00
|
|
|
/** @file squirrel_class.hpp Defines templates for converting C++ classes to Squirrel classes */
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
#ifndef SQUIRREL_CLASS_HPP
|
|
|
|
#define SQUIRREL_CLASS_HPP
|
|
|
|
|
2010-08-26 22:01:16 +00:00
|
|
|
#include "squirrel_helper.hpp"
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/**
|
|
|
|
* The template to define classes in Squirrel. It takes care of the creation
|
2011-11-19 15:50:10 +00:00
|
|
|
* and calling of such classes, to minimize the API layer.
|
2009-01-12 17:11:45 +00:00
|
|
|
*/
|
2011-11-29 22:23:33 +00:00
|
|
|
template <class CL, ScriptType ST>
|
2009-01-12 17:11:45 +00:00
|
|
|
class DefSQClass {
|
|
|
|
private:
|
|
|
|
const char *classname;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DefSQClass(const char *_classname) :
|
|
|
|
classname(_classname)
|
|
|
|
{}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This defines a method inside a class for Squirrel.
|
|
|
|
*/
|
|
|
|
template <typename Func>
|
|
|
|
void DefSQMethod(Squirrel *engine, Func function_proc, const char *function_name)
|
|
|
|
{
|
|
|
|
using namespace SQConvert;
|
2011-11-29 22:23:33 +00:00
|
|
|
engine->AddMethod(function_name, DefSQNonStaticCallback<CL, Func, ST>, 0, NULL, &function_proc, sizeof(function_proc));
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This defines a method inside a class for Squirrel, which has access to the 'engine' (experts only!).
|
|
|
|
*/
|
|
|
|
template <typename Func>
|
|
|
|
void DefSQAdvancedMethod(Squirrel *engine, Func function_proc, const char *function_name)
|
|
|
|
{
|
|
|
|
using namespace SQConvert;
|
2011-11-29 22:23:33 +00:00
|
|
|
engine->AddMethod(function_name, DefSQAdvancedNonStaticCallback<CL, Func, ST>, 0, NULL, &function_proc, sizeof(function_proc));
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This defines a method inside a class for Squirrel with defined params.
|
|
|
|
* @note If you define nparam, make sure that he first param is always 'x',
|
|
|
|
* which is the 'this' inside the function. This is hidden from the rest
|
|
|
|
* of the code, but without it calling your function will fail!
|
|
|
|
*/
|
|
|
|
template <typename Func>
|
|
|
|
void DefSQMethod(Squirrel *engine, Func function_proc, const char *function_name, int nparam, const char *params)
|
|
|
|
{
|
|
|
|
using namespace SQConvert;
|
2011-11-29 22:23:33 +00:00
|
|
|
engine->AddMethod(function_name, DefSQNonStaticCallback<CL, Func, ST>, nparam, params, &function_proc, sizeof(function_proc));
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This defines a static method inside a class for Squirrel.
|
|
|
|
*/
|
|
|
|
template <typename Func>
|
|
|
|
void DefSQStaticMethod(Squirrel *engine, Func function_proc, const char *function_name)
|
|
|
|
{
|
|
|
|
using namespace SQConvert;
|
|
|
|
engine->AddMethod(function_name, DefSQStaticCallback<CL, Func>, 0, NULL, &function_proc, sizeof(function_proc));
|
|
|
|
}
|
|
|
|
|
2011-12-19 21:00:32 +00:00
|
|
|
/**
|
|
|
|
* This defines a static method inside a class for Squirrel, which has access to the 'engine' (experts only!).
|
|
|
|
*/
|
|
|
|
template <typename Func>
|
|
|
|
void DefSQAdvancedStaticMethod(Squirrel *engine, Func function_proc, const char *function_name)
|
|
|
|
{
|
|
|
|
using namespace SQConvert;
|
|
|
|
engine->AddMethod(function_name, DefSQAdvancedStaticCallback<CL, Func>, 0, NULL, &function_proc, sizeof(function_proc));
|
|
|
|
}
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/**
|
|
|
|
* This defines a static method inside a class for Squirrel with defined params.
|
|
|
|
* @note If you define nparam, make sure that he first param is always 'x',
|
|
|
|
* which is the 'this' inside the function. This is hidden from the rest
|
|
|
|
* of the code, but without it calling your function will fail!
|
|
|
|
*/
|
|
|
|
template <typename Func>
|
|
|
|
void DefSQStaticMethod(Squirrel *engine, Func function_proc, const char *function_name, int nparam, const char *params)
|
|
|
|
{
|
|
|
|
using namespace SQConvert;
|
|
|
|
engine->AddMethod(function_name, DefSQStaticCallback<CL, Func>, nparam, params, &function_proc, sizeof(function_proc));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Var>
|
|
|
|
void DefSQConst(Squirrel *engine, Var value, const char *var_name)
|
|
|
|
{
|
|
|
|
engine->AddConst(var_name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreRegister(Squirrel *engine)
|
|
|
|
{
|
|
|
|
engine->AddClassBegin(this->classname);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreRegister(Squirrel *engine, const char *parent_class)
|
|
|
|
{
|
|
|
|
engine->AddClassBegin(this->classname, parent_class);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Func, int Tnparam>
|
|
|
|
void AddConstructor(Squirrel *engine, const char *params)
|
|
|
|
{
|
|
|
|
using namespace SQConvert;
|
|
|
|
engine->AddMethod("constructor", DefSQConstructorCallback<CL, Func, Tnparam>, Tnparam, params);
|
|
|
|
}
|
|
|
|
|
2011-12-21 14:55:28 +00:00
|
|
|
void AddSQAdvancedConstructor(Squirrel *engine)
|
|
|
|
{
|
|
|
|
using namespace SQConvert;
|
|
|
|
engine->AddMethod("constructor", DefSQAdvancedConstructorCallback<CL>, 0, NULL);
|
|
|
|
}
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
void PostRegister(Squirrel *engine)
|
|
|
|
{
|
|
|
|
engine->AddClassEnd();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* SQUIRREL_CLASS_HPP */
|