]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/Any.cxx
Salome HOME
af845e2ce6b49b1db847704552583193b503d68d
[modules/yacs.git] / src / engine / Any.cxx
1 #include "Any.hxx"
2 #include "Runtime.hxx"
3 #include "TypeCode.hxx"
4 #include "InvalidExtractionException.hxx"
5
6 #include <cstring>
7
8 using namespace YACS::ENGINE;
9 using namespace std;
10
11 StringOnHeap::StringOnHeap(const char *val):_dealloc(0),_str(strdup(val))
12 {
13 }
14
15 StringOnHeap::StringOnHeap(const std::string& val):_dealloc(0),_str(strdup(val.c_str()))
16 {
17 }
18
19 /*! 
20  * \Note : no copy is performed if a deallocator is given.
21  * \param val     : String in C format that is NOT copied if
22  *                  deAlloc != 0
23  * \param deAlloc : pointer on function to deallocate val after
24  *                  last use.
25  */
26 StringOnHeap::StringOnHeap(char *val, Deallocator deAlloc):_dealloc(deAlloc)
27 {
28   if(deAlloc)
29     _str=val;
30   else
31     _str=strdup(val);
32 }
33
34 bool StringOnHeap::operator ==(const StringOnHeap& other) const
35 {
36   return strcmp(_str, other._str)==0;
37 }
38
39 StringOnHeap *StringOnHeap::deepCopy() const
40 {
41   return new StringOnHeap(_str);
42 }
43
44 StringOnHeap::~StringOnHeap()
45 {
46   if(_dealloc)
47     _dealloc(_str);
48   else
49     free(_str);
50 }
51
52 Any::Any(TypeCode* type):_type(type)
53 {
54   _type->incrRef();
55 }
56
57 Any::Any(const Any& other):_type(other._type)
58 {
59   _type->incrRef();
60 }
61
62 Any::~Any()
63 {
64   _type->decrRef();
65 }
66
67 AtomAny::AtomAny(int val):Any(Runtime::_tc_int)
68 {
69   _value._i=val;
70 }
71
72 AtomAny::AtomAny(bool val):Any(Runtime::_tc_bool)
73 {
74   _value._b=val;
75 }
76
77 AtomAny::AtomAny(double val):Any(Runtime::_tc_double)
78 {
79   _value._d=val;
80 }
81
82 AtomAny::AtomAny(const char *val):Any(Runtime::_tc_string)
83 {
84   _value._s=new StringOnHeap(val);
85 }
86
87 AtomAny::AtomAny(const std::string& val):Any(Runtime::_tc_string)
88 {
89   _value._s=new StringOnHeap(val);
90 }
91
92 AtomAny::AtomAny(const AtomAny& other):Any(other)
93 {
94   if(_type->isA(Runtime::_tc_string))
95     {
96       StringOnHeap *cpy=(other._value._s)->deepCopy();
97       memcpy(&_value._s,&cpy,_type->getSizeInByteOfAnyReprInSeq());
98     }
99   else if(_type->isA(Runtime::_tc_double))
100     memcpy(&_value._d,&other._value._d,_type->getSizeInByteOfAnyReprInSeq());
101   else if(_type->isA(Runtime::_tc_int))
102     memcpy(&_value._i,&other._value._i,_type->getSizeInByteOfAnyReprInSeq());
103   else if(_type->isA(Runtime::_tc_bool))
104     memcpy(&_value._b,&other._value._b,_type->getSizeInByteOfAnyReprInSeq());
105 }
106
107 AtomAny::AtomAny(char *val, Deallocator deAlloc):Any(Runtime::_tc_string)
108 {
109   _value._s=new StringOnHeap(val,deAlloc);
110 }
111
112 AtomAny::AtomAny(char *data, TypeCode* type):Any(type)
113 {
114   if(type->isA(Runtime::_tc_string))
115     {
116       void **tmp=(void **)data;
117       StringOnHeap *cpy=((StringOnHeap *)(*tmp))->deepCopy();
118       memcpy(&_value._s,&cpy,type->getSizeInByteOfAnyReprInSeq());
119     }
120   else if(type->isA(Runtime::_tc_double))
121     memcpy(&_value._d,data,type->getSizeInByteOfAnyReprInSeq());
122   else if(type->isA(Runtime::_tc_int))
123     memcpy(&_value._i,data,type->getSizeInByteOfAnyReprInSeq());
124   else if(type->isA(Runtime::_tc_bool))
125     memcpy(&_value._b,data,type->getSizeInByteOfAnyReprInSeq());
126 }
127
128 Any *AtomAny::clone() const
129 {
130   return new AtomAny(*this);
131 }
132
133 AtomAny *AtomAny::New(char *val,Deallocator dealloc)
134 {
135   return new AtomAny(val,dealloc);
136 }
137
138 AnyPtr AtomAny::operator[](int i) const throw(Exception)
139 {
140   throw InvalidExtractionException(_type->kind(),Sequence);
141 }
142
143 AnyPtr AtomAny::operator[](const char *key) const throw(Exception)
144 {
145   throw Exception("AtomAny::operator[] : try to get a part of a partitionned data whereas atomical.");
146 }
147
148 bool AtomAny::operator ==(const Any& other) const
149 {
150   if(!_type->isA(other.getType()))
151     return false;
152   const AtomAny& otherC=(const AtomAny&) other;//cast granted due to previous lines
153   if(_type->isA(Runtime::_tc_double))
154     return _value._d==otherC._value._d;
155   else if(_type->isA(Runtime::_tc_int))
156     return _value._i==otherC._value._i;
157   else if(_type->isA(Runtime::_tc_bool))
158     return _value._b==otherC._value._b;
159   else if(_type->isA(Runtime::_tc_string))
160     return (*_value._s)==*(otherC._value._s);
161   else
162     return false;
163 }
164
165 int AtomAny::getIntValue() const throw(Exception)
166 {
167   if(_type->isA(Runtime::_tc_int))
168     return _value._i;
169   else
170     throw Exception("Value is not an Int");
171 }
172
173 bool AtomAny::getBoolValue() const throw(Exception)
174 {
175   if(_type->isA(Runtime::_tc_bool))
176     return _value._b;
177   else
178     throw Exception("Value is not a Bool");
179 }
180
181 double AtomAny::getDoubleValue() const throw(Exception)
182 {
183   if(_type->isA(Runtime::_tc_double))
184     return _value._d;
185   else
186     throw Exception("Value is not a Double");
187 }
188
189 std::string AtomAny::getStringValue() const throw(Exception)
190 {
191   if(_type->isA(Runtime::_tc_string))
192     return string(_value._s->cStr());
193   else
194     throw Exception("Value is not a String");
195 }
196
197 /*!
198  * \note : This method put in data its zipped recursive content in data.
199  *         The ownership of the recursive content is tranfered to data.
200  *         So this owns nothing and its counter fall by 1.
201  *         For memory space minimal use, not all of '*this' is pushed at data location. 
202  * \param data : already allocated memory zone where to put compressed content of 'this'
203  */
204 void AtomAny::putMyReprAtPlace(char *data) const
205 {
206   if(_type->isA(Runtime::_tc_string))
207     {
208       StringOnHeap *tmp=_value._s->deepCopy();
209       memcpy(data,&tmp,_type->getSizeInByteOfAnyReprInSeq());
210     }
211   else if(_type->isA(Runtime::_tc_double))
212     memcpy(data,&_value._d,_type->getSizeInByteOfAnyReprInSeq());
213   else if(_type->isA(Runtime::_tc_int))
214     memcpy(data,&_value._i,_type->getSizeInByteOfAnyReprInSeq());
215   else if(_type->isA(Runtime::_tc_bool))
216     memcpy(data,&_value._b,_type->getSizeInByteOfAnyReprInSeq());
217 }
218
219 /*!
220  * \note : This method put in data its zipped recursive content in data.
221  *         The ownership of the recursive content is tranfered to data.
222  *         So this owns nothing and its counter fall by 1.
223  *         For memory space minimal use, not all of '*this' is pushed at data location.
224  *         'deepCpy' param is not used here because by definition of AtomAny deep copy is performed.
225  * \param data : already allocated memory zone where to put compressed content of 'this'
226  */
227 void AtomAny::putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy)
228 {
229   if(type->isA(Runtime::_tc_string))
230     {
231       void **tmp1=(void **)src;
232       StringOnHeap *tmp=((const StringOnHeap *)(*tmp1))->deepCopy();
233       memcpy(data,&tmp,type->getSizeInByteOfAnyReprInSeq());
234     }
235   else if(type->isA(Runtime::_tc_double))
236     memcpy(data,src,type->getSizeInByteOfAnyReprInSeq());
237   else if(type->isA(Runtime::_tc_int))
238     memcpy(data,src,type->getSizeInByteOfAnyReprInSeq());
239   else if(type->isA(Runtime::_tc_bool))
240     memcpy(data,src,type->getSizeInByteOfAnyReprInSeq());
241 }
242
243 /*!
244  * \note : Opposite method of putMyReprAtPlace. But static because due to data compression
245  *         instance is lost.
246  */
247 void AtomAny::destroyReprAtPlace(char *data, const TypeCode *type)
248 {
249   DynType typ=type->kind();
250   if(typ==String)
251     {
252       void **tmp=(void **)data;
253       delete ((StringOnHeap *)(*tmp));
254     }
255 }
256
257 AnyPtr AtomAny::getOrBuildFromData(char *data, const TypeCode *type)
258 {
259   Any *ret;
260   ret=new AtomAny(data,(TypeCode *)type);
261   return AnyPtr(ret);
262 }
263
264 bool AtomAny::takeInChargeStorageOf(TypeCode *type)
265 {
266   DynType typ=type->kind();
267   return (typ==Double || typ==Int || typ==Bool || typ==String);
268 }
269
270 AtomAny::~AtomAny()
271 {
272   if(_type->isA(Runtime::_tc_string))
273     delete _value._s;
274 }
275
276 ComposedAny::ComposedAny(const ComposedAny& other):Any(other)
277 {
278 }
279
280 ComposedAny::ComposedAny(TypeCode* type, bool isNew):Any(type)
281 {
282   if(isNew)
283     _type->decrRef();
284 }
285
286 AnyPtr ComposedAny::operator[](const char *key) const throw(Exception)
287 {
288   throw Exception("AtomAny::operator[] : try to get a part of a partitionned data not localizable by a string.");
289 }
290
291 void ComposedAny::checkTypeOf(const Any *elem) const throw(Exception)
292 {
293   if(!elem->getType()->isA(_type->contentType()))
294     throw Exception("ComposedAny::checkTypeOf : invalid type.");
295 }
296
297 int ComposedAny::getIntValue() const throw(Exception)
298 {
299  throw InvalidExtractionException(_type->kind(),Runtime::_tc_int->kind());
300 }
301
302 bool ComposedAny::getBoolValue() const throw(Exception)
303 {
304   throw InvalidExtractionException(_type->kind(),Runtime::_tc_bool->kind());
305 }
306
307 double ComposedAny::getDoubleValue() const throw(Exception)
308 {
309   throw InvalidExtractionException(_type->kind(),Runtime::_tc_double->kind());
310 }
311
312 std::string ComposedAny::getStringValue() const throw(Exception)
313 {
314   throw InvalidExtractionException(_type->kind(),Runtime::_tc_string->kind());
315 }
316
317 SeqAlloc::SeqAlloc(const SeqAlloc& other):_sizeOf1Elm(other._sizeOf1Elm),_notStdDeAlloc(0),
318  _start(0),_finish(0),_endOfStorage(0)
319 {
320   _start=allocate(other._finish-other._start);
321   _finish=_start+(other._finish-other._start);
322   _endOfStorage=_finish;
323 }
324
325 SeqAlloc::SeqAlloc(unsigned int sizeOf1Elm):_sizeOf1Elm(sizeOf1Elm),_notStdDeAlloc(0),
326                                             _start(0),_finish(0),_endOfStorage(0)
327 {
328 }
329
330 SeqAlloc::~SeqAlloc()
331 {
332   deallocate(_start);
333 }
334
335 void SeqAlloc::clear()
336 {
337   deallocate(_start);
338   _start=0;
339   _finish=0;
340   _endOfStorage=0;
341 }
342
343 /*!
344  * \note : This method is exclusively reserved for arrays of C++ built-in types because no
345  *         constructor is applied atomically.
346  */
347 void SeqAlloc::initCoarseMemory(char *mem, unsigned int size, Deallocator dealloc)
348 {
349   unsigned sizeInByte=size*_sizeOf1Elm;
350   if(dealloc)
351     {
352       _notStdDeAlloc=dealloc;
353       _start=mem;
354     }
355   else
356     {
357       _start=allocate(sizeInByte);
358       if(mem)
359         memcpy(_start,mem,sizeInByte);
360       else
361         {
362           for(unsigned int i=0;i<sizeInByte;i++) _start[i]=0;
363         }
364     }
365   _finish=_start+sizeInByte;
366   _endOfStorage=_finish;
367 }
368
369 void SeqAlloc::construct(char *pt, const Any *val)
370 {
371   val->putMyReprAtPlace(pt);
372 }
373
374 /*!
375  * \note: This performs the placement new or zip info into pt.
376  * \param val     : the source from which the construction will be performed.
377  * \param deepCpy : If true in pt place a deep copy pointed by val will be put.
378  */
379 void SeqAlloc::construct(char *pt, const char *val, const TypeCode *tc, bool deepCpy)
380 {
381   tc->putReprAtPlace(pt,val,deepCpy);
382 }
383
384 char *SeqAlloc::allocate(unsigned int nbOfByte)
385
386   if(nbOfByte>0)
387     return (char *)::operator new(nbOfByte);
388   else
389     return 0;
390 }
391
392 // pt is not permitted to be a null pointer.
393 void SeqAlloc::deallocate(char *pt)
394
395   if(pt)
396     {
397       if(!_notStdDeAlloc)
398         ::operator delete(pt); 
399       else
400         {
401           _notStdDeAlloc(pt);
402           _notStdDeAlloc=0;
403         }
404     }
405 }
406
407 void SeqAlloc::destroy(char *pt, const TypeCode *tc) 
408
409   tc->destroyZippedAny(pt);
410 }
411
412 unsigned int SeqAlloc::size() const
413 {
414   return (_finish-_start)/_sizeOf1Elm;
415 }
416
417 void SequenceAny::clear()
418 {
419   for (char *cur=_alloc._start;cur!=_alloc._finish;cur+=_alloc._sizeOf1Elm)
420     _alloc.destroy(cur,_type->contentType());
421   _alloc.clear();
422 }
423
424 void SequenceAny::popBack()
425 {
426   _alloc._finish-=_alloc._sizeOf1Elm;
427   _alloc.destroy(_alloc._finish,_type->contentType());
428 }
429
430 void SequenceAny::pushBack(const Any* elem)
431 {
432   if(!elem->_type->isA(_type->contentType()))
433     throw InvalidExtractionException(elem->_type->kind(),_type->contentType()->kind());
434   if(_alloc._finish != _alloc._endOfStorage)
435     {
436       _alloc.construct(_alloc._finish, elem);
437       _alloc._finish+=_alloc._sizeOf1Elm;
438     }
439   else
440     realloc(_alloc._finish, elem);
441 }
442
443 bool SequenceAny::operator ==(const Any& other) const
444 {
445   if(!_type->isA(other.getType()))
446     return false;
447   const SequenceAny& otherC=(const SequenceAny&) other;//cast granted due to previous lines
448   if(size()!=otherC.size())
449     return false;
450   for(unsigned i=0;i<size();i++)
451     if(!((*(*this)[i])==(*otherC[i])))
452       return false;
453   return true;
454 }
455
456 void SequenceAny::setEltAtRank(int i, const Any *elem) throw(Exception)
457 {
458   checkTypeOf(elem);
459   _alloc.destroy(_alloc._start+i*_alloc._sizeOf1Elm,_type->contentType());
460   _alloc.construct(_alloc._start+i*_alloc._sizeOf1Elm,elem);
461 }
462
463 AnyPtr SequenceAny::operator[](int i) const throw(Exception)
464 {
465   return _type->contentType()->getOrBuildAnyFromZippedData(_alloc._start+i*_alloc._sizeOf1Elm);
466 }
467
468 /*!
469  * \note : Contrary to AtomAny 'this' (ref) is put in data NOT a deep copy.
470  * \param data : already allocated memory zone where to put address of 'this'
471  */
472 void SequenceAny::putMyReprAtPlace(char *data) const
473 {
474   const void *tmp=(const void *)this;
475   memcpy(data,&tmp,_type->getSizeInByteOfAnyReprInSeq());
476   const void **tmp2=(const void **) data;
477   ((SequenceAny *)(*tmp2))->incrRef();
478   //::new((SequenceAny *)data) SequenceAny((SequenceAny&) (*this));
479 }
480
481 void SequenceAny::putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy)
482 {
483   void **tmp2=(void **) src;
484   if(!deepCpy)
485     {
486       ((SequenceAny *)(*tmp2))->incrRef();
487       memcpy(data,src,type->getSizeInByteOfAnyReprInSeq());
488     }
489   else
490     {
491       SequenceAny *cpy=new SequenceAny(*((SequenceAny *)(*tmp2)));
492       memcpy(data,&cpy,type->getSizeInByteOfAnyReprInSeq());
493     }
494   //::new((SequenceAny *)data) SequenceAny((SequenceAny&) (*this));
495 }
496
497 void SequenceAny::destroyReprAtPlace(char *data, const TypeCode *type)
498 {
499   void **tmp=(void **) data;
500   if(*tmp)
501     ((SequenceAny *)(*tmp))->decrRef();
502   //((SequenceAny *)data)->~SequenceAny();
503 }
504
505 AnyPtr SequenceAny::getOrBuildFromData(char *data, const TypeCode *type)
506 {
507   void **tmp=(void **) data;
508   ((SequenceAny *) (*tmp))->incrRef();
509   return AnyPtr((SequenceAny *)(*tmp));
510 }
511
512 Any *SequenceAny::clone() const
513 {
514   return new SequenceAny(*this);
515 }
516
517 SequenceAny *SequenceAny::New(const TypeCode *typeOfContent)
518 {
519   if(typeOfContent->kind() == Objref)
520     {
521       //In case of Objref, use a sequence of string
522       return new SequenceAny(Runtime::_tc_string);
523     }
524   else
525     return new SequenceAny(typeOfContent);
526 }
527
528 SequenceAny *SequenceAny::New(const TypeCode *typeOfContent, unsigned lgth)
529 {
530   if(typeOfContent->kind() == Objref)
531     {
532       //In case of Objref, use a sequence of string
533       return new SequenceAny(Runtime::_tc_string,lgth);
534     }
535   else
536     return new SequenceAny(typeOfContent,lgth);
537 }
538
539 bool SequenceAny::takeInChargeStorageOf(TypeCode *type)
540 {
541   DynType typ=type->kind();
542   return (typ==Sequence);
543 }
544
545 SequenceAny::SequenceAny(const SequenceAny& other):ComposedAny(other),_alloc(other._alloc)
546 {
547   const char *srcCur=other._alloc._start;
548   for(char *cur=_alloc._start;srcCur != other._alloc._finish; srcCur+=_alloc._sizeOf1Elm, cur+=_alloc._sizeOf1Elm)
549     _alloc.construct(cur, srcCur, _type->contentType(),true);
550 }
551
552 SequenceAny::~SequenceAny()
553 {
554   for (char *cur=_alloc._start;cur!=_alloc._finish;cur+=_alloc._sizeOf1Elm)
555     _alloc.destroy(cur,_type->contentType());
556 }
557
558 /*!
559  * \param typeOfContent : typeCode of the type of elements stored in sequence.
560  */
561 SequenceAny::SequenceAny(const TypeCode *typeOfContent):ComposedAny(new TypeCodeSeq("","",typeOfContent)),
562                                                         _alloc(typeOfContent->getSizeInByteOfAnyReprInSeq())
563 {
564 }
565
566 SequenceAny::SequenceAny(const TypeCode *typeOfContent, unsigned lgth):ComposedAny(new TypeCodeSeq("","",typeOfContent)),
567                                                                        _alloc(typeOfContent->getSizeInByteOfAnyReprInSeq())
568 {
569   _alloc.initCoarseMemory(0,lgth,0);
570 }
571
572 SequenceAny::SequenceAny(int *val, unsigned int lgth, Deallocator deAlloc):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_int)),
573                                                                            _alloc(Runtime::_tc_int->getSizeInByteOfAnyReprInSeq())
574 {
575   _alloc.initCoarseMemory((char *)val,lgth,deAlloc);
576 }
577
578 SequenceAny::SequenceAny(bool *val, unsigned int lgth, Deallocator deAlloc):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_bool)),
579                                                                             _alloc(Runtime::_tc_bool->getSizeInByteOfAnyReprInSeq())
580 {
581   _alloc.initCoarseMemory((char *)val,lgth,deAlloc);
582 }
583
584 SequenceAny::SequenceAny(double *val, unsigned int lgth, Deallocator deAlloc):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_double)),
585                                                                               _alloc(Runtime::_tc_double->getSizeInByteOfAnyReprInSeq())
586 {
587   _alloc.initCoarseMemory((char *)val,lgth,deAlloc);
588 }
589
590 SequenceAny::SequenceAny(const std::vector<int>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_int)),
591                                                       _alloc(Runtime::_tc_int->getSizeInByteOfAnyReprInSeq())
592 {
593   _alloc.initCoarseMemory((char *)&val[0],val.size(),0);
594 }
595
596 SequenceAny::SequenceAny(const std::vector<bool>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_bool)),
597                                                        _alloc(Runtime::_tc_bool->getSizeInByteOfAnyReprInSeq())
598 {
599   for(vector<bool>::const_iterator iter=val.begin();iter!=val.end();iter++)
600     {
601       AtomAnyPtr tmp=AtomAny::New(*iter);
602       pushBack(tmp);
603     }
604 }
605
606 SequenceAny::SequenceAny(const std::vector<double>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_double)),
607                                                          _alloc(Runtime::_tc_double->getSizeInByteOfAnyReprInSeq())
608 {
609   _alloc.initCoarseMemory((char *)&val[0],val.size(),0);
610 }
611
612 SequenceAny::SequenceAny(const std::vector<std::string>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_string)),
613                                                               _alloc(Runtime::_tc_string->getSizeInByteOfAnyReprInSeq())
614 {
615   for(vector<string>::const_iterator iter=val.begin();iter!=val.end();iter++)
616     {
617       AtomAnyPtr tmp=AtomAny::New(*iter);
618       pushBack(tmp);
619     }
620 }
621
622 void SequenceAny::realloc(char *endOfCurrentAllocated, const Any *elem)
623 {
624   unsigned int oldSize=_alloc._finish-_alloc._start;
625   unsigned int newSize = oldSize != 0 ? 2 * oldSize : _alloc._sizeOf1Elm;
626   char *newStart=_alloc.allocate(newSize);
627   //
628   char *newFinish=performCpy(_alloc._start, endOfCurrentAllocated,newStart);
629   _alloc.construct(newFinish, elem);
630   newFinish+=_alloc._sizeOf1Elm;
631   newFinish=performCpy(endOfCurrentAllocated, _alloc._finish, newFinish);
632   //
633   for (char *cur=_alloc._start;cur!=_alloc._finish;cur+=_alloc._sizeOf1Elm)
634     _alloc.destroy(cur,_type->contentType());
635   _alloc.deallocate(_alloc._start);
636   _alloc._start = newStart;
637   _alloc._finish = newFinish;
638   _alloc._endOfStorage=newStart+newSize;
639 }
640
641 char *SequenceAny::performCpy(char *srcStart, char *srcFinish, char *destStart)
642 {
643   char *cur=destStart;
644   for (;srcStart != srcFinish; srcStart+=_alloc._sizeOf1Elm, cur+=_alloc._sizeOf1Elm)
645     _alloc.construct(cur, srcStart, _type->contentType(),false);
646   return cur;
647 }
648
649 ArrayAny::~ArrayAny()
650 {
651   const TypeCode *subType=_type->contentType();
652   unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
653   unsigned int size=((TypeCodeArray *)_type)->getStaticLgth();
654   char *tmp=_data;
655   for(unsigned i=0;i<size;i++,tmp+=sizePerContent)
656     subType->destroyZippedAny(tmp);
657   delete [] _data;
658 }
659
660 ArrayAny::ArrayAny(const TypeCode *typeOfContent, unsigned int lgth):ComposedAny(new TypeCodeArray("","",typeOfContent,lgth))
661 {
662   _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
663   for(unsigned int i=0;i<_type->getSizeInByteOfAnyReprInSeq();i++)
664     _data[i]='\0';
665 }
666
667 ArrayAny::ArrayAny(char *data, TypeCodeArray * type):ComposedAny(type,false),_data(0)
668 {
669   _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
670   const TypeCode *subType=_type->contentType();
671   unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
672   for(unsigned i=0;i<type->getStaticLgth();i++)
673     subType->putReprAtPlace(_data+i*sizePerContent,data+i*sizePerContent,false);
674 }
675
676 ArrayAny::ArrayAny(const ArrayAny& other):ComposedAny(other)
677 {
678   _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
679   const TypeCode *subType=_type->contentType();
680   unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
681   for(unsigned i=0;i<((TypeCodeArray *)_type)->getStaticLgth();i++)
682     subType->putReprAtPlace(_data+i*sizePerContent,other._data+i*sizePerContent,true);
683 }
684
685 ArrayAny::ArrayAny(const int *val, unsigned int lgth):ComposedAny(new TypeCodeArray("","",Runtime::_tc_int,lgth)),
686                                                       _data(0)
687 {
688   _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
689   memcpy(_data,val,_type->getSizeInByteOfAnyReprInSeq());
690 }
691
692 ArrayAny::ArrayAny(const bool *val, unsigned int lgth):ComposedAny(new TypeCodeArray("","",Runtime::_tc_bool,lgth)),
693                                                        _data(0)
694 {
695   _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
696   memcpy(_data,val,_type->getSizeInByteOfAnyReprInSeq());
697 }
698
699 ArrayAny::ArrayAny(const double *val, unsigned int lgth):ComposedAny(new TypeCodeArray("","",Runtime::_tc_double,lgth)),
700                                                          _data(0)
701 {
702   _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
703   memcpy(_data,val,_type->getSizeInByteOfAnyReprInSeq());
704 }
705
706 ArrayAny::ArrayAny(const std::vector<int>& val):ComposedAny(new TypeCodeArray("","",Runtime::_tc_int,val.size())),
707                                                 _data(0)
708 {
709   _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
710   memcpy(_data,&val[0],_type->getSizeInByteOfAnyReprInSeq());
711 }
712
713 ArrayAny::ArrayAny(const std::vector<double>& val):ComposedAny(new TypeCodeArray("","",Runtime::_tc_double,val.size())),
714                                                 _data(0)
715 {
716   _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
717   memcpy(_data,&val[0],_type->getSizeInByteOfAnyReprInSeq());
718 }
719
720 ArrayAny::ArrayAny(const std::vector<std::string>& val):ComposedAny(new TypeCodeArray("","",Runtime::_tc_string,val.size())),
721                                                         _data(0)
722 {
723   _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
724   unsigned i=0;
725   const TypeCode *subType=_type->contentType();
726   unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
727   for(vector<std::string>::const_iterator iter=val.begin();iter!=val.end();iter++,i++)
728     {
729       StringOnHeap *st=new StringOnHeap(*iter);
730       memcpy(_data+i*sizePerContent,&st,sizePerContent);
731     }
732 }
733
734 void ArrayAny::setEltAtRank(int i, const Any *elem) throw(Exception)
735 {
736   checkTypeOf(elem);
737   const TypeCode *subType=_type->contentType();
738   subType->destroyZippedAny(_data+i*subType->getSizeInByteOfAnyReprInSeq());
739   elem->putMyReprAtPlace(_data+i*subType->getSizeInByteOfAnyReprInSeq());
740 }
741
742 bool ArrayAny::operator ==(const Any& other) const
743 {
744   if(!_type->isA(other.getType()))
745     return false;
746   const ArrayAny& otherC=(const ArrayAny&) other;//cast granted due to previous lines
747   for(unsigned i=0;i<((const TypeCodeArray *)_type)->getStaticLgth();i++)
748     if(!((*(*this)[i])==(*otherC[i])))
749       return false;
750   return true;
751 }
752
753 AnyPtr ArrayAny::operator[](int i) const throw(Exception)
754 {
755   const TypeCode *subType=_type->contentType();
756   unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
757   if(i<0 || i>=((TypeCodeArray *)_type)->getStaticLgth())
758     throw Exception("Trying to access to an invalid index in an Any Tuple");
759   return _type->contentType()->getOrBuildAnyFromZippedData(_data+i*sizePerContent);
760 }
761
762 unsigned int ArrayAny::size() const
763 {
764   return ((TypeCodeArray *)_type)->getStaticLgth();
765 }
766
767 Any *ArrayAny::clone() const
768 {
769   return new ArrayAny(*this);
770 }
771
772 ArrayAny *ArrayAny::New(const TypeCode *typeOfContent, unsigned int lgth)
773 {
774   return new ArrayAny(typeOfContent,lgth);
775 }
776
777 void ArrayAny::putMyReprAtPlace(char *data) const
778 {
779   const TypeCode *subType=_type->contentType();
780   unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
781   for(unsigned i=0;i<((const TypeCodeArray *)_type)->getStaticLgth();i++)
782     subType->putReprAtPlace(data+i*sizePerContent,_data+i*sizePerContent,false);
783 }
784
785 void ArrayAny::putReprAtPlace(char *data, const char *src, const TypeCodeArray *type, bool deepCpy)
786 {
787   const TypeCode *subType=type->contentType();
788   unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
789   for(unsigned i=0;i<type->getStaticLgth();i++)
790     subType->putReprAtPlace(data+i*sizePerContent,src+i*sizePerContent,deepCpy);
791 }
792
793 void ArrayAny::destroyReprAtPlace(char *data, const TypeCodeArray *type)
794 {
795   const TypeCode *subType=type->contentType();
796   unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
797   for(unsigned i=0;i<type->getStaticLgth();i++)
798     subType->destroyZippedAny(data+i*sizePerContent);
799 }
800
801 AnyPtr ArrayAny::getOrBuildFromData(char *data, const TypeCodeArray *type)
802 {
803   Any *ret;
804   ret=new ArrayAny(data,(TypeCodeArray *)type);
805   return AnyPtr(ret);
806 }
807
808 bool ArrayAny::takeInChargeStorageOf(TypeCode *type)
809 {
810   DynType typ=type->kind();
811   return (typ==Array);
812 }
813
814 Any *StructAny::clone() const
815 {
816   return new StructAny(*this);
817 }
818
819 bool StructAny::operator ==(const Any& other) const
820 {
821   if(!_type->isA(other.getType()))
822     return false;
823   const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
824   vector< pair<string,TypeCode*> >::const_iterator iter;
825   for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
826     if(!((*(*this)[(*iter).first.c_str()]==(*other[(*iter).first.c_str()]))))
827       return false;
828   return true;
829 }
830
831 AnyPtr StructAny::operator[](int i) const throw(Exception)
832 {
833   const char what[]="StructAny::operator[](int i) : Struct key are strings not integers.";
834   throw Exception(what);
835 }
836
837 AnyPtr StructAny::operator[](const char *key) const throw(Exception)
838 {
839   const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
840   char *whereToGet=_data;
841   vector< pair<string,TypeCode*> >::const_iterator iter;
842   for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
843     if((*iter).first!=key)
844       whereToGet+=(*iter).second->getSizeInByteOfAnyReprInSeq();
845     else
846       break;
847   if(iter==typeC->_members.end())
848     {
849       string what("Unexisting key \""); what+=key; what+="\" for struct extraction.";
850       throw Exception(what);
851     }
852   return (*iter).second->getOrBuildAnyFromZippedData(whereToGet);
853 }
854
855 void StructAny::setEltAtRank(int i, const Any *elem) throw(Exception)
856 {
857   const char what[]="Struct key are strings not integers.";
858   throw Exception(what);
859 }
860
861 void StructAny::setEltAtRank(const char *key, const Any *elem) throw(Exception)
862 {
863   const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
864   unsigned offset;
865   const TypeCode *tcOnKey=typeC->getMember(key,offset);
866   if(!tcOnKey)
867     throw Exception("StructAny::setEltAtRank : invalid key given.");
868   if(!elem->getType()->isA(tcOnKey))
869     throw Exception("StructAny::setEltAtRank : invalid data type on the specified given key.");
870   tcOnKey->destroyZippedAny(_data+offset);
871   elem->putMyReprAtPlace(_data+offset);
872 }
873
874 void StructAny::putMyReprAtPlace(char *data) const
875 {
876   const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
877   unsigned offset=0;
878   vector< pair<string,TypeCode*> >::const_iterator iter;
879   for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
880     {
881       (*iter).second->putReprAtPlace(data+offset,_data+offset,false);
882       offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
883     }
884 }
885
886 void StructAny::putReprAtPlace(char *data, const char *src, const TypeCodeStruct *type, bool deepCpy)
887 {
888   unsigned offset=0;
889   vector< pair<string,TypeCode*> >::const_iterator iter;
890   for(iter=type->_members.begin();iter!=type->_members.end();iter++)
891     {
892       (*iter).second->putReprAtPlace(data+offset,src+offset,deepCpy);
893       offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
894     }
895 }
896
897 void StructAny::destroyReprAtPlace(char *data, const TypeCodeStruct *type)
898 {
899   char *whereToGet=data;
900   vector< pair<string,TypeCode*> >::const_iterator iter;
901   for(iter=type->_members.begin();iter!=type->_members.end();iter++)
902     {
903       (*iter).second->destroyZippedAny(whereToGet);
904       whereToGet+=(*iter).second->getSizeInByteOfAnyReprInSeq();
905     }
906 }
907
908 AnyPtr StructAny::getOrBuildFromData(char *data, const TypeCodeStruct *type)
909 {
910   Any *ret;
911   ret=new StructAny(data,(TypeCodeStruct *)type);
912   return AnyPtr(ret);
913 }
914
915 StructAny::~StructAny()
916 {
917   const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
918   vector< pair<string,TypeCode*> >::const_iterator iter;
919   char *whereToGet=_data;
920   for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
921     {
922       (*iter).second->destroyZippedAny(whereToGet);
923       whereToGet+=(*iter).second->getSizeInByteOfAnyReprInSeq();
924     }
925   delete [] _data;
926 }
927
928 StructAny::StructAny(TypeCodeStruct *type):ComposedAny(type,false)
929 {
930   _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
931   for(unsigned int i=0;i<_type->getSizeInByteOfAnyReprInSeq();i++)
932     _data[i]='\0';
933 }
934
935 StructAny::StructAny(const StructAny& other):ComposedAny(other)
936 {
937   _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
938   const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
939   vector< pair<string,TypeCode*> >::const_iterator iter;
940   unsigned offset=0;
941   for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
942     {
943      (*iter).second->putReprAtPlace(_data+offset,other._data+offset,true);
944      offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
945     }
946 }
947
948 StructAny::StructAny(char *data, TypeCodeStruct * type):ComposedAny(type,false),_data(0)
949 {
950   _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
951   vector< pair<string,TypeCode*> >::const_iterator iter;
952   unsigned offset=0;
953   for(iter=type->_members.begin();iter!=type->_members.end();iter++)
954     {
955       (*iter).second->putReprAtPlace(_data+offset,data+offset,false);
956       offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
957     }
958 }
959
960 StructAny *StructAny::New(TypeCodeStruct *type)
961 {
962   return new StructAny(type);
963 }