Object scriptingObject scripting overview
The KVIrc scripting language is not object oriented in nature.
Anyway , objects are a highlevel abstraction that allow
to write complex code in a "clean" way.
So I've added at least some pseudo-object support.|
Objects are arranged in tree structures.
Each object that you create is either toplevel object or a children
of another object. A toplevel object is a parentless one.
Obviously all objects can have children objects.|
When an object is destroyed , all its children are also destroyed. The toplevel objects are automatically destroyed when KVIrc quits. The objects are global to the entire application (this is different from previous KVIrc releases where the objects were local to the current frame window and arranged in a single tree with a builtin root object).
Each object is an instance of a class that defines its features. Each object has also a name , that is not necessary unique and is assigned by the programmer; the name is just a mnemonic expedient, and you may also not need it.
Each object is identified by an OPAQUE UNIQUE ID. The ID is assigned by KVIrc and can be held in any variable. You can think the object id as a "handle for the object" or the object's pointer. Any action performed on the object will require its ID.
Creation and destruction
To create an object you must use the $new()
function. $new() requires three parameters:|
- The object class (more about object classes later in this document)
- The ID of the parent object , (that can be 0 for toplevel objects).
- The object name (eventually empty)
To destroy an object use the delete command. (In previous versions this command was named "destroy" and delete is currently aliased to that name too).
Fields : objects as pseudo-structures
All the objects can contain variable fields.
You can set an object's field by using the object scope operator "->":|
So in the above example %fieldVariable is owned by the object.
The first character of the variable name has no special meaning in the object namespace (in the global namespace the variables starting with an uppercase letter are global to the application , the other ones are local to the command sequence). The variable names are completely case insensitive.
Any operator can be used with the object field variables:
The KVIrc scripting language is not typed. Any object class (be patient...I'll explain classes in a while) identifier can be stored in any KVIrc variable: it is not possible to find out the object features by "examining" its identifier. This may make the usage of objects a bit "unclear"; Howewer , with some experience you will be able to use the objects in a very powerful way. The type-safety can be also simulated by a careful usage of object names; in the above example , the %myobj object was created with the "userDescription" name. The storetofile alias could check the passed object's name and refuse to work if that does not match "userDescription".
A more complex use of fields will be described later in this document.
Just like in C++ , the objects have member functions.
For example , the "object" class (again...read on) objects export the $name()
and $classname() functions.|
The object functions can be reimplemented on-the-fly by using the privateimpl command: you can simply modify the behaviour of the function by writing your own function body. (This is an uncommon feature: unlike many other languages , you can reimplement object functions at run-time, when the object has been already created.)
A more complex example
Then the parent object is destroyed causing to:
- trigger the parent destructor.
- destroy all the children (and conseguently trigger all the "individual" destructors).
Not all the object functions must return a value: If a function does not return a meaningful value , or you just want to ignore it , you can call it in the following way:
As said before , all objects are instances of a specific class.
This concept is common to almost all object oriented languages.
A class is a collection of methods that define an object's behaviour.
Hehe...it is not easy to explain it , so I'll try with an example:|
The internal job of the object is defined by the "implementation of the class". Obviously , the programmer that creates the class has to write that implementation.
Let's go back to the real world.
KVirc contains a set of built-in ready-to-use classes. The basic class is object: all the other classes are derived from this (more about object inheritance later in this doc).
Another available class is socket that is an interface to the real system sockets. An instance of the socket class can connect and communicate with other hosts on the net.
The class definitions are GLOBAL to the entire application: all server windows share them.
So now we can say that in KVIrc a CLASS is a collection of features that define the behaviour of an object. The user interface to the class are the member functions and the events.
Someone asked for derived classes ?|
Here we go:
The class command allows you to define new object classes. In KVI++, A new class must be always derived from some other class: the lowest possible level of inheritance is 1: deriving from class object.
In the above example , all the values of $this</a>->%language that are not equal to "italian" are assumed to be "english". This is not always true , for example , just after the object creation the %language variable field is effectively empty. The above class works correctly in this case , but we might want to have always a coherent state of the field variables , so we need another concept: the class constructor that will be discussed in the next paragraph.
Note: multiple inheritance (inheritance from more than one base class) is not implemented , KVIrc is not a compiler :)
Objects are much more powerful....
Do a clearobjects to cleanup the old class definitions , and read on.
Constructors and destructors
The class constructor is a function that is called automatically just after the object
has been created internally by KVIrc and just before the $new
function returns. It should be used to setup the internal object state.|
Unlike in C++ , in KVIrc , the constructor CAN return a value:
If it returns 0 it signals the object creation failure : the object is immediately destroyed and $new() returns 0 to the caller. Any other return value is treated as success , so the object is effectively created and $new() returns its ID to the caller.
All the builtin classes have a constructor defined that will almost never fail (only if we run out of memory), so you can avoid to check the $new() return value when creating the instances of the built-in classes.
In derived classes you can override the constructor to setup your object's state.
You should always call the base class constructor in your overridden one , to setup the base class state , and propagate its return value (eventually modified if the base class constructor is succesfull but your derived class initialization fails).
In practice , the builtin class constructors do nothing other than setting the return value to 1 so you can even avoid to call them, but in any other case you must do it.
This is different from C (for example), where the constructors are called (more or less) automatically.
Signals and slots
The signals and slots are a powerful mean of inter-object communication.
A signal is emitted by an object to notify a change in its state.
For example , the button class emits the
clicked signal when the user clicks the button.
A signal is emitted by an object and can be received and handled by any other existing object (including the object that emits the signal).
The handler function for a signal is called "slot".
It is just a convention : in fact , a slot is a normal object function (and any object function can be a slot). More than one slot can be connected to a single signal , and more signals can be connected to a single slot.
In this way , many objects can be notified of a change in a single object , as well as a single object can easily handle state-changes for many objects.
The signal / slot behaviour could be easily implemented by a careful usage of object functions. So why signals and slots ?
Because signals are much more powerful in many situations. The signals have no equivalent in C/C++...but they have been implemented in many highlevel C/C++ libraries and development kits (including the system-wide signal/handler mechanism implemented by all the modern kernels and used in inter-process communication).