current stable: 0.99.6 unstable: cvs (0.99.7) |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Working With Objects And ClassesCreating ClassesRegistering classes is much the same as registering namespaces. You first reigster the class, then you add ] what variables and functions you wish to publish in them. To register a class you use the ferite_register_inherited_class function call. This will create the class, setup the inheritence, register the class wihin a namespace for you and return it in one fell swoop.
The first parameter is the script, the second is the namespace in which you want to place the class, the third is the name of the class by which programmers can reference it and the fourth is the name of the class the new class inheirts from. The fourth argument can be in standard dot notation and is the name of the parent class. For instance it could be "Sys.Stream". The function will start looking for the class in the namespace that is passed to the function, and then start in the top level script namespace. For instance if the "Sys" namespace was passed to the function, you would want to specify "Stream". If you do not wish for your class to inherit from any existing class simply pass NULL and the new class will be automatically placed as a subclass of the base class "Obj". Registering variables and functions with a class is much the same as registering them with a namespace, you simply pass an extra parameter to say whether or not the item is static (linked to the class) or an instance variable (linked to the object create from the class). To add a variable you call:
The second argument is the class to add the variable to [which can be obtained from creating a new class or pulling one out of a namespace], the third argument is the variable to add, and the fourth variable is whether or not the variable is static. To add a function you call:
The arguments are almost identical except for the third one which is a pointer to a ferite function. Creating ObjectsCreating objects is very straight forward. There are two main method calls that can be used. The first is ferite_build_object, it's pupose is to simply allocate a FeriteVariable*, allocate the necessary structures [such as it's instance variables, and pointers to functions] and adds it to the ferite garbage collector. ferite_build_object does not call the new objects constructor. This is very useful for when you are doing manual setup of an object. The prototype for the function is:
The second is ferite_new_object which does all the same things ferite_build_object does except it will call the constructor for the new object. It will return an FeriteVariable* that is ready to be cleaned up by ferite as and when it is returned to the engine and no longer wanted. It has the prototype:
The first two arguments are the same for ferite_build_object, the current script and the class you wish to instantiate. The third argument is the parameter list to be passed to the objects constructor. Read the next section on calling functions to find out how to create one and what they consist of. Accessing VariablesFirstly, we'll cover how to access variables within objects and classes. It is done essentially the same way for each. Both FeriteClass and FeriteObject structs have a variables element that is a hash of all variables within them. To make life slightly easier and code more understandable there are a couple of functions for retrieving the variables from either a class or an object.
This is for getting the value out of an object. It should be noted that the second argument is not a FeriteVariable* but a FeriteObject*. This means that is it necessary, if you have a FeriteVariable* pointing to an object, to call it with VAO(nameOfVariable) - otherwise all sorts of issues will arise.
Both the above functions take the name of the variable to obtain and will return a pointer to the variable if it exists, or will return NULL if it doesn't. For example, for objects you would do this: (assume that some_object is of type FeriteVariable *, and it is a valid object)
If myvar is not NULL, then it was successfully retrieved. If you want to do the same with a class, you do this: (assume that some_class is of type FeriteClass *, and it is a valid class)
Again, if myvar is not NULL, it was successfully retrieved. Accessing FunctionsGetting functions from objects or classes is easy if you can get a variable from them [Hint: make sure you read the last section!]. To get your hands on a function in an object you simply use the function call ferite_object_get_function. Suprised? You shouldn't be. It looks, feels and tastes very similar to ferite_object_get_var except this time you get a function not a variable.
To get your hands on a function tucked away in a class you simply need to use the function call ferite_class_get_function.
So there you have it. Easy.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ferite et al © 2000-2004, Chris Ross |