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
)
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"),
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"),
42 ACE_DEBUG ((LM_DEBUG
, "Static Test Passed\n"));
44 catch (const CORBA::Exception
& ex
)
46 ex
._tao_print_exception ("runStatic");
53 // Creates DynUnion from typecode created programmatically
55 int runDynamic(CORBA::ORB_ptr orb
)
59 // Create typecode for EventKind.
61 CORBA::EnumMemberSeq eseq
;
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",
74 // Create typecode for EventData. EventKind tc is used.
77 CORBA::UnionMemberSeq useq
;
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",
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"),
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"),
133 ACE_DEBUG ((LM_DEBUG
, "Dynamic Test Passed\n"));
138 catch (const CORBA::Exception
& ex
)
140 ex
._tao_print_exception ("Dynamic Test Failed with exception");
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)
154 if (runDynamic(orb
.in()) != 0)
157 catch (const CORBA::Exception
& ex
)
159 ex
._tao_print_exception ("Tests failed with exception");