Home Notices Documents Classes Download Others Rice
Documents  >  Development  >  Dynamic embedded classes  >  Getter
Implementation of getter

Purpose of this page

This page details the fourth part of the five part that divided the Rtemplate.cs.

In the part describes here, the _getter() method is defined. This is an abstract method declared in the definition of Rtype, and must be implemented in a derived class of Rtype. That is, it must be implemented in an embedded class.

The _getter() method is a method to define the behavior of the getter which is the interface of the class. Getter is an interface for getting values represents state of instance, it corresponds to the get-accessor of the property of C#.

The first section explains the getter that is interface of the class.

What is important in the _getter() method is the method's arguments. We will make a section for each argument and explain.

Finally, we will have a section for implementation example.

What is getter

The getter is an interface for getting values represents state of instance.

The behavior of the getter is shown using the file class as an example.

file someFile = new file("c:\somewhere....");

string path = someFile.FullName;

The FullName is getter. As shown in the example, calling FullName getter of someFile returns the path of someFile. Like this, the getter returns a value represents state of instance

Explains the behavior of Rice when the getter is called.

When a getter appears, Rice creates a name for calling the getter from the getter's name. The name is called getter-signature. The getter-signature is as same as the getter's name.

In the example above, the getter's name is the "FullName", so created getter-signature is the "FullName". Passing this getter-signature and calling _getter() method of someFile returns the path of someFile as a string value.

The first argument : string signature

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

Mentioned above, the getter-signature will be as same as the getter's name.

The second argument : VirtualMachine vm

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

The purpose of this argument is to get an instance of the exception class. If you need to throw an exception, get one from VirtualMachine.

For example.

throw vm.GetRtypeException("Message of exception");

Since it is an exception thrown from the _getter() method, it is appropriate to use the RtypeException class that is assuming class related exceptions. So we get that instance with GetRtypeException() and throw it.

This argument is useful if you want to give a detailed message about the exception. In addition, if you throw an exception from near the place where the exception occurs, additional information will be more accurate. The additional information is location information such as the source file name and the number of lines where the exception occurred. If you get an exception from the VirtualMachine class, that information will be automatically set to the exception.

Even if you throw a normal exception as C#, it will be caught immediately and converted to UnknownException class. The additional information is set automatically at this time.

Example of Implementation

We can do the necessary processing using the fact that the setter-signature is stored in the signature.

The following is an implementation example of getters for returning int class and string class.

1:public override Rtype _getter(string signature, VirtualMachine vm) {
2:switch (signature) {
3:case "IntGetter": {
4:// Returns a Rint corresponding to the state of the instance.
5:return new Rint();
7:case "StringGetter": {
8:// Returns a Rstring corresponding to the state of the instance.
9:return new Rstring();
12:return null;

In this example, the switch statement and the getter-signature are used to branch. The value of each case can be created by the getter name.

The implementation of the actual embedded class should return a value coresponding the state of an instance. Please note that it returns a Rtype derived class when processing is complete. A Rtype derived class indicates that the getter exists and processing is successful.

The "default:" returns null. Null indicates that class do not have a suitable getter. An exception will be thrown if null is returned.

TypeName and Fitted getter

In the "Implementation of non-interface part" page, we mentioned that the TypeName getter refers to the _typename field and the Fitted getter refers to the Fitted() method.

These two getters will be automatically implemented and behave like that. Therefore, there is no need to implement these getters in the _getter() method.

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

Implementation when do not need a getter

A class does not need a getter in some cases.

In such a case, implement the _getter() method as follows.

1:public override Rtype _getter(string signature, VirtualMachine vm) {
2:return null;

Next page

It concludes the description of the getter.

The next page describes the implementation of method.

Copyright © CookerGX All rights reserved.