(svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others noai
authortruelight
Fri, 13 Apr 2007 09:52:18 +0000
branchnoai
changeset 9588 01b2435c977b
parent 9587 b0716d4c0128
child 9589 2fbda08db406
(svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
src/squirrel.cpp
src/squirrel.hpp
src/squirrel_class.hpp
--- a/src/squirrel.cpp	Thu Apr 12 14:35:24 2007 +0000
+++ b/src/squirrel.cpp	Fri Apr 13 09:52:18 2007 +0000
@@ -98,6 +98,19 @@
 	sq_newclass(this->vm, SQFalse);
 }
 
+void Squirrel::AddClassBegin(const char *class_name, const char *parent_class)
+{
+	sq_pushroottable(this->vm);
+	sq_pushstring(this->vm, OTTD2FS(class_name), -1);
+	sq_pushstring(this->vm, OTTD2FS(parent_class), -1);
+	if (SQ_FAILED(sq_get(this->vm, -3))) {
+		DEBUG(misc, 0, "[squirrel] Failed to initialize class '%s' based on parent class '%s'", class_name, parent_class);
+		DEBUG(misc, 0, "[squirrel] Make sure that '%s' exists before trying to define '%s'", parent_class, class_name);
+		return;
+	}
+	sq_newclass(this->vm, SQTrue);
+}
+
 void Squirrel::AddClassEnd()
 {
 	sq_newslot(vm, -3, SQFalse);
--- a/src/squirrel.hpp	Thu Apr 12 14:35:24 2007 +0000
+++ b/src/squirrel.hpp	Fri Apr 13 09:52:18 2007 +0000
@@ -65,6 +65,12 @@
 	void AddClassBegin(const char *class_name);
 
 	/**
+	 * Adds a class to the global scope, extending 'parent_class'.
+	 * Make sure to call AddClassEnd when you are done adding methods.
+	 */
+	void AddClassBegin(const char *class_name, const char *parent_class);
+
+	/**
 	 * Finishes adding a class to the global scope. If this isn't called, no
 	 *  class is really created.
 	 */
--- a/src/squirrel_class.hpp	Thu Apr 12 14:35:24 2007 +0000
+++ b/src/squirrel_class.hpp	Fri Apr 13 09:52:18 2007 +0000
@@ -95,6 +95,11 @@
 		engine->AddClassBegin(this->classname);
 	}
 
+	void PreRegister(Squirrel *engine, const char *parent_class)
+	{
+		engine->AddClassBegin(this->classname, parent_class);
+	}
+
 	void AddConstructor(Squirrel *engine)
 	{
 		engine->AddMethod("constructor", &DefSQClass::Constructor);