current stable:
cvs (0.99.7)
  Home / News
  The Team

  Source Tarball
  CVS Web View
  Misc. Files

The Manual
  Download [html]
  Download [pdf]
  View Online
  Download [html]
  View Online
  Script Examples

Developer Guide
  Download [html]
  Download [pdf]
  View Online
Ferite C API
  Download [html]
  View Online

Open Source Approved

SourceForge Logo - browse the Keyword Map of

[previous] Working With Namespaces[up][toc]Calling Functions [next]

Working With Objects And Classes

Creating Classes

Registering 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.

    FeriteClass *ferite_register_inherited_class( FeriteScript *script, FeriteNamespace *ns, char *name, char *parent )

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:

    int ferite_register_class_variable( FeriteScript *script, FeriteClass *klass, FeriteVariable *variable, int is_static )

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:

    int ferite_register_class_function( FeriteScript *script, FeriteClass *klass, FeriteFunction *f, int is_static )

The arguments are almost identical except for the third one which is a pointer to a ferite function.

Creating Objects

Creating 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:

    FeriteVariable *ferite_build_object( FeriteScript *script, FeriteClass *nclass);

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:

    FeriteVariable *ferite_new_object( FeriteScript *script, FeriteClass *nclass, FeriteVariable **plist);

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 Variables

Firstly, 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.

    FeriteVariable *ferite_object_get_var( FeriteScript *script, FeriteObject *object, char *name );

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.

    FeriteVariable *ferite_class_get_var( FeriteScript *script, FeriteClass *klass, char *name )

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)

    FeriteVariable *myvar = ferite_object_get_var(script, VAO(some_object), "myvar");

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)

    FeriteVariable *myvar = ferite_class_get_var(script, some_class, "myvar");

Again, if myvar is not NULL, it was successfully retrieved.

Accessing Functions

Getting 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.

    FeriteFunction *ferite_object_get_function( FeriteScript *script, FeriteObject *object, char *name );

To get your hands on a function tucked away in a class you simply need to use the function call ferite_class_get_function.

    FeriteFunction *ferite_class_get_function( FeriteScript *script, FeriteClass *cls, char *name ) 

So there you have it. Easy.

[previous] Working With Namespaces[up][toc]Calling Functions [next]
ferite et al © 2000-2004, Chris Ross