1 #ifndef SCRIPT_SCRIPTUTIL_H
2 #define SCRIPT_SCRIPTUTIL_H
4 #include "Scriptable.h"
12 * @brief Method type of scriptable object
14 * Method type of scriptable object. Convenience class
15 * for representing scripting methods on a Scriptable class.
20 /** Method prototype. */
21 typedef int (T::*FuncType
)(const char *funcName
);
25 Creates method with specified name and function address.
26 Note that the name is *not* copied.
28 ScriptMethod(const char *name
, FuncType func
)
29 : name(name
), func(func
) {}
32 /** Returns name of the method. */
33 const char *methodName() const { return name
; }
35 /** Returns function address of the method. */
36 FuncType
function() const { return func
; }
46 * @brief Helper functions for scripting.
48 * Helper functions for scripting.
49 * @param T Derived scriptable class.
50 * @param B Base scriptable class.
52 template <class T
, class B
>
56 Adds n C++ methods to be usable from the script.
58 @return Base index for the added methods.
60 static int addMethods(T
*obj
, ScriptMethod
<T
> *methods
, int n
) {
64 methodBase
= obj
->addMethod( methods
[0].methodName() );
65 for(int i
= 1; i
< n
; ++i
)
66 obj
->addMethod( methods
[i
].methodName() );
73 Calls C++ method. If the index is out of range then base
74 class (B) methodCall is called.
76 @param obj The object calling this method.
77 @param vm Script virtual machine requesting the method execution.
78 @param i Index of the method to be executed.
79 @param methodBase Base index for methods of this class.
80 @param methods An array of methods of this class.
81 @param n Number of methods in the array.
82 @return Number of return values in the script stack.
84 static int methodCall(T
*obj
, int i
, int methodBase
,
85 ScriptMethod
<T
>* methods
, int n
) {
86 int index
= i
- methodBase
;
87 if( index
>= 0 && index
< n
)
88 return ( obj
->*methods
[index
].function() )(methods
[index
].methodName() );
90 return obj
->B::methodCall(i
);