Home Notices Documents Classes Download Others Rice
Documents  >  Development  >  Dynamic embedded classes  >  Setter
Implementation of setter

Purpose of this page

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



In the part describes here, the _setter() 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 _setter () method is a method to define the behavior of the setter which is the interface of the class. Setter is an interface for setting values to instances, it corresponds to the set-accessor of the property of C#.


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

What is important in the _setter() 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 setter

Setter is an interface for setting values to instances.

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


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

someFile.IsReadOnly = true;


The IsReadOnly is setter. As shown in the example, setter appears on the left side of assignment operator and can assign a value. Such interface for assigning a value is the setter. In the example, true is assigned to IsReadOnly, and someFile becomes a read-only file.

In Rice, only variable and setter appear on the left side of assignment statement and can be assigned a value. Therefore, you must define a setter if you want to change the state of an instance using the form of an assignment statement.


Explains the behavior of Rice when the setter is called.

If a setter appears on the left side of the assignment statement, Rice firstly evaluates the expression on the right side of the assignment statement to determine the assignment value. And creates a name for calling the setter from the setter name and the class name of the assignment value. This name for calling the setter is called the setter-signature.

In the example above, the setter name is "IsReadOnly" and the class name of the assignment value is "bool", so the "IsReadOnly(bool)" creates as the setter-signature. And the _setter() method which is passed this setter-signature and the assignment value is called for changing the state of someFile.

The first argument : string signature

The created setter-signature is passed to the _setter() method as the first argument.

The setter-signature generation rules are as described above. It is a concatenation of the setter name and the class name of the assignment value enclosed in parentheses.

Please be careful about the class names of container classes - list, dictionary, stack, queue -. These classes can specify a holded class.

For example, the class name of plain list is the "list", but if the string class is kept, the class name is list{string}. The same is true for other container classes.

Please note that this qualified class name appears in the setter-signature.

The setter-signature created in this way is passed to the _setter() method as the first argument.

The second argiment : 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 _setter() 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.

The third argument : Rtype arg

An instance which is got from the evaluation of the right side of the assignment statement is stored in the arg and passed to the _setter() method.

Example of Implementation

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

The following is an implementation example of a setter for int class and string class.

1:public override bool _setter(string signature, VirtualMachine vm, Rtype arg) {
2:switch (signature) {
3:case "IntSetter(" + Rint.TYPENAME + ")": {
4:int intVal = (Rint)arg;
5:// Implements necessary code here.
6:return true;
7:}
8:case "StringSetter(" + Rstring.TYPENAME + ")": {
9:string strVal = (Rstring)arg;
10:// Implements necessary code here.
11:return true;
12:}
13:default:
14:return false;
15:}
16:}

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

Actual argument is stored in the arg as instances of the Rtype class which is the base class. so if you take out from the arg and cast it to an Rtype derived class, you can get the necessary argument correctly. Please refer to the manual or check the object browser of Visual Studio for the cast of the embedded class.

The implementation of the actual embedded class performs necessary processing after branching in the case. Please note that it returns true when processing is complete. True indicates that the setter exists and processing is successful.

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

Implementation when do not need a setter

A class does not need a setter in some cases.

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

1:public override bool _setter(string signature, VirtualMachine vm, Rtype arg) {
2:return false;
3:}

Next page

It concludes the description of the setter.

The next page describes the implementation of getter.

Next
Previous
Copyright © CookerGX All rights reserved.