2 #include "tao/AnyTypeCode/TypeCode.h"
3 #include "tao/AnyTypeCode/Any_Unknown_IDL_Type.h"
4 #include "tao/AnyTypeCode/AnyTypeCode_methods.h"
5 #include "tao/DynamicAny/DynEnum_i.h"
6 #include "tao/DynamicAny/DynAnyFactory.h"
9 #include "ace/OS_NS_string.h"
11 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
13 TAO_DynEnum_i::TAO_DynEnum_i (CORBA::Boolean allow_truncation
)
14 : TAO_DynCommon (allow_truncation
)
19 TAO_DynEnum_i::~TAO_DynEnum_i ()
24 TAO_DynEnum_i::init_common ()
26 this->ref_to_component_
= false;
27 this->container_is_destroying_
= false;
28 this->has_components_
= false;
29 this->destroyed_
= false;
30 this->current_position_
= -1;
31 this->component_count_
= 0;
35 TAO_DynEnum_i::init (const CORBA::Any
&any
)
37 CORBA::TypeCode_var tc
= any
.type ();
38 CORBA::TCKind kind
= TAO_DynAnyFactory::unalias (tc
.in ());
40 if (kind
!= CORBA::tk_enum
)
42 throw DynamicAny::DynAnyFactory::InconsistentTypeCode ();
47 TAO::Any_Impl
*impl
= any
.impl ();
51 TAO::Unknown_IDL_Type
* const unk
=
52 dynamic_cast<TAO::Unknown_IDL_Type
*> (impl
);
55 throw CORBA::INTERNAL ();
57 // We don't want unk's rd_ptr to move, in case we are shared by
58 // another Any, so we use this to copy the state, not the buffer.
59 TAO_InputCDR
for_reading (unk
->_tao_get_cdr ());
60 for_reading
.read_ulong (this->value_
);
65 impl
->marshal_value (out
);
66 TAO_InputCDR
in (out
);
67 in
.read_ulong (this->value_
);
74 TAO_DynEnum_i::init (CORBA::TypeCode_ptr tc
)
76 CORBA::TCKind kind
= TAO_DynAnyFactory::unalias (tc
);
78 if (kind
!= CORBA::tk_enum
)
80 throw DynamicAny::DynAnyFactory::InconsistentTypeCode ();
83 this->type_
= CORBA::TypeCode::_duplicate (tc
);
90 // ****************************************************************
93 TAO_DynEnum_i::_narrow (CORBA::Object_ptr _tao_objref
)
95 if (CORBA::is_nil (_tao_objref
))
100 return dynamic_cast<TAO_DynEnum_i
*> (_tao_objref
);
103 // ****************************************************************
106 TAO_DynEnum_i::get_as_string ()
108 CORBA::TypeCode_var ct
= TAO_DynAnyFactory::strip_alias (this->type_
.in ());
110 const char *retval
= ct
.in ()->member_name (this->value_
);
112 return CORBA::string_dup (retval
);
116 TAO_DynEnum_i::set_as_string (const char *value_as_string
)
118 CORBA::TypeCode_var ct
= TAO_DynAnyFactory::strip_alias (this->type_
.in ());
120 CORBA::ULong count
= ct
.in ()->member_count ();
123 const char *temp
= 0;
125 for (i
= 0; i
< count
; ++i
)
127 temp
= ct
.in ()->member_name (i
);
129 if (!ACE_OS::strcmp (value_as_string
, temp
))
141 throw DynamicAny::DynAny::InvalidValue ();
146 TAO_DynEnum_i::get_as_ulong ()
152 TAO_DynEnum_i::set_as_ulong (CORBA::ULong value_as_ulong
)
154 CORBA::TypeCode_var ct
= TAO_DynAnyFactory::strip_alias (this->type_
.in ());
155 CORBA::ULong
const max
= ct
.in ()->member_count ();
157 if (value_as_ulong
< max
)
159 this->value_
= value_as_ulong
;
163 throw DynamicAny::DynAny::InvalidValue ();
167 // ****************************************************************
170 TAO_DynEnum_i::from_any (const CORBA::Any
& any
)
172 CORBA::TypeCode_var tc
= any
.type ();
173 CORBA::TCKind kind
= TAO_DynAnyFactory::unalias (tc
.in ());
175 if (kind
== CORBA::tk_enum
)
177 // Get the CDR stream of the Any, if there isn't one, make one.
178 TAO::Any_Impl
*impl
= any
.impl ();
180 if (impl
->encoded ())
182 TAO::Unknown_IDL_Type
* const unk
=
183 dynamic_cast<TAO::Unknown_IDL_Type
*> (impl
);
186 throw CORBA::INTERNAL ();
188 // We don't want unk's rd_ptr to move, in case we are shared by
189 // another Any, so we use this to copy the state, not the buffer.
190 TAO_InputCDR
for_reading (unk
->_tao_get_cdr ());
191 for_reading
.read_ulong (this->value_
);
196 impl
->marshal_value (out
);
197 TAO_InputCDR
in (out
);
198 in
.read_ulong (this->value_
);
203 throw DynamicAny::DynAny::TypeMismatch ();
208 TAO_DynEnum_i::to_any ()
210 TAO_OutputCDR out_cdr
;
212 out_cdr
.write_ulong (this->value_
);
215 ACE_NEW_THROW_EX (retval
,
217 CORBA::NO_MEMORY ());
219 TAO_InputCDR
in_cdr (out_cdr
);
220 TAO::Unknown_IDL_Type
*unk
= 0;
221 ACE_NEW_THROW_EX (unk
,
222 TAO::Unknown_IDL_Type (this->type_
.in (),
224 CORBA::NO_MEMORY ());
226 retval
->replace (unk
);
231 TAO_DynEnum_i::equal (DynamicAny::DynAny_ptr rhs
)
233 CORBA::TypeCode_var tc
= rhs
->type ();
235 CORBA::Boolean equivalent
= tc
->equivalent (this->type_
.in ());
242 CORBA::Any_var any
= rhs
->to_any ();
244 TAO::Any_Impl
*impl
= any
->impl ();
247 if (impl
->encoded ())
249 TAO::Unknown_IDL_Type
* const unk
=
250 dynamic_cast<TAO::Unknown_IDL_Type
*> (impl
);
253 throw CORBA::INTERNAL ();
255 // We don't want unk's rd_ptr to move, in case we are shared by
256 // another Any, so we use this to copy the state, not the buffer.
257 TAO_InputCDR
for_reading (unk
->_tao_get_cdr ());
258 for_reading
.read_ulong (value
);
263 impl
->marshal_value (out
);
264 TAO_InputCDR
in (out
);
265 in
.read_ulong (value
);
268 return value
== this->value_
;
272 TAO_DynEnum_i::destroy ()
274 if (this->destroyed_
)
276 throw ::CORBA::OBJECT_NOT_EXIST ();
279 if (!this->ref_to_component_
|| this->container_is_destroying_
)
281 this->destroyed_
= true;
285 DynamicAny::DynAny_ptr
286 TAO_DynEnum_i::current_component ()
288 if (this->destroyed_
)
290 throw ::CORBA::OBJECT_NOT_EXIST ();
293 throw DynamicAny::DynAny::TypeMismatch ();
296 TAO_END_VERSIONED_NAMESPACE_DECL