The objects created with obj_create have similar qualities as maps (see MapCreate). But they have some advantages over maps:
Simpler syntax, e.g.
obj = obj_create();
obj.a = 123;
instead of
hMap = MapCreate();
MapSetAt(hMap, "a", 123);
They provide integer keys, e.g.
obj = obj_create();
obj[23] = "Test";
(or obj.set_at(23, “Test”);)
The keys are sorted to have a fast access to a range of keys (see obj_keys).
Objects can be saved in XML files (see obj_save and obj_load).
Objects can contain objects (see obj_set_at).
Automatic garbage collector: obj = 0 will destroy all objects obj and all nested objects.
Functions (methods) can be added to objects (see obj_set_methods) and methods and variables (properties) can be inherited from existing objects (see obj_set_parent).
The following functions can always be written as
obj_xxx(o, ...)
or shorter in the form
o.xxx(...)
Instead of a = o.lookup() one can write a = obj_lookup(o).
| UniScript-Object-Functions | |
|---|---|
| obj_copy | obj_copy creates a copy of an object. |
| obj_count | obj_count returns the number of variables in an object. |
| obj_count_num | obj_count_num returns the number of number keys in an object. |
| obj_count_str | obj_count_str returns the number of string keys in an object. |
| obj_create | obj_create creates an UniScript object. |
| obj_has_key | obj_has_key checks if a given key exists. |
| obj_info | obj_info returns a string of the form “obj-name,hex-address”. |
| obj_keys | obj_keys returns all keys or a range of keys for the given object. |
| obj_load | obj_load loads an object saved with obj_save from the hard drive. |
| obj_lookup | obj_lookup returns the value of a given key. |
| obj_methods | obj_methods returns an object with methods (member functions) for the given object. |
| obj_parent | obj_parent returns the parent object of 0 if the object does not have a parent. |
| obj_remove | obj_remove removes a key-value pair from an object. |
| obj_save | obj_save saves an object as an XML file on the hard drive or as a string. |
| obj_set_at | obj_set_at adds a key-value pair to the given object. If the key already exists the value will be overwritten. |
| obj_set_methods | obj_set_methods sets the methods (member functions) for the given object. |
| obj_set_parent | obj_set_parent sets the parent object. |
| obj_set_str_return | obj_set_str_return specifies the return value of obj_lookup if a key does not exist. |
| set_method_table | set_method_table sets the methods (member functions) for the given object. |
Functions (methods) can be added to an object.
def file_open(ssFileName, ssMode)
{
fp = fopen(ssFileName, ssMode);
if (!fp) {
return 0;
}
file = obj_create();
file.fp = fp;
file.name = ssFileName;
methods = obj_create();
methods.read = "file_read";
methods.write = "file_write";
methods.name = "file_get_name";
methods.close = "file_close";
methods.__destructor__ = "file_close";
methods.__format__ = "file_format";
file.set_methods(methods);
return file;
}
def file_format(file)
{
return sprintf("fp = %d\nname = %s", file.fp, file.name);
}
def file_read(file, ssType, n)
{
if (nargsin() == 2) {
return fread(file.fp, ssType);
} else {
return fread(file.fp, ssType, n);
}
}
def file_write(file, ssType, dat)
{
return fwrite(file.fp, ssType, dat);
}
def file_get_name(file)
{
return file["name"];
}
def file_close(file)
{
fclose(file.fp);
}
def test()
{
file = file_open("d:/test.txt", "wt")
print file.name()
file.write("char", "This is a test!")
file.close()
file = file_open("d:/test.txt", "rt")
print file.read("char")
file.close();
}
See also
id-2115846