Issue 1670: DynAny::current_component if no components? (port-rtf) Source: (, ) Nature: Uncategorized Issue Severity: Summary: Summary: What does DynAny::CurrentComponent() return if there are no components, for example if I iterate over an empty exception or empty sequence? Resolution: Revised Text: Actions taken: July 13, 1998: received issue Discussion: received issue End of Annotations:===== Return-Path: X-Authentication-Warning: tigger.dstc.edu.au: michi owned process doing -bs Date: Sun, 12 Jul 1998 14:23:49 +1000 (EST) From: Michi Henning Reply-To: Michi Henning To: port-rtf@omg.org, issues@omg.org Subject: DynAny::current_component if no components? What does DynAny::CurrentComponent() return if there are no components, for example if I iterate over an empty exception or empty sequence? Assume I want to write code that iterates over arbitrary structures and exceptions: void display(CORBA::DynStruct_ptr dsp) { CORBA::DynAny_var da = dsp->current_component(); // What now if the DynStruct contains an empty exception? } There appears to be no way to get the number of members first. I can call seek(0) and test its return value to detect whether the exception has members (seek(0) will return FALSE for an empty exception) but that's rather obscure. The same problem arises if I call current_component on a DynAny for a basic type. What does it return? Same problem with current_member_name(). What should it return if I call it on a DynStruct for an empty exception? Suggestion: current_component() should return a nil reference if the current position does not point at a valid component or if invoked on a DynAny for a basic type. current_member_name() should return an empty string if the current position does not point at a valid component. Rationale: Returning nil is nicer on the programmer than raising an exception. Consider: CORBA::DynStruct_var ds = ...; CORBA::DynAny_var cm = ds->current_component; CORBA::String_var name = ds->current_member_name(); while (!CORBA::is_nil(cm)) { cout << name << " = "; show_component(cm); // Some helper function ds->next(); cm = ds->current_component(); name = ds->current_member_name(); } If current_component() returns a nil reference, I can control the loop by testing for nil as above. Regardless of whether current_component() and current_member_name() should raise an exception or not, it might also be a good idea to add a readonly member_count attribute to DynStruct. If I have a member_count attribute, I can write my loop like this: for (int i = 0; i < ds->member_count(); i++, ds->next()) { CORBA::String_var name = ds->current_member_name(); CORBA::DynAny_var cm = ds->current_component; cout << name << " = "; show_component(cm); // Some helper function } Cheers, Michi. -- Michi Henning +61 7 33654310 DSTC Pty Ltd +61 7 33654311 (fax) University of Qld 4072 michi@dstc.edu.au AUSTRALIA http://www.dstc.edu.au/BDU/staff/michi-henning.html Return-Path: Sender: jon@floorboard.com Date: Sat, 11 Jul 1998 21:56:23 -0700 From: Jonathan Biggar To: Michi Henning CC: port-rtf@omg.org, issues@omg.org Subject: Re: DynAny::current_component if no components? References: Michi Henning wrote: > > What does DynAny::CurrentComponent() return if there are no > components, > for example if I iterate over an empty exception or empty sequence? > > Assume I want to write code that iterates over arbitrary structures > and exceptions: > > void > display(CORBA::DynStruct_ptr dsp) > { > CORBA::DynAny_var da = dsp->current_component(); > // What now if the DynStruct contains an empty > exception? > } > > There appears to be no way to get the number of members first. > I can call seek(0) and test its return value to detect whether the > exception has members (seek(0) will return FALSE for an empty > exception) but that's rather obscure. > > The same problem arises if I call current_component on a DynAny for > a > basic type. What does it return? Issue #1144. > Same problem with current_member_name(). What should it > return if I call it on a DynStruct for an empty exception? > > Suggestion: > > current_component() should return a nil reference if the > current position does not point at a valid component or > if invoked on a DynAny for a basic type. > > current_member_name() should return an empty string if > the current position does not point at a valid component. > > Rationale: > > Returning nil is nicer on the programmer than raising an > exception. Consider: > > CORBA::DynStruct_var ds = ...; > > CORBA::DynAny_var cm = ds->current_component; > CORBA::String_var name = ds->current_member_name(); > while (!CORBA::is_nil(cm)) { > cout << name << " = "; > show_component(cm); // Some helper function > ds->next(); > cm = ds->current_component(); > name = ds->current_member_name(); > } > > If current_component() returns a nil reference, I can control the > loop by testing for nil as above. Reasonable proposal. > Regardless of whether current_component() and current_member_name() > should raise an exception or not, it might also be a good idea > to add a readonly member_count attribute to DynStruct. If I have > a member_count attribute, I can write my loop like this: > > for (int i = 0; i < ds->member_count(); i++, ds->next()) { > CORBA::String_var name = ds->current_member_name(); > CORBA::DynAny_var cm = ds->current_component; > cout << name << " = "; > show_component(cm); // Some helper function > } > You could, of course do this: CORBA::TypeCode_var tc = ds->type(); for (int i = 0; i < tc->member_count(); i++, ds->next()) { CORBA::String_var name = ds->current_member_name(); CORBA::DynAny_var cm = ds->current_component; cout << name << " = "; show_component(cm); // Some helper function } -- Jon Biggar Floorboard Software jon@floorboard.com jon@biggar.org Return-Path: X-Authentication-Warning: tigger.dstc.edu.au: michi owned process doing -bs Date: Sun, 12 Jul 1998 15:03:02 +1000 (EST) From: Michi Henning To: Jonathan Biggar cc: port-rtf@omg.org, issues@omg.org Subject: Re: DynAny::current_component if no components? On Sat, 11 Jul 1998, Jonathan Biggar wrote: > > There appears to be no way to get the number of members first. > > I can call seek(0) and test its return value to detect whether the > > exception has members (seek(0) will return FALSE for an empty > > exception) but that's rather obscure. > > > > The same problem arises if I call current_component on a DynAny > for a > > basic type. What does it return? > > Issue #1144. Yep, but that only talks about primitive types, not empy DynStructs ;-) > You could, of course do this: > > CORBA::TypeCode_var tc = ds->type(); > for (int i = 0; i < tc->member_count(); i++, ds->next()) > { > CORBA::String_var name = ds->current_member_name(); > CORBA::DynAny_var cm = ds->current_component; > cout << name << " = "; > show_component(cm); // Some helper function > } I thought of that too, but I don't like it much because it forces me to also fiddle with the type code. It would be easier if there was something on DynStruct itself. Cheers, Michi. -- Michi Henning +61 7 33654310 DSTC Pty Ltd +61 7 33654311 (fax) University of Qld 4072 michi@dstc.edu.au AUSTRALIA http://www.dstc.edu.au/BDU/staff/michi-henning.html