-// Copyright (C) 2006-2008 CEA/DEN, EDF R&D
+// Copyright (C) 2006-2016 CEA/DEN, EDF R&D
//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
//
-// This library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
+
#include "Any.hxx"
#include "Runtime.hxx"
#include "TypeCode.hxx"
using namespace YACS::ENGINE;
using namespace std;
+// forbidden value int=-269488145 double=-1.54947e+231 bool=239
+const char SeqAlloc::DFT_CHAR_VAR=-17;//0xEF
+
StringOnHeap::StringOnHeap(const char *val):_dealloc(0),_str(strdup(val))
{
}
}
/*!
- * \Note : no copy is performed if a deallocator is given.
+ * \note : no copy is performed if a deallocator is given.
* \param val : String in C format that is NOT copied if
* deAlloc != 0
* \param deAlloc : pointer on function to deallocate val after
_type->decrRef();
}
+bool Any::IsNull(char *data)
+{
+ if(!data)
+ return true;
+ bool isNull(true);
+ for(std::size_t i=0;i<sizeof(void *) && isNull;i++)
+ isNull=(data[i]==SeqAlloc::DFT_CHAR_VAR);
+ return isNull;
+}
+
AtomAny::AtomAny(int val):Any(Runtime::_tc_int)
{
_value._i=val;
return new AtomAny(val,dealloc);
}
-AnyPtr AtomAny::operator[](int i) const throw(Exception)
+AnyPtr AtomAny::operator[](int i) const throw(YACS::Exception)
{
throw InvalidExtractionException(_type->kind(),Sequence);
}
-AnyPtr AtomAny::operator[](const char *key) const throw(Exception)
+AnyPtr AtomAny::operator[](const char *key) const throw(YACS::Exception)
{
throw Exception("AtomAny::operator[] : try to get a part of a partitionned data whereas atomical.");
}
return false;
}
-int AtomAny::getIntValue() const throw(Exception)
+int AtomAny::getIntValue() const throw(YACS::Exception)
{
if(_type->isA(Runtime::_tc_int))
return _value._i;
throw Exception("Value is not an int");
}
-bool AtomAny::getBoolValue() const throw(Exception)
+bool AtomAny::getBoolValue() const throw(YACS::Exception)
{
if(_type->isA(Runtime::_tc_bool))
return _value._b;
throw Exception("Value is not a bool");
}
-double AtomAny::getDoubleValue() const throw(Exception)
+double AtomAny::getDoubleValue() const throw(YACS::Exception)
{
if(_type->isA(Runtime::_tc_double))
return _value._d;
throw Exception("Value is not a double");
}
-std::string AtomAny::getStringValue() const throw(Exception)
+std::string AtomAny::getStringValue() const throw(YACS::Exception)
{
if(_type->isA(Runtime::_tc_string))
return string(_value._s->cStr());
* For memory space minimal use, not all of '*this' is pushed at data location.
* 'deepCpy' param is not used here because by definition of AtomAny deep copy is performed.
* \param data : already allocated memory zone where to put compressed content of 'this'
+ * \param src :
+ * \param type :
+ * \param deepCpy :
*/
void AtomAny::putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy)
{
DynType typ=type->kind();
if(typ==String)
{
- void **tmp=(void **)data;
- delete ((StringOnHeap *)(*tmp));
+ if(!Any::IsNull(data))
+ {
+ void **tmp=(void **)data;
+ delete ((StringOnHeap *)(*tmp));
+ }
}
}
AtomAny::~AtomAny()
{
- if(_type->isA(Runtime::_tc_string))
+ if(_type->kind() == String)
delete _value._s;
}
_type->decrRef();
}
-AnyPtr ComposedAny::operator[](const char *key) const throw(Exception)
+AnyPtr ComposedAny::operator[](const char *key) const throw(YACS::Exception)
{
throw Exception("AtomAny::operator[] : try to get a part of a partitionned data not localizable by a string.");
}
-void ComposedAny::checkTypeOf(const Any *elem) const throw(Exception)
+void ComposedAny::checkTypeOf(const Any *elem) const throw(YACS::Exception)
{
if(!elem->getType()->isA(_type->contentType()))
throw Exception("ComposedAny::checkTypeOf : invalid type.");
}
-int ComposedAny::getIntValue() const throw(Exception)
+int ComposedAny::getIntValue() const throw(YACS::Exception)
{
throw InvalidExtractionException(_type->kind(),Runtime::_tc_int->kind());
}
-bool ComposedAny::getBoolValue() const throw(Exception)
+bool ComposedAny::getBoolValue() const throw(YACS::Exception)
{
throw InvalidExtractionException(_type->kind(),Runtime::_tc_bool->kind());
}
-double ComposedAny::getDoubleValue() const throw(Exception)
+double ComposedAny::getDoubleValue() const throw(YACS::Exception)
{
throw InvalidExtractionException(_type->kind(),Runtime::_tc_double->kind());
}
-std::string ComposedAny::getStringValue() const throw(Exception)
+std::string ComposedAny::getStringValue() const throw(YACS::Exception)
{
throw InvalidExtractionException(_type->kind(),Runtime::_tc_string->kind());
}
memcpy(_start,mem,sizeInByte);
else
{
- for(unsigned int i=0;i<sizeInByte;i++) _start[i]=0;
+ for(unsigned int i=0;i<sizeInByte;i++) _start[i]=DFT_CHAR_VAR;// see getSetItems
}
}
_finish=_start+sizeInByte;
/*!
* \note: This performs the placement new or zip info into pt.
+ * \param pt :
* \param val : the source from which the construction will be performed.
+ * \param tc :
* \param deepCpy : If true in pt place a deep copy pointed by val will be put.
*/
void SeqAlloc::construct(char *pt, const char *val, const TypeCode *tc, bool deepCpy)
return (_finish-_start)/_sizeOf1Elm;
}
+std::vector<unsigned int> SeqAlloc::getSetItems() const
+{
+ std::vector<unsigned int> ret;
+ unsigned int sz(size());
+ for(unsigned int i=0;i<sz;i++)
+ {
+ const char *pt(_start+i*_sizeOf1Elm);
+ for(unsigned j=0;j<_sizeOf1Elm && *pt==DFT_CHAR_VAR;j++,pt++); //see initCoarseMemory
+ if(pt!=_start+(i+1)*_sizeOf1Elm)
+ ret.push_back(i);
+ }
+ return ret;
+}
+
void SequenceAny::clear()
{
for (char *cur=_alloc._start;cur!=_alloc._finish;cur+=_alloc._sizeOf1Elm)
return true;
}
-void SequenceAny::setEltAtRank(int i, const Any *elem) throw(Exception)
+void SequenceAny::setEltAtRank(int i, const Any *elem) throw(YACS::Exception)
{
checkTypeOf(elem);
_alloc.destroy(_alloc._start+i*_alloc._sizeOf1Elm,_type->contentType());
_alloc.construct(_alloc._start+i*_alloc._sizeOf1Elm,elem);
}
-AnyPtr SequenceAny::operator[](int i) const throw(Exception)
+AnyPtr SequenceAny::operator[](int i) const throw(YACS::Exception)
{
return _type->contentType()->getOrBuildAnyFromZippedData(_alloc._start+i*_alloc._sizeOf1Elm);
}
void SequenceAny::destroyReprAtPlace(char *data, const TypeCode *type)
{
void **tmp=(void **) data;
- if(*tmp)
+ if(!Any::IsNull(data))
((SequenceAny *)(*tmp))->decrRef();
//((SequenceAny *)data)->~SequenceAny();
}
return new SequenceAny(*this);
}
+SequenceAny *SequenceAny::removeUnsetItemsFromThis() const
+{
+ std::vector<unsigned int> its(getSetItems());
+ std::size_t sz(its.size());
+ SequenceAny *ret(SequenceAny::New(getType()->contentType(),sz));
+ for(std::size_t i=0;i<sz;i++)
+ {
+ AnyPtr obj((*this)[its[i]]);
+ ret->setEltAtRank(i,obj);
+ }
+ return ret;
+}
+
SequenceAny *SequenceAny::New(const TypeCode *typeOfContent)
{
if(typeOfContent->kind() == Objref)
{
_data=new char[_type->getSizeInByteOfAnyReprInSeq()];
for(unsigned int i=0;i<_type->getSizeInByteOfAnyReprInSeq();i++)
- _data[i]='\0';
+ _data[i]=SeqAlloc::DFT_CHAR_VAR;
}
ArrayAny::ArrayAny(char *data, TypeCodeArray * type):ComposedAny(type,false),_data(0)
}
}
-void ArrayAny::setEltAtRank(int i, const Any *elem) throw(Exception)
+void ArrayAny::setEltAtRank(int i, const Any *elem) throw(YACS::Exception)
{
checkTypeOf(elem);
const TypeCode *subType=_type->contentType();
return true;
}
-AnyPtr ArrayAny::operator[](int i) const throw(Exception)
+AnyPtr ArrayAny::operator[](int i) const throw(YACS::Exception)
{
const TypeCode *subType=_type->contentType();
unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
return true;
}
-AnyPtr StructAny::operator[](int i) const throw(Exception)
+AnyPtr StructAny::operator[](int i) const throw(YACS::Exception)
{
const char what[]="StructAny::operator[](int i) : Struct key are strings not integers.";
throw Exception(what);
}
-AnyPtr StructAny::operator[](const char *key) const throw(Exception)
+AnyPtr StructAny::operator[](const char *key) const throw(YACS::Exception)
{
const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
char *whereToGet=_data;
return (*iter).second->getOrBuildAnyFromZippedData(whereToGet);
}
-void StructAny::setEltAtRank(int i, const Any *elem) throw(Exception)
+void StructAny::setEltAtRank(int i, const Any *elem) throw(YACS::Exception)
{
const char what[]="Struct key are strings not integers.";
throw Exception(what);
}
-void StructAny::setEltAtRank(const char *key, const Any *elem) throw(Exception)
+void StructAny::setEltAtRank(const char *key, const Any *elem) throw(YACS::Exception)
{
const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
unsigned offset;
{
_data=new char[_type->getSizeInByteOfAnyReprInSeq()];
for(unsigned int i=0;i<_type->getSizeInByteOfAnyReprInSeq();i++)
- _data[i]='\0';
+ _data[i]=SeqAlloc::DFT_CHAR_VAR;
}
StructAny::StructAny(const StructAny& other):ComposedAny(other)