summaryrefslogtreecommitdiff
path: root/src/script/squirrel_class.hpp
diff options
context:
space:
mode:
authortruebrain <truebrain@openttd.org>2009-01-12 17:11:45 +0000
committertruebrain <truebrain@openttd.org>2009-01-12 17:11:45 +0000
commita3dd7506d377b1434f913bd65c019eed52b64b6e (patch)
treeced1a262eb143ad6e64ec02f4a4c89835c0c32fd /src/script/squirrel_class.hpp
parent9294f9616866b9778c22076c19b5a32b4f85f788 (diff)
downloadopenttd-a3dd7506d377b1434f913bd65c019eed52b64b6e.tar.xz
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
NoAI is an API (a framework) to build your own AIs in. See: http://wiki.openttd.org/wiki/index.php/AI:Main_Page With many thanks to: - glx and Rubidium for their syncing, feedback and hard work - Yexo for his feedback, patches, and AIs which tested the system very deep - Morloth for his feedback and patches - TJIP for hosting a challenge which kept NoAI on track - All AI authors for testing our AI API, and all other people who helped in one way or another -Remove: all old AIs and their cheats/hacks
Diffstat (limited to 'src/script/squirrel_class.hpp')
-rw-r--r--src/script/squirrel_class.hpp116
1 files changed, 116 insertions, 0 deletions
diff --git a/src/script/squirrel_class.hpp b/src/script/squirrel_class.hpp
new file mode 100644
index 000000000..20e838089
--- /dev/null
+++ b/src/script/squirrel_class.hpp
@@ -0,0 +1,116 @@
+/* $Id$ */
+
+/** @file squirrel_class.cpp defines templates for converting C++ classes to Squirrel classes */
+
+#ifndef SQUIRREL_CLASS_HPP
+#define SQUIRREL_CLASS_HPP
+
+#if (__GNUC__ == 2)
+/* GCC 2.95 doesn't like to have SQConvert::DefSQStaticCallback inside a
+ * template (it gives an internal error 373). Above that, it doesn't listen
+ * to 'using namespace' inside a function of a template. So for GCC 2.95 we
+ * do it in the global space to avoid compiler errors. */
+using namespace SQConvert;
+#endif /* __GNUC__ == 2 */
+
+/**
+ * The template to define classes in Squirrel. It takes care of the creation
+ * and calling of such classes, to make the AI Layer cleaner while having a
+ * powerful script as possible AI language.
+ */
+template <class CL>
+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;
+ engine->AddMethod(function_name, DefSQNonStaticCallback<CL, Func>, 0, NULL, &function_proc, sizeof(function_proc));
+ }
+
+ /**
+ * 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;
+ engine->AddMethod(function_name, DefSQAdvancedNonStaticCallback<CL, Func>, 0, NULL, &function_proc, sizeof(function_proc));
+ }
+
+ /**
+ * 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;
+ engine->AddMethod(function_name, DefSQNonStaticCallback<CL, Func>, nparam, params, &function_proc, sizeof(function_proc));
+ }
+
+ /**
+ * 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));
+ }
+
+ /**
+ * 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);
+ }
+
+ void PostRegister(Squirrel *engine)
+ {
+ engine->AddClassEnd();
+ }
+};
+
+#endif /* SQUIRREL_CLASS_HPP */