Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / DynValue_Test / Analyzer.cpp
blob9dd484ea6d28041db5039f4b8bfbef46cf8b6803
1 #include "Analyzer.h"
2 #include "tao/DynamicAny/DynCommon.h"
4 DynAnyAnalyzer::DynAnyAnalyzer (int argc, ACE_TCHAR *argv[])
5 : level_ (1u),
6 orb_ (CORBA::ORB_init (argc, argv)),
7 dany_fact_ (0)
9 if (CORBA::is_nil (orb_.in ()))
11 ACE_DEBUG ((LM_ERROR, "Could not init orb\n"));
12 throw 0;
15 CORBA::Object_var
16 obj (orb_->resolve_initial_references ("DynAnyFactory"));
17 dany_fact_ = DynamicAny::DynAnyFactory::_narrow(obj.in ());
18 if (CORBA::is_nil (dany_fact_.in ()))
20 ACE_DEBUG ((LM_ERROR, "Nil dynamic any factory after narrow\n"));
21 throw 0;
25 DynamicAny::DynAny_ptr
26 DynAnyAnalyzer::DynAny_NoTrunc (const CORBA::Any &any)
28 return this->dany_fact_
29 ->create_dyn_any_without_truncation (any);
32 DynamicAny::DynAny_ptr
33 DynAnyAnalyzer::DynAny (const CORBA::Any &any)
35 return this->dany_fact_
36 ->create_dyn_any (any);
39 DynamicAny::DynAny_ptr
40 DynAnyAnalyzer::DynAny (const CORBA::TypeCode_ptr &tc)
42 return this->dany_fact_
43 ->create_dyn_any_from_type_code (tc);
46 void
47 DynAnyAnalyzer::register_factory (
48 CORBA::ValueFactoryBase_var factory)
50 factory_id * new_fact (
51 dynamic_cast<factory_id *> (factory.in()));
52 if (new_fact)
54 tab("Registering factory for ValueType ");
55 ACE_DEBUG ((LM_DEBUG, "%C\n", new_fact->id ()));
56 CORBA::ValueFactoryBase_var old_fact (
57 orb_->register_value_factory (
58 new_fact->id (), factory.in () ));
62 void
63 DynAnyAnalyzer::get_base_types (
64 CORBA::TypeCode_ptr tc,
65 BaseTypesList_t &base_types,
66 CORBA::ULong *total_member_count)
68 // First initalize to the fully derived type we are
69 // starting with.
71 CORBA::ULong numberOfBases = 1u;
72 base_types.size (numberOfBases);
73 base_types[0] = TAO_DynAnyFactory::strip_alias (tc);
74 if (total_member_count)
76 *total_member_count = base_types[0]->member_count ();
79 // Obtain each derived type's basetype and add this to the list
81 CORBA::TypeCode_var
82 base (base_types[0]->concrete_base_type());
83 while (0 != base.in() &&
84 CORBA::tk_value ==
85 (base = TAO_DynAnyFactory::strip_alias (base.in()))
86 ->kind ())
88 if (total_member_count)
90 *total_member_count += base->member_count ();
93 base_types.size (numberOfBases + 1);
94 base_types[numberOfBases++] = CORBA::TypeCode::_duplicate (base.in ());
95 base = base->concrete_base_type();
99 CORBA::TypeCode_ptr
100 DynAnyAnalyzer::get_correct_base_type (
101 const BaseTypesList_t &base_types,
102 CORBA::ULong &index)
104 // We work backwards through the list of derived types,
105 // so index 0 is the first member of the extreme base type
106 // (assuming it has any members) once we run out of that
107 // base types members we move up the list to the next
108 // derived type until that type's members are exhausted
109 // and so on until we reach the member we have asked for.
111 CORBA::ULong
112 currentBase = base_types.size ();
113 if (!currentBase)
115 ACE_DEBUG ((LM_DEBUG,
116 ACE_TEXT ("TAO_DynValue_i::get_correct_base_type () ")
117 ACE_TEXT ("BaseTypesList_t is not initialised\n")));
118 return 0;
121 while (base_types[--currentBase]->member_count () <= index)
123 index -= base_types[currentBase]->member_count ();
124 if (!currentBase)
126 ACE_DEBUG ((LM_DEBUG,
127 ACE_TEXT ("TAO_DynValue_i::get_correct_base_type () ")
128 ACE_TEXT ("BaseTypesList_t is not large enough\n")));
129 return 0;
133 // Note that the "index" has now been reduced to the range
134 // of the returning base_type.
135 return base_types[currentBase].in ();
138 #define CASEE(type,CT,str) case CORBA::tk_##type: {\
139 CORBA::CT b = da->get_##type();\
140 if (!newline) tab(""); ACE_DEBUG ((LM_DEBUG, str , b));\
141 } break;
143 void
144 DynAnyAnalyzer::analyze (
145 DynamicAny::DynAny_ptr da,
146 CORBA::Boolean newline)
148 CORBA::TypeCode_var tc (da->type ());
149 CORBA::TCKind kind (tc->kind ());
150 CORBA::TypeCode_var
151 dup (CORBA::TypeCode::_duplicate (tc.in ()));
153 // strip aliases
154 while (CORBA::tk_alias == kind)
156 dup = dup->content_type ();
157 kind = dup->kind ();
160 switch (kind)
162 case CORBA::tk_value_box:
164 DynamicAny::DynValueBox_var
165 box (DynamicAny::DynValueBox::_narrow (da));
167 if (!newline)
169 tab ("");
171 if (box->is_null ())
173 ACE_DEBUG ((LM_DEBUG, "{NULL} "));
175 ACE_DEBUG ((LM_DEBUG, "ValueBox Type: %C \"%C\": ",
176 tc->name (), tc->id ()));
177 if (box->is_null ())
179 ACE_DEBUG ((LM_DEBUG, "\n"));
181 else
183 DynamicAny::DynAny_var
184 cc (box->current_component ());
185 this->analyze (cc.in (), true);
188 break; // end tk_valuebox
190 case CORBA::tk_value:
192 DynamicAny::DynValue_var
193 dvt (DynamicAny::DynValue::_narrow (da));
195 if (!newline)
197 tab ("");
199 if (dvt->is_null ())
201 ACE_DEBUG ((LM_DEBUG, "{NULL} "));
204 BaseTypesList_t base_types;
205 get_base_types (tc.in (), base_types);
207 for (CORBA::ULong i= 0u; i < base_types.size(); ++i)
209 if (i)
211 tab (": ");
213 else
215 ACE_DEBUG ((LM_DEBUG, "ValueType: "));
218 switch (base_types[i]->type_modifier ())
220 case CORBA::VM_CUSTOM:
221 ACE_DEBUG ((LM_DEBUG, "Custom "));
222 break;
224 case CORBA::VM_ABSTRACT:
225 ACE_DEBUG ((LM_DEBUG, "Abstract "));
226 break;
228 case CORBA::VM_TRUNCATABLE:
229 ACE_DEBUG ((LM_DEBUG, "Truncatable "));
230 break;
232 default:
233 break;
236 ACE_DEBUG ((LM_DEBUG, "%C \"%C\"\n",
237 base_types[i]->name (),
238 base_types[i]->id () ));
239 ++level_;
241 level_ -= base_types.size();
243 if (!(dvt->is_null ()))
245 CORBA::ULong member_number = 0u;
246 ++level_;
247 if (da->seek (0)) do
249 DynamicAny::DynAny_var
250 cc (dvt->current_component ());
251 DynamicAny::FieldName_var
252 fn (dvt->current_member_name ());
253 CORBA::ULong sub_member_number = member_number;
254 const CORBA::TypeCode_ptr
255 base = get_correct_base_type (
256 base_types,
257 sub_member_number);
258 const char *const visability =
259 ((CORBA::PRIVATE_MEMBER ==
260 base->member_visibility (sub_member_number)) ?
261 "Private" : "Public ");
262 tab ("[");
263 ACE_DEBUG ((LM_DEBUG, "%03u] %C \"%C\": ",
264 ++member_number, visability, fn.in () ));
265 if (CORBA::is_nil (cc.in ()))
267 ACE_DEBUG ((LM_DEBUG, " {Null}\n"));
269 else
271 this->analyze (cc.in (), true);
273 } while (da->next ());
274 else
276 tab("{Has No Members}\n");
278 --level_;
281 break; // end tk_value
283 case CORBA::tk_struct:
285 DynamicAny::DynStruct_var
286 ds (DynamicAny::DynStruct::_narrow (da));
288 if (newline)
290 ACE_DEBUG ((LM_DEBUG, "\n"));
292 tab ("Struct: ");
293 ACE_DEBUG ((LM_DEBUG, "%C \"%C\"",
294 tc->name (), tc->id () ));
295 if (da->seek (0))
297 ACE_DEBUG ((LM_DEBUG, "\n"));
298 ++level_;
301 DynamicAny::DynAny_var
302 cc (ds->current_component ());
303 DynamicAny::FieldName_var
304 fn (ds->current_member_name ());
306 tab ("Member = ");
307 ACE_DEBUG ((LM_DEBUG, "%C\n", fn.in ()));
308 if (!CORBA::is_nil (cc.in ()))
310 this->analyze (cc.in ());
312 } while (da->next ());
313 --level_;
315 else
317 ACE_DEBUG ((LM_DEBUG, " Null\n"));
320 break; // end tk_struct
322 case CORBA::tk_union:
324 if (newline)
326 ACE_DEBUG ((LM_DEBUG, "\n"));
328 tab ("Union: ");
329 ACE_DEBUG ((LM_DEBUG, "%C \"%C\"",
330 tc->name (), tc->id () ));
331 DynamicAny::DynUnion_var
332 value (DynamicAny::DynUnion::_narrow (da));
333 if (value->has_no_active_member ())
335 ACE_DEBUG ((LM_DEBUG, " No Active member\n"));
337 else
339 ACE_DEBUG ((LM_DEBUG, "\n"));
340 DynamicAny::DynAny_var disc (value->member ());
341 if (!CORBA::is_nil (disc.in ()))
343 ++level_;
344 this->analyze (disc.in ());
345 --level_;
349 break; // end tk_union
351 case CORBA::tk_enum:
353 DynamicAny::DynEnum_var
354 value (DynamicAny::DynEnum::_narrow (da));
356 if (newline)
358 ACE_DEBUG ((LM_DEBUG, "\n"));
360 tab ("Enum: ");
361 ACE_DEBUG ((LM_DEBUG, "%C \"%C\"",
362 tc->name (), tc->id () ));
363 CORBA::String_var s (value->get_as_string ());
364 ACE_DEBUG ((LM_DEBUG, " value %C\n", s.in()));
366 break; // end tk_enum
368 case CORBA::tk_sequence:
370 if (TAO_DynCommon::is_basic_type_seq (tc.in ()))
372 if (newline) ACE_DEBUG ((LM_DEBUG, "\n"));
373 this->analyze_basic_seq (dup.in (), da);
375 else
377 DynamicAny::DynSequence_var
378 ds (DynamicAny::DynSequence::_narrow (da));
380 int i = 0;
381 if (newline)
383 ACE_DEBUG ((LM_DEBUG, "\n"));
385 tab ("Sequence:\n");
386 if (ds->seek (0))
388 ++level_;
391 tab ("");
392 ACE_DEBUG ((LM_DEBUG, "[%03d]\n", i));
393 DynamicAny::DynAny_var
394 cc (ds->current_component ());
395 if (!CORBA::is_nil (cc.in ()))
397 analyze (cc.in ());
399 ++i;
400 } while (da->next ());
401 --level_;
405 break; // end tk_sequence
407 case CORBA::tk_array:
409 if (newline)
411 ACE_DEBUG ((LM_DEBUG, "\n"));
413 tab ("Array\n");
414 --level_;
416 CORBA::ULong const len = dup->length ();
417 for (CORBA::ULong i = 0u; i < len; ++i)
419 tab ("");
420 ACE_DEBUG ((LM_DEBUG, "[%03d]\n", i));
421 DynamicAny::DynAny_var
422 cc (da->current_component());
423 if (!CORBA::is_nil (cc.in ()))
425 analyze (cc.in ());
427 da->next ();
429 --level_;
431 break; // end tk_array
433 case CORBA::tk_any:
435 tab ("Any\n");
436 CORBA::Any_var any (da->get_any ());
437 DynamicAny::DynAny_var
438 dynany (DynAny (any.in ()));
439 if (!CORBA::is_nil (dynany.in ()))
441 ++level_;
442 this->analyze (dynany.in ());
443 --level_;
446 break; // end tk_any
448 CASEE (boolean, Boolean, "(bool) = %d\n");
449 CASEE (short, Short, "(short) = %hd\n");
450 CASEE (ushort, UShort, "(unsigned short) = %hu\n");
451 CASEE (long, Long, "(long) = %d\n");
452 CASEE (ulong, ULong, "(unsigned long) = %u\n");
453 CASEE (longlong, LongLong, "(longlong) %Ld\n");
454 CASEE (ulonglong, ULongLong, "(unsigned longlong) %Lu\n");
455 CASEE (char, Char, "(char) = %c\n");
456 CASEE (float, Float, "(float) = %f\n");
457 CASEE (double, Double, "(double) = %f\n");
458 CASEE (octet, Octet, "(octet) = %c\n");
460 case CORBA::tk_string:
462 CORBA::String_var b (da->get_string ());
463 if (!newline)
465 tab("");
467 ACE_DEBUG ((LM_DEBUG, "(string) = \"%C\"\n",
468 b.in () ));
470 break; // end tk_string
472 case CORBA::tk_TypeCode:
474 const CORBA::TCKind
475 kind =da->get_typecode ()->kind ();
476 if (!newline)
478 tab("");
480 ACE_DEBUG ((LM_DEBUG, "(TypeCode) = %d\n",
481 static_cast<int> (kind) ));
483 break; // end tk_TypeCode
485 default:
487 const CORBA::TCKind
488 kind = tc->kind ();
489 if (newline)
491 ACE_DEBUG ((LM_DEBUG, "\n"));
493 tab ("***Unhandled*** TypeCode = ");
494 ACE_DEBUG ((LM_DEBUG, "%d\n",
495 static_cast<int> (kind) ));
497 break;
501 #define CASEBS(type,CT,str) case CORBA::tk_##type: {\
502 CORBA::CT##Seq_var seq (da->get_##type##_seq ());\
503 ++level_; CORBA::ULong len = seq->length ();\
504 tab ("length = "); ACE_DEBUG ((LM_DEBUG, "%u\n", len));\
505 for (CORBA::ULong i = 0; i < len; ++i) {\
506 CORBA::CT b = seq[i];\
507 tab (""); ACE_DEBUG ((LM_DEBUG, "[%3d] ", i));\
508 ACE_DEBUG ((LM_DEBUG, str, b)); }\
509 --level_;\
510 } break;
512 void
513 DynAnyAnalyzer::analyze_basic_seq (
514 CORBA::TypeCode_ptr tc,
515 DynamicAny::DynAny_ptr da)
517 CORBA::TypeCode_var ct (tc->content_type ());
518 CORBA::TCKind tk = ct->kind ();
520 tab ("BASIC Type Sequence - ");
521 switch (tk)
523 CASEBS (boolean, Boolean, "Value (bool) = %d\n");
524 CASEBS (octet, Octet, "Value (octet) = %c\n");
525 CASEBS (char, Char, "Value (char) = %c\n");
526 CASEBS (wchar, WChar, "Value (wchar) = %u\n");
527 CASEBS (short, Short, "Value (short) = %d\n");
528 CASEBS (ushort, UShort, "Value (ushort) = %u\n");
529 CASEBS (long, Long, "Value (long) = %d\n");
530 CASEBS (ulong, ULong, "Value (ulong) = %u\n");
531 CASEBS (longlong, LongLong, "Value (longlong) = %Ld\n");
532 CASEBS (ulonglong, ULongLong, "Value (ulonglong) = %Lu\n");
533 CASEBS (float, Float, "Value (float) = %f\n");
534 CASEBS (double, Double, "Value (double) = %f\n");
536 case CORBA::tk_longdouble:
537 default:
538 ACE_DEBUG ((LM_DEBUG,
539 "***Unhandled*** TypeCode = %d\n",
540 static_cast<int> (tk) ));
541 break;