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