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

Purpose of this page

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



The _getter() method defines the behavior of getter which is an interface of the Rice class. Getter is an interface for getting the internal state of an instance.

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


We explain how to define a getter by defining "Value" getter in the fake class.


Table of contents:


What is getter

First argument: string signature

Second argument: VirtualMachine vm

Fixed code

TypeName and Fitted getter

When do not need a getter

What is getter

A getter is an interface for getting the internal state of an instance. Its behavior would be:


fake fakeInstance = new fake("Initial data");

string data = fakeInstance.Value; // data is "Initial data".


The "Value" is the getter. You can get the state of an instance as value.

First argument: string signature

When a getter appears, Rice creates a getter-signature which is a name in order to call a getter. The getter-signature will be the same as the getter's name.

The getter-signature is passed as the first argument of the _getter() method.

Second argument: 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.

Fixed code

We modify the _getter() method for the fake class.

1:

public override Rtype _getter(string signature, VirtualMachine vm) {

2:

switch (signature) {

3:

case "Value": { // "Value"

4:

return new Rfake(InternalData);

5:

}

6:

default:

7:

return null;

8:

}

9:

}

The switch statement is used to branch to the corresponding routine.


The Value getter is defined in the 3rd to 5th lines. Returns a new instance of the fake class initialized with internal data.


If an instance of the Rtype-derived class returns from a getter routine, it indicates that the getter corresponding to the first argument exists and the processing was successful.

Returns a null at "default:". The null indicates a processing failure due to the absence of a corresponding getter. Rice throws an exception if null is returned.

TypeName and Fitted getter

There are two special getters that every Rice class has.

The first is the TypeName getter. As mentioned above, it refers to the _typename field and returns the class name.

The second is the Fitted getter. Refers to the Fitted () method and returns the initialization state of the instance.

These two getters are automatically generated by Rice. You do not need to implement these getters in the _getter() method.

Even if you implement these two getters, they are never called.

When do not need a getter

If a class does not need a getter, implement the _getter() method as follows.

1:

public override Rtype _getter(string signature, VirtualMachine vm) {

2:

return null;

3:

}

Next
Previous
Copyright © Cooker All rights reserved.