Fixed problem in DeviceSettings::strParam, returned wrong string
[avr-sim.git] / src / script / Scriptable.h
bloba21fc59c01a18b6fb44752490dfbb73d1b4c0df5
1 #ifndef SCRIPT_SCRIPTABLE_H
2 #define SCRIPT_SCRIPTABLE_H
5 #include "Table.h"
7 namespace script {
8 class VM;
11 /**
12 @author Tom Haber
13 @brief Scriptable object base
15 Base class for C++ objects which can interact with scripts.
16 For each C++ Scriptable object there is a Lua entity, table.
17 Both Lua and C++ methods can be added to the table so that
18 C++ methods can call Lua methods and vice versa.
20 class Scriptable {
21 public:
22 /**
23 Initializes scriptable object in specified environment.
25 @param vm Script execution environment.
27 Scriptable(VM *vm);
28 virtual ~Scriptable();
30 public:
31 /**
32 Compiles script from ASCII-text file to executable code.
34 @exception ScriptException
36 void compileFile(const char *name);
38 /**
39 Compiles script from zero-terminated string to executable code.
41 @exception ScriptException
43 void compileSource(const std::string & src);
45 /**
46 Compiles script from buffer to executable code.
48 @exception ScriptException
50 void compileSource(const char *buffer, int size);
52 public:
53 void registerClass(const char *name);
54 void unregisterClass(const char *name);
56 /**
57 Adds a (C++) member function to be usable from scripts.
58 When the function is called from a script, virtual methodCall()
59 is executed and unique function identifier is passed as parameter.
61 @return Unique function identifier (index).
62 @see methodCall
64 int addMethod(const std::string & name);
66 /**
67 Pushes a script object member function followed by
68 script object table to the script stack.
69 To call a script function, first push the function
70 with this (pushMethod), then push arguments (pushParam)
71 and finally call(nargs, nresults) method.
73 @exception ScriptException
74 @see call
75 @see pushParam
77 void pushMethod(const char *name) const;
78 void pushMethod(const std::string & name) const;
80 /**
81 Pushes the script object table to the script stack.
83 void pushTable() const;
85 /**
86 Calls a script function using this object as global environment.
87 Method and parameters must be pushed to script stack before the call.
88 Specified number of results is left to the stack after execution.
90 @exception ScriptException
91 @see pushMethod
92 @see pushParam
94 void call(int nargs, int nresults) const;
96 /**
97 When a (C++) member function is called from a script,
98 this function is executed and unique function identifier
99 is passed as parameter. Derived classes must override
100 this if they add new scriptable functions.
102 @param vm Script virtual machine executing the method.
103 @param i Unique function identifier (index).
104 @return Number of arguments returned in the script stack.
105 @exception ScriptException
106 @see addMethod
108 virtual int methodCall(int i);
111 Returns true if the object has a script function with
112 specified name.
114 bool hasMethod(const std::string & name) const;
115 bool hasMethod(const char *name) const;
118 Checks from script stack that the tags of the parameters passed
119 to the (C++) method match specified sequence of tags.
121 @param tags Array of type tags to check.
122 @param n Maximum number of parameters.
123 @param opt Number of optional parameters (at the end).
124 @return true if the parameter tags match, false otherwise.
126 bool hasParams(const int *tags, int n, int opt = 0) const;
128 /** Returns script execution environment of the object. */
129 VM *vmPtr() const;
132 Returns number of (C++) member functions there is in the
133 script object.
135 int nrMethods() const;
137 /** Prints debug info about functions in the object table. */
138 // void printFunctions(const std::string & objectType) const;
141 Returns C++ 'this' ptr from Lua table.
143 @exception ScriptException
145 // static Scriptable *getThisPtr(VM *vm, int stackIndex);
147 public:
148 void setString(int index, const char *v);
149 void setString(int index, const std::string & v);
150 void setNumber(int index, float v);
151 void setTable(int index, const Table & v);
152 void setBoolean(int index, bool v);
153 void setString(const char *name, const std::string & v);
154 void setNumber(const char *name, float v);
155 void setTable(const char *name, const Table & v);
156 void setBoolean(const char *name, bool v);
157 void setLightUserData(int index, void *ud);
158 void setLightUserData(const char *name, void *ud);
160 const char *getString(const char *name) const;
161 const char *getString(int index) const;
162 float getNumber(const char *name) const;
163 float getNumber(int index) const;
164 Table getTable(const char *name) const;
165 Table getTable(int index) const;
166 bool getBoolean(const char *name) const;
167 bool getBoolean(int index) const;
169 protected:
170 VM *vm;
172 private:
173 int methods;
174 Table table;
176 private:
177 Scriptable( const Scriptable& );
178 Scriptable & operator=( const Scriptable& );
181 inline VM *Scriptable::vmPtr() const {
182 return vm;
185 inline int Scriptable::nrMethods() const {
186 return methods;
189 inline void Scriptable::pushMethod(const std::string & name) const {
190 pushMethod( name.c_str() );
193 inline bool Scriptable::hasMethod(const std::string & name) const {
194 return hasMethod( name.c_str() );
197 inline void Scriptable::setString(int index, const char *v) {
198 table.setString(index, v);
201 inline void Scriptable::setString(int index, const std::string & v) {
202 table.setString(index, v);
205 inline void Scriptable::setNumber(int index, float v) {
206 table.setNumber(index, v);
209 inline void Scriptable::setTable(int index, const Table & v) {
210 table.setTable(index, v);
213 inline void Scriptable::setBoolean(int index, bool v) {
214 table.setBoolean(index, v);
217 inline void Scriptable::setString(const char *name, const std::string & v) {
218 table.setString(name, v);
221 inline void Scriptable::setNumber(const char *name, float v) {
222 table.setNumber(name, v);
225 inline void Scriptable::setTable(const char *name, const Table & v) {
226 table.setTable(name, v);
229 inline void Scriptable::setBoolean(const char *name, bool v) {
230 table.setBoolean(name, v);
233 inline void Scriptable::setLightUserData(int index, void *ud) {
234 table.setLightUserData(index, ud);
237 inline void Scriptable::setLightUserData(const char *name, void *ud) {
238 table.setLightUserData(name, ud);
241 inline const char *Scriptable::getString(const char *name) const {
242 return table.getString(name);
245 inline const char *Scriptable::getString(int index) const {
246 return table.getString(index);
249 inline float Scriptable::getNumber(const char *name) const {
250 return table.getNumber(name);
253 inline float Scriptable::getNumber(int index) const {
254 return table.getNumber(index);
257 inline Table Scriptable::getTable(const char *name) const {
258 return table.getTable(name);
261 inline Table Scriptable::getTable(int index) const {
262 return table.getTable(index);
265 inline bool Scriptable::getBoolean(const char *name) const {
266 return table.getBoolean(name);
269 inline bool Scriptable::getBoolean(int index) const {
270 return table.getBoolean(index);
273 #endif