899 Rate this article:
No rating

Implicit Get/SetProperty calls in user-defined classes, part II

Mark Alonzo
A few months ago, I posted a demonstration of how to use the implicit Get/SetProperty call allowed by subclasses of IDL_Object, using existing Object Graphics classes as the basis. Here, I’d like to go a little further: I’ll write a class that inherits from IDL_Object and implements its own Get/SetProperty methods, then show how the implicit call works with them. The class Integer represents a single long integer value. Start with the class data definition, nothing that we inherit from IDL_Object:
pro integer__define
   compile_opt idl2

   !null = {integer, inherits idl_object, value:0L}
end
Recall that to work seamlessly with IDL’s calling mechanism, this routine should be the last in the file and its methods should be listed before/above it in the file. Next, the Init method, where the user can initialize an Integer to a value (otherwise, the default value of 42 is used):
function integer::init, value=v
   compile_opt idl2

   self.value = ~isa(v, /number) || ~isa(v, /scalar) ? 42L : v
   return, 1
end
Note that, counter to standard technique, it’s not necessary to call IDL_Object::Init from here. For simplicity (and because there’s nothing to clean up), I’ll not write a Cleanup method for this class. Next, the GetProperty and SetProperty methods, which are straightforward:
pro integer::getproperty, value=v
   compile_opt idl2

   if arg_present(v) then v = self.value
end 

pro integer::setproperty, value=v
   compile_opt idl2

   if v ne !null then self.value = v
end
At this point, let’s stop and create an instance of Integer, int1:
IDL> int1 = integer(value=5)
and then perform a few simple diagnostics on it to check that everything is working correctly:
IDL> help, int1
INT1            OBJREF    = 
IDL> print, int1

IDL> print, isa(int1, 'Integer')
    1
Now, get the value of the Integer int1. Prior to IDL 8, this required an explicit call to Integer::GetProperty, like this:
IDL> int1->getproperty, value=v1
IDL> print, v1
       5
whereas IDL 8 allows an implicit call to GetProperty when using the dot “.” operator:
IDL> print, int1.value
       5
Note that the keyword name in the GetProperty formal parameter list is what is used in the implicit call. Had, for example, the keyword instead been named TH3_$VALUE, the implicit call to GetProperty would be int1.th3_$value. Analogous syntax holds for the Integer::SetProperty method. For example, use the IDL 8 syntax to change the value of int1 to 7:
IDL> int1.value = 7
IDL> print, int1.value
       7
The implicit calls to Get/SetProperty only work for subclasses of IDL_Object. Also, this does not change the fact that instance variables in IDL are protected. In my opinion, this syntax is more elegant and easier to use than an explicit call to a Get- or SetProperty method. But we can go further! What I’ll do next overlaps with the description and example of operator overloading that Mike Galloy gave some time ago, but it feels natural to extend the present example a bit further. Here, we’ll overload the PRINT procedure and the plus sign “+” for Integer. First, overload PRINT by overriding the IDL_Object::_overloadPrint method:
function integer::_overloadprint
   compile_opt idl2

   return, self.value
end
Now when we print an Integer we get its value instead of some cryptic reference to an object heap variable:
IDL> print, int1
       7
Next, overload the plus operator “+” so that we can add two Integers:
function integer::_overloadplus, left, right
   compile_opt idl2

   if ~isa(left, obj_class(self)) || ~isa(right, obj_class(self)) then $
      return, obj_new() $
   else $
      return, integer(value=left.value + right.value)
end
The parameters left and right are inherited from the IDL_Object::_overloadPlus method: they represent the two Integer objects to be added. They’re tested to make sure they’re Integers. If they are, the implicit GetProperty call is used to get their values, the sum of which is used to make a new Integer, which is returned. For example, define another Integer, int2, and add it to int1 to get a third Integer, int3:
IDL> int2 = integer(value=3)
IDL> int3 = int1 + int2
IDL> print, int3
       10
This operation wouldn’t be possible without operator overloading. A complete list of operators than can be overloaded in a user-defined class can be found in the IDL Help; search for “overload.” You can download the complete, IDLdoc-ed code for this Integer class from here.

Please login or register to post comments.