Home News DOC Class DL Others Rice
DOC  >  Development - Dynamic embedded classes  >  Fitter
Fitter

Purpose of this page

The part described on this page is shown below. This is the second part of the template divided into five parts.



The _fitter() method defines the behavior regarding the initialization of the Rice class. Specifically, this method defines the fitter that is called by Rice's new expression.

This is an abstract method declared in the Rtype class definition and must be implemented in an Rtype-derived class.


Table of contents:


new expression - Rice -

First argument: string signature

Second argument: VirtualMachine vm

Third argument: Queue<Rtype> argqueue

Fixed code

Default fitter

When do not need a fitter

new expression - Rice -

First, we explain about Rice's new expression.

The new expression is an expression for creating an instance. For example:


fake fakeInstance = new fake();


The right-hand side of the assignment symbol is the new expression. It starts with the keyword new, followed by the class name, and arguments are specified. Rice creates an instance of Rtype-derived class from this information and calls the _fitter() method to execute the initialization routine.


First argument: string signature

Rice creates a fake class instance from the new expression above. Then a fitter-signature is created to determine the initialization routine to call on the instance.

The fitter-signature is a name that distinguishes the initialization routine to call. It is passed as the first argument of the _fitter() method.

The fitter-signature generation rules are as follows:


Second argiment: VirtualMachine vm

An instance of the VirtualMachine class is passed as the second argument. This is the virtual machine running the script.

If you need to throw an exception, get it from this argument. You can get an exception in which information such as the source file name and the number of lines where the exception occurred is set.

throw vm.GetRtypeException("Exception message", "Exception name"); // Exception with supplementary information.

Third argument: Queue<Rtype> argqueue

Rice parses the new expression, stores its arguments in the third argument and passes it to the _fitter() method.

The third argument contains instances of Rtype-derived class corresponding to the arguments in order of appearance.

Fixed code

We modify the _fiiter() method for the fake class. the fake class has an fitter with one string. It also implements a default fitter.

1:

public override bool _fitter(string signature, VirtualMachine vm, Queue<Rtype> argqueue) {

2:

switch (signature) {

3:

case TYPENAME + "()": { // new fake() -> "fake()"

4:

return true;

5:

}

6:

case TYPENAME + "(" + Rstring.TYPENAME + ")": { // new fake("Any string.") -> "fake(string)"

7:

InternalData = (Rstring)argqueue.Dequeue();

8:

return true;

9:

}

10:

default:

11:

return false;

12:

}

13:

}

The switch statement is used to branch to the corresponding routine. The value of each case can be created by combining the TYPENAME constant of classes.


The 3rd to 5th lines are the definitions default fitter that have no arguments. We've implemented a default fitter here for illustration purposes, but the default fitter for the fake class just returns true and does nothing.


The 6th to 9th lines are the definition of the fitter that has one argument of string.

On line 7, the argument passed to the fitter is retrieved from the third argument. The class and number of instances stored in the third argument absolutely match to the fitter-signature. Therefore, you can safely cast the retrieved argument to Rstring class.

We are assigning the argument of the Rstring class directly to the InternalData, but the Rstring class can be assigned because it can be implicitly cast to a C# string.


Please notice that each initialization routine returns true and exits. True indicates that the fitter corresponding to the signature exists and the initialization was successful.

Returns false at "default:". False indicates a processing failure due to the absence of a corresponding fitter. Rice throws an exception if false is returned.

Default fitter

The fitter with no arguments is called the default fitter.

The default fitter above was implemented for illustration purposes, but you don't need to implement a default fitter that do nothing. An equivalent default fitter is automatically generated.

The automatically implemented default fitter will return the same instance that is returned in the declaration statement.

When do not need a fitter

If a class does not need a fitter, implement the _fitter() method as follows.

1:

public override bool _fitter(string signature, VirtualMachine vm, Queue<Rtype> argqueue) {

2:

return false;

3:

}

Next
Previous
Copyright © Cooker All rights reserved.