Salome HOME
Merge branch 'V8_5_BR'
[modules/yacs.git] / src / engine / Any.cxx
index 0ca4b843a4d2f1c2047761e45642dd3d527868e3..633f348c4c49035656c230c1f2f4d45bf39c9f70 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2006-2014  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
@@ -28,6 +28,9 @@
 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))
 {
 }
@@ -84,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;
@@ -272,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));
+        }
     }
 }
 
@@ -382,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;
@@ -439,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)
@@ -522,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();
 }
@@ -539,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)
@@ -686,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)
@@ -954,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)