3 #include "TypeCode.hxx"
4 #include "InvalidExtractionException.hxx"
8 using namespace YACS::ENGINE;
11 StringOnHeap::StringOnHeap(const char *val):_dealloc(0),_str(strdup(val))
15 StringOnHeap::StringOnHeap(const std::string& val):_dealloc(0),_str(strdup(val.c_str()))
20 * \Note : no copy is performed if a deallocator is given.
21 * \param val : String in C format that is NOT copied if
23 * \param deAlloc : pointer on function to deallocate val after
26 StringOnHeap::StringOnHeap(char *val, Deallocator deAlloc):_dealloc(deAlloc)
34 bool StringOnHeap::operator ==(const StringOnHeap& other) const
36 return strcmp(_str, other._str)==0;
39 StringOnHeap *StringOnHeap::deepCopy() const
41 return new StringOnHeap(_str);
44 StringOnHeap::~StringOnHeap()
52 Any::Any(TypeCode* type):_type(type)
57 Any::Any(const Any& other):_type(other._type)
67 AtomAny::AtomAny(int val):Any(Runtime::_tc_int)
72 AtomAny::AtomAny(bool val):Any(Runtime::_tc_bool)
77 AtomAny::AtomAny(double val):Any(Runtime::_tc_double)
82 AtomAny::AtomAny(const char *val):Any(Runtime::_tc_string)
84 _value._s=new StringOnHeap(val);
87 AtomAny::AtomAny(const std::string& val):Any(Runtime::_tc_string)
89 _value._s=new StringOnHeap(val);
92 AtomAny::AtomAny(const AtomAny& other):Any(other)
94 if(_type->isA(Runtime::_tc_string))
96 StringOnHeap *cpy=(other._value._s)->deepCopy();
97 memcpy(&_value._s,&cpy,_type->getSizeInByteOfAnyReprInSeq());
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());
107 AtomAny::AtomAny(char *val, Deallocator deAlloc):Any(Runtime::_tc_string)
109 _value._s=new StringOnHeap(val,deAlloc);
112 AtomAny::AtomAny(char *data, TypeCode* type):Any(type)
114 if(type->isA(Runtime::_tc_string))
116 void **tmp=(void **)data;
117 StringOnHeap *cpy=((StringOnHeap *)(*tmp))->deepCopy();
118 memcpy(&_value._s,&cpy,type->getSizeInByteOfAnyReprInSeq());
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());
128 Any *AtomAny::clone() const
130 return new AtomAny(*this);
133 AtomAny *AtomAny::New(char *val,Deallocator dealloc)
135 return new AtomAny(val,dealloc);
138 AnyPtr AtomAny::operator[](int i) const throw(Exception)
140 throw InvalidExtractionException(_type->kind(),Sequence);
143 AnyPtr AtomAny::operator[](const char *key) const throw(Exception)
145 throw Exception("AtomAny::operator[] : try to get a part of a partitionned data whereas atomical.");
148 bool AtomAny::operator ==(const Any& other) const
150 if(!_type->isA(other.getType()))
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);
165 int AtomAny::getIntValue() const throw(Exception)
167 if(_type->isA(Runtime::_tc_int))
170 throw Exception("Value is not an Int");
173 bool AtomAny::getBoolValue() const throw(Exception)
175 if(_type->isA(Runtime::_tc_bool))
178 throw Exception("Value is not a Bool");
181 double AtomAny::getDoubleValue() const throw(Exception)
183 if(_type->isA(Runtime::_tc_double))
186 throw Exception("Value is not a Double");
189 std::string AtomAny::getStringValue() const throw(Exception)
191 if(_type->isA(Runtime::_tc_string))
192 return string(_value._s->cStr());
194 throw Exception("Value is not a String");
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'
204 void AtomAny::putMyReprAtPlace(char *data) const
206 if(_type->isA(Runtime::_tc_string))
208 StringOnHeap *tmp=_value._s->deepCopy();
209 memcpy(data,&tmp,_type->getSizeInByteOfAnyReprInSeq());
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());
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'
227 void AtomAny::putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy)
229 if(type->isA(Runtime::_tc_string))
231 void **tmp1=(void **)src;
232 StringOnHeap *tmp=((const StringOnHeap *)(*tmp1))->deepCopy();
233 memcpy(data,&tmp,type->getSizeInByteOfAnyReprInSeq());
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());
244 * \note : Opposite method of putMyReprAtPlace. But static because due to data compression
247 void AtomAny::destroyReprAtPlace(char *data, const TypeCode *type)
249 DynType typ=type->kind();
252 void **tmp=(void **)data;
253 delete ((StringOnHeap *)(*tmp));
257 AnyPtr AtomAny::getOrBuildFromData(char *data, const TypeCode *type)
260 ret=new AtomAny(data,(TypeCode *)type);
264 bool AtomAny::takeInChargeStorageOf(TypeCode *type)
266 DynType typ=type->kind();
267 return (typ==Double || typ==Int || typ==Bool || typ==String);
272 if(_type->isA(Runtime::_tc_string))
276 ComposedAny::ComposedAny(const ComposedAny& other):Any(other)
280 ComposedAny::ComposedAny(TypeCode* type, bool isNew):Any(type)
286 AnyPtr ComposedAny::operator[](const char *key) const throw(Exception)
288 throw Exception("AtomAny::operator[] : try to get a part of a partitionned data not localizable by a string.");
291 void ComposedAny::checkTypeOf(const Any *elem) const throw(Exception)
293 if(!elem->getType()->isA(_type->contentType()))
294 throw Exception("ComposedAny::checkTypeOf : invalid type.");
297 int ComposedAny::getIntValue() const throw(Exception)
299 throw InvalidExtractionException(_type->kind(),Runtime::_tc_int->kind());
302 bool ComposedAny::getBoolValue() const throw(Exception)
304 throw InvalidExtractionException(_type->kind(),Runtime::_tc_bool->kind());
307 double ComposedAny::getDoubleValue() const throw(Exception)
309 throw InvalidExtractionException(_type->kind(),Runtime::_tc_double->kind());
312 std::string ComposedAny::getStringValue() const throw(Exception)
314 throw InvalidExtractionException(_type->kind(),Runtime::_tc_string->kind());
317 SeqAlloc::SeqAlloc(const SeqAlloc& other):_sizeOf1Elm(other._sizeOf1Elm),_notStdDeAlloc(0),
318 _start(0),_finish(0),_endOfStorage(0)
320 _start=allocate(other._finish-other._start);
321 _finish=_start+(other._finish-other._start);
322 _endOfStorage=_finish;
325 SeqAlloc::SeqAlloc(unsigned int sizeOf1Elm):_sizeOf1Elm(sizeOf1Elm),_notStdDeAlloc(0),
326 _start(0),_finish(0),_endOfStorage(0)
330 SeqAlloc::~SeqAlloc()
335 void SeqAlloc::clear()
344 * \note : This method is exclusively reserved for arrays of C++ built-in types because no
345 * constructor is applied atomically.
347 void SeqAlloc::initCoarseMemory(char *mem, unsigned int size, Deallocator dealloc)
349 unsigned sizeInByte=size*_sizeOf1Elm;
352 _notStdDeAlloc=dealloc;
357 _start=allocate(sizeInByte);
359 memcpy(_start,mem,sizeInByte);
362 for(unsigned int i=0;i<sizeInByte;i++) _start[i]=0;
365 _finish=_start+sizeInByte;
366 _endOfStorage=_finish;
369 void SeqAlloc::construct(char *pt, const Any *val)
371 val->putMyReprAtPlace(pt);
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.
379 void SeqAlloc::construct(char *pt, const char *val, const TypeCode *tc, bool deepCpy)
381 tc->putReprAtPlace(pt,val,deepCpy);
384 char *SeqAlloc::allocate(unsigned int nbOfByte)
387 return (char *)::operator new(nbOfByte);
392 // pt is not permitted to be a null pointer.
393 void SeqAlloc::deallocate(char *pt)
398 ::operator delete(pt);
407 void SeqAlloc::destroy(char *pt, const TypeCode *tc)
409 tc->destroyZippedAny(pt);
412 unsigned int SeqAlloc::size() const
414 return (_finish-_start)/_sizeOf1Elm;
417 void SequenceAny::clear()
419 for (char *cur=_alloc._start;cur!=_alloc._finish;cur+=_alloc._sizeOf1Elm)
420 _alloc.destroy(cur,_type->contentType());
424 void SequenceAny::popBack()
426 _alloc._finish-=_alloc._sizeOf1Elm;
427 _alloc.destroy(_alloc._finish,_type->contentType());
430 void SequenceAny::pushBack(const Any* elem)
432 if(!elem->_type->isA(_type->contentType()))
433 throw InvalidExtractionException(elem->_type->kind(),_type->contentType()->kind());
434 if(_alloc._finish != _alloc._endOfStorage)
436 _alloc.construct(_alloc._finish, elem);
437 _alloc._finish+=_alloc._sizeOf1Elm;
440 realloc(_alloc._finish, elem);
443 bool SequenceAny::operator ==(const Any& other) const
445 if(!_type->isA(other.getType()))
447 const SequenceAny& otherC=(const SequenceAny&) other;//cast granted due to previous lines
448 if(size()!=otherC.size())
450 for(unsigned i=0;i<size();i++)
451 if(!((*(*this)[i])==(*otherC[i])))
456 void SequenceAny::setEltAtRank(int i, const Any *elem) throw(Exception)
459 _alloc.destroy(_alloc._start+i*_alloc._sizeOf1Elm,_type->contentType());
460 _alloc.construct(_alloc._start+i*_alloc._sizeOf1Elm,elem);
463 AnyPtr SequenceAny::operator[](int i) const throw(Exception)
465 return _type->contentType()->getOrBuildAnyFromZippedData(_alloc._start+i*_alloc._sizeOf1Elm);
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'
472 void SequenceAny::putMyReprAtPlace(char *data) const
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));
481 void SequenceAny::putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy)
483 void **tmp2=(void **) src;
486 ((SequenceAny *)(*tmp2))->incrRef();
487 memcpy(data,src,type->getSizeInByteOfAnyReprInSeq());
491 SequenceAny *cpy=new SequenceAny(*((SequenceAny *)(*tmp2)));
492 memcpy(data,&cpy,type->getSizeInByteOfAnyReprInSeq());
494 //::new((SequenceAny *)data) SequenceAny((SequenceAny&) (*this));
497 void SequenceAny::destroyReprAtPlace(char *data, const TypeCode *type)
499 void **tmp=(void **) data;
501 ((SequenceAny *)(*tmp))->decrRef();
502 //((SequenceAny *)data)->~SequenceAny();
505 AnyPtr SequenceAny::getOrBuildFromData(char *data, const TypeCode *type)
507 void **tmp=(void **) data;
508 ((SequenceAny *) (*tmp))->incrRef();
509 return AnyPtr((SequenceAny *)(*tmp));
512 Any *SequenceAny::clone() const
514 return new SequenceAny(*this);
517 SequenceAny *SequenceAny::New(const TypeCode *typeOfContent)
519 if(typeOfContent->kind() == Objref)
521 //In case of Objref, use a sequence of string
522 return new SequenceAny(Runtime::_tc_string);
525 return new SequenceAny(typeOfContent);
528 SequenceAny *SequenceAny::New(const TypeCode *typeOfContent, unsigned lgth)
530 if(typeOfContent->kind() == Objref)
532 //In case of Objref, use a sequence of string
533 return new SequenceAny(Runtime::_tc_string,lgth);
536 return new SequenceAny(typeOfContent,lgth);
539 bool SequenceAny::takeInChargeStorageOf(TypeCode *type)
541 DynType typ=type->kind();
542 return (typ==Sequence);
545 SequenceAny::SequenceAny(const SequenceAny& other):ComposedAny(other),_alloc(other._alloc)
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);
552 SequenceAny::~SequenceAny()
554 for (char *cur=_alloc._start;cur!=_alloc._finish;cur+=_alloc._sizeOf1Elm)
555 _alloc.destroy(cur,_type->contentType());
559 * \param typeOfContent : typeCode of the type of elements stored in sequence.
561 SequenceAny::SequenceAny(const TypeCode *typeOfContent):ComposedAny(new TypeCodeSeq("","",typeOfContent)),
562 _alloc(typeOfContent->getSizeInByteOfAnyReprInSeq())
566 SequenceAny::SequenceAny(const TypeCode *typeOfContent, unsigned lgth):ComposedAny(new TypeCodeSeq("","",typeOfContent)),
567 _alloc(typeOfContent->getSizeInByteOfAnyReprInSeq())
569 _alloc.initCoarseMemory(0,lgth,0);
572 SequenceAny::SequenceAny(int *val, unsigned int lgth, Deallocator deAlloc):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_int)),
573 _alloc(Runtime::_tc_int->getSizeInByteOfAnyReprInSeq())
575 _alloc.initCoarseMemory((char *)val,lgth,deAlloc);
578 SequenceAny::SequenceAny(bool *val, unsigned int lgth, Deallocator deAlloc):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_bool)),
579 _alloc(Runtime::_tc_bool->getSizeInByteOfAnyReprInSeq())
581 _alloc.initCoarseMemory((char *)val,lgth,deAlloc);
584 SequenceAny::SequenceAny(double *val, unsigned int lgth, Deallocator deAlloc):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_double)),
585 _alloc(Runtime::_tc_double->getSizeInByteOfAnyReprInSeq())
587 _alloc.initCoarseMemory((char *)val,lgth,deAlloc);
590 SequenceAny::SequenceAny(const std::vector<int>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_int)),
591 _alloc(Runtime::_tc_int->getSizeInByteOfAnyReprInSeq())
593 _alloc.initCoarseMemory((char *)&val[0],val.size(),0);
596 SequenceAny::SequenceAny(const std::vector<bool>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_bool)),
597 _alloc(Runtime::_tc_bool->getSizeInByteOfAnyReprInSeq())
599 for(vector<bool>::const_iterator iter=val.begin();iter!=val.end();iter++)
601 AtomAnyPtr tmp=AtomAny::New(*iter);
606 SequenceAny::SequenceAny(const std::vector<double>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_double)),
607 _alloc(Runtime::_tc_double->getSizeInByteOfAnyReprInSeq())
609 _alloc.initCoarseMemory((char *)&val[0],val.size(),0);
612 SequenceAny::SequenceAny(const std::vector<std::string>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_string)),
613 _alloc(Runtime::_tc_string->getSizeInByteOfAnyReprInSeq())
615 for(vector<string>::const_iterator iter=val.begin();iter!=val.end();iter++)
617 AtomAnyPtr tmp=AtomAny::New(*iter);
622 void SequenceAny::realloc(char *endOfCurrentAllocated, const Any *elem)
624 unsigned int oldSize=_alloc._finish-_alloc._start;
625 unsigned int newSize = oldSize != 0 ? 2 * oldSize : _alloc._sizeOf1Elm;
626 char *newStart=_alloc.allocate(newSize);
628 char *newFinish=performCpy(_alloc._start, endOfCurrentAllocated,newStart);
629 _alloc.construct(newFinish, elem);
630 newFinish+=_alloc._sizeOf1Elm;
631 newFinish=performCpy(endOfCurrentAllocated, _alloc._finish, newFinish);
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;
641 char *SequenceAny::performCpy(char *srcStart, char *srcFinish, char *destStart)
644 for (;srcStart != srcFinish; srcStart+=_alloc._sizeOf1Elm, cur+=_alloc._sizeOf1Elm)
645 _alloc.construct(cur, srcStart, _type->contentType(),false);
649 ArrayAny::~ArrayAny()
651 const TypeCode *subType=_type->contentType();
652 unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
653 unsigned int size=((TypeCodeArray *)_type)->getStaticLgth();
655 for(unsigned i=0;i<size;i++,tmp+=sizePerContent)
656 subType->destroyZippedAny(tmp);
660 ArrayAny::ArrayAny(const TypeCode *typeOfContent, unsigned int lgth):ComposedAny(new TypeCodeArray("","",typeOfContent,lgth))
662 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
663 for(unsigned int i=0;i<_type->getSizeInByteOfAnyReprInSeq();i++)
667 ArrayAny::ArrayAny(char *data, TypeCodeArray * type):ComposedAny(type,false),_data(0)
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);
676 ArrayAny::ArrayAny(const ArrayAny& other):ComposedAny(other)
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);
685 ArrayAny::ArrayAny(const int *val, unsigned int lgth):ComposedAny(new TypeCodeArray("","",Runtime::_tc_int,lgth)),
688 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
689 memcpy(_data,val,_type->getSizeInByteOfAnyReprInSeq());
692 ArrayAny::ArrayAny(const bool *val, unsigned int lgth):ComposedAny(new TypeCodeArray("","",Runtime::_tc_bool,lgth)),
695 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
696 memcpy(_data,val,_type->getSizeInByteOfAnyReprInSeq());
699 ArrayAny::ArrayAny(const double *val, unsigned int lgth):ComposedAny(new TypeCodeArray("","",Runtime::_tc_double,lgth)),
702 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
703 memcpy(_data,val,_type->getSizeInByteOfAnyReprInSeq());
706 ArrayAny::ArrayAny(const std::vector<int>& val):ComposedAny(new TypeCodeArray("","",Runtime::_tc_int,val.size())),
709 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
710 memcpy(_data,&val[0],_type->getSizeInByteOfAnyReprInSeq());
713 ArrayAny::ArrayAny(const std::vector<double>& val):ComposedAny(new TypeCodeArray("","",Runtime::_tc_double,val.size())),
716 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
717 memcpy(_data,&val[0],_type->getSizeInByteOfAnyReprInSeq());
720 ArrayAny::ArrayAny(const std::vector<std::string>& val):ComposedAny(new TypeCodeArray("","",Runtime::_tc_string,val.size())),
723 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
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++)
729 StringOnHeap *st=new StringOnHeap(*iter);
730 memcpy(_data+i*sizePerContent,&st,sizePerContent);
734 void ArrayAny::setEltAtRank(int i, const Any *elem) throw(Exception)
737 const TypeCode *subType=_type->contentType();
738 subType->destroyZippedAny(_data+i*subType->getSizeInByteOfAnyReprInSeq());
739 elem->putMyReprAtPlace(_data+i*subType->getSizeInByteOfAnyReprInSeq());
742 bool ArrayAny::operator ==(const Any& other) const
744 if(!_type->isA(other.getType()))
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])))
753 AnyPtr ArrayAny::operator[](int i) const throw(Exception)
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);
762 unsigned int ArrayAny::size() const
764 return ((TypeCodeArray *)_type)->getStaticLgth();
767 Any *ArrayAny::clone() const
769 return new ArrayAny(*this);
772 ArrayAny *ArrayAny::New(const TypeCode *typeOfContent, unsigned int lgth)
774 return new ArrayAny(typeOfContent,lgth);
777 void ArrayAny::putMyReprAtPlace(char *data) const
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);
785 void ArrayAny::putReprAtPlace(char *data, const char *src, const TypeCodeArray *type, bool deepCpy)
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);
793 void ArrayAny::destroyReprAtPlace(char *data, const TypeCodeArray *type)
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);
801 AnyPtr ArrayAny::getOrBuildFromData(char *data, const TypeCodeArray *type)
804 ret=new ArrayAny(data,(TypeCodeArray *)type);
808 bool ArrayAny::takeInChargeStorageOf(TypeCode *type)
810 DynType typ=type->kind();
814 Any *StructAny::clone() const
816 return new StructAny(*this);
819 bool StructAny::operator ==(const Any& other) const
821 if(!_type->isA(other.getType()))
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()]))))
831 AnyPtr StructAny::operator[](int i) const throw(Exception)
833 const char what[]="StructAny::operator[](int i) : Struct key are strings not integers.";
834 throw Exception(what);
837 AnyPtr StructAny::operator[](const char *key) const throw(Exception)
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();
847 if(iter==typeC->_members.end())
849 string what("Unexisting key \""); what+=key; what+="\" for struct extraction.";
850 throw Exception(what);
852 return (*iter).second->getOrBuildAnyFromZippedData(whereToGet);
855 void StructAny::setEltAtRank(int i, const Any *elem) throw(Exception)
857 const char what[]="Struct key are strings not integers.";
858 throw Exception(what);
861 void StructAny::setEltAtRank(const char *key, const Any *elem) throw(Exception)
863 const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
865 const TypeCode *tcOnKey=typeC->getMember(key,offset);
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);
874 void StructAny::putMyReprAtPlace(char *data) const
876 const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
878 vector< pair<string,TypeCode*> >::const_iterator iter;
879 for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
881 (*iter).second->putReprAtPlace(data+offset,_data+offset,false);
882 offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
886 void StructAny::putReprAtPlace(char *data, const char *src, const TypeCodeStruct *type, bool deepCpy)
889 vector< pair<string,TypeCode*> >::const_iterator iter;
890 for(iter=type->_members.begin();iter!=type->_members.end();iter++)
892 (*iter).second->putReprAtPlace(data+offset,src+offset,deepCpy);
893 offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
897 void StructAny::destroyReprAtPlace(char *data, const TypeCodeStruct *type)
899 char *whereToGet=data;
900 vector< pair<string,TypeCode*> >::const_iterator iter;
901 for(iter=type->_members.begin();iter!=type->_members.end();iter++)
903 (*iter).second->destroyZippedAny(whereToGet);
904 whereToGet+=(*iter).second->getSizeInByteOfAnyReprInSeq();
908 AnyPtr StructAny::getOrBuildFromData(char *data, const TypeCodeStruct *type)
911 ret=new StructAny(data,(TypeCodeStruct *)type);
915 StructAny::~StructAny()
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++)
922 (*iter).second->destroyZippedAny(whereToGet);
923 whereToGet+=(*iter).second->getSizeInByteOfAnyReprInSeq();
928 StructAny::StructAny(TypeCodeStruct *type):ComposedAny(type,false)
930 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
931 for(unsigned int i=0;i<_type->getSizeInByteOfAnyReprInSeq();i++)
935 StructAny::StructAny(const StructAny& other):ComposedAny(other)
937 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
938 const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
939 vector< pair<string,TypeCode*> >::const_iterator iter;
941 for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
943 (*iter).second->putReprAtPlace(_data+offset,other._data+offset,true);
944 offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
948 StructAny::StructAny(char *data, TypeCodeStruct * type):ComposedAny(type,false),_data(0)
950 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
951 vector< pair<string,TypeCode*> >::const_iterator iter;
953 for(iter=type->_members.begin();iter!=type->_members.end();iter++)
955 (*iter).second->putReprAtPlace(_data+offset,data+offset,false);
956 offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
960 StructAny *StructAny::New(TypeCodeStruct *type)
962 return new StructAny(type);