obj_set_methods sets the methods (member functions) for the given object.
Return Value
bool is TRUE (1) if the function was successful or FALSE (0) if an error occurred. .. us.params
Parameters
obj is the object created with obj_create.
m is an object created with obj_create which contains the names of the methods (see Comment).
Example
def my_obj_print(this)
{
k = this.keys
for (i in 1:len(k)) {
print k[i]
print this[k[i]]
}
}
def my_obj_create()
{
obj = obj_create();
m = obj_create();
m.print = "my_obj_print";
m.__print__ = "my_obj_print";
obj.set_methods(m);
return obj;
}
o = my_obj_create()
o.a = 1
o.b = 2
o
print o
o.print
Comment
To add a new function (member function or method) to an object a new object must be created that contains the pair method name and the UniScript function name.
Example:
obj = obj_create()
m = obj_create();
m.test = "my_test";
obj.set_methods(m);
When the method test is invoked
obj.test(1,2,3)
the UniScript interpreter will call the function “my_test”. The first parameter is the object obj.
Here is the UniScript function “my_test”:
def my_test(obj, a, b, c)
{
// do something with ob, a, b, c
return 123;
}
Beside the normal methods
m.name = "uniscript function name";
an object can have special methods starting and ending with 2 underscores (__name__). So far the following methods are defined:
| __destructor__ |
| __print__ |
| __format__ |
| __getprop__ |
| __setprop__ |
| __veceval__ |
| __vecassign__ |
| __add__ |
| __uminus__ |
| __sub__ |
| __div__ |
| __ediv__ |
| __ldiv__ |
| __mod__ |
| __mul__ |
| __emul__ |
| __power__ |
| __epower__ |
| __trans__ |
| __etrans__ |
| __shl__ |
| __shr__ |
| __lt__ |
| __le__ |
| __eq__ |
| __ne__ |
| __gt__ |
| __ge__ |
| __not__ |
| __and__ |
| __or__ |
| __xor__ |
| __bit_cmpl__ |
| __bit_and__ |
| __bit_or__ |
| __cat_col__ |
| __cat_row__ |
__destructor__
If a object has a method __destructor__, the destructor function will be invoked when the object gets invalid.
Example:
m.__destructor__ = "file_close";
The destructor function has one parameter, the object pointer:
def file_close(file)
{
fclose(file.fp);
}
f = file_open(...)
f = 0;
__print__
The __print__ method will be invoked when an object is typed into the command window or if the print method is executed.
__format__
The __format__ method will be invoked when the mouse cursor is placed on the object name during debugging. The format function should return a scalar string
Example:
def file_tooltip(file)
{
return sprintf("fp = %d\nname = %s", file.fp, file.name);
}
__setprop__
With the __setprop__ method a function can be invoked if an expression like obj.prop = value is executed.
Example:
def prop_set_func(obj, val)
{
obj["prop_name"] = val;
}
Inside the property-set function an expression of the form obj.prop = value cannot be used. Otherwise the function would be invoked recursively. Only the following forms should be used:
obj["prop_name"] = val;
obj.set_at("prop_name", val);
__setprop__ is defined as following:
m.__setprop__ = obj_create();
m.__setprop__.prop_name = set_name_func;
__getprop__
__getprop__ is invoked when an term of the form obj.prop is used on the right side of an exression like val = obj.prop.
def prop_get_func(obj)
{
return obj["prop_name"];
}
__veceval__
Will be invoked for expressions like v = obj[index].
def vec_eval_func(obj, index)
{
return obj.lookup(index);
}
__vecassign__
Will be invoked for expressions line obj[index] = val.
def vec_assign_func(obj, index, val)
{
obj.set_at(index, val);
}
Summary
m = obj_create();
m.name = name_func;
m.__destructor__ = destroy_func;
m.__print__ = print_func;
m.__getprop__ = obj_create();
m.__getprop__.prop_name = get_name_func;
m.__setprop__ = obj_create();
m.__setprop__.prop_name = set_name_func;
id-1457530