src/squirrel_helper.hpp
branchnoai
changeset 10992 4dd4f4327c3a
parent 10734 06f95974ac15
child 11002 3a11ba88eb7c
equal deleted inserted replaced
10978:13fd0364b2c6 10992:4dd4f4327c3a
     7 
     7 
     8 #include <squirrel.h>
     8 #include <squirrel.h>
     9 #include "core/math_func.hpp"
     9 #include "core/math_func.hpp"
    10 #include "economy_type.h"
    10 #include "economy_type.h"
    11 #include "misc/smallvec.h"
    11 #include "misc/smallvec.h"
       
    12 #include "squirrel_helper_type.hpp"
    12 
    13 
    13 /**
    14 /**
    14  * The Squirrel convert routines
    15  * The Squirrel convert routines
    15  */
    16  */
    16 namespace SQConvert {
    17 namespace SQConvert {
    93 	template <> inline int32       GetParam(ForceType<int32>       , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger     tmp; sq_getinteger    (vm, index, &tmp); return tmp; }
    94 	template <> inline int32       GetParam(ForceType<int32>       , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger     tmp; sq_getinteger    (vm, index, &tmp); return tmp; }
    94 	template <> inline bool        GetParam(ForceType<bool>        , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQBool        tmp; sq_getbool       (vm, index, &tmp); return tmp != 0; }
    95 	template <> inline bool        GetParam(ForceType<bool>        , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQBool        tmp; sq_getbool       (vm, index, &tmp); return tmp != 0; }
    95 	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; }
    96 	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; }
    96 	template <> inline void       *GetParam(ForceType<void *>      , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; }
    97 	template <> inline void       *GetParam(ForceType<void *>      , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; }
    97 
    98 
       
    99 	template <> inline Array      *GetParam(ForceType<Array *>,      HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr)
       
   100 	{
       
   101 		SQObject obj;
       
   102 		sq_getstackobj(vm, index, &obj);
       
   103 		sq_pushobject(vm, obj);
       
   104 		sq_pushnull(vm);
       
   105 
       
   106 		SmallVector<int32, 2> data;
       
   107 
       
   108 		while (SQ_SUCCEEDED(sq_next(vm, -2))) {
       
   109 			SQInteger tmp;
       
   110 			if (SQ_SUCCEEDED(sq_getinteger(vm, -1, &tmp))) {
       
   111 				*data.Append() = (int32)tmp;
       
   112 			} else {
       
   113 				// TODO: handle following error properly: "array member is not numeric; ignoring member"
       
   114 			}
       
   115 
       
   116 			sq_pop(vm, 2);
       
   117 		}
       
   118 		sq_pop(vm, 2);
       
   119 
       
   120 		Array *arr = (Array*)MallocT<byte>(sizeof(Array) + sizeof(int32) * data.Length());
       
   121 		arr->size = data.Length();
       
   122 		memcpy(arr->array, data.Begin(), sizeof(int32) * data.Length());
       
   123 
       
   124 		*ptr->Append() = arr;
       
   125 		return arr;
       
   126 	}
       
   127 
    98 	/**
   128 	/**
    99 	* Helper class to recognize the function type (retval type, args) and use the proper specialization
   129 	* Helper class to recognize the function type (retval type, args) and use the proper specialization
   100 	* for SQ callback. The partial specializations for the second arg (Tis_void_retval) are not possible
   130 	* for SQ callback. The partial specializations for the second arg (Tis_void_retval) are not possible
   101 	* on the function. Therefore the class is used instead.
   131 	* on the function. Therefore the class is used instead.
   102 	*/
   132 	*/