Merge pull request #2218 from jwillemsen/jwi-pthreadsigmask
[ACE_TAO.git] / TAO / tao / DynamicAny / DynEnum_i.cpp
blob7f25e22f02dd9d64df0b91caa22787aef96e99b4
1 // -*- C++ -*-
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"
7 #include "tao/CDR.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)
15 , value_ (0u)
19 TAO_DynEnum_i::~TAO_DynEnum_i ()
23 void
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;
34 void
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 ();
45 this->type_ = tc;
47 TAO::Any_Impl *impl = any.impl ();
49 if (impl->encoded ())
51 TAO::Unknown_IDL_Type * const unk =
52 dynamic_cast<TAO::Unknown_IDL_Type *> (impl);
54 if (!unk)
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_);
62 else
64 TAO_OutputCDR out;
65 impl->marshal_value (out);
66 TAO_InputCDR in (out);
67 in.read_ulong (this->value_);
70 this->init_common ();
73 void
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);
85 this->value_ = 0;
87 this->init_common ();
90 // ****************************************************************
92 TAO_DynEnum_i *
93 TAO_DynEnum_i::_narrow (CORBA::Object_ptr _tao_objref)
95 if (CORBA::is_nil (_tao_objref))
97 return 0;
100 return dynamic_cast<TAO_DynEnum_i *> (_tao_objref);
103 // ****************************************************************
105 char *
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);
115 void
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 ();
122 CORBA::ULong i;
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))
131 break;
135 if (i < count)
137 this->value_ = i;
139 else
141 throw DynamicAny::DynAny::InvalidValue ();
145 CORBA::ULong
146 TAO_DynEnum_i::get_as_ulong ()
148 return this->value_;
151 void
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;
161 else
163 throw DynamicAny::DynAny::InvalidValue ();
167 // ****************************************************************
169 void
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);
185 if (!unk)
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_);
193 else
195 TAO_OutputCDR out;
196 impl->marshal_value (out);
197 TAO_InputCDR in (out);
198 in.read_ulong (this->value_);
201 else
203 throw DynamicAny::DynAny::TypeMismatch ();
207 CORBA::Any_ptr
208 TAO_DynEnum_i::to_any ()
210 TAO_OutputCDR out_cdr;
212 out_cdr.write_ulong (this->value_);
214 CORBA::Any *retval;
215 ACE_NEW_THROW_EX (retval,
216 CORBA::Any,
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 (),
223 in_cdr),
224 CORBA::NO_MEMORY ());
226 retval->replace (unk);
227 return retval;
230 CORBA::Boolean
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 ());
237 if (!equivalent)
239 return false;
242 CORBA::Any_var any = rhs->to_any ();
244 TAO::Any_Impl *impl = any->impl ();
245 CORBA::ULong value;
247 if (impl->encoded ())
249 TAO::Unknown_IDL_Type * const unk =
250 dynamic_cast<TAO::Unknown_IDL_Type *> (impl);
252 if (!unk)
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);
260 else
262 TAO_OutputCDR out;
263 impl->marshal_value (out);
264 TAO_InputCDR in (out);
265 in.read_ulong (value);
268 return value == this->value_;
271 void
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