Fixed problem in DeviceSettings::strParam, returned wrong string
[avr-sim.git] / src / script / ScriptUtil.h
blob07e22d3be44cde31c440d8e15174c505b2c0447f
1 #ifndef SCRIPT_SCRIPTUTIL_H
2 #define SCRIPT_SCRIPTUTIL_H
4 #include "Scriptable.h"
5 #include "VM.h"
6 #include <string>
8 namespace script {
9 /**
10 * @author Tom Haber
11 * @date May 23, 2008
12 * @brief Method type of scriptable object
14 * Method type of scriptable object. Convenience class
15 * for representing scripting methods on a Scriptable class.
17 template <class T>
18 class ScriptMethod {
19 public:
20 /** Method prototype. */
21 typedef int (T::*FuncType)(const char *funcName);
23 public:
24 /**
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) {}
31 public:
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; }
38 private:
39 const char *name;
40 FuncType func;
43 /**
44 * @author Tom Haber
45 * @date May 23, 2008
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>
53 class ScriptUtil {
54 public:
55 /**
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) {
61 int methodBase = 0;
63 if( n > 0 ) {
64 methodBase = obj->addMethod( methods[0].methodName() );
65 for(int i = 1; i < n ; ++i)
66 obj->addMethod( methods[i].methodName() );
69 return methodBase;
72 /**
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() );
89 else
90 return obj->B::methodCall(i);
94 #endif