Example: Overloading the SIZE and N_ELEMENTS Functions
In this example, we create the MyArrayOper class, which merely wraps a pointer to an externally-created array. We overload the class’ _overloadSize
, _overloadBracketsLeftSide
, and _overloadBracketsRightSide
methods.
The MyArrayOper_doc object definition and operator overloading code listed in this section is contained in the procedure file myarrayoper_doc__define.pro
, and is located in the examples/doc/objects
subdirectory of the IDL distribution. To view the file in an IDL editor window, enter .EDIT
myarrayoper_doc__define.pro
at the IDL command line.
For more information, refer to Overloading the SIZE and N_ELEMENTS Functions.
FUNCTION MyArrayOper_doc::Init, data
self.pData = PTR_NEW(/ALLOCATE)
IF (N_ELEMENTS(data) GT 0) THEN *self.pData = data
RETURN, 1
END
FUNCTION MyArrayOper_doc::_overloadSize
; Just pretend that I am an array with multiple dimensions.
RETURN, SIZE(*self.pData, /DIMENSIONS)
END
PRO MyArrayOper_doc::_overloadBracketsLeftSide, arg, value, $
isRange, i1, i2, i3
CASE (N_PARAMS()-3) OF
1: (*self.pData)[i1] = value
2: (*self.pData)[i1, i2] = value
3: (*self.pData)[i1, i2, i3] = value
ENDCASE
END
FUNCTION MyArrayOper_doc::_overloadBracketsRightSide, $
isRange, i1, i2, i3
CASE (N_PARAMS()-1) OF
1: result = (*self.pData)[i1]
2: result = (*self.pData)[i1, i2]
3: result = (*self.pData)[i1, i2, i3]
ENDCASE
RETURN, result
END
PRO MyArrayOper_doc__define
void = { MyArrayOper_doc, INHERITS IDL_Object, $
pData: PTR_NEW() }
END
When we call the N_ELEMENTS and SIZE functions with the custom MyArrayOper
object, these functions in turn query the MyArrayOper::_overloadSize
function for dimension information.
a=MyArrayOper_doc(FINDGEN(10,7,8))
PRINT, N_ELEMENTS(a)
IDL prints:
560
PRINT, SIZE(a, /DIMENSIONS)
IDL prints:
10 7 8
PRINT, a[0,0,0], a[3,4,5], a[9,6,7]
IDL prints:
0.000000 393.000 559.000