Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / DynUnion_Test / DynUnionTest.cpp
blobb2b8b06a7ddf49efc4575bf5508065b685f6d319
1 #include "dynanyC.h"
2 #include "tao/DynamicAny/DynamicAny.h"
3 #include "tao/TypeCodeFactory/TypeCodeFactory_Loader.h"
4 #include "tao/TypeCodeFactory/TypeCodeFactory_Adapter_Impl.h"
6 // Creates DynUnion using typecodes generated through IDL compiler.
7 int runStatic(CORBA::ORB_ptr orb)
9 try
11 CORBA::Object_var obj =
12 orb->resolve_initial_references("DynAnyFactory");
14 DynamicAny::DynAnyFactory_var dfact =
15 DynamicAny::DynAnyFactory::_narrow(obj.in ());
17 if (CORBA::is_nil (dfact.in ()))
19 ACE_ERROR_RETURN ((LM_ERROR,
20 "Nil dynamic any factory after narrow\n"),
21 -1);
24 DynamicAny::DynAny_var dany =
25 dfact->create_dyn_any_from_type_code(DynAnyTest::_tc_EventData);
27 DynamicAny::DynUnion_var dunion =
28 DynamicAny::DynUnion::_narrow(dany.in ());
30 DynamicAny::DynAny_var disc = dunion->get_discriminator();
32 DynamicAny::DynEnum_var denum =
33 DynamicAny::DynEnum::_narrow(disc.in ());
35 if (CORBA::is_nil (denum.in ()))
37 ACE_ERROR_RETURN ((LM_ERROR,
38 "Nil dynamic enum after narrow\n"),
39 -1);
42 ACE_DEBUG ((LM_DEBUG, "Static Test Passed\n"));
44 catch (const CORBA::Exception & ex)
46 ex._tao_print_exception ("runStatic");
47 return -1;
50 return 0;
53 // Creates DynUnion from typecode created programmatically
55 int runDynamic(CORBA::ORB_ptr orb)
57 try
59 // Create typecode for EventKind.
61 CORBA::EnumMemberSeq eseq;
63 eseq.length(2);
65 eseq[0] = CORBA::string_dup("ALARM_EVENT");
66 eseq[1] = CORBA::string_dup("LOG_EVENT");
68 CORBA::TypeCode_var eventKindTc;
70 eventKindTc = orb->create_enum_tc("IDL:DynAnyTest/EventKind:1.0",
71 "EventKind",
72 eseq);
74 // Create typecode for EventData. EventKind tc is used.
76 CORBA::Any label;
77 CORBA::UnionMemberSeq useq;
79 useq.length(2);
81 label <<= DynAnyTest::ALARM_EVENT;
82 useq[0].name = CORBA::string_dup("ev_data");
83 useq[0].label = label;
84 useq[0].type = CORBA::TypeCode::_duplicate(CORBA::_tc_any);
85 useq[0].type_def = CORBA::IDLType::_nil ();
87 label <<= DynAnyTest::LOG_EVENT;
88 useq[1].name = CORBA::string_dup("ev_desc");
89 useq[1].label = label;
90 useq[1].type = CORBA::TypeCode::_duplicate(CORBA::_tc_string);
91 useq[1].type_def = CORBA::IDLType::_nil ();
93 CORBA::TypeCode_var eventDataTc;
95 eventDataTc = orb->create_union_tc("IDL:DynAnyTest/EventData:1.0",
96 "EventData",
97 eventKindTc.in (),
98 useq);
100 // Now create DynUnion
102 CORBA::Object_var obj = orb->resolve_initial_references("DynAnyFactory");
104 DynamicAny::DynAnyFactory_var dfact =
105 DynamicAny::DynAnyFactory::_narrow(obj.in ());
107 if (CORBA::is_nil (dfact.in ()))
109 ACE_ERROR_RETURN ((LM_ERROR,
110 "Nil dynamic any factory after narrow\n"),
111 -1);
114 DynamicAny::DynAny_var dany =
115 dfact->create_dyn_any_from_type_code(eventDataTc.in ());
117 DynamicAny::DynUnion_var dunion =
118 DynamicAny::DynUnion::_narrow(dany.in ());
120 DynamicAny::DynAny_var disc = dunion->get_discriminator();
122 DynamicAny::DynEnum_var denum =
123 DynamicAny::DynEnum::_narrow(disc.in ());
125 // This fails. The type of discriminator is ulong
126 if (CORBA::is_nil (denum.in ()))
128 ACE_ERROR_RETURN ((LM_ERROR,
129 "Nil dynamic enum after narrow\n"),
130 -1);
133 ACE_DEBUG ((LM_DEBUG, "Dynamic Test Passed\n"));
135 denum->destroy();
136 dunion->destroy();
138 catch (const CORBA::Exception & ex)
140 ex._tao_print_exception ("Dynamic Test Failed with exception");
142 return 0;
145 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
149 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
151 if (runStatic(orb.in()) != 0)
152 return -1;
154 if (runDynamic(orb.in()) != 0)
155 return -1;
157 catch (const CORBA::Exception & ex)
159 ex._tao_print_exception ("Tests failed with exception");
160 return 1;
163 return 0;