Version: 8.3.0
Any.cxx
Go to the documentation of this file.
1 // Copyright (C) 2006-2016 CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 
20 #include "Any.hxx"
21 #include "Runtime.hxx"
22 #include "TypeCode.hxx"
24 
25 #include <cstring>
26 #include <cstdlib>
27 
28 using namespace YACS::ENGINE;
29 using namespace std;
30 
31 // forbidden value int=-269488145 double=-1.54947e+231 bool=239
32 const char SeqAlloc::DFT_CHAR_VAR=-17;//0xEF
33 
34 StringOnHeap::StringOnHeap(const char *val):_dealloc(0),_str(strdup(val))
35 {
36 }
37 
38 StringOnHeap::StringOnHeap(const std::string& val):_dealloc(0),_str(strdup(val.c_str()))
39 {
40 }
41 
49 StringOnHeap::StringOnHeap(char *val, Deallocator deAlloc):_dealloc(deAlloc)
50 {
51  if(deAlloc)
52  _str=val;
53  else
54  _str=strdup(val);
55 }
56 
57 bool StringOnHeap::operator ==(const StringOnHeap& other) const
58 {
59  return strcmp(_str, other._str)==0;
60 }
61 
63 {
64  return new StringOnHeap(_str);
65 }
66 
68 {
69  if(_dealloc)
70  _dealloc(_str);
71  else
72  free(_str);
73 }
74 
75 Any::Any(TypeCode* type):_type(type)
76 {
77  _type->incrRef();
78 }
79 
80 Any::Any(const Any& other):_type(other._type)
81 {
82  _type->incrRef();
83 }
84 
86 {
87  _type->decrRef();
88 }
89 
90 bool Any::IsNull(char *data)
91 {
92  if(!data)
93  return true;
94  bool isNull(true);
95  for(std::size_t i=0;i<sizeof(void *) && isNull;i++)
96  isNull=(data[i]==SeqAlloc::DFT_CHAR_VAR);
97  return isNull;
98 }
99 
100 AtomAny::AtomAny(int val):Any(Runtime::_tc_int)
101 {
102  _value._i=val;
103 }
104 
105 AtomAny::AtomAny(bool val):Any(Runtime::_tc_bool)
106 {
107  _value._b=val;
108 }
109 
110 AtomAny::AtomAny(double val):Any(Runtime::_tc_double)
111 {
112  _value._d=val;
113 }
114 
115 AtomAny::AtomAny(const char *val):Any(Runtime::_tc_string)
116 {
117  _value._s=new StringOnHeap(val);
118 }
119 
120 AtomAny::AtomAny(const std::string& val):Any(Runtime::_tc_string)
121 {
122  _value._s=new StringOnHeap(val);
123 }
124 
125 AtomAny::AtomAny(const AtomAny& other):Any(other)
126 {
128  {
129  StringOnHeap *cpy=(other._value._s)->deepCopy();
130  memcpy(&_value._s,&cpy,_type->getSizeInByteOfAnyReprInSeq());
131  }
132  else if(_type->isA(Runtime::_tc_double))
133  memcpy(&_value._d,&other._value._d,_type->getSizeInByteOfAnyReprInSeq());
134  else if(_type->isA(Runtime::_tc_int))
135  memcpy(&_value._i,&other._value._i,_type->getSizeInByteOfAnyReprInSeq());
136  else if(_type->isA(Runtime::_tc_bool))
137  memcpy(&_value._b,&other._value._b,_type->getSizeInByteOfAnyReprInSeq());
138 }
139 
140 AtomAny::AtomAny(char *val, Deallocator deAlloc):Any(Runtime::_tc_string)
141 {
142  _value._s=new StringOnHeap(val,deAlloc);
143 }
144 
145 AtomAny::AtomAny(char *data, TypeCode* type):Any(type)
146 {
147  if(type->isA(Runtime::_tc_string))
148  {
149  void **tmp=(void **)data;
150  StringOnHeap *cpy=((StringOnHeap *)(*tmp))->deepCopy();
151  memcpy(&_value._s,&cpy,type->getSizeInByteOfAnyReprInSeq());
152  }
153  else if(type->isA(Runtime::_tc_double))
154  memcpy(&_value._d,data,type->getSizeInByteOfAnyReprInSeq());
155  else if(type->isA(Runtime::_tc_int))
156  memcpy(&_value._i,data,type->getSizeInByteOfAnyReprInSeq());
157  else if(type->isA(Runtime::_tc_bool))
158  memcpy(&_value._b,data,type->getSizeInByteOfAnyReprInSeq());
159 }
160 
162 {
163  return new AtomAny(*this);
164 }
165 
166 AtomAny *AtomAny::New(char *val,Deallocator dealloc)
167 {
168  return new AtomAny(val,dealloc);
169 }
170 
172 {
173  throw InvalidExtractionException(_type->kind(),Sequence);
174 }
175 
176 AnyPtr AtomAny::operator[](const char *key) const throw(YACS::Exception)
177 {
178  throw Exception("AtomAny::operator[] : try to get a part of a partitionned data whereas atomical.");
179 }
180 
181 bool AtomAny::operator ==(const Any& other) const
182 {
183  if(!_type->isA(other.getType()))
184  return false;
185  const AtomAny& otherC=(const AtomAny&) other;//cast granted due to previous lines
187  return _value._d==otherC._value._d;
188  else if(_type->isA(Runtime::_tc_int))
189  return _value._i==otherC._value._i;
190  else if(_type->isA(Runtime::_tc_bool))
191  return _value._b==otherC._value._b;
192  else if(_type->isA(Runtime::_tc_string))
193  return (*_value._s)==*(otherC._value._s);
194  else
195  return false;
196 }
197 
199 {
201  return _value._i;
202  else
203  throw Exception("Value is not an int");
204 }
205 
207 {
209  return _value._b;
210  else
211  throw Exception("Value is not a bool");
212 }
213 
214 double AtomAny::getDoubleValue() const throw(YACS::Exception)
215 {
217  return _value._d;
218  else
219  throw Exception("Value is not a double");
220 }
221 
222 std::string AtomAny::getStringValue() const throw(YACS::Exception)
223 {
225  return string(_value._s->cStr());
226  else
227  throw Exception("Value is not a string");
228 }
229 
237 void AtomAny::putMyReprAtPlace(char *data) const
238 {
240  {
241  StringOnHeap *tmp=_value._s->deepCopy();
242  memcpy(data,&tmp,_type->getSizeInByteOfAnyReprInSeq());
243  }
244  else if(_type->isA(Runtime::_tc_double))
245  memcpy(data,&_value._d,_type->getSizeInByteOfAnyReprInSeq());
246  else if(_type->isA(Runtime::_tc_int))
247  memcpy(data,&_value._i,_type->getSizeInByteOfAnyReprInSeq());
248  else if(_type->isA(Runtime::_tc_bool))
249  memcpy(data,&_value._b,_type->getSizeInByteOfAnyReprInSeq());
250 }
251 
263 void AtomAny::putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy)
264 {
265  if(type->isA(Runtime::_tc_string))
266  {
267  void **tmp1=(void **)src;
268  StringOnHeap *tmp=((const StringOnHeap *)(*tmp1))->deepCopy();
269  memcpy(data,&tmp,type->getSizeInByteOfAnyReprInSeq());
270  }
271  else if(type->isA(Runtime::_tc_double))
272  memcpy(data,src,type->getSizeInByteOfAnyReprInSeq());
273  else if(type->isA(Runtime::_tc_int))
274  memcpy(data,src,type->getSizeInByteOfAnyReprInSeq());
275  else if(type->isA(Runtime::_tc_bool))
276  memcpy(data,src,type->getSizeInByteOfAnyReprInSeq());
277 }
278 
283 void AtomAny::destroyReprAtPlace(char *data, const TypeCode *type)
284 {
285  DynType typ=type->kind();
286  if(typ==String)
287  {
288  if(!Any::IsNull(data))
289  {
290  void **tmp=(void **)data;
291  delete ((StringOnHeap *)(*tmp));
292  }
293  }
294 }
295 
297 {
298  Any *ret;
299  ret=new AtomAny(data,(TypeCode *)type);
300  return AnyPtr(ret);
301 }
302 
304 {
305  DynType typ=type->kind();
306  return (typ==Double || typ==Int || typ==Bool || typ==String);
307 }
308 
310 {
311  if(_type->kind() == String)
312  delete _value._s;
313 }
314 
316 {
317 }
318 
319 ComposedAny::ComposedAny(TypeCode* type, bool isNew):Any(type)
320 {
321  if(isNew)
322  _type->decrRef();
323 }
324 
325 AnyPtr ComposedAny::operator[](const char *key) const throw(YACS::Exception)
326 {
327  throw Exception("AtomAny::operator[] : try to get a part of a partitionned data not localizable by a string.");
328 }
329 
330 void ComposedAny::checkTypeOf(const Any *elem) const throw(YACS::Exception)
331 {
332  if(!elem->getType()->isA(_type->contentType()))
333  throw Exception("ComposedAny::checkTypeOf : invalid type.");
334 }
335 
337 {
339 }
340 
342 {
344 }
345 
347 {
349 }
350 
351 std::string ComposedAny::getStringValue() const throw(YACS::Exception)
352 {
354 }
355 
356 SeqAlloc::SeqAlloc(const SeqAlloc& other):_sizeOf1Elm(other._sizeOf1Elm),_notStdDeAlloc(0),
357  _start(0),_finish(0),_endOfStorage(0)
358 {
359  _start=allocate(other._finish-other._start);
360  _finish=_start+(other._finish-other._start);
362 }
363 
364 SeqAlloc::SeqAlloc(unsigned int sizeOf1Elm):_sizeOf1Elm(sizeOf1Elm),_notStdDeAlloc(0),
365  _start(0),_finish(0),_endOfStorage(0)
366 {
367 }
368 
370 {
372 }
373 
375 {
377  _start=0;
378  _finish=0;
379  _endOfStorage=0;
380 }
381 
386 void SeqAlloc::initCoarseMemory(char *mem, unsigned int size, Deallocator dealloc)
387 {
388  unsigned sizeInByte=size*_sizeOf1Elm;
389  if(dealloc)
390  {
391  _notStdDeAlloc=dealloc;
392  _start=mem;
393  }
394  else
395  {
396  _start=allocate(sizeInByte);
397  if(mem)
398  memcpy(_start,mem,sizeInByte);
399  else
400  {
401  for(unsigned int i=0;i<sizeInByte;i++) _start[i]=DFT_CHAR_VAR;// see getSetItems
402  }
403  }
404  _finish=_start+sizeInByte;
406 }
407 
408 void SeqAlloc::construct(char *pt, const Any *val)
409 {
410  val->putMyReprAtPlace(pt);
411 }
412 
420 void SeqAlloc::construct(char *pt, const char *val, const TypeCode *tc, bool deepCpy)
421 {
422  tc->putReprAtPlace(pt,val,deepCpy);
423 }
424 
425 char *SeqAlloc::allocate(unsigned int nbOfByte)
426 {
427  if(nbOfByte>0)
428  return (char *)::operator new(nbOfByte);
429  else
430  return 0;
431 }
432 
433 // pt is not permitted to be a null pointer.
434 void SeqAlloc::deallocate(char *pt)
435 {
436  if(pt)
437  {
438  if(!_notStdDeAlloc)
439  ::operator delete(pt);
440  else
441  {
442  _notStdDeAlloc(pt);
443  _notStdDeAlloc=0;
444  }
445  }
446 }
447 
448 void SeqAlloc::destroy(char *pt, const TypeCode *tc)
449 {
450  tc->destroyZippedAny(pt);
451 }
452 
453 unsigned int SeqAlloc::size() const
454 {
455  return (_finish-_start)/_sizeOf1Elm;
456 }
457 
458 std::vector<unsigned int> SeqAlloc::getSetItems() const
459 {
460  std::vector<unsigned int> ret;
461  unsigned int sz(size());
462  for(unsigned int i=0;i<sz;i++)
463  {
464  const char *pt(_start+i*_sizeOf1Elm);
465  for(unsigned j=0;j<_sizeOf1Elm && *pt==DFT_CHAR_VAR;j++,pt++); //see initCoarseMemory
466  if(pt!=_start+(i+1)*_sizeOf1Elm)
467  ret.push_back(i);
468  }
469  return ret;
470 }
471 
473 {
474  for (char *cur=_alloc._start;cur!=_alloc._finish;cur+=_alloc._sizeOf1Elm)
476  _alloc.clear();
477 }
478 
480 {
483 }
484 
485 void SequenceAny::pushBack(const Any* elem)
486 {
487  if(!elem->_type->isA(_type->contentType()))
490  {
493  }
494  else
495  realloc(_alloc._finish, elem);
496 }
497 
498 bool SequenceAny::operator ==(const Any& other) const
499 {
500  if(!_type->isA(other.getType()))
501  return false;
502  const SequenceAny& otherC=(const SequenceAny&) other;//cast granted due to previous lines
503  if(size()!=otherC.size())
504  return false;
505  for(unsigned i=0;i<size();i++)
506  if(!((*(*this)[i])==(*otherC[i])))
507  return false;
508  return true;
509 }
510 
511 void SequenceAny::setEltAtRank(int i, const Any *elem) throw(YACS::Exception)
512 {
513  checkTypeOf(elem);
514  _alloc.destroy(_alloc._start+i*_alloc._sizeOf1Elm,_type->contentType());
515  _alloc.construct(_alloc._start+i*_alloc._sizeOf1Elm,elem);
516 }
517 
519 {
520  return _type->contentType()->getOrBuildAnyFromZippedData(_alloc._start+i*_alloc._sizeOf1Elm);
521 }
522 
527 void SequenceAny::putMyReprAtPlace(char *data) const
528 {
529  const void *tmp=(const void *)this;
530  memcpy(data,&tmp,_type->getSizeInByteOfAnyReprInSeq());
531  const void **tmp2=(const void **) data;
532  ((SequenceAny *)(*tmp2))->incrRef();
533  //::new((SequenceAny *)data) SequenceAny((SequenceAny&) (*this));
534 }
535 
536 void SequenceAny::putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy)
537 {
538  void **tmp2=(void **) src;
539  if(!deepCpy)
540  {
541  ((SequenceAny *)(*tmp2))->incrRef();
542  memcpy(data,src,type->getSizeInByteOfAnyReprInSeq());
543  }
544  else
545  {
546  SequenceAny *cpy=new SequenceAny(*((SequenceAny *)(*tmp2)));
547  memcpy(data,&cpy,type->getSizeInByteOfAnyReprInSeq());
548  }
549  //::new((SequenceAny *)data) SequenceAny((SequenceAny&) (*this));
550 }
551 
552 void SequenceAny::destroyReprAtPlace(char *data, const TypeCode *type)
553 {
554  void **tmp=(void **) data;
555  if(!Any::IsNull(data))
556  ((SequenceAny *)(*tmp))->decrRef();
557  //((SequenceAny *)data)->~SequenceAny();
558 }
559 
561 {
562  void **tmp=(void **) data;
563  ((SequenceAny *) (*tmp))->incrRef();
564  return AnyPtr((SequenceAny *)(*tmp));
565 }
566 
568 {
569  return new SequenceAny(*this);
570 }
571 
573 {
574  std::vector<unsigned int> its(getSetItems());
575  std::size_t sz(its.size());
576  SequenceAny *ret(SequenceAny::New(getType()->contentType(),sz));
577  for(std::size_t i=0;i<sz;i++)
578  {
579  AnyPtr obj((*this)[its[i]]);
580  ret->setEltAtRank(i,obj);
581  }
582  return ret;
583 }
584 
585 SequenceAny *SequenceAny::New(const TypeCode *typeOfContent)
586 {
587  if(typeOfContent->kind() == Objref)
588  {
589  //In case of Objref, use a sequence of string
590  return new SequenceAny(Runtime::_tc_string);
591  }
592  else
593  return new SequenceAny(typeOfContent);
594 }
595 
596 SequenceAny *SequenceAny::New(const TypeCode *typeOfContent, unsigned lgth)
597 {
598  if(typeOfContent->kind() == Objref)
599  {
600  //In case of Objref, use a sequence of string
601  return new SequenceAny(Runtime::_tc_string,lgth);
602  }
603  else
604  return new SequenceAny(typeOfContent,lgth);
605 }
606 
608 {
609  DynType typ=type->kind();
610  return (typ==Sequence);
611 }
612 
613 SequenceAny::SequenceAny(const SequenceAny& other):ComposedAny(other),_alloc(other._alloc)
614 {
615  const char *srcCur=other._alloc._start;
616  for(char *cur=_alloc._start;srcCur != other._alloc._finish; srcCur+=_alloc._sizeOf1Elm, cur+=_alloc._sizeOf1Elm)
617  _alloc.construct(cur, srcCur, _type->contentType(),true);
618 }
619 
621 {
622  for (char *cur=_alloc._start;cur!=_alloc._finish;cur+=_alloc._sizeOf1Elm)
624 }
625 
629 SequenceAny::SequenceAny(const TypeCode *typeOfContent):ComposedAny(new TypeCodeSeq("","",typeOfContent)),
630  _alloc(typeOfContent->getSizeInByteOfAnyReprInSeq())
631 {
632 }
633 
634 SequenceAny::SequenceAny(const TypeCode *typeOfContent, unsigned lgth):ComposedAny(new TypeCodeSeq("","",typeOfContent)),
635  _alloc(typeOfContent->getSizeInByteOfAnyReprInSeq())
636 {
637  _alloc.initCoarseMemory(0,lgth,0);
638 }
639 
640 SequenceAny::SequenceAny(int *val, unsigned int lgth, Deallocator deAlloc):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_int)),
641  _alloc(Runtime::_tc_int->getSizeInByteOfAnyReprInSeq())
642 {
643  _alloc.initCoarseMemory((char *)val,lgth,deAlloc);
644 }
645 
646 SequenceAny::SequenceAny(bool *val, unsigned int lgth, Deallocator deAlloc):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_bool)),
647  _alloc(Runtime::_tc_bool->getSizeInByteOfAnyReprInSeq())
648 {
649  _alloc.initCoarseMemory((char *)val,lgth,deAlloc);
650 }
651 
652 SequenceAny::SequenceAny(double *val, unsigned int lgth, Deallocator deAlloc):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_double)),
653  _alloc(Runtime::_tc_double->getSizeInByteOfAnyReprInSeq())
654 {
655  _alloc.initCoarseMemory((char *)val,lgth,deAlloc);
656 }
657 
658 SequenceAny::SequenceAny(const std::vector<int>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_int)),
659  _alloc(Runtime::_tc_int->getSizeInByteOfAnyReprInSeq())
660 {
661  _alloc.initCoarseMemory((char *)&val[0],val.size(),0);
662 }
663 
664 SequenceAny::SequenceAny(const std::vector<bool>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_bool)),
665  _alloc(Runtime::_tc_bool->getSizeInByteOfAnyReprInSeq())
666 {
667  for(vector<bool>::const_iterator iter=val.begin();iter!=val.end();iter++)
668  {
669  AtomAnyPtr tmp=AtomAny::New(*iter);
670  pushBack(tmp);
671  }
672 }
673 
674 SequenceAny::SequenceAny(const std::vector<double>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_double)),
675  _alloc(Runtime::_tc_double->getSizeInByteOfAnyReprInSeq())
676 {
677  _alloc.initCoarseMemory((char *)&val[0],val.size(),0);
678 }
679 
680 SequenceAny::SequenceAny(const std::vector<std::string>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_string)),
681  _alloc(Runtime::_tc_string->getSizeInByteOfAnyReprInSeq())
682 {
683  for(vector<string>::const_iterator iter=val.begin();iter!=val.end();iter++)
684  {
685  AtomAnyPtr tmp=AtomAny::New(*iter);
686  pushBack(tmp);
687  }
688 }
689 
690 void SequenceAny::realloc(char *endOfCurrentAllocated, const Any *elem)
691 {
692  unsigned int oldSize=_alloc._finish-_alloc._start;
693  unsigned int newSize = oldSize != 0 ? 2 * oldSize : _alloc._sizeOf1Elm;
694  char *newStart=_alloc.allocate(newSize);
695  //
696  char *newFinish=performCpy(_alloc._start, endOfCurrentAllocated,newStart);
697  _alloc.construct(newFinish, elem);
698  newFinish+=_alloc._sizeOf1Elm;
699  newFinish=performCpy(endOfCurrentAllocated, _alloc._finish, newFinish);
700  //
701  for (char *cur=_alloc._start;cur!=_alloc._finish;cur+=_alloc._sizeOf1Elm)
704  _alloc._start = newStart;
705  _alloc._finish = newFinish;
706  _alloc._endOfStorage=newStart+newSize;
707 }
708 
709 char *SequenceAny::performCpy(char *srcStart, char *srcFinish, char *destStart)
710 {
711  char *cur=destStart;
712  for (;srcStart != srcFinish; srcStart+=_alloc._sizeOf1Elm, cur+=_alloc._sizeOf1Elm)
713  _alloc.construct(cur, srcStart, _type->contentType(),false);
714  return cur;
715 }
716 
718 {
719  const TypeCode *subType=_type->contentType();
720  unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
721  unsigned int size=((TypeCodeArray *)_type)->getStaticLgth();
722  char *tmp=_data;
723  for(unsigned i=0;i<size;i++,tmp+=sizePerContent)
724  subType->destroyZippedAny(tmp);
725  delete [] _data;
726 }
727 
728 ArrayAny::ArrayAny(const TypeCode *typeOfContent, unsigned int lgth):ComposedAny(new TypeCodeArray("","",typeOfContent,lgth))
729 {
731  for(unsigned int i=0;i<_type->getSizeInByteOfAnyReprInSeq();i++)
733 }
734 
735 ArrayAny::ArrayAny(char *data, TypeCodeArray * type):ComposedAny(type,false),_data(0)
736 {
738  const TypeCode *subType=_type->contentType();
739  unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
740  for(unsigned i=0;i<type->getStaticLgth();i++)
741  subType->putReprAtPlace(_data+i*sizePerContent,data+i*sizePerContent,false);
742 }
743 
745 {
747  const TypeCode *subType=_type->contentType();
748  unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
749  for(unsigned i=0;i<((TypeCodeArray *)_type)->getStaticLgth();i++)
750  subType->putReprAtPlace(_data+i*sizePerContent,other._data+i*sizePerContent,true);
751 }
752 
753 ArrayAny::ArrayAny(const int *val, unsigned int lgth):ComposedAny(new TypeCodeArray("","",Runtime::_tc_int,lgth)),
754  _data(0)
755 {
757  memcpy(_data,val,_type->getSizeInByteOfAnyReprInSeq());
758 }
759 
760 ArrayAny::ArrayAny(const bool *val, unsigned int lgth):ComposedAny(new TypeCodeArray("","",Runtime::_tc_bool,lgth)),
761  _data(0)
762 {
764  memcpy(_data,val,_type->getSizeInByteOfAnyReprInSeq());
765 }
766 
767 ArrayAny::ArrayAny(const double *val, unsigned int lgth):ComposedAny(new TypeCodeArray("","",Runtime::_tc_double,lgth)),
768  _data(0)
769 {
771  memcpy(_data,val,_type->getSizeInByteOfAnyReprInSeq());
772 }
773 
774 ArrayAny::ArrayAny(const std::vector<int>& val):ComposedAny(new TypeCodeArray("","",Runtime::_tc_int,val.size())),
775  _data(0)
776 {
778  memcpy(_data,&val[0],_type->getSizeInByteOfAnyReprInSeq());
779 }
780 
781 ArrayAny::ArrayAny(const std::vector<double>& val):ComposedAny(new TypeCodeArray("","",Runtime::_tc_double,val.size())),
782  _data(0)
783 {
785  memcpy(_data,&val[0],_type->getSizeInByteOfAnyReprInSeq());
786 }
787 
788 ArrayAny::ArrayAny(const std::vector<std::string>& val):ComposedAny(new TypeCodeArray("","",Runtime::_tc_string,val.size())),
789  _data(0)
790 {
792  unsigned i=0;
793  const TypeCode *subType=_type->contentType();
794  unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
795  for(vector<std::string>::const_iterator iter=val.begin();iter!=val.end();iter++,i++)
796  {
797  StringOnHeap *st=new StringOnHeap(*iter);
798  memcpy(_data+i*sizePerContent,&st,sizePerContent);
799  }
800 }
801 
802 void ArrayAny::setEltAtRank(int i, const Any *elem) throw(YACS::Exception)
803 {
804  checkTypeOf(elem);
805  const TypeCode *subType=_type->contentType();
806  subType->destroyZippedAny(_data+i*subType->getSizeInByteOfAnyReprInSeq());
807  elem->putMyReprAtPlace(_data+i*subType->getSizeInByteOfAnyReprInSeq());
808 }
809 
810 bool ArrayAny::operator ==(const Any& other) const
811 {
812  if(!_type->isA(other.getType()))
813  return false;
814  const ArrayAny& otherC=(const ArrayAny&) other;//cast granted due to previous lines
815  for(unsigned i=0;i<((const TypeCodeArray *)_type)->getStaticLgth();i++)
816  if(!((*(*this)[i])==(*otherC[i])))
817  return false;
818  return true;
819 }
820 
822 {
823  const TypeCode *subType=_type->contentType();
824  unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
825  if(i<0 || i>=((TypeCodeArray *)_type)->getStaticLgth())
826  throw Exception("Trying to access to an invalid index in an Any Tuple");
827  return _type->contentType()->getOrBuildAnyFromZippedData(_data+i*sizePerContent);
828 }
829 
830 unsigned int ArrayAny::size() const
831 {
832  return ((TypeCodeArray *)_type)->getStaticLgth();
833 }
834 
836 {
837  return new ArrayAny(*this);
838 }
839 
840 ArrayAny *ArrayAny::New(const TypeCode *typeOfContent, unsigned int lgth)
841 {
842  return new ArrayAny(typeOfContent,lgth);
843 }
844 
845 void ArrayAny::putMyReprAtPlace(char *data) const
846 {
847  const TypeCode *subType=_type->contentType();
848  unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
849  for(unsigned i=0;i<((const TypeCodeArray *)_type)->getStaticLgth();i++)
850  subType->putReprAtPlace(data+i*sizePerContent,_data+i*sizePerContent,false);
851 }
852 
853 void ArrayAny::putReprAtPlace(char *data, const char *src, const TypeCodeArray *type, bool deepCpy)
854 {
855  const TypeCode *subType=type->contentType();
856  unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
857  for(unsigned i=0;i<type->getStaticLgth();i++)
858  subType->putReprAtPlace(data+i*sizePerContent,src+i*sizePerContent,deepCpy);
859 }
860 
861 void ArrayAny::destroyReprAtPlace(char *data, const TypeCodeArray *type)
862 {
863  const TypeCode *subType=type->contentType();
864  unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
865  for(unsigned i=0;i<type->getStaticLgth();i++)
866  subType->destroyZippedAny(data+i*sizePerContent);
867 }
868 
870 {
871  Any *ret;
872  ret=new ArrayAny(data,(TypeCodeArray *)type);
873  return AnyPtr(ret);
874 }
875 
877 {
878  DynType typ=type->kind();
879  return (typ==Array);
880 }
881 
883 {
884  return new StructAny(*this);
885 }
886 
887 bool StructAny::operator ==(const Any& other) const
888 {
889  if(!_type->isA(other.getType()))
890  return false;
891  const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
892  vector< pair<string,TypeCode*> >::const_iterator iter;
893  for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
894  if(!((*(*this)[(*iter).first.c_str()]==(*other[(*iter).first.c_str()]))))
895  return false;
896  return true;
897 }
898 
900 {
901  const char what[]="StructAny::operator[](int i) : Struct key are strings not integers.";
902  throw Exception(what);
903 }
904 
905 AnyPtr StructAny::operator[](const char *key) const throw(YACS::Exception)
906 {
907  const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
908  char *whereToGet=_data;
909  vector< pair<string,TypeCode*> >::const_iterator iter;
910  for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
911  if((*iter).first!=key)
912  whereToGet+=(*iter).second->getSizeInByteOfAnyReprInSeq();
913  else
914  break;
915  if(iter==typeC->_members.end())
916  {
917  string what("Unexisting key \""); what+=key; what+="\" for struct extraction.";
918  throw Exception(what);
919  }
920  return (*iter).second->getOrBuildAnyFromZippedData(whereToGet);
921 }
922 
923 void StructAny::setEltAtRank(int i, const Any *elem) throw(YACS::Exception)
924 {
925  const char what[]="Struct key are strings not integers.";
926  throw Exception(what);
927 }
928 
929 void StructAny::setEltAtRank(const char *key, const Any *elem) throw(YACS::Exception)
930 {
931  const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
932  unsigned offset;
933  const TypeCode *tcOnKey=typeC->getMember(key,offset);
934  if(!tcOnKey)
935  throw Exception("StructAny::setEltAtRank : invalid key given.");
936  if(!elem->getType()->isA(tcOnKey))
937  throw Exception("StructAny::setEltAtRank : invalid data type on the specified given key.");
938  tcOnKey->destroyZippedAny(_data+offset);
939  elem->putMyReprAtPlace(_data+offset);
940 }
941 
942 void StructAny::putMyReprAtPlace(char *data) const
943 {
944  const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
945  unsigned offset=0;
946  vector< pair<string,TypeCode*> >::const_iterator iter;
947  for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
948  {
949  (*iter).second->putReprAtPlace(data+offset,_data+offset,false);
950  offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
951  }
952 }
953 
954 void StructAny::putReprAtPlace(char *data, const char *src, const TypeCodeStruct *type, bool deepCpy)
955 {
956  unsigned offset=0;
957  vector< pair<string,TypeCode*> >::const_iterator iter;
958  for(iter=type->_members.begin();iter!=type->_members.end();iter++)
959  {
960  (*iter).second->putReprAtPlace(data+offset,src+offset,deepCpy);
961  offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
962  }
963 }
964 
965 void StructAny::destroyReprAtPlace(char *data, const TypeCodeStruct *type)
966 {
967  char *whereToGet=data;
968  vector< pair<string,TypeCode*> >::const_iterator iter;
969  for(iter=type->_members.begin();iter!=type->_members.end();iter++)
970  {
971  (*iter).second->destroyZippedAny(whereToGet);
972  whereToGet+=(*iter).second->getSizeInByteOfAnyReprInSeq();
973  }
974 }
975 
977 {
978  Any *ret;
979  ret=new StructAny(data,(TypeCodeStruct *)type);
980  return AnyPtr(ret);
981 }
982 
984 {
985  const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
986  vector< pair<string,TypeCode*> >::const_iterator iter;
987  char *whereToGet=_data;
988  for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
989  {
990  (*iter).second->destroyZippedAny(whereToGet);
991  whereToGet+=(*iter).second->getSizeInByteOfAnyReprInSeq();
992  }
993  delete [] _data;
994 }
995 
997 {
999  for(unsigned int i=0;i<_type->getSizeInByteOfAnyReprInSeq();i++)
1001 }
1002 
1004 {
1006  const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
1007  vector< pair<string,TypeCode*> >::const_iterator iter;
1008  unsigned offset=0;
1009  for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
1010  {
1011  (*iter).second->putReprAtPlace(_data+offset,other._data+offset,true);
1012  offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
1013  }
1014 }
1015 
1016 StructAny::StructAny(char *data, TypeCodeStruct * type):ComposedAny(type,false),_data(0)
1017 {
1019  vector< pair<string,TypeCode*> >::const_iterator iter;
1020  unsigned offset=0;
1021  for(iter=type->_members.begin();iter!=type->_members.end();iter++)
1022  {
1023  (*iter).second->putReprAtPlace(_data+offset,data+offset,false);
1024  offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
1025  }
1026 }
1027 
1029 {
1030  return new StructAny(type);
1031 }