//---------------------------------------------------------------------- std::string AnyAide::enumToString(const CORBA::Any& value) { CORBA::ORB_ptr orb_p = ORBHelper::getORB(); //get the dynamic any factory CORBA::Object_var factory_obj = orb_p->resolve_initial_references ("DynAnyFactory"); //narrow it DynamicAny::DynAnyFactory_var dynany_factory = DynamicAny::DynAnyFactory::_narrow(factory_obj.in()); //sanity check ACE_ASSERT(!CORBA::is_nil(dynany_factory.in())); //get the dynamic any DynamicAny::DynAny_var dynValue = dynany_factory->create_dyn_any(value); //sanity check ACE_ASSERT(!CORBA::is_nil(dynValue.in())); //narrow it to an enum DynamicAny::DynEnum_var dynEnum = DynamicAny::DynEnum::_narrow(dynValue.in()); //another sanity check ACE_ASSERT(!CORBA::is_nil(dynEnum.in())); char* tString = dynEnum->get_as_string(); std::string retVal(tString); //destroy the dynamic any dynEnum->destroy(); //free up some dynamic memory CORBA::string_free(tString); return retVal; }
// Creates DynUnion using typecodes generated through IDL compiler. int runStatic(CORBA::ORB_ptr orb) { try { CORBA::Object_var obj = orb->resolve_initial_references("DynAnyFactory"); DynamicAny::DynAnyFactory_var dfact = DynamicAny::DynAnyFactory::_narrow(obj.in ()); if (CORBA::is_nil (dfact.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "Nil dynamic any factory after narrow\n"), -1); } DynamicAny::DynAny_var dany = dfact->create_dyn_any_from_type_code(DynAnyTest::_tc_EventData); DynamicAny::DynUnion_var dunion = DynamicAny::DynUnion::_narrow(dany.in ()); DynamicAny::DynAny_var disc = dunion->get_discriminator(); DynamicAny::DynEnum_var denum = DynamicAny::DynEnum::_narrow(disc.in ()); if (CORBA::is_nil (denum.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "Nil dynamic enum after narrow\n"), -1); } ACE_DEBUG ((LM_DEBUG, "Static Test Passed\n")); } catch (const CORBA::Exception & ex) { ex._tao_print_exception ("runStatic"); return -1; } return 0; }
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { int result= 0; ACE_DEBUG (( LM_DEBUG, "Start\n" )); try { // init orb CORBA::ORB_var the_orb = CORBA::ORB_init (argc, argv); if (parse_args (argc, argv) == -1) return -1; ACE_DEBUG (( LM_DEBUG, "Get IFR\n" )); CORBA::Object_var objref = the_orb->string_to_object (ifr_ior_file); if (objref.in () == 0) { ACE_ERROR_RETURN ((LM_ERROR, "The received objref is nil\n"), -1); } ACE_DEBUG (( LM_DEBUG, "Narrow IFR\n" )); CORBA::ComponentIR::Repository_var the_repo_ref; the_repo_ref = CORBA::ComponentIR::Repository::_narrow (objref.in ()); ACE_DEBUG (( LM_DEBUG, "Obtaining DynamicAny\n" )); CORBA::Object_var factory_obj = the_orb->resolve_initial_references ("DynAnyFactory"); DynamicAny::DynAnyFactory_var dynanyfactory = DynamicAny::DynAnyFactory::_narrow (factory_obj.in ()); ACE_DEBUG (( LM_DEBUG, "\nLook up c2\n" )); CORBA::Contained_var c2 = the_repo_ref->lookup_id ("IDL:m1/c2:1.0"); if (CORBA::is_nil (c2.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "Can't look up the const m1/c2\n"), -1); } CORBA::ConstantDef_var c2_def = CORBA::ConstantDef::_narrow (c2.in ()); CORBA::String_var c2_name= c2_def->absolute_name (); CORBA::TypeCode_var c2_tc = c2_def->type (); CORBA::String_var c2_id = c2_tc->id (); ACE_DEBUG (( LM_DEBUG, "constant \"%C\" is type \"%C\"", c2_name.in (), c2_id.in () )); CORBA::TCKind c2_tckind = c2_tc->kind (); ACE_DEBUG (( LM_DEBUG, ", tkkind %d", c2_tckind )); if (CORBA::tk_enum == c2_tckind) { ACE_DEBUG (( LM_DEBUG, " (CORBA::tk_enum)\n" )); CORBA::Any_var the_value = c2_def->value (); DynamicAny::DynAny_var dany = dynanyfactory->create_dyn_any (the_value.in ()); DynamicAny::DynEnum_var denum = DynamicAny::DynEnum::_narrow (dany.in ()); CORBA::String_var the_strValue = denum->get_as_string (); CORBA::ULong the_intValue = denum->get_as_ulong (); ACE_DEBUG ((LM_DEBUG, "Whose value is \"%C\" which has an integer value of %d\n", the_strValue.in (), the_intValue )); if (0 == ACE_OS::strcmp( "e1_2", the_strValue.in () )) { ACE_DEBUG ((LM_DEBUG, "The string value is correct\n" )); } else { ACE_DEBUG ((LM_DEBUG, "ERROR: The string value should be \"e1_2\"\n" )); result = -1; } if (1 == the_intValue ) { ACE_DEBUG ((LM_DEBUG, "The corresponding integer value is correct\n" )); } else { ACE_DEBUG ((LM_DEBUG, "ERROR: The corresponding integer value should be 1\n" )); result = -1; } } else { ACE_DEBUG ((LM_DEBUG, "\nERROR: Wrong tkkind for m1::c2, should be %d\n", CORBA::tk_enum)); result= -1; } } catch (CORBA::Exception &ex) { ex._tao_print_exception ("ERROR: CORBA Exception"); result= -1; } catch (...) { ACE_DEBUG ((LM_DEBUG, "ERROR: UNKNOWN Excetion\n")); result= -1; } ACE_DEBUG (( LM_DEBUG, "\nDone\n" )); return result; }
int runDynamic(CORBA::ORB_ptr orb) { try { // Create typecode for EventKind. CORBA::EnumMemberSeq eseq; eseq.length(2); eseq[0] = CORBA::string_dup("ALARM_EVENT"); eseq[1] = CORBA::string_dup("LOG_EVENT"); CORBA::TypeCode_var eventKindTc; eventKindTc = orb->create_enum_tc("IDL:DynAnyTest/EventKind:1.0", "EventKind", eseq); // Create typecode for EventData. EventKind tc is used. CORBA::Any label; CORBA::UnionMemberSeq useq; useq.length(2); label <<= DynAnyTest::ALARM_EVENT; useq[0].name = CORBA::string_dup("ev_data"); useq[0].label = label; useq[0].type = CORBA::TypeCode::_duplicate(CORBA::_tc_any); useq[0].type_def = CORBA::IDLType::_nil (); label <<= DynAnyTest::LOG_EVENT; useq[1].name = CORBA::string_dup("ev_desc"); useq[1].label = label; useq[1].type = CORBA::TypeCode::_duplicate(CORBA::_tc_string); useq[1].type_def = CORBA::IDLType::_nil (); CORBA::TypeCode_var eventDataTc; eventDataTc = orb->create_union_tc("IDL:DynAnyTest/EventData:1.0", "EventData", eventKindTc.in (), useq); // Now create DynUnion CORBA::Object_var obj = orb->resolve_initial_references("DynAnyFactory"); DynamicAny::DynAnyFactory_var dfact = DynamicAny::DynAnyFactory::_narrow(obj.in ()); if (CORBA::is_nil (dfact.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "Nil dynamic any factory after narrow\n"), -1); } DynamicAny::DynAny_var dany = dfact->create_dyn_any_from_type_code(eventDataTc.in ()); DynamicAny::DynUnion_var dunion = DynamicAny::DynUnion::_narrow(dany.in ()); DynamicAny::DynAny_var disc = dunion->get_discriminator(); DynamicAny::DynEnum_var denum = DynamicAny::DynEnum::_narrow(disc.in ()); // This fails. The type of discriminator is ulong if (CORBA::is_nil (denum.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "Nil dynamic enum after narrow\n"), -1); } ACE_DEBUG ((LM_DEBUG, "Dynamic Test Passed\n")); denum->destroy(); dunion->destroy(); } catch (const CORBA::Exception & ex) { ex._tao_print_exception ("Dynamic Test Failed with exception"); } return 0; }
void DynAnyAnalyzer::analyze (DynamicAny::DynAny_ptr da) { CORBA::TypeCode_var tc = da->type (); CORBA::TCKind kind = tc->kind (); CORBA::TypeCode_var dup = CORBA::TypeCode::_duplicate (tc.in ()); // strip aliases while (kind == CORBA::tk_alias) { dup = dup->content_type (); kind = dup->kind (); } switch (kind) { case CORBA::tk_struct: { DynamicAny::DynStruct_var ds = DynamicAny::DynStruct::_narrow (da); tab (level_); if (debug_) { ACE_DEBUG ((LM_DEBUG, "STRUCT\n")); } if (da->seek (0) ) { level_++; do { DynamicAny::DynAny_var cc = ds->current_component (); DynamicAny::FieldName_var fn = ds->current_member_name (); tab(level_); if (debug_) { ACE_DEBUG ((LM_DEBUG, "Member = %C\n", fn.in ())); } if (!CORBA::is_nil (cc.in ())) { this->analyze (cc.in ()); } } while (da->next ()); level_--; } } break; // end tk_struct case CORBA::tk_sequence: { if (TAO_DynCommon::is_basic_type_seq (tc.in ())) { this->analyze_basic_seq (dup.in (), da); } else { DynamicAny::DynSequence_var ds = DynamicAny::DynSequence::_narrow (da); int i = 0; tab(level_); if (debug_) { ACE_DEBUG ((LM_DEBUG, "SEQUENCE\n")); } if (ds->seek (0UL)) { level_++; do { tab(level_); if (debug_) ACE_DEBUG ((LM_DEBUG, "[%d]\n", i)); DynamicAny::DynAny_var cc (ds->current_component ()); if (!CORBA::is_nil (cc.in ())) { analyze (cc.in ()); } i++; } while (da->next ()); level_--; } } } break; // end tk_sequence case CORBA::tk_array: { tab (level_); if (debug_) { ACE_DEBUG ((LM_DEBUG, "ARRAY\n")); } level_++; CORBA::ULong const len = dup->length (); for (CORBA::ULong i = 0 ; i < len; ++i) { tab (level_); if (debug_) { ACE_DEBUG ((LM_DEBUG, "[%d]\n", i)); } DynamicAny::DynAny_var cc = da->current_component(); if (!CORBA::is_nil (cc.in ())) { analyze (cc.in ()); } da->next (); } level_--; } break; case CORBA::tk_union: { DynamicAny::DynUnion_var value = DynamicAny::DynUnion::_narrow (da); if (!value->has_no_active_member ()) { DynamicAny::DynAny_var disc = value->member (); if (!CORBA::is_nil (disc.in ())) { this->analyze (disc.in ()); } } } break; case CORBA::tk_any: { DynamicAny::DynAny_var dynany; CORBA::Any_var any = da->get_any (); dynany = dynany_factory_->create_dyn_any (any.in ()); if (!CORBA::is_nil (dynany.in ())) { this->analyze (dynany.in ()); } dynany->destroy (); } break; case CORBA::tk_enum: { DynamicAny::DynEnum_var value = DynamicAny::DynEnum::_narrow (da); CORBA::String_var s = value->get_as_string (); tab (level_); if (debug_) { ACE_DEBUG ((LM_DEBUG, " Value (enum) = %C\n", s.in())); } } break; CASEE (boolean, Boolean, " Value (bool) = %d\n"); CASEE (short, Short, " Value (short) = %d\n"); CASEE (ushort, UShort, " Value (ushort) = %u\n"); CASEE (long, Long, " Value (long) = %d\n"); CASEE (ulong, ULong, " Value (ulong) = %u\n"); CASEE (longlong, LongLong, " Value (longlong) %Ld\n"); CASEE (ulonglong, ULongLong, " Value (ulonglong) %Lu\n"); CASEE (char, Char, " Value (char) = %c\n"); CASEE (float, Float, " Value (float) = %f\n"); CASEE (double, Double, " Value (double) = %f\n"); CASEE (octet, Octet, " Value (octet) = %c\n"); case CORBA::tk_string: { CORBA::String_var b (da->get_string ()); tab (level_); if (debug_) { ACE_DEBUG ((LM_DEBUG, " Value (string) = %C\n", b.in ())); } } break; case CORBA::tk_TypeCode: { tab (level_); if (debug_) { CORBA::TCKind const kind = da->get_typecode ()->kind (); ACE_DEBUG ((LM_DEBUG, " Value (TypeCode) = %d\n", static_cast<int> (kind))); } } break; default: { tab (level_); if (debug_) { CORBA::TCKind const kind = tc->kind (); ACE_DEBUG ((LM_DEBUG, " unhandled typecode = %d\n", static_cast<int> (kind))); } } break; } if (debug_) { ACE_DEBUG ((LM_DEBUG, "\n")); } }