1 // Copyright (C) 2006-2016 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, or (at your option) any later version.
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
21 #include "Runtime.hxx"
22 #include "TypeCode.hxx"
23 #include "InvalidExtractionException.hxx"
28 using namespace YACS::ENGINE;
31 // forbidden value int=-269488145 double=-1.54947e+231 bool=239
32 const char SeqAlloc::DFT_CHAR_VAR=-17;//0xEF
34 StringOnHeap::StringOnHeap(const char *val):_dealloc(0),_str(strdup(val))
38 StringOnHeap::StringOnHeap(const std::string& val):_dealloc(0),_str(strdup(val.c_str()))
43 * \note : no copy is performed if a deallocator is given.
44 * \param val : String in C format that is NOT copied if
46 * \param deAlloc : pointer on function to deallocate val after
49 StringOnHeap::StringOnHeap(char *val, Deallocator deAlloc):_dealloc(deAlloc)
57 bool StringOnHeap::operator ==(const StringOnHeap& other) const
59 return strcmp(_str, other._str)==0;
62 StringOnHeap *StringOnHeap::deepCopy() const
64 return new StringOnHeap(_str);
67 StringOnHeap::~StringOnHeap()
75 Any::Any(TypeCode* type):_type(type)
80 Any::Any(const Any& other):_type(other._type)
90 bool Any::IsNull(char *data)
95 for(std::size_t i=0;i<sizeof(void *) && isNull;i++)
96 isNull=(data[i]==SeqAlloc::DFT_CHAR_VAR);
100 AtomAny::AtomAny(int val):Any(Runtime::_tc_int)
105 AtomAny::AtomAny(bool val):Any(Runtime::_tc_bool)
110 AtomAny::AtomAny(double val):Any(Runtime::_tc_double)
115 AtomAny::AtomAny(const char *val):Any(Runtime::_tc_string)
117 _value._s=new StringOnHeap(val);
120 AtomAny::AtomAny(const std::string& val):Any(Runtime::_tc_string)
122 _value._s=new StringOnHeap(val);
125 AtomAny::AtomAny(const AtomAny& other):Any(other)
127 if(_type->isA(Runtime::_tc_string))
129 StringOnHeap *cpy=(other._value._s)->deepCopy();
130 memcpy(&_value._s,&cpy,_type->getSizeInByteOfAnyReprInSeq());
132 else if(_type->isA(Runtime::_tc_double))
133 memcpy(&_value._d,&other._value._d,_type->getSizeInByteOfAnyReprInSeq());
134 else if(_type->isA(Runtime::_tc_int))
135 memcpy(&_value._i,&other._value._i,_type->getSizeInByteOfAnyReprInSeq());
136 else if(_type->isA(Runtime::_tc_bool))
137 memcpy(&_value._b,&other._value._b,_type->getSizeInByteOfAnyReprInSeq());
140 AtomAny::AtomAny(char *val, Deallocator deAlloc):Any(Runtime::_tc_string)
142 _value._s=new StringOnHeap(val,deAlloc);
145 AtomAny::AtomAny(char *data, TypeCode* type):Any(type)
147 if(type->isA(Runtime::_tc_string))
149 void **tmp=(void **)data;
150 StringOnHeap *cpy=((StringOnHeap *)(*tmp))->deepCopy();
151 memcpy(&_value._s,&cpy,type->getSizeInByteOfAnyReprInSeq());
153 else if(type->isA(Runtime::_tc_double))
154 memcpy(&_value._d,data,type->getSizeInByteOfAnyReprInSeq());
155 else if(type->isA(Runtime::_tc_int))
156 memcpy(&_value._i,data,type->getSizeInByteOfAnyReprInSeq());
157 else if(type->isA(Runtime::_tc_bool))
158 memcpy(&_value._b,data,type->getSizeInByteOfAnyReprInSeq());
161 Any *AtomAny::clone() const
163 return new AtomAny(*this);
166 AtomAny *AtomAny::New(char *val,Deallocator dealloc)
168 return new AtomAny(val,dealloc);
171 AnyPtr AtomAny::operator[](int i) const throw(YACS::Exception)
173 throw InvalidExtractionException(_type->kind(),Sequence);
176 AnyPtr AtomAny::operator[](const char *key) const throw(YACS::Exception)
178 throw Exception("AtomAny::operator[] : try to get a part of a partitionned data whereas atomical.");
181 bool AtomAny::operator ==(const Any& other) const
183 if(!_type->isA(other.getType()))
185 const AtomAny& otherC=(const AtomAny&) other;//cast granted due to previous lines
186 if(_type->isA(Runtime::_tc_double))
187 return _value._d==otherC._value._d;
188 else if(_type->isA(Runtime::_tc_int))
189 return _value._i==otherC._value._i;
190 else if(_type->isA(Runtime::_tc_bool))
191 return _value._b==otherC._value._b;
192 else if(_type->isA(Runtime::_tc_string))
193 return (*_value._s)==*(otherC._value._s);
198 int AtomAny::getIntValue() const throw(YACS::Exception)
200 if(_type->isA(Runtime::_tc_int))
203 throw Exception("Value is not an int");
206 bool AtomAny::getBoolValue() const throw(YACS::Exception)
208 if(_type->isA(Runtime::_tc_bool))
211 throw Exception("Value is not a bool");
214 double AtomAny::getDoubleValue() const throw(YACS::Exception)
216 if(_type->isA(Runtime::_tc_double))
219 throw Exception("Value is not a double");
222 std::string AtomAny::getStringValue() const throw(YACS::Exception)
224 if(_type->isA(Runtime::_tc_string))
225 return string(_value._s->cStr());
227 throw Exception("Value is not a string");
231 * \note : This method put in data its zipped recursive content in data.
232 * The ownership of the recursive content is tranfered to data.
233 * So this owns nothing and its counter fall by 1.
234 * For memory space minimal use, not all of '*this' is pushed at data location.
235 * \param data : already allocated memory zone where to put compressed content of 'this'
237 void AtomAny::putMyReprAtPlace(char *data) const
239 if(_type->isA(Runtime::_tc_string))
241 StringOnHeap *tmp=_value._s->deepCopy();
242 memcpy(data,&tmp,_type->getSizeInByteOfAnyReprInSeq());
244 else if(_type->isA(Runtime::_tc_double))
245 memcpy(data,&_value._d,_type->getSizeInByteOfAnyReprInSeq());
246 else if(_type->isA(Runtime::_tc_int))
247 memcpy(data,&_value._i,_type->getSizeInByteOfAnyReprInSeq());
248 else if(_type->isA(Runtime::_tc_bool))
249 memcpy(data,&_value._b,_type->getSizeInByteOfAnyReprInSeq());
253 * \note : This method put in data its zipped recursive content in data.
254 * The ownership of the recursive content is tranfered to data.
255 * So this owns nothing and its counter fall by 1.
256 * For memory space minimal use, not all of '*this' is pushed at data location.
257 * 'deepCpy' param is not used here because by definition of AtomAny deep copy is performed.
258 * \param data : already allocated memory zone where to put compressed content of 'this'
263 void AtomAny::putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy)
265 if(type->isA(Runtime::_tc_string))
267 void **tmp1=(void **)src;
268 StringOnHeap *tmp=((const StringOnHeap *)(*tmp1))->deepCopy();
269 memcpy(data,&tmp,type->getSizeInByteOfAnyReprInSeq());
271 else if(type->isA(Runtime::_tc_double))
272 memcpy(data,src,type->getSizeInByteOfAnyReprInSeq());
273 else if(type->isA(Runtime::_tc_int))
274 memcpy(data,src,type->getSizeInByteOfAnyReprInSeq());
275 else if(type->isA(Runtime::_tc_bool))
276 memcpy(data,src,type->getSizeInByteOfAnyReprInSeq());
280 * \note : Opposite method of putMyReprAtPlace. But static because due to data compression
283 void AtomAny::destroyReprAtPlace(char *data, const TypeCode *type)
285 DynType typ=type->kind();
288 if(!Any::IsNull(data))
290 void **tmp=(void **)data;
291 delete ((StringOnHeap *)(*tmp));
296 AnyPtr AtomAny::getOrBuildFromData(char *data, const TypeCode *type)
299 ret=new AtomAny(data,(TypeCode *)type);
303 bool AtomAny::takeInChargeStorageOf(TypeCode *type)
305 DynType typ=type->kind();
306 return (typ==Double || typ==Int || typ==Bool || typ==String);
311 if(_type->kind() == String)
315 ComposedAny::ComposedAny(const ComposedAny& other):Any(other)
319 ComposedAny::ComposedAny(TypeCode* type, bool isNew):Any(type)
325 AnyPtr ComposedAny::operator[](const char *key) const throw(YACS::Exception)
327 throw Exception("AtomAny::operator[] : try to get a part of a partitionned data not localizable by a string.");
330 void ComposedAny::checkTypeOf(const Any *elem) const throw(YACS::Exception)
332 if(!elem->getType()->isA(_type->contentType()))
333 throw Exception("ComposedAny::checkTypeOf : invalid type.");
336 int ComposedAny::getIntValue() const throw(YACS::Exception)
338 throw InvalidExtractionException(_type->kind(),Runtime::_tc_int->kind());
341 bool ComposedAny::getBoolValue() const throw(YACS::Exception)
343 throw InvalidExtractionException(_type->kind(),Runtime::_tc_bool->kind());
346 double ComposedAny::getDoubleValue() const throw(YACS::Exception)
348 throw InvalidExtractionException(_type->kind(),Runtime::_tc_double->kind());
351 std::string ComposedAny::getStringValue() const throw(YACS::Exception)
353 throw InvalidExtractionException(_type->kind(),Runtime::_tc_string->kind());
356 SeqAlloc::SeqAlloc(const SeqAlloc& other):_sizeOf1Elm(other._sizeOf1Elm),_notStdDeAlloc(0),
357 _start(0),_finish(0),_endOfStorage(0)
359 _start=allocate(other._finish-other._start);
360 _finish=_start+(other._finish-other._start);
361 _endOfStorage=_finish;
364 SeqAlloc::SeqAlloc(unsigned int sizeOf1Elm):_sizeOf1Elm(sizeOf1Elm),_notStdDeAlloc(0),
365 _start(0),_finish(0),_endOfStorage(0)
369 SeqAlloc::~SeqAlloc()
374 void SeqAlloc::clear()
383 * \note : This method is exclusively reserved for arrays of C++ built-in types because no
384 * constructor is applied atomically.
386 void SeqAlloc::initCoarseMemory(char *mem, unsigned int size, Deallocator dealloc)
388 unsigned sizeInByte=size*_sizeOf1Elm;
391 _notStdDeAlloc=dealloc;
396 _start=allocate(sizeInByte);
398 memcpy(_start,mem,sizeInByte);
401 for(unsigned int i=0;i<sizeInByte;i++) _start[i]=DFT_CHAR_VAR;// see getSetItems
404 _finish=_start+sizeInByte;
405 _endOfStorage=_finish;
408 void SeqAlloc::construct(char *pt, const Any *val)
410 val->putMyReprAtPlace(pt);
414 * \note: This performs the placement new or zip info into pt.
416 * \param val : the source from which the construction will be performed.
418 * \param deepCpy : If true in pt place a deep copy pointed by val will be put.
420 void SeqAlloc::construct(char *pt, const char *val, const TypeCode *tc, bool deepCpy)
422 tc->putReprAtPlace(pt,val,deepCpy);
425 char *SeqAlloc::allocate(unsigned int nbOfByte)
428 return (char *)::operator new(nbOfByte);
433 // pt is not permitted to be a null pointer.
434 void SeqAlloc::deallocate(char *pt)
439 ::operator delete(pt);
448 void SeqAlloc::destroy(char *pt, const TypeCode *tc)
450 tc->destroyZippedAny(pt);
453 unsigned int SeqAlloc::size() const
455 return (_finish-_start)/_sizeOf1Elm;
458 std::vector<unsigned int> SeqAlloc::getSetItems() const
460 std::vector<unsigned int> ret;
461 unsigned int sz(size());
462 for(unsigned int i=0;i<sz;i++)
464 const char *pt(_start+i*_sizeOf1Elm);
465 for(unsigned j=0;j<_sizeOf1Elm && *pt==DFT_CHAR_VAR;j++,pt++); //see initCoarseMemory
466 if(pt!=_start+(i+1)*_sizeOf1Elm)
472 void SequenceAny::clear()
474 for (char *cur=_alloc._start;cur!=_alloc._finish;cur+=_alloc._sizeOf1Elm)
475 _alloc.destroy(cur,_type->contentType());
479 void SequenceAny::popBack()
481 _alloc._finish-=_alloc._sizeOf1Elm;
482 _alloc.destroy(_alloc._finish,_type->contentType());
485 void SequenceAny::pushBack(const Any* elem)
487 if(!elem->_type->isA(_type->contentType()))
488 throw InvalidExtractionException(elem->_type->kind(),_type->contentType()->kind());
489 if(_alloc._finish != _alloc._endOfStorage)
491 _alloc.construct(_alloc._finish, elem);
492 _alloc._finish+=_alloc._sizeOf1Elm;
495 realloc(_alloc._finish, elem);
498 bool SequenceAny::operator ==(const Any& other) const
500 if(!_type->isA(other.getType()))
502 const SequenceAny& otherC=(const SequenceAny&) other;//cast granted due to previous lines
503 if(size()!=otherC.size())
505 for(unsigned i=0;i<size();i++)
506 if(!((*(*this)[i])==(*otherC[i])))
511 void SequenceAny::setEltAtRank(int i, const Any *elem) throw(YACS::Exception)
514 _alloc.destroy(_alloc._start+i*_alloc._sizeOf1Elm,_type->contentType());
515 _alloc.construct(_alloc._start+i*_alloc._sizeOf1Elm,elem);
518 AnyPtr SequenceAny::operator[](int i) const throw(YACS::Exception)
520 return _type->contentType()->getOrBuildAnyFromZippedData(_alloc._start+i*_alloc._sizeOf1Elm);
524 * \note : Contrary to AtomAny 'this' (ref) is put in data NOT a deep copy.
525 * \param data : already allocated memory zone where to put address of 'this'
527 void SequenceAny::putMyReprAtPlace(char *data) const
529 const void *tmp=(const void *)this;
530 memcpy(data,&tmp,_type->getSizeInByteOfAnyReprInSeq());
531 const void **tmp2=(const void **) data;
532 ((SequenceAny *)(*tmp2))->incrRef();
533 //::new((SequenceAny *)data) SequenceAny((SequenceAny&) (*this));
536 void SequenceAny::putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy)
538 void **tmp2=(void **) src;
541 ((SequenceAny *)(*tmp2))->incrRef();
542 memcpy(data,src,type->getSizeInByteOfAnyReprInSeq());
546 SequenceAny *cpy=new SequenceAny(*((SequenceAny *)(*tmp2)));
547 memcpy(data,&cpy,type->getSizeInByteOfAnyReprInSeq());
549 //::new((SequenceAny *)data) SequenceAny((SequenceAny&) (*this));
552 void SequenceAny::destroyReprAtPlace(char *data, const TypeCode *type)
554 void **tmp=(void **) data;
555 if(!Any::IsNull(data))
556 ((SequenceAny *)(*tmp))->decrRef();
557 //((SequenceAny *)data)->~SequenceAny();
560 AnyPtr SequenceAny::getOrBuildFromData(char *data, const TypeCode *type)
562 void **tmp=(void **) data;
563 ((SequenceAny *) (*tmp))->incrRef();
564 return AnyPtr((SequenceAny *)(*tmp));
567 Any *SequenceAny::clone() const
569 return new SequenceAny(*this);
572 SequenceAny *SequenceAny::removeUnsetItemsFromThis() const
574 std::vector<unsigned int> its(getSetItems());
575 std::size_t sz(its.size());
576 SequenceAny *ret(SequenceAny::New(getType()->contentType(),sz));
577 for(std::size_t i=0;i<sz;i++)
579 AnyPtr obj((*this)[its[i]]);
580 ret->setEltAtRank(i,obj);
585 SequenceAny *SequenceAny::New(const TypeCode *typeOfContent)
587 if(typeOfContent->kind() == Objref)
589 //In case of Objref, use a sequence of string
590 return new SequenceAny(Runtime::_tc_string);
593 return new SequenceAny(typeOfContent);
596 SequenceAny *SequenceAny::New(const TypeCode *typeOfContent, unsigned lgth)
598 if(typeOfContent->kind() == Objref)
600 //In case of Objref, use a sequence of string
601 return new SequenceAny(Runtime::_tc_string,lgth);
604 return new SequenceAny(typeOfContent,lgth);
607 bool SequenceAny::takeInChargeStorageOf(TypeCode *type)
609 DynType typ=type->kind();
610 return (typ==Sequence);
613 SequenceAny::SequenceAny(const SequenceAny& other):ComposedAny(other),_alloc(other._alloc)
615 const char *srcCur=other._alloc._start;
616 for(char *cur=_alloc._start;srcCur != other._alloc._finish; srcCur+=_alloc._sizeOf1Elm, cur+=_alloc._sizeOf1Elm)
617 _alloc.construct(cur, srcCur, _type->contentType(),true);
620 SequenceAny::~SequenceAny()
622 for (char *cur=_alloc._start;cur!=_alloc._finish;cur+=_alloc._sizeOf1Elm)
623 _alloc.destroy(cur,_type->contentType());
627 * \param typeOfContent : typeCode of the type of elements stored in sequence.
629 SequenceAny::SequenceAny(const TypeCode *typeOfContent):ComposedAny(new TypeCodeSeq("","",typeOfContent)),
630 _alloc(typeOfContent->getSizeInByteOfAnyReprInSeq())
634 SequenceAny::SequenceAny(const TypeCode *typeOfContent, unsigned lgth):ComposedAny(new TypeCodeSeq("","",typeOfContent)),
635 _alloc(typeOfContent->getSizeInByteOfAnyReprInSeq())
637 _alloc.initCoarseMemory(0,lgth,0);
640 SequenceAny::SequenceAny(int *val, unsigned int lgth, Deallocator deAlloc):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_int)),
641 _alloc(Runtime::_tc_int->getSizeInByteOfAnyReprInSeq())
643 _alloc.initCoarseMemory((char *)val,lgth,deAlloc);
646 SequenceAny::SequenceAny(bool *val, unsigned int lgth, Deallocator deAlloc):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_bool)),
647 _alloc(Runtime::_tc_bool->getSizeInByteOfAnyReprInSeq())
649 _alloc.initCoarseMemory((char *)val,lgth,deAlloc);
652 SequenceAny::SequenceAny(double *val, unsigned int lgth, Deallocator deAlloc):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_double)),
653 _alloc(Runtime::_tc_double->getSizeInByteOfAnyReprInSeq())
655 _alloc.initCoarseMemory((char *)val,lgth,deAlloc);
658 SequenceAny::SequenceAny(const std::vector<int>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_int)),
659 _alloc(Runtime::_tc_int->getSizeInByteOfAnyReprInSeq())
661 _alloc.initCoarseMemory((char *)&val[0],val.size(),0);
664 SequenceAny::SequenceAny(const std::vector<bool>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_bool)),
665 _alloc(Runtime::_tc_bool->getSizeInByteOfAnyReprInSeq())
667 for(vector<bool>::const_iterator iter=val.begin();iter!=val.end();iter++)
669 AtomAnyPtr tmp=AtomAny::New(*iter);
674 SequenceAny::SequenceAny(const std::vector<double>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_double)),
675 _alloc(Runtime::_tc_double->getSizeInByteOfAnyReprInSeq())
677 _alloc.initCoarseMemory((char *)&val[0],val.size(),0);
680 SequenceAny::SequenceAny(const std::vector<std::string>& val):ComposedAny(new TypeCodeSeq("","",Runtime::_tc_string)),
681 _alloc(Runtime::_tc_string->getSizeInByteOfAnyReprInSeq())
683 for(vector<string>::const_iterator iter=val.begin();iter!=val.end();iter++)
685 AtomAnyPtr tmp=AtomAny::New(*iter);
690 void SequenceAny::realloc(char *endOfCurrentAllocated, const Any *elem)
692 unsigned int oldSize=_alloc._finish-_alloc._start;
693 unsigned int newSize = oldSize != 0 ? 2 * oldSize : _alloc._sizeOf1Elm;
694 char *newStart=_alloc.allocate(newSize);
696 char *newFinish=performCpy(_alloc._start, endOfCurrentAllocated,newStart);
697 _alloc.construct(newFinish, elem);
698 newFinish+=_alloc._sizeOf1Elm;
699 newFinish=performCpy(endOfCurrentAllocated, _alloc._finish, newFinish);
701 for (char *cur=_alloc._start;cur!=_alloc._finish;cur+=_alloc._sizeOf1Elm)
702 _alloc.destroy(cur,_type->contentType());
703 _alloc.deallocate(_alloc._start);
704 _alloc._start = newStart;
705 _alloc._finish = newFinish;
706 _alloc._endOfStorage=newStart+newSize;
709 char *SequenceAny::performCpy(char *srcStart, char *srcFinish, char *destStart)
712 for (;srcStart != srcFinish; srcStart+=_alloc._sizeOf1Elm, cur+=_alloc._sizeOf1Elm)
713 _alloc.construct(cur, srcStart, _type->contentType(),false);
717 ArrayAny::~ArrayAny()
719 const TypeCode *subType=_type->contentType();
720 unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
721 unsigned int size=((TypeCodeArray *)_type)->getStaticLgth();
723 for(unsigned i=0;i<size;i++,tmp+=sizePerContent)
724 subType->destroyZippedAny(tmp);
728 ArrayAny::ArrayAny(const TypeCode *typeOfContent, unsigned int lgth):ComposedAny(new TypeCodeArray("","",typeOfContent,lgth))
730 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
731 for(unsigned int i=0;i<_type->getSizeInByteOfAnyReprInSeq();i++)
732 _data[i]=SeqAlloc::DFT_CHAR_VAR;
735 ArrayAny::ArrayAny(char *data, TypeCodeArray * type):ComposedAny(type,false),_data(0)
737 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
738 const TypeCode *subType=_type->contentType();
739 unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
740 for(unsigned i=0;i<type->getStaticLgth();i++)
741 subType->putReprAtPlace(_data+i*sizePerContent,data+i*sizePerContent,false);
744 ArrayAny::ArrayAny(const ArrayAny& other):ComposedAny(other)
746 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
747 const TypeCode *subType=_type->contentType();
748 unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
749 for(unsigned i=0;i<((TypeCodeArray *)_type)->getStaticLgth();i++)
750 subType->putReprAtPlace(_data+i*sizePerContent,other._data+i*sizePerContent,true);
753 ArrayAny::ArrayAny(const int *val, unsigned int lgth):ComposedAny(new TypeCodeArray("","",Runtime::_tc_int,lgth)),
756 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
757 memcpy(_data,val,_type->getSizeInByteOfAnyReprInSeq());
760 ArrayAny::ArrayAny(const bool *val, unsigned int lgth):ComposedAny(new TypeCodeArray("","",Runtime::_tc_bool,lgth)),
763 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
764 memcpy(_data,val,_type->getSizeInByteOfAnyReprInSeq());
767 ArrayAny::ArrayAny(const double *val, unsigned int lgth):ComposedAny(new TypeCodeArray("","",Runtime::_tc_double,lgth)),
770 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
771 memcpy(_data,val,_type->getSizeInByteOfAnyReprInSeq());
774 ArrayAny::ArrayAny(const std::vector<int>& val):ComposedAny(new TypeCodeArray("","",Runtime::_tc_int,val.size())),
777 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
778 memcpy(_data,&val[0],_type->getSizeInByteOfAnyReprInSeq());
781 ArrayAny::ArrayAny(const std::vector<double>& val):ComposedAny(new TypeCodeArray("","",Runtime::_tc_double,val.size())),
784 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
785 memcpy(_data,&val[0],_type->getSizeInByteOfAnyReprInSeq());
788 ArrayAny::ArrayAny(const std::vector<std::string>& val):ComposedAny(new TypeCodeArray("","",Runtime::_tc_string,val.size())),
791 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
793 const TypeCode *subType=_type->contentType();
794 unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
795 for(vector<std::string>::const_iterator iter=val.begin();iter!=val.end();iter++,i++)
797 StringOnHeap *st=new StringOnHeap(*iter);
798 memcpy(_data+i*sizePerContent,&st,sizePerContent);
802 void ArrayAny::setEltAtRank(int i, const Any *elem) throw(YACS::Exception)
805 const TypeCode *subType=_type->contentType();
806 subType->destroyZippedAny(_data+i*subType->getSizeInByteOfAnyReprInSeq());
807 elem->putMyReprAtPlace(_data+i*subType->getSizeInByteOfAnyReprInSeq());
810 bool ArrayAny::operator ==(const Any& other) const
812 if(!_type->isA(other.getType()))
814 const ArrayAny& otherC=(const ArrayAny&) other;//cast granted due to previous lines
815 for(unsigned i=0;i<((const TypeCodeArray *)_type)->getStaticLgth();i++)
816 if(!((*(*this)[i])==(*otherC[i])))
821 AnyPtr ArrayAny::operator[](int i) const throw(YACS::Exception)
823 const TypeCode *subType=_type->contentType();
824 unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
825 if(i<0 || i>=((TypeCodeArray *)_type)->getStaticLgth())
826 throw Exception("Trying to access to an invalid index in an Any Tuple");
827 return _type->contentType()->getOrBuildAnyFromZippedData(_data+i*sizePerContent);
830 unsigned int ArrayAny::size() const
832 return ((TypeCodeArray *)_type)->getStaticLgth();
835 Any *ArrayAny::clone() const
837 return new ArrayAny(*this);
840 ArrayAny *ArrayAny::New(const TypeCode *typeOfContent, unsigned int lgth)
842 return new ArrayAny(typeOfContent,lgth);
845 void ArrayAny::putMyReprAtPlace(char *data) const
847 const TypeCode *subType=_type->contentType();
848 unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
849 for(unsigned i=0;i<((const TypeCodeArray *)_type)->getStaticLgth();i++)
850 subType->putReprAtPlace(data+i*sizePerContent,_data+i*sizePerContent,false);
853 void ArrayAny::putReprAtPlace(char *data, const char *src, const TypeCodeArray *type, bool deepCpy)
855 const TypeCode *subType=type->contentType();
856 unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
857 for(unsigned i=0;i<type->getStaticLgth();i++)
858 subType->putReprAtPlace(data+i*sizePerContent,src+i*sizePerContent,deepCpy);
861 void ArrayAny::destroyReprAtPlace(char *data, const TypeCodeArray *type)
863 const TypeCode *subType=type->contentType();
864 unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
865 for(unsigned i=0;i<type->getStaticLgth();i++)
866 subType->destroyZippedAny(data+i*sizePerContent);
869 AnyPtr ArrayAny::getOrBuildFromData(char *data, const TypeCodeArray *type)
872 ret=new ArrayAny(data,(TypeCodeArray *)type);
876 bool ArrayAny::takeInChargeStorageOf(TypeCode *type)
878 DynType typ=type->kind();
882 Any *StructAny::clone() const
884 return new StructAny(*this);
887 bool StructAny::operator ==(const Any& other) const
889 if(!_type->isA(other.getType()))
891 const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
892 vector< pair<string,TypeCode*> >::const_iterator iter;
893 for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
894 if(!((*(*this)[(*iter).first.c_str()]==(*other[(*iter).first.c_str()]))))
899 AnyPtr StructAny::operator[](int i) const throw(YACS::Exception)
901 const char what[]="StructAny::operator[](int i) : Struct key are strings not integers.";
902 throw Exception(what);
905 AnyPtr StructAny::operator[](const char *key) const throw(YACS::Exception)
907 const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
908 char *whereToGet=_data;
909 vector< pair<string,TypeCode*> >::const_iterator iter;
910 for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
911 if((*iter).first!=key)
912 whereToGet+=(*iter).second->getSizeInByteOfAnyReprInSeq();
915 if(iter==typeC->_members.end())
917 string what("Unexisting key \""); what+=key; what+="\" for struct extraction.";
918 throw Exception(what);
920 return (*iter).second->getOrBuildAnyFromZippedData(whereToGet);
923 void StructAny::setEltAtRank(int i, const Any *elem) throw(YACS::Exception)
925 const char what[]="Struct key are strings not integers.";
926 throw Exception(what);
929 void StructAny::setEltAtRank(const char *key, const Any *elem) throw(YACS::Exception)
931 const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
933 const TypeCode *tcOnKey=typeC->getMember(key,offset);
935 throw Exception("StructAny::setEltAtRank : invalid key given.");
936 if(!elem->getType()->isA(tcOnKey))
937 throw Exception("StructAny::setEltAtRank : invalid data type on the specified given key.");
938 tcOnKey->destroyZippedAny(_data+offset);
939 elem->putMyReprAtPlace(_data+offset);
942 void StructAny::putMyReprAtPlace(char *data) const
944 const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
946 vector< pair<string,TypeCode*> >::const_iterator iter;
947 for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
949 (*iter).second->putReprAtPlace(data+offset,_data+offset,false);
950 offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
954 void StructAny::putReprAtPlace(char *data, const char *src, const TypeCodeStruct *type, bool deepCpy)
957 vector< pair<string,TypeCode*> >::const_iterator iter;
958 for(iter=type->_members.begin();iter!=type->_members.end();iter++)
960 (*iter).second->putReprAtPlace(data+offset,src+offset,deepCpy);
961 offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
965 void StructAny::destroyReprAtPlace(char *data, const TypeCodeStruct *type)
967 char *whereToGet=data;
968 vector< pair<string,TypeCode*> >::const_iterator iter;
969 for(iter=type->_members.begin();iter!=type->_members.end();iter++)
971 (*iter).second->destroyZippedAny(whereToGet);
972 whereToGet+=(*iter).second->getSizeInByteOfAnyReprInSeq();
976 AnyPtr StructAny::getOrBuildFromData(char *data, const TypeCodeStruct *type)
979 ret=new StructAny(data,(TypeCodeStruct *)type);
983 StructAny::~StructAny()
985 const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
986 vector< pair<string,TypeCode*> >::const_iterator iter;
987 char *whereToGet=_data;
988 for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
990 (*iter).second->destroyZippedAny(whereToGet);
991 whereToGet+=(*iter).second->getSizeInByteOfAnyReprInSeq();
996 StructAny::StructAny(TypeCodeStruct *type):ComposedAny(type,false)
998 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
999 for(unsigned int i=0;i<_type->getSizeInByteOfAnyReprInSeq();i++)
1000 _data[i]=SeqAlloc::DFT_CHAR_VAR;
1003 StructAny::StructAny(const StructAny& other):ComposedAny(other)
1005 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
1006 const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
1007 vector< pair<string,TypeCode*> >::const_iterator iter;
1009 for(iter=typeC->_members.begin();iter!=typeC->_members.end();iter++)
1011 (*iter).second->putReprAtPlace(_data+offset,other._data+offset,true);
1012 offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
1016 StructAny::StructAny(char *data, TypeCodeStruct * type):ComposedAny(type,false),_data(0)
1018 _data=new char[_type->getSizeInByteOfAnyReprInSeq()];
1019 vector< pair<string,TypeCode*> >::const_iterator iter;
1021 for(iter=type->_members.begin();iter!=type->_members.end();iter++)
1023 (*iter).second->putReprAtPlace(_data+offset,data+offset,false);
1024 offset+=(*iter).second->getSizeInByteOfAnyReprInSeq();
1028 StructAny *StructAny::New(TypeCodeStruct *type)
1030 return new StructAny(type);