(svn r13546) [NoAI] -Codechange: add support to pass arrays with integers to C++ functions. noai
authorrubidium
Tue, 17 Jun 2008 11:46:09 +0000
branchnoai
changeset 10992 4dd4f4327c3a
parent 10978 13fd0364b2c6
child 10993 203b90795f80
(svn r13546) [NoAI] -Codechange: add support to pass arrays with integers to C++ functions.
src/ai/api/squirrel_export.awk
src/squirrel_helper.hpp
src/squirrel_helper_type.hpp
--- a/src/ai/api/squirrel_export.awk	Mon Jun 16 14:43:19 2008 +0000
+++ b/src/ai/api/squirrel_export.awk	Tue Jun 17 11:46:09 2008 +0000
@@ -332,6 +332,10 @@
 				types = types "s"
 			} else if (match(params[len], "^void")) {
 				types = types "p"
+			} else if (match(params[len], "^Array")) {
+				types = types "a"
+			} else if (match(params[len], "^struct Array")) {
+				types = types "a"
 			} else {
 				types = types "x"
 			}
--- a/src/squirrel_helper.hpp	Mon Jun 16 14:43:19 2008 +0000
+++ b/src/squirrel_helper.hpp	Tue Jun 17 11:46:09 2008 +0000
@@ -9,6 +9,7 @@
 #include "core/math_func.hpp"
 #include "economy_type.h"
 #include "misc/smallvec.h"
+#include "squirrel_helper_type.hpp"
 
 /**
  * The Squirrel convert routines
@@ -95,6 +96,35 @@
 	template <> inline const char *GetParam(ForceType<const char *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { const SQChar *tmp; sq_getstring     (vm, index, &tmp); char *tmp_str = strdup(FS2OTTD(tmp)); *ptr->Append() = (void *)tmp_str; return tmp_str; }
 	template <> inline void       *GetParam(ForceType<void *>      , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; }
 
+	template <> inline Array      *GetParam(ForceType<Array *>,      HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr)
+	{
+		SQObject obj;
+		sq_getstackobj(vm, index, &obj);
+		sq_pushobject(vm, obj);
+		sq_pushnull(vm);
+
+		SmallVector<int32, 2> data;
+
+		while (SQ_SUCCEEDED(sq_next(vm, -2))) {
+			SQInteger tmp;
+			if (SQ_SUCCEEDED(sq_getinteger(vm, -1, &tmp))) {
+				*data.Append() = (int32)tmp;
+			} else {
+				// TODO: handle following error properly: "array member is not numeric; ignoring member"
+			}
+
+			sq_pop(vm, 2);
+		}
+		sq_pop(vm, 2);
+
+		Array *arr = (Array*)MallocT<byte>(sizeof(Array) + sizeof(int32) * data.Length());
+		arr->size = data.Length();
+		memcpy(arr->array, data.Begin(), sizeof(int32) * data.Length());
+
+		*ptr->Append() = arr;
+		return arr;
+	}
+
 	/**
 	* Helper class to recognize the function type (retval type, args) and use the proper specialization
 	* for SQ callback. The partial specializations for the second arg (Tis_void_retval) are not possible
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/squirrel_helper_type.hpp	Tue Jun 17 11:46:09 2008 +0000
@@ -0,0 +1,13 @@
+/* $Id$ */
+
+/** @file squirrel_helper_type.hpp Helper structs for converting Squirrel data structures to C++. */
+
+#ifndef SQUIRREL_HELPER_TYPE_HPP
+#define SQUIRREL_HELPER_TYPE_HPP
+
+struct Array {
+	int32 size;
+	int32 array[VARARRAY_SIZE];
+};
+
+#endif /* SQUIRREL_HELPER_TYPE_HPP */