Salome HOME
Useful access method to available resources
[modules/yacs.git] / src / engine / Any.cxx
index af845e2ce6b49b1db847704552583193b503d68d..633f348c4c49035656c230c1f2f4d45bf39c9f70 100644 (file)
@@ -1,13 +1,36 @@
+// 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, 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.
+//
+// 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
+//
+
 #include "Any.hxx"
 #include "Runtime.hxx"
 #include "TypeCode.hxx"
 #include "InvalidExtractionException.hxx"
 
 #include <cstring>
+#include <cstdlib>
 
 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))
 {
 }
@@ -17,7 +40,7 @@ StringOnHeap::StringOnHeap(const std::string& val):_dealloc(0),_str(strdup(val.c
 }
 
 /*! 
- * \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
@@ -64,6 +87,16 @@ Any::~Any()
   _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;
@@ -135,12 +168,12 @@ AtomAny *AtomAny::New(char *val,Deallocator dealloc)
   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.");
 }
@@ -162,36 +195,36 @@ bool AtomAny::operator ==(const Any& other) const
     return false;
 }
 
-int AtomAny::getIntValue() const throw(Exception)
+int AtomAny::getIntValue() const throw(YACS::Exception)
 {
   if(_type->isA(Runtime::_tc_int))
     return _value._i;
   else
-    throw Exception("Value is not an Int");
+    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;
   else
-    throw Exception("Value is not a Bool");
+    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;
   else
-    throw Exception("Value is not a Double");
+    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());
   else
-    throw Exception("Value is not a String");
+    throw Exception("Value is not a string");
 }
 
 /*!
@@ -223,6 +256,9 @@ void AtomAny::putMyReprAtPlace(char *data) const
  *         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)
 {
@@ -249,8 +285,11 @@ void AtomAny::destroyReprAtPlace(char *data, const TypeCode *type)
   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));
+        }
     }
 }
 
@@ -269,7 +308,7 @@ bool AtomAny::takeInChargeStorageOf(TypeCode *type)
 
 AtomAny::~AtomAny()
 {
-  if(_type->isA(Runtime::_tc_string))
+  if(_type->kind() == String)
     delete _value._s;
 }
 
@@ -283,33 +322,33 @@ ComposedAny::ComposedAny(TypeCode* type, bool isNew):Any(type)
     _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());
 }
@@ -359,7 +398,7 @@ void SeqAlloc::initCoarseMemory(char *mem, unsigned int size, Deallocator deallo
         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;
@@ -373,7 +412,9 @@ void SeqAlloc::construct(char *pt, const Any *val)
 
 /*!
  * \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)
@@ -414,6 +455,20 @@ unsigned int SeqAlloc::size() const
   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)
@@ -453,14 +508,14 @@ bool SequenceAny::operator ==(const Any& other) const
   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);
 }
@@ -497,7 +552,7 @@ void SequenceAny::putReprAtPlace(char *data, const char *src, const TypeCode *ty
 void SequenceAny::destroyReprAtPlace(char *data, const TypeCode *type)
 {
   void **tmp=(void **) data;
-  if(*tmp)
+  if(!Any::IsNull(data))
     ((SequenceAny *)(*tmp))->decrRef();
   //((SequenceAny *)data)->~SequenceAny();
 }
@@ -514,6 +569,19 @@ Any *SequenceAny::clone() const
   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)
@@ -661,7 +729,7 @@ ArrayAny::ArrayAny(const TypeCode *typeOfContent, unsigned int lgth):ComposedAny
 {
   _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)
@@ -731,7 +799,7 @@ ArrayAny::ArrayAny(const std::vector<std::string>& val):ComposedAny(new TypeCode
     }
 }
 
-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();
@@ -750,7 +818,7 @@ bool ArrayAny::operator ==(const Any& other) const
   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();
@@ -828,13 +896,13 @@ bool StructAny::operator ==(const Any& other) const
   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;
@@ -852,13 +920,13 @@ AnyPtr StructAny::operator[](const char *key) const throw(Exception)
   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;
@@ -929,7 +997,7 @@ StructAny::StructAny(TypeCodeStruct *type):ComposedAny(type,false)
 {
   _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)