Salome HOME
Unwarningization under Win.
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingRefCountObject.cxx
index c8de20ea996aef5ae3d3f85be783a7c5c690a308..39794d403244ec9746667c94fc3018b66fcf281e 100644 (file)
@@ -21,6 +21,9 @@
 #include "MEDCouplingRefCountObject.hxx"
 #include "MED_version.h"
 
+#include <sstream>
+#include <set>
+
 using namespace ParaMEDMEM;
 
 const char *ParaMEDMEM::MEDCouplingVersionStr()
@@ -46,15 +49,110 @@ int ParaMEDMEM::MEDCouplingSizeOfVoidStar()
   return 8*sizeof(std::size_t);
 }
 
-RefCountObject::RefCountObject():_cnt(1)
+/*!
+ * If true is returned it is a LittleEndian machine.
+ * If false it is a BigEndian machine.
+ * \return the coding mode of integers of the machine.
+ */
+bool ParaMEDMEM::MEDCouplingByteOrder()
+{
+  unsigned int x(1);
+  unsigned char *xc(reinterpret_cast<unsigned char *>(&x));
+  return xc[0]==1;
+}
+
+const char *ParaMEDMEM::MEDCouplingByteOrderStr()
+{
+  static const char LITTLEENDIAN_STR[]="LittleEndian";
+  static const char BIGENDIAN_STR[]="BigEndian";
+  if(MEDCouplingByteOrder())
+    return LITTLEENDIAN_STR;
+  else
+    return BIGENDIAN_STR;
+}
+
+//=
+
+std::size_t BigMemoryObject::getHeapMemorySize() const
+{
+  std::size_t ret(getHeapMemorySizeWithoutChildren());
+  std::vector<const BigMemoryObject *> v(getDirectChildren());
+  std::set<const BigMemoryObject *> s1,s2(v.begin(),v.end());
+  while(!s2.empty())
+    {
+      std::set<const BigMemoryObject *> s3;
+      for(std::set<const BigMemoryObject *>::const_iterator it=s2.begin();it!=s2.end();it++)
+        {
+          if(s1.find(*it)==s1.end())
+            {
+              ret+=(*it)->getHeapMemorySizeWithoutChildren();
+              s1.insert(*it);
+              std::vector<const BigMemoryObject *> v2((*it)->getDirectChildren());
+              for(std::vector<const BigMemoryObject *>::const_iterator it2=v2.begin();it2!=v2.end();it2++)
+                if(s1.find(*it2)==s1.end())
+                  s3.insert(*it2);
+            }
+        }
+      s2=s3;
+    }
+  return ret;
+}
+
+std::string BigMemoryObject::getHeapMemorySizeStr() const
+{
+  static const char *UNITS[4]={"B","kB","MB","GB"};
+  std::size_t m(getHeapMemorySize());
+  std::ostringstream oss; oss.precision(3);
+  std::size_t remain(0);
+  int i(0);
+  for(;i<4;i++)
+    {
+      if(m<1024)
+        {
+          oss << m;
+          if(remain!=0)
+            {
+              std::ostringstream oss2; oss2 << std::fixed << ((double)remain)/1024.;
+              std::string s(oss2.str());
+              s=s.substr(1,4);
+              std::size_t pos(s.find_last_not_of('0'));
+              if(pos==4)
+                oss << s;
+              else
+                oss << s.substr(0,pos+1);
+            }
+          oss << " " << UNITS[i];
+          break;
+        }
+      else
+        {
+          if(i!=3)
+            {
+              remain=(m%1024);
+              m/=1024;
+            }
+        }
+    }
+  if(i==4)
+    oss << m << " " << UNITS[3];
+  return oss.str();
+}
+
+BigMemoryObject::~BigMemoryObject()
+{
+}
+
+//=
+
+RefCountObjectOnly::RefCountObjectOnly():_cnt(1)
 {
 }
 
-RefCountObject::RefCountObject(const RefCountObject& other):_cnt(1)
+RefCountObjectOnly::RefCountObjectOnly(const RefCountObjectOnly& other):_cnt(1)
 {
 }
 
-bool RefCountObject::decrRef() const
+bool RefCountObjectOnly::decrRef() const
 {
   bool ret=((--_cnt)==0);
   if(ret)
@@ -62,16 +160,39 @@ bool RefCountObject::decrRef() const
   return ret;
 }
 
-void RefCountObject::incrRef() const
+void RefCountObjectOnly::incrRef() const
 {
   _cnt++;
 }
 
-int RefCountObject::getRCValue() const
+int RefCountObjectOnly::getRCValue() const
 {
   return _cnt;
 }
 
+RefCountObjectOnly::~RefCountObjectOnly()
+{
+}
+
+/*!
+ * Do nothing here ! It is not a bug ( I hope :) ) because all subclasses that
+ * copies using operator= should not copy the ref counter of \a other !
+ */
+RefCountObjectOnly& RefCountObjectOnly::operator=(const RefCountObjectOnly& other)
+{
+  return *this;
+}
+
+//=
+
+RefCountObject::RefCountObject()
+{
+}
+
+RefCountObject::RefCountObject(const RefCountObject& other):RefCountObjectOnly(other)
+{
+}
+
 RefCountObject::~RefCountObject()
 {
 }