1 // Copyright (C) 2006-2008 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "Runtime.hxx"
21 #include "TypeCode.hxx"
22 #include "InvalidExtractionException.hxx"
27 using namespace YACS::ENGINE;
30 StringOnHeap::StringOnHeap(const char *val):_dealloc(0),_str(strdup(val))
34 StringOnHeap::StringOnHeap(const std::string& val):_dealloc(0),_str(strdup(val.c_str()))
39 * \Note : no copy is performed if a deallocator is given.
40 * \param val : String in C format that is NOT copied if
42 * \param deAlloc : pointer on function to deallocate val after
45 StringOnHeap::StringOnHeap(char *val, Deallocator deAlloc):_dealloc(deAlloc)
53 bool StringOnHeap::operator ==(const StringOnHeap& other) const
55 return strcmp(_str, other._str)==0;
58 StringOnHeap *StringOnHeap::deepCopy() const
60 return new StringOnHeap(_str);
63 StringOnHeap::~StringOnHeap()
71 Any::Any(TypeCode* type):_type(type)
76 Any::Any(const Any& other):_type(other._type)
86 AtomAny::AtomAny(int val):Any(Runtime::_tc_int)
91 AtomAny::AtomAny(bool val):Any(Runtime::_tc_bool)
96 AtomAny::AtomAny(double val):Any(Runtime::_tc_double)
101 AtomAny::AtomAny(const char *val):Any(Runtime::_tc_string)
103 _value._s=new StringOnHeap(val);
106 AtomAny::AtomAny(const std::string& val):Any(Runtime::_tc_string)
108 _value._s=new StringOnHeap(val);
111 AtomAny::AtomAny(const AtomAny& other):Any(other)
113 if(_type->isA(Runtime::_tc_string))
115 StringOnHeap *cpy=(other._value._s)->deepCopy();
116 memcpy(&_value._s,&cpy,_type->getSizeInByteOfAnyReprInSeq());
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());
126 AtomAny::AtomAny(char *val, Deallocator deAlloc):Any(Runtime::_tc_string)
128 _value._s=new StringOnHeap(val,deAlloc);
131 AtomAny::AtomAny(char *data, TypeCode* type):Any(type)
133 if(type->isA(Runtime::_tc_string))
135 void **tmp=(void **)data;
136 StringOnHeap *cpy=((StringOnHeap *)(*tmp))->deepCopy();
137 memcpy(&_value._s,&cpy,type->getSizeInByteOfAnyReprInSeq());
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());
147 Any *AtomAny::clone() const
149 return new AtomAny(*this);
152 AtomAny *AtomAny::New(char *val,Deallocator dealloc)
154 return new AtomAny(val,dealloc);
157 AnyPtr AtomAny::operator[](int i) const throw(Exception)
159 throw InvalidExtractionException(_type->kind(),Sequence);
162 AnyPtr AtomAny::operator[](const char *key) const throw(Exception)
164 throw Exception("AtomAny::operator[] : try to get a part of a partitionned data whereas atomical.");
167 bool AtomAny::operator ==(const Any& other) const
169 if(!_type->isA(other.getType()))
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);
184 int AtomAny::getIntValue() const throw(Exception)
186 if(_type->isA(Runtime::_tc_int))
189 throw Exception("Value is not an int");
192 bool AtomAny::getBoolValue() const throw(Exception)
194 if(_type->isA(Runtime::_tc_bool))
197 throw Exception("Value is not a bool");
200 double AtomAny::getDoubleValue() const throw(Exception)
202 if(_type->isA(Runtime::_tc_double))
205 throw Exception("Value is not a double");
208 std::string AtomAny::getStringValue() const throw(Exception)
210 if(_type->isA(Runtime::_tc_string))
211 return string(_value._s->cStr());
213 throw Exception("Value is not a string");
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'
223 void AtomAny::putMyReprAtPlace(char *data) const
225 if(_type->isA(Runtime::_tc_string))
227 StringOnHeap *tmp=_value._s->deepCopy();
228 memcpy(data,&tmp,_type->getSizeInByteOfAnyReprInSeq());
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());
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'
246 void AtomAny::putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy)
248 if(type->isA(Runtime::_tc_string))
250 void **tmp1=(void **)src;
251 StringOnHeap *tmp=((const StringOnHeap *)(*tmp1))->deepCopy();
252 memcpy(data,&tmp,type->getSizeInByteOfAnyReprInSeq());
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());
263 * \note : Opposite method of putMyReprAtPlace. But static because due to data compression
266 void AtomAny::destroyReprAtPlace(char *data, const TypeCode *type)
268 DynType typ=type->kind();
271 void **tmp=(void **)data;
272 delete ((StringOnHeap *)(*tmp));
276 AnyPtr AtomAny::getOrBuildFromData(char *data, const TypeCode *type)
279 ret=new AtomAny(data,(TypeCode *)type);
283 bool AtomAny::takeInChargeStorageOf(TypeCode *type)
285 DynType typ=type->kind();
286 return (typ==Double || typ==Int || typ==Bool || typ==String);
291 if(_type->isA(Runtime::_tc_string))
295 ComposedAny::ComposedAny(const ComposedAny& other):Any(other)
299 ComposedAny::ComposedAny(TypeCode* type, bool isNew):Any(type)
305 AnyPtr ComposedAny::operator[](const char *key) const throw(Exception)
307 throw Exception("AtomAny::operator[] : try to get a part of a partitionned data not localizable by a string.");
310 void ComposedAny::checkTypeOf(const Any *elem) const throw(Exception)
312 if(!elem->getType()->isA(_type->contentType()))
313 throw Exception("ComposedAny::checkTypeOf : invalid type.");
316 int ComposedAny::getIntValue() const throw(Exception)
318 throw InvalidExtractionException(_type->kind(),Runtime::_tc_int->kind());
321 bool ComposedAny::getBoolValue() const throw(Exception)
323 throw InvalidExtractionException(_type->kind(),Runtime::_tc_bool->kind());
326 double ComposedAny::getDoubleValue() const throw(Exception)
328 throw InvalidExtractionException(_type->kind(),Runtime::_tc_double->kind());
331 std::string ComposedAny::getStringValue() const throw(Exception)
333 throw InvalidExtractionException(_type->kind(),Runtime::_tc_string->kind());
336 SeqAlloc::SeqAlloc(const SeqAlloc& other):_sizeOf1Elm(other._sizeOf1Elm),_notStdDeAlloc(0),
337 _start(0),_finish(0),_endOfStorage(0)
339 _start=allocate(other._finish-other._start);
340 _finish=_start+(other._finish-other._start);
341 _endOfStorage=_finish;
344 SeqAlloc::SeqAlloc(unsigned int sizeOf1Elm):_sizeOf1Elm(sizeOf1Elm),_notStdDeAlloc(0),
345 _start(0),_finish(0),_endOfStorage(0)
349 SeqAlloc::~SeqAlloc()
354 void SeqAlloc::clear()
363 * \note : This method is exclusively reserved for arrays of C++ built-in types because no
364 * constructor is applied atomically.
366 void SeqAlloc::initCoarseMemory(char *mem, unsigned int size, Deallocator dealloc)
368 unsigned sizeInByte=size*_sizeOf1Elm;
371 _notStdDeAlloc=dealloc;
376 _start=allocate(sizeInByte);
378 memcpy(_start,mem,sizeInByte);
381 for(unsigned int i=0;i<sizeInByte;i++) _start[i]=0;
384 _finish=_start+sizeInByte;
385 _endOfStorage=_finish;
388 void SeqAlloc::construct(char *pt, const Any *val)
390 val->putMyReprAtPlace(pt);
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.
398 void SeqAlloc::construct(char *pt, const char *val, const TypeCode *tc, bool deepCpy)
400 tc->putReprAtPlace(pt,val,deepCpy);
403 char *SeqAlloc::allocate(unsigned int nbOfByte)
406 return (char *)::operator new(nbOfByte);
411 // pt is not permitted to be a null pointer.
412 void SeqAlloc::deallocate(char *pt)
417 ::operator delete(pt);
426 void SeqAlloc::destroy(char *pt, const TypeCode *tc)
428 tc->destroyZippedAny(pt);
431 unsigned int SeqAlloc::size() const
433 return (_finish-_start)/_sizeOf1Elm;
436 void SequenceAny::clear()
438 for (char *cur=_alloc._start;cur!=_alloc._finish;cur+=_alloc._sizeOf1Elm)
439 _alloc.destroy(cur,_type->contentType());
443 void SequenceAny::popBack()
445 _alloc._finish-=_alloc._sizeOf1Elm;
446 _alloc.destroy(_alloc._finish,_type->contentType());
449 void SequenceAny::pushBack(const Any* elem)
451 if(!elem->_type->isA(_type->contentType()))
452 throw InvalidExtractionException(elem->_type->kind(),_type->contentType()->kind());
453 if(_alloc._finish != _alloc._endOfStorage)
455 _alloc.construct(_alloc._finish, elem);
456 _alloc._finish+=_alloc._sizeOf1Elm;
459 realloc(_alloc._finish, elem);
462 bool SequenceAny::operator ==(const Any& other) const
464 if(!_type->isA(other.getType()))
466 const SequenceAny& otherC=(const SequenceAny&) other;//cast granted due to previous lines
467 if(size()!=otherC.size())
469 for(unsigned i=0;i<size();i++)
470 if(!((*(*this)[i])==(*otherC[i])))
475 void SequenceAny::setEltAtRank(int i, const Any *elem) throw(Exception)
478 _alloc.destroy(_alloc._start+i*_alloc._sizeOf1Elm,_type->contentType());
479 _alloc.construct(_alloc._start+i*_alloc._sizeOf1Elm,elem);
482 AnyPtr SequenceAny::operator[](int i) const throw(Exception)
484 return _type->contentType()->getOrBuildAnyFromZippedData(_alloc._start+i*_alloc._sizeOf1Elm);
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'
491 void SequenceAny::putMyReprAtPlace(char *data) const
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));
500 void SequenceAny::putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy)
502 void **tmp2=(void **) src;
505 ((SequenceAny *)(*tmp2))->incrRef();
506 memcpy(data,src,type->getSizeInByteOfAnyReprInSeq());
510 SequenceAny *cpy=new SequenceAny(*((SequenceAny *)(*tmp2)));
511 memcpy(data,&cpy,type->getSizeInByteOfAnyReprInSeq());
513 //::new((SequenceAny *)data) SequenceAny((SequenceAny&) (*this));
516 void SequenceAny::destroyReprAtPlace(char *data, const TypeCode *type)
518 void **tmp=(void **) data;
520 ((SequenceAny *)(*tmp))->decrRef();
521 //((SequenceAny *)data)->~SequenceAny();
524 AnyPtr SequenceAny::getOrBuildFromData(char *data, const TypeCode *type)
526 void **tmp=(void **) data;
527 ((SequenceAny *) (*tmp))->incrRef();
528 return AnyPtr((SequenceAny *)(*tmp));
531 Any *SequenceAny::clone() const
533 return new SequenceAny(*this);
536 SequenceAny *SequenceAny::New(const TypeCode *typeOfContent)
538 if(typeOfContent->kind() == Objref)
540 //In case of Objref, use a sequence of string
541 return new SequenceAny(Runtime::_tc_string);
544 return new SequenceAny(typeOfContent);
547 SequenceAny *SequenceAny::New(const TypeCode *typeOfContent, unsigned lgth)
549 if(typeOfContent->kind() == Objref)
551 //In case of Objref, use a sequence of string
552 return new SequenceAny(Runtime::_tc_string,lgth);
555 return new SequenceAny(typeOfContent,lgth);
558 bool SequenceAny::takeInChargeStorageOf(TypeCode *type)
560 DynType typ=type->kind();
561 return (typ==Sequence);
564 SequenceAny::SequenceAny(const SequenceAny& other):ComposedAny(other),_alloc(other._alloc)
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);
571 SequenceAny::~SequenceAny()
573 for (char *cur=_alloc._start;cur!=_alloc._finish;cur+=_alloc._sizeOf1Elm)
574 _alloc.destroy(cur,_type->contentType());
578 * \param typeOfContent : typeCode of the type of elements stored in sequence.
580 SequenceAny::SequenceAny(const TypeCode *typeOfContent):ComposedAny(new TypeCodeSeq("","",typeOfContent)),
581 _alloc(typeOfContent->getSizeInByteOfAnyReprInSeq())
585 SequenceAny::SequenceAny(const TypeCode *typeOfContent, unsigned lgth):ComposedAny(new TypeCodeSeq("","",typeOfContent)),
586 _alloc(typeOfContent->getSizeInByteOfAnyReprInSeq())
588 _alloc.initCoarseMemory(0,lgth,0);
591 SequenceAny::SequenceAny(int *val, unsigned int lgth, Deallocator deAlloc):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_int)),
592 _alloc(Runtime::_tc_int->getSizeInByteOfAnyReprInSeq())
594 _alloc.initCoarseMemory((char *)val,lgth,deAlloc);
597 SequenceAny::SequenceAny(bool *val, unsigned int lgth, Deallocator deAlloc):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_bool)),
598 _alloc(Runtime::_tc_bool->getSizeInByteOfAnyReprInSeq())
600 _alloc.initCoarseMemory((char *)val,lgth,deAlloc);
603 SequenceAny::SequenceAny(double *val, unsigned int lgth, Deallocator deAlloc):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_double)),
604 _alloc(Runtime::_tc_double->getSizeInByteOfAnyReprInSeq())
606 _alloc.initCoarseMemory((char *)val,lgth,deAlloc);
609 SequenceAny::SequenceAny(const std::vector<int>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_int)),
610 _alloc(Runtime::_tc_int->getSizeInByteOfAnyReprInSeq())
612 _alloc.initCoarseMemory((char *)&val[0],val.size(),0);
615 SequenceAny::SequenceAny(const std::vector<bool>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_bool)),
616 _alloc(Runtime::_tc_bool->getSizeInByteOfAnyReprInSeq())
618 for(vector<bool>::const_iterator iter=val.begin();iter!=val.end();iter++)
620 AtomAnyPtr tmp=AtomAny::New(*iter);
625 SequenceAny::SequenceAny(const std::vector<double>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_double)),
626 _alloc(Runtime::_tc_double->getSizeInByteOfAnyReprInSeq())
628 _alloc.initCoarseMemory((char *)&val[0],val.size(),0);
631 SequenceAny::SequenceAny(const std::vector<std::string>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_string)),
632 _alloc(Runtime::_tc_string->getSizeInByteOfAnyReprInSeq())
634 for(vector<string>::const_iterator iter=val.begin();iter!=val.end();iter++)
636 AtomAnyPtr tmp=AtomAny::New(*iter);
641 void SequenceAny::realloc(char *endOfCurrentAllocated, const Any *elem)
643 unsigned int oldSize=_alloc._finish-_alloc._start;
644 unsigned int newSize = oldSize != 0 ? 2 * oldSize : _alloc._sizeOf1Elm;
645 char *newStart=_alloc.allocate(newSize);
647 char *newFinish=performCpy(_alloc._start, endOfCurrentAllocated,newStart);
648 _alloc.construct(newFinish, elem);
649 newFinish+=_alloc._sizeOf1Elm;
650 newFinish=performCpy(endOfCurrentAllocated, _alloc._finish, newFinish);
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;
660 char *SequenceAny::performCpy(char *srcStart, char *srcFinish, char *destStart)
663 for (;srcStart != srcFinish; srcStart+=_alloc._sizeOf1Elm, cur+=_alloc._sizeOf1Elm)
664 _alloc.construct(cur, srcStart, _type->contentType(),false);
668 ArrayAny::~ArrayAny()
670 const TypeCode *subType=_type->contentType();
671 unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
672 unsigned int size=((TypeCodeArray *)_type)->getStaticLgth();
674 for(unsigned i=0;i<size;i++,tmp+=sizePerContent)
675 subType->destroyZippedAny(tmp);
679 ArrayAny::ArrayAny(const TypeCode *typeOfContent, unsigned int lgth):ComposedAny(new TypeCodeArray("","",typeOfContent,lgth))
681 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
682 for(unsigned int i=0;i<_type->getSizeInByteOfAnyReprInSeq();i++)
686 ArrayAny::ArrayAny(char *data, TypeCodeArray * type):ComposedAny(type,false),_data(0)
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);
695 ArrayAny::ArrayAny(const ArrayAny& other):ComposedAny(other)
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);
704 ArrayAny::ArrayAny(const int *val, unsigned int lgth):ComposedAny(new TypeCodeArray("","",Runtime::_tc_int,lgth)),
707 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
708 memcpy(_data,val,_type->getSizeInByteOfAnyReprInSeq());
711 ArrayAny::ArrayAny(const bool *val, unsigned int lgth):ComposedAny(new TypeCodeArray("","",Runtime::_tc_bool,lgth)),
714 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
715 memcpy(_data,val,_type->getSizeInByteOfAnyReprInSeq());
718 ArrayAny::ArrayAny(const double *val, unsigned int lgth):ComposedAny(new TypeCodeArray("","",Runtime::_tc_double,lgth)),
721 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
722 memcpy(_data,val,_type->getSizeInByteOfAnyReprInSeq());
725 ArrayAny::ArrayAny(const std::vector<int>& val):ComposedAny(new TypeCodeArray("","",Runtime::_tc_int,val.size())),
728 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
729 memcpy(_data,&val[0],_type->getSizeInByteOfAnyReprInSeq());
732 ArrayAny::ArrayAny(const std::vector<double>& val):ComposedAny(new TypeCodeArray("","",Runtime::_tc_double,val.size())),
735 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
736 memcpy(_data,&val[0],_type->getSizeInByteOfAnyReprInSeq());
739 ArrayAny::ArrayAny(const std::vector<std::string>& val):ComposedAny(new TypeCodeArray("","",Runtime::_tc_string,val.size())),
742 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
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++)
748 StringOnHeap *st=new StringOnHeap(*iter);
749 memcpy(_data+i*sizePerContent,&st,sizePerContent);
753 void ArrayAny::setEltAtRank(int i, const Any *elem) throw(Exception)
756 const TypeCode *subType=_type->contentType();
757 subType->destroyZippedAny(_data+i*subType->getSizeInByteOfAnyReprInSeq());
758 elem->putMyReprAtPlace(_data+i*subType->getSizeInByteOfAnyReprInSeq());
761 bool ArrayAny::operator ==(const Any& other) const
763 if(!_type->isA(other.getType()))
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])))
772 AnyPtr ArrayAny::operator[](int i) const throw(Exception)
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);
781 unsigned int ArrayAny::size() const
783 return ((TypeCodeArray *)_type)->getStaticLgth();
786 Any *ArrayAny::clone() const
788 return new ArrayAny(*this);
791 ArrayAny *ArrayAny::New(const TypeCode *typeOfContent, unsigned int lgth)
793 return new ArrayAny(typeOfContent,lgth);
796 void ArrayAny::putMyReprAtPlace(char *data) const
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);
804 void ArrayAny::putReprAtPlace(char *data, const char *src, const TypeCodeArray *type, bool deepCpy)
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);
812 void ArrayAny::destroyReprAtPlace(char *data, const TypeCodeArray *type)
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);
820 AnyPtr ArrayAny::getOrBuildFromData(char *data, const TypeCodeArray *type)
823 ret=new ArrayAny(data,(TypeCodeArray *)type);
827 bool ArrayAny::takeInChargeStorageOf(TypeCode *type)
829 DynType typ=type->kind();
833 Any *StructAny::clone() const
835 return new StructAny(*this);
838 bool StructAny::operator ==(const Any& other) const
840 if(!_type->isA(other.getType()))
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()]))))
850 AnyPtr StructAny::operator[](int i) const throw(Exception)
852 const char what[]="StructAny::operator[](int i) : Struct key are strings not integers.";
853 throw Exception(what);
856 AnyPtr StructAny::operator[](const char *key) const throw(Exception)
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();
866 if(iter==typeC->_members.end())
868 string what("Unexisting key \""); what+=key; what+="\" for struct extraction.";
869 throw Exception(what);
871 return (*iter).second->getOrBuildAnyFromZippedData(whereToGet);
874 void StructAny::setEltAtRank(int i, const Any *elem) throw(Exception)
876 const char what[]="Struct key are strings not integers.";
877 throw Exception(what);
880 void StructAny::setEltAtRank(const char *key, const Any *elem) throw(Exception)
882 const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
884 const TypeCode *tcOnKey=typeC->getMember(key,offset);
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);
893 void StructAny::putMyReprAtPlace(char *data) const
895 const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
897 vector< pair<string,TypeCode*> >::const_iterator iter;
898 for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
900 (*iter).second->putReprAtPlace(data+offset,_data+offset,false);
901 offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
905 void StructAny::putReprAtPlace(char *data, const char *src, const TypeCodeStruct *type, bool deepCpy)
908 vector< pair<string,TypeCode*> >::const_iterator iter;
909 for(iter=type->_members.begin();iter!=type->_members.end();iter++)
911 (*iter).second->putReprAtPlace(data+offset,src+offset,deepCpy);
912 offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
916 void StructAny::destroyReprAtPlace(char *data, const TypeCodeStruct *type)
918 char *whereToGet=data;
919 vector< pair<string,TypeCode*> >::const_iterator iter;
920 for(iter=type->_members.begin();iter!=type->_members.end();iter++)
922 (*iter).second->destroyZippedAny(whereToGet);
923 whereToGet+=(*iter).second->getSizeInByteOfAnyReprInSeq();
927 AnyPtr StructAny::getOrBuildFromData(char *data, const TypeCodeStruct *type)
930 ret=new StructAny(data,(TypeCodeStruct *)type);
934 StructAny::~StructAny()
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++)
941 (*iter).second->destroyZippedAny(whereToGet);
942 whereToGet+=(*iter).second->getSizeInByteOfAnyReprInSeq();
947 StructAny::StructAny(TypeCodeStruct *type):ComposedAny(type,false)
949 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
950 for(unsigned int i=0;i<_type->getSizeInByteOfAnyReprInSeq();i++)
954 StructAny::StructAny(const StructAny& other):ComposedAny(other)
956 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
957 const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
958 vector< pair<string,TypeCode*> >::const_iterator iter;
960 for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
962 (*iter).second->putReprAtPlace(_data+offset,other._data+offset,true);
963 offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
967 StructAny::StructAny(char *data, TypeCodeStruct * type):ComposedAny(type,false),_data(0)
969 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
970 vector< pair<string,TypeCode*> >::const_iterator iter;
972 for(iter=type->_members.begin();iter!=type->_members.end();iter++)
974 (*iter).second->putReprAtPlace(_data+offset,data+offset,false);
975 offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
979 StructAny *StructAny::New(TypeCodeStruct *type)
981 return new StructAny(type);