set_method_table¶
set_method_table sets the methods (member functions) for the given object.
- obj = set_method_table(obj, ssClass)
- obj = set_method_table(obj, ssClass, bUpdate)
Return Value
obj is a pointer for the first parameter or 0, if an error occurred.
Parameters
- obj
obj is the object created with obj_create.
- ssClass
To set the method table, a function with the name ssClass + “_methods” will be invoked. This function must set the methods, see comment.
- bUpdate
If the bUpdate parameter is TRUE (1), the function ssClass +
"_method"
will be invoked and the method table will be copied to_g()._method_table
. The method table will be assigned to obj by calling obj_set_methods.If the bUpdate parameter is FALSE (0), only the current
_g()._method_table
will be assigned to the obj object.If the bUpdate is not specified, the value set in
_g()._debug
will be used for bUpdate. The first call after F4 (save/execute)_g()._debug
is set to 1.
Comment
To create a method table, you must specify global functions witch can be added to the table. The first parameter of the function must be the object itself:
def my_obj_print(this)
{
// ...
}
methodTable = [.
print = "my_obj_print"
];
methodTable is the method table.
The set_method_table function expects a function with the name ssClass + “_methods” to exist. This function must return an object with a method table:
def my_methods()
{
return [.
print = "my_obj_print"
];
}
o = [. a = 1, b = 2];
set_method_table(o, "my")
The method table will be copied into the global variable _g()._method_table.my
(see _g) and the pointer will be saved with obj_set_methods
in the object o. With o.methods
(see obj_methods) the method
can be retrieved.
Example
1def my_obj_print(this)
2{
3 k = this.keys
4 for (i in 1:len(k)) {
5 printf("%s = ", k[i]);
6 print this[k[i]]
7 }
8}
9def my_methods()
10{
11 return [.
12 print = "my_obj_print",
13 __print__ = "my_obj_print"];
14}
15def my_create()
16{
17 this = [.];
18 set_method_table(this, "my");
19 return this;
20}
21o = my_create();
22o.a = 1;
23o.b = 2;
o
print o
o.print()
History
Version |
Description |
---|---|
R2013.2 |
Default parameter bUpdate modified (see above) and documentation modified. |
4.1.1 |
New. |
id-1586543