Discussion:
[omniORB] insert data to union
Zsolt SZALAI
2007-07-16 21:23:26 UTC
Permalink
Hi!

I'm using omniorb 4.0.x. It always throws TypeMismatch exception(at
codeline marked with *) when i would like to insert data into an enum.
IDL:
module a {
enum swtype {
INT,
STR
};
union sw switch(swtype){
case INT: long intv;
case STR: string strv;
};
};

code:
CORBA::Object_var dafo;
dafo=orb->resolve_initial_references("DynAnyFactory");
DynamicAny::DynAnyFactory_var daf;
daf=DynamicAny::DynAnyFactory::_narrow(dafo);
DynamicAny::DynAny_var dv = daf->create_dyn_any_from_type_code(_tc_sw());
DynamicAny::DynAny_ptr disc =
daf->create_dyn_any_from_type_code(_tc_swtype());

DynamicAny::DynUnion_ptr dUnion = DynamicAny::DynUnion::_narrow(dv);
DynamicAny::DynEnum_ptr edisc = DynamicAny::DynEnum::_narrow(disc);
disc = dUnion->get_discriminator();

if (selected == 0) {
edisc->set_as_ulong(0); //swtype
DynamicAny::DynAny_ptr dm = dUnion->member();
dm->insert_long(5);
}
if (selected == 1) {
edisc->set_as_ulong(1); //swtype
DynamicAny::DynAny_ptr dm = dUnion->member();
* dm->insert_string("FOOBAR");
}
return(dUnion->to_any());

There is no problem, when selected == 0, so the problem is how to tell
that the STR case is valid, and would like to insert a string?

thx,
--
Zsolt
Duncan Grisby
2007-07-16 23:55:27 UTC
Permalink
Post by Zsolt SZALAI
I'm using omniorb 4.0.x. It always throws TypeMismatch exception(at
codeline marked with *) when i would like to insert data into an enum.
I haven't looked really closely at your code, but this looks fishy...

[...]
Post by Zsolt SZALAI
DynamicAny::DynAny_ptr disc = daf->create_dyn_any_from_type_code(_tc_swtype());
DynamicAny::DynUnion_ptr dUnion = DynamicAny::DynUnion::_narrow(dv);
DynamicAny::DynEnum_ptr edisc = DynamicAny::DynEnum::_narrow(disc);
You've created a DynAny with the right type for the discriminator, but
it's not the union's discriminator...
Post by Zsolt SZALAI
disc = dUnion->get_discriminator();
Here you're getting the actual discriminator from the union.
Post by Zsolt SZALAI
if (selected == 0) {
edisc->set_as_ulong(0); //swtype
And now you're setting the one that isn't the union's discriminator. It
doesn't matter because it defaults to that case.
Post by Zsolt SZALAI
DynamicAny::DynAny_ptr dm = dUnion->member();
dm->insert_long(5);
}
if (selected == 1) {
edisc->set_as_ulong(1); //swtype
Now you're setting the wrong thing again, but this time the default is
wrong.
Post by Zsolt SZALAI
DynamicAny::DynAny_ptr dm = dUnion->member();
* dm->insert_string("FOOBAR");
I think it'll probably work if you rearrange the code so that edisc is
the union's discriminator, not some other unrelated DynEnum.

Cheers,

Duncan.
--
-- Duncan Grisby --
-- ***@grisby.org --
-- http://www.grisby.org --
Continue reading on narkive:
Loading...