; docformat = 'rst' ;+ ; Subclass of IDL_Container that also has a getByName method. ; ; :Properties: ; name ; name of the container to be used by the getByName method ; _extra ; properties of IDL_Container ; _ref_extra ; properties of IDL_Container ;- ;+ ; Finds the name of an object, even if it does not have a NAME property. ; Returns the empty string if the object does not have a NAME property. ; ; :Returns: ; string ; ; :Params: ; obj : in, required, type=object ; object to find name of ;- function mg_container::_askName, obj compile_opt strictarr catch, error if (error ne 0L) then begin catch, /cancel return, '' endif obj->getProperty, name=name return, name end ;+ ; Returns an immediate child of a container by name. ; ; :Returns: ; object ; ; :Params: ; name : in, required, type=string ; name of immediate child ; container : in, required, type=object ; container to search children of ;- function mg_container::_getChildByName, name, container compile_opt strictarr for i = 0L, container->count() - 1L do begin child = container->get(position=i) childName = self->_askName(child) if (childName eq name) then return, child endfor return, obj_new() end ;+ ; Traverses a hierarchy of named objects using a path of names delimited with ; /'s. ; ; :Returns: ; object ; ; :Params: ; name : in, required, type=string ; path of names to the desired object; names are delimited with /'s ;- function mg_container::getByName, name compile_opt strictarr tokens = strsplit(name, '/', /extract, count=ntokens) child = self for depth = 0L, ntokens - 1L do begin child = self->_getChildByName(tokens[depth], child) if (~obj_valid(child)) then return, obj_new() endfor return, child end ;+ ; Get properties. ;- pro mg_container::getProperty, name=name, _ref_extra=e compile_opt strictarr if (arg_present(name)) then name = self.name ;if (n_elements(e) gt 0L) then begin ; self->IDL_Container::getProperty, _extra=e ;endif end ;+ ; Set properties. ;- pro mg_container::setProperty, name=name, _extra=e compile_opt strictarr if (n_elements(name) gt 0L) then self.name = name ;if (n_elements(e) gt 0L) then begin ; self->IDL_Container::setProperty, _extra=e ;endif end ;+ ; Free resources of the object. ;- pro mg_container::cleanup compile_opt strictarr self->IDL_Container::cleanup end ;+ ; Create an MG_Container object. ; ; :Returns: ; 1 for success, 0 for failure ; ; :Keywords: ; _extra : in, optional, type=keywords ; properties of MG_Container ;- function mg_container::init, _extra=e compile_opt strictarr if (~self->IDL_Container::init()) then return, 0 self->setProperty, _extra=e return, 1 end ;+ ; Define instance variables and inheritance. ; ; :Fields: ; name ; name of object to be used in getByName method ;- pro mg_container__define compile_opt strictarr define = { MG_Container, inherits IDL_Container, name: '' } end ; main-level example program ; create hierarchy of containers c1 = obj_new('MG_Container', name='c1') c2 = obj_new('MG_Container', name='c2') c1->add, c2 c3 = obj_new('MG_Container', name='c3') c1->add, c3 c4 = obj_new('MG_Container', name='c4') c2->add, c4 c5 = obj_new('MG_Container', name='c5') c2->add, c5 checkC4 = c1->getByName('c2/c4') checkC5 = c1->getByName('c2/c5') checkC4->getProperty, name=checkC4Name help, checkC4Name checkC5->getProperty, name=checkC5Name help, checkC5Name end