Use =default for skeleton copy constructor
[ACE_TAO.git] / TAO / tao / ETCL / TAO_ETCL_Constraint.cpp
blobd3be0e91089685942652e1e7f1f2efe27833014b
1 // -*- C++ -*-
2 #include "ace/ETCL/ETCL_Constraint_Visitor.h"
4 #include "tao/AnyTypeCode/AnyTypeCode_methods.h"
5 #include "tao/AnyTypeCode/Any_Unknown_IDL_Type.h"
6 #include "tao/AnyTypeCode/TypeCode.h"
7 #include "tao/AnyTypeCode/Any.h"
8 #include "tao/CDR.h"
9 #include "tao/SystemException.h"
11 #include "tao/ETCL/TAO_ETCL_Constraint.h"
13 #if ! defined (__ACE_INLINE__)
14 #include "tao/ETCL/TAO_ETCL_Constraint.inl"
15 #endif /* __ACE_INLINE__ */
17 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
19 TAO_ETCL_Literal_Constraint::TAO_ETCL_Literal_Constraint (
20 const TAO_ETCL_Literal_Constraint & lit)
21 : ETCL_Literal_Constraint (),
22 any_ (0)
24 this->copy (lit);
27 TAO_ETCL_Literal_Constraint::TAO_ETCL_Literal_Constraint (CORBA::Any * any)
28 : any_ (0)
30 CORBA::Any& any_ref = *any;
31 CORBA::TypeCode_var type = any_ref.type ();
32 CORBA::TCKind corba_type = CORBA::tk_null;
34 try
36 corba_type = type->kind ();
38 catch (const CORBA::Exception&)
40 // @@ Seth: Don't know what else to do. Make sure we can tell
41 // when this constructor fails.
42 return;
45 this->type_ =
46 TAO_ETCL_Literal_Constraint::comparable_type (type.in ());
48 switch (this->type_)
50 case ACE_ETCL_SIGNED:
51 this->op_.integer_ = 0;
53 if (corba_type == CORBA::tk_short)
55 CORBA::Short sh;
56 any_ref >>= sh;
57 this->op_.integer_ = (CORBA::Long) sh;
59 else
61 any_ref >>= this->op_.integer_;
64 break;
65 case ACE_ETCL_UNSIGNED:
66 this->op_.uinteger_ = 0;
68 if (corba_type == CORBA::tk_ushort)
70 CORBA::UShort sh;
71 any_ref >>= sh;
72 this->op_.uinteger_ = (CORBA::ULong) sh;
74 else if (corba_type == CORBA::tk_enum)
76 TAO::Any_Impl *impl = any->impl ();
78 if (impl->encoded ())
80 TAO::Unknown_IDL_Type *unk =
81 dynamic_cast<TAO::Unknown_IDL_Type *> (impl);
83 if (unk == 0)
85 throw CORBA::INTERNAL ();
88 // We don't want unk's rd_ptr to move, in case we are shared by
89 // another Any, so we use this to copy the state, not the buffer.
90 TAO_InputCDR for_reading (unk->_tao_get_cdr ());
91 for_reading.read_ulong (this->op_.uinteger_);
93 else
95 TAO_OutputCDR out;
96 impl->marshal_value (out);
97 TAO_InputCDR in (out);
98 in.read_ulong (this->op_.uinteger_);
101 else
103 any_ref >>= this->op_.uinteger_;
106 break;
107 case ACE_ETCL_DOUBLE:
108 if (corba_type == CORBA::tk_float)
110 CORBA::Float fl;
111 (*any) >>= fl;
112 this->op_.double_ = (CORBA::Double) fl;
114 else
116 (*any) >>= this->op_.double_;
119 break;
120 case ACE_ETCL_BOOLEAN:
122 CORBA::Any::to_boolean tmp (this->op_.bool_);
123 (*any) >>= tmp;
125 break;
126 case ACE_ETCL_STRING:
128 const char* s;
129 any_ref >>= s;
130 this->op_.str_ = CORBA::string_dup (s);
132 break;
133 case ACE_ETCL_COMPONENT:
134 this->any_ = any->impl ();
135 this->any_->_add_ref ();
136 break;
140 TAO_ETCL_Literal_Constraint::~TAO_ETCL_Literal_Constraint ()
142 if (this->any_ != 0)
144 this->any_->_remove_ref ();
148 void
149 TAO_ETCL_Literal_Constraint::operator= (const TAO_ETCL_Literal_Constraint& co)
151 this->copy (co);
154 TAO_ETCL_Literal_Constraint::operator CORBA::Boolean () const
156 return this->ETCL_Literal_Constraint::operator ACE_CDR::Boolean ();
159 TAO_ETCL_Literal_Constraint::operator CORBA::ULong () const
161 switch (this->type_)
163 case ACE_ETCL_COMPONENT:
165 CORBA::ULong retval = 0;
166 CORBA::Any tmp;
167 tmp.replace (this->any_);
168 this->any_->_add_ref ();
169 tmp >>= retval;
170 return retval;
172 default:
173 return this->ETCL_Literal_Constraint::operator ACE_CDR::ULong ();
177 TAO_ETCL_Literal_Constraint::operator CORBA::Long () const
179 switch (this->type_)
181 case ACE_ETCL_COMPONENT:
183 CORBA::Long retval = 0;
184 CORBA::Any tmp;
185 tmp.replace (this->any_);
186 this->any_->_add_ref ();
187 tmp >>= retval;
188 return retval;
190 default:
191 return this->ETCL_Literal_Constraint::operator ACE_CDR::Long ();
195 TAO_ETCL_Literal_Constraint::operator CORBA::Double () const
197 switch (this->type_)
199 case ACE_ETCL_COMPONENT:
201 CORBA::Double retval = 0.0;
202 CORBA::Any tmp;
203 tmp.replace (this->any_);
204 this->any_->_add_ref ();
205 tmp >>= retval;
206 return retval;
208 default:
209 return this->ETCL_Literal_Constraint::operator ACE_CDR::Double ();
213 TAO_ETCL_Literal_Constraint::operator const char* () const
215 switch (this->type_)
217 case ACE_ETCL_COMPONENT:
219 const char *retval = 0;
220 CORBA::Any tmp;
221 tmp.replace (this->any_);
222 this->any_->_add_ref ();
223 tmp >>= retval;
224 return retval;
226 default:
227 return this->ETCL_Literal_Constraint::operator const char * ();
231 TAO_ETCL_Literal_Constraint::operator TAO::Any_Impl* () const
233 return (this->type_ == ACE_ETCL_COMPONENT) ? this->any_ : 0;
236 Literal_Type
237 TAO_ETCL_Literal_Constraint::comparable_type (CORBA::TypeCode_ptr type)
239 // Convert a CORBA::TCKind into a Literal_Type
240 Literal_Type return_value = ACE_ETCL_UNKNOWN;
241 CORBA::TCKind kind = CORBA::tk_null;
245 kind = type->kind ();
246 CORBA::TypeCode_var tmp = CORBA::TypeCode::_duplicate (type);
248 while (kind == CORBA::tk_alias)
250 tmp = tmp->content_type ();
252 kind = tmp->kind ();
255 catch (const CORBA::Exception&)
257 return return_value;
259 // Since this is a top level try block, no need to check again.
261 switch (kind)
263 case CORBA::tk_ushort:
264 case CORBA::tk_ulong:
265 case CORBA::tk_enum:
266 case CORBA::tk_ulonglong:
267 return_value = ACE_ETCL_UNSIGNED;
268 break;
269 case CORBA::tk_short:
270 case CORBA::tk_long:
271 case CORBA::tk_longlong:
272 return_value = ACE_ETCL_SIGNED;
273 break;
274 case CORBA::tk_boolean:
275 return_value = ACE_ETCL_BOOLEAN;
276 break;
277 case CORBA::tk_float:
278 case CORBA::tk_double:
279 return_value = ACE_ETCL_DOUBLE;
280 break;
281 case CORBA::tk_string:
282 return_value = ACE_ETCL_STRING;
283 break;
284 default:
285 return_value = ACE_ETCL_COMPONENT;
286 break;
289 return return_value;
292 TAO_ETCL_Literal_Constraint
293 TAO_ETCL_Literal_Constraint::operator+ (const TAO_ETCL_Literal_Constraint & rhs)
295 Literal_Type widest_type = this->widest_type (rhs);
297 switch (widest_type)
299 case ACE_ETCL_DOUBLE:
301 CORBA::Double result = (CORBA::Double) *this + (CORBA::Double) rhs;
302 return TAO_ETCL_Literal_Constraint ((CORBA::Double) result);
304 case ACE_ETCL_INTEGER:
305 case ACE_ETCL_SIGNED:
307 CORBA::Long result = (CORBA::Long) *this + (CORBA::Long) rhs;
308 return TAO_ETCL_Literal_Constraint ((CORBA::Long) result);
310 case ACE_ETCL_UNSIGNED:
312 CORBA::ULong result = (CORBA::ULong) *this + (CORBA::ULong) rhs;
313 return TAO_ETCL_Literal_Constraint ((CORBA::ULong) result);
315 default:
316 return TAO_ETCL_Literal_Constraint ((CORBA::Long) 0);
320 TAO_ETCL_Literal_Constraint
321 TAO_ETCL_Literal_Constraint::operator- (const TAO_ETCL_Literal_Constraint & rhs)
323 Literal_Type widest_type = this->widest_type (rhs);
325 switch (widest_type)
327 case ACE_ETCL_DOUBLE:
329 CORBA::Double result = (CORBA::Double) *this - (CORBA::Double) rhs;
330 return TAO_ETCL_Literal_Constraint ((CORBA::Double) result);
332 case ACE_ETCL_INTEGER:
333 case ACE_ETCL_SIGNED:
335 CORBA::Long result = (CORBA::Long) *this - (CORBA::Long) rhs;
336 return TAO_ETCL_Literal_Constraint ((CORBA::Long) result);
338 case ACE_ETCL_UNSIGNED:
340 CORBA::ULong result = (CORBA::ULong) *this - (CORBA::ULong) rhs;
341 return TAO_ETCL_Literal_Constraint ((CORBA::ULong) result);
343 default:
344 return TAO_ETCL_Literal_Constraint ((CORBA::Long) 0);
348 TAO_ETCL_Literal_Constraint
349 TAO_ETCL_Literal_Constraint::operator* (const TAO_ETCL_Literal_Constraint & rhs)
351 Literal_Type widest_type = this->widest_type (rhs);
353 switch (widest_type)
355 case ACE_ETCL_DOUBLE:
357 CORBA::Double result = (CORBA::Double) *this * (CORBA::Double) rhs;
358 return TAO_ETCL_Literal_Constraint ((CORBA::Double) result);
360 case ACE_ETCL_INTEGER:
361 case ACE_ETCL_SIGNED:
363 CORBA::Long result = (CORBA::Long) *this * (CORBA::Long) rhs;
364 return TAO_ETCL_Literal_Constraint ((CORBA::Long) result);
366 case ACE_ETCL_UNSIGNED:
368 CORBA::ULong result = (CORBA::ULong) *this * (CORBA::ULong) rhs;
369 return TAO_ETCL_Literal_Constraint ((CORBA::ULong) result);
371 default:
372 return TAO_ETCL_Literal_Constraint ((CORBA::Long) 0);
376 TAO_ETCL_Literal_Constraint
377 TAO_ETCL_Literal_Constraint::operator/ (const TAO_ETCL_Literal_Constraint & rhs)
379 Literal_Type widest_type = this->widest_type (rhs);
381 switch (widest_type)
383 case ACE_ETCL_DOUBLE:
385 if (ACE::is_equal ((ACE_CDR::Double) rhs, 0.0))
386 return TAO_ETCL_Literal_Constraint ((ACE_CDR::Double) 0.0);
388 CORBA::Double result = (CORBA::Double) *this / (CORBA::Double) rhs;
389 return TAO_ETCL_Literal_Constraint ((CORBA::Double) result);
391 case ACE_ETCL_INTEGER:
392 case ACE_ETCL_SIGNED:
394 if ((ACE_CDR::Long) rhs == 0)
395 return TAO_ETCL_Literal_Constraint ((ACE_CDR::Long) 0);
397 CORBA::Long result = (CORBA::Long) *this / (CORBA::Long) rhs;
398 return TAO_ETCL_Literal_Constraint ((CORBA::Long) result);
400 case ACE_ETCL_UNSIGNED:
402 if ((ACE_CDR::ULong) rhs == 0)
403 return TAO_ETCL_Literal_Constraint ((ACE_CDR::ULong) 0);
405 CORBA::ULong result = (CORBA::ULong) *this / (CORBA::ULong) rhs;
406 return TAO_ETCL_Literal_Constraint ((CORBA::ULong) result);
408 default:
409 return TAO_ETCL_Literal_Constraint ((CORBA::Long) 0);
413 TAO_ETCL_Literal_Constraint
414 TAO_ETCL_Literal_Constraint::operator- ()
416 switch (this->type_)
418 case ACE_ETCL_DOUBLE:
419 return TAO_ETCL_Literal_Constraint (- this->op_.double_);
420 case ACE_ETCL_INTEGER:
421 case ACE_ETCL_SIGNED:
422 return TAO_ETCL_Literal_Constraint (- this->op_.integer_);
423 case ACE_ETCL_UNSIGNED:
424 return TAO_ETCL_Literal_Constraint (- (CORBA::Long) this->op_.uinteger_);
425 default:
426 return TAO_ETCL_Literal_Constraint ((CORBA::Long) 0);
430 Literal_Type
431 TAO_ETCL_Literal_Constraint::widest_type (
432 const ETCL_Literal_Constraint & rhs)
434 Literal_Type rhs_type = rhs.expr_type ();
435 Literal_Type return_value = rhs_type;
437 if (rhs_type == ACE_ETCL_COMPONENT)
439 const TAO_ETCL_Literal_Constraint& actual =
440 dynamic_cast<const TAO_ETCL_Literal_Constraint&> (rhs);
441 CORBA::TypeCode_var tc = actual.any_->type ();
442 rhs_type = TAO_ETCL_Literal_Constraint::comparable_type (tc.in ());
443 return return_value;
446 return this->ETCL_Literal_Constraint::widest_type (rhs);
449 void
450 TAO_ETCL_Literal_Constraint::copy (const TAO_ETCL_Literal_Constraint &lit)
452 if (this->type_ == ACE_ETCL_COMPONENT && this->any_ != 0)
454 this->any_->_remove_ref ();
455 this->any_ = 0;
458 switch (lit.type_)
460 case ACE_ETCL_COMPONENT:
461 this->type_ = ACE_ETCL_COMPONENT;
462 this->any_ = lit.any_;
463 this->any_->_add_ref ();
464 break;
465 default:
466 this->ETCL_Literal_Constraint::copy (lit);
467 break;
471 TAO_END_VERSIONED_NAMESPACE_DECL