|
|
|
|||||
|
Sequence of 'unions'?
Is it possible to return a sequence of (simulated) union values?
I'm trying to do something along the lines of the following: class SymValue { }; class ByteValue extends SymValue { byte byteval; }; class ShortValue extends SymValue { short shortval; }; class LongValue extends SymValue { long longval; }; class FloatValue extends SymValue { float floatval; }; class DoubleValue extends SymValue { double doubleval; }; struct SymDatum { string name; double rate; short units; SymValue* value; }; sequence<string> SymNameSeq; sequence<SymDatum> SymDataSeq; // Return symbols matching 'symname' class Source { nonmutating SymDataSeq SymQuery( string symname ); . . }; I've been having problems getting a test program for this to compile, and just wanted to ask: is this even possible, or does the Slice-to-C++ mapping not permit such usage? |
|
|||||
|
> If I'm right, you need to remove the * from the value member.
Thanks for the help, Marc. It was a bit confusing at first, but... it compiles as long as the right-hand of the last assignment below is a DoubleValuePtr: SmartNode::SymData symdata; SmartNode::DoubleValuePtr symvalue = new SmartNode::DoubleValue; symvalue->doubleval = 30.0; symdata.name = "VDM:RPM"; symdata.value = symvalue; <-- this assignment I didn't realize that the left-hand side was of type 'SymValuePtr&' rather than 'SymValue&'. I'm still struggling with how to access the returned values, though. This code: cout << "Found the following matches:\n"; for( SymDataSeq::const_iterator it = matches.begin(); it != matches.end(); ++it ) { cout << "\t" << (*it).name << " : Type = " << (*it).value->ice_id() << "\n"; } prints out: Sending SymQuery for 'VDM:*'... Found the following matches: VDM:RPM : Type = ::SmartNode::DoubleValue VDM:RATE : Type = ::SmartNode::DoubleValue VDM:RATIO : Type = ::SmartNode::DoubleValue Which is just as it should be. Only the *type* is SymValuePtr&, so I've got to figure out how to convert a SymValuePtr& to a DoubleValuePtr& to access the member I'm looking for. I'm sure I'll hit upon the proper incantation Real Soon Now t.m. ... :-} Thanks again, - Dave P.S. Ice is *great*! |
|
||||||
|
Quote:
Code:
for( SymDataSeq::const_iterator it = matches.begin();
it != matches.end(); ++it )
{
DoubleValuePtr p = DoubleValuePtr::dynamicCast(*it);
if (p)
{
cout << p->doubleVal << "\n";
}
}
Cheers, Michi. Quote:
![]() |
|
|||||
|
You need to use a dynamic cast...
See page 190 in the manual. Thanks for the pointer--it just saved me a lot of time. I was trying to commit all sorts of horrible atrocities with dynamic_cast<> and C-style casts. I even attempted to call a (thankfully, non-existent) function called DoubleValue::checkedCast()--d'oh! :-% It's really quite nice, actually, that such rude hacks failed to compile! That seems to be a real strength of Ice: it seems to be kind of hard to do something really wrong and have the code compile properly. Thinks are working much, much better now... :-D |
|
||||||
|
Quote:
![]() When using Ice, you never need to use a cast. If you want to do something and find that you can do it only with a cast, it is a sure-fire sign that you doing something you had better not ![]() Quote:
Cheers, Michi. |
|
||||||
|
Quote:
Cheers, Michi. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| a question about Classes as Unions | fengxb | Help Center | 9 | 04-11-2007 10:00 AM |
| null sequence in C# | chris92 | Help Center | 3 | 09-29-2006 09:58 AM |
| Questions about sanity-check of sequence sizes during sequence unmarshaling | rc_hz | Help Center | 4 | 06-22-2005 10:33 PM |
| About Classes as Unions | level | Help Center | 2 | 04-09-2004 02:45 AM |
| Questions about sequence<byte> in C++? | rodrigc | Help Center | 3 | 02-24-2003 11:21 PM |