Salome HOME
scotch6.0.4 needs pthread... Quick and dirty solution to be improved
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingRefCountObject.cxx
index e2b93ea4ec628d6d9f284297bf535d9d6c69b8c4..b7e08b45aa54556587edca3bd70a1e51899e9c7e 100644 (file)
@@ -1,9 +1,9 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D
+// Copyright (C) 2007-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.
+// 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
 // Author : Anthony Geay (CEA/DEN)
 
 #include "MEDCouplingRefCountObject.hxx"
-#include "MED_version.h"
+#include "MEDCoupling_version.h"
+
+#include "InterpKernelException.hxx"
 
 #include <sstream>
+#include <algorithm>
+
+using namespace MEDCoupling;
 
-using namespace ParaMEDMEM;
+GlobalDict *GlobalDict::UNIQUE_INSTANCE=0;
 
-const char *ParaMEDMEM::MEDCouplingVersionStr()
+const char *MEDCoupling::MEDCouplingVersionStr()
 {
-  return SALOMEMED_VERSION_STR;
+  return MEDCOUPLING_VERSION_STR;
 }
 
-int ParaMEDMEM::MEDCouplingVersion()
+int MEDCoupling::MEDCouplingVersion()
 {
-  return SALOMEMED_VERSION;
+  return MEDCOUPLING_VERSION;
 }
 
-void ParaMEDMEM::MEDCouplingVersionMajMinRel(int& maj, int& minor, int& releas)
+void MEDCoupling::MEDCouplingVersionMajMinRel(int& maj, int& minor, int& releas)
 {
-  int ver=SALOMEMED_VERSION;
+  int ver=MEDCOUPLING_VERSION;
   maj=(ver & 0xFF0000) >> 16;
   minor=(ver & 0xFF00) >> 8;
   releas=(ver & 0xFF);
 }
 
-int ParaMEDMEM::MEDCouplingSizeOfVoidStar()
+int MEDCoupling::MEDCouplingSizeOfVoidStar()
 {
   return 8*sizeof(std::size_t);
 }
@@ -53,14 +58,14 @@ int ParaMEDMEM::MEDCouplingSizeOfVoidStar()
  * If false it is a BigEndian machine.
  * \return the coding mode of integers of the machine.
  */
-bool ParaMEDMEM::MEDCouplingByteOrder()
+bool MEDCoupling::MEDCouplingByteOrder()
 {
   unsigned int x(1);
   unsigned char *xc(reinterpret_cast<unsigned char *>(&x));
   return xc[0]==1;
 }
 
-const char *ParaMEDMEM::MEDCouplingByteOrderStr()
+const char *MEDCoupling::MEDCouplingByteOrderStr()
 {
   static const char LITTLEENDIAN_STR[]="LittleEndian";
   static const char BIGENDIAN_STR[]="BigEndian";
@@ -70,6 +75,15 @@ const char *ParaMEDMEM::MEDCouplingByteOrderStr()
     return BIGENDIAN_STR;
 }
 
+bool MEDCoupling::IsCXX11Compiled()
+{
+#if __cplusplus >= 201103L
+  return true;
+#else
+  return false;
+#endif
+}
+
 //=
 
 std::size_t BigMemoryObject::getHeapMemorySize() const
@@ -80,6 +94,49 @@ std::size_t BigMemoryObject::getHeapMemorySize() const
   return ret+GetHeapMemoryOfSet(s1,s2);
 }
 
+/*!
+ * This method returns all the progeny of \a this (this is \b not included in returned vector).
+ * All the progeny means all the subobjects (children), subsubobjects (little children), ... of \a this.
+ * The elements in returned array are reported only once even if they appear several times in the progeny of \a this.
+ */
+std::vector<const BigMemoryObject *> BigMemoryObject::getAllTheProgeny() const
+{
+  std::vector<const BigMemoryObject *> s1(getDirectChildren());
+  std::vector<const BigMemoryObject *> ret;
+  while(!s1.empty())
+    {
+      ret.insert(ret.end(),s1.begin(),s1.end());
+      std::vector<const BigMemoryObject *> s3;
+      for(std::vector<const BigMemoryObject *>::const_iterator it0=s1.begin();it0!=s1.end();it0++)
+        {
+          std::vector<const BigMemoryObject *> s2;
+          if(*it0)
+            s2=(*it0)->getDirectChildren();
+          for(std::vector<const BigMemoryObject *>::const_iterator it1=s2.begin();it1!=s2.end();it1++)
+            {
+              if(*it1)
+                if(std::find(ret.begin(),ret.end(),*it1)==ret.end())
+                  s3.push_back(*it1);
+            }
+        }
+      s1=s3;
+    }
+  return ret;
+}
+
+/*!
+ * This method scan all the progeny of \a this (\a this excluded) to see if \a obj is part of it.
+ * If obj is NULL false is returned.
+ * \sa BigMemoryObject::getAllTheProgeny
+ */
+bool BigMemoryObject::isObjectInTheProgeny(const BigMemoryObject *obj) const
+{
+  if(!obj)
+    return false;
+  std::vector<const BigMemoryObject *> objs(getAllTheProgeny());
+  return std::find(objs.begin(),objs.end(),obj)!=objs.end();
+}
+
 std::size_t BigMemoryObject::GetHeapMemorySizeOfObjs(const std::vector<const BigMemoryObject *>& objs)
 {
   std::size_t ret(0);
@@ -161,6 +218,16 @@ std::string BigMemoryObject::getHeapMemorySizeStr() const
   return oss.str();
 }
 
+std::vector<const BigMemoryObject *> BigMemoryObject::getDirectChildren() const
+{
+  std::vector<const BigMemoryObject *> ret;
+  std::vector<const BigMemoryObject *> retWithNull(getDirectChildrenWithNull());
+  for(std::vector<const BigMemoryObject *>::const_iterator it=retWithNull.begin();it!=retWithNull.end();it++)
+    if(*it)
+      ret.push_back(*it);
+  return ret;
+}
+
 BigMemoryObject::~BigMemoryObject()
 {
 }
@@ -219,3 +286,82 @@ RefCountObject::RefCountObject(const RefCountObject& other):RefCountObjectOnly(o
 RefCountObject::~RefCountObject()
 {
 }
+
+//=
+
+GlobalDict *GlobalDict::GetInstance()
+{
+  if(!UNIQUE_INSTANCE)
+    UNIQUE_INSTANCE=new GlobalDict;
+  return UNIQUE_INSTANCE;
+}
+
+bool GlobalDict::hasKey(const std::string& key) const
+{
+  std::map<std::string, std::string>::const_iterator it(_my_map.find(key));
+  return it!=_my_map.end();
+}
+
+std::string GlobalDict::value(const std::string& key) const
+{
+  std::map<std::string, std::string>::const_iterator it(_my_map.find(key));
+  if(it==_my_map.end())
+    {
+      std::ostringstream oss;
+      oss << "GlobalDict::value : key \"" << key << "\" is not in map !";
+      throw INTERP_KERNEL::Exception(oss.str().c_str());
+    }
+  return (*it).second;
+}
+
+std::vector<std::string> GlobalDict::keys() const
+{
+  std::vector<std::string> ret;
+  for(std::map<std::string, std::string>::const_iterator it=_my_map.begin();it!=_my_map.end();it++)
+    ret.push_back((*it).first);
+  return ret;
+}
+
+void GlobalDict::erase(const std::string& key)
+{
+  std::map<std::string, std::string>::iterator it(_my_map.find(key));
+  if(it==_my_map.end())
+    {
+      std::ostringstream oss;
+      oss << "GlobalDict::erase : key \"" << key << "\" is not in map !";
+      throw INTERP_KERNEL::Exception(oss.str().c_str());
+    }
+  _my_map.erase(it);
+}
+
+void GlobalDict::clear()
+{
+  _my_map.clear();
+}
+
+void GlobalDict::setKeyValue(const std::string& key, const std::string& val)
+{
+  std::map<std::string, std::string>::const_iterator it(_my_map.find(key));
+  if(it!=_my_map.end())
+    {
+      std::ostringstream oss;
+      oss << "GlobalDict::setKeyValue : key \"" << key << "\" already exists !";
+      throw INTERP_KERNEL::Exception(oss.str().c_str());
+    }
+  _my_map[key]=val;
+}
+
+void GlobalDict::setKeyValueForce(const std::string& key, const std::string& val)
+{
+  _my_map[key]=val;
+}
+
+std::string GlobalDict::printSelf() const
+{
+  std::ostringstream oss;
+  for(std::map<std::string, std::string>::const_iterator it=_my_map.begin();it!=_my_map.end();it++)
+    {
+      oss << "(" << (*it).first << "," << (*it).second << ")" << std::endl;
+    }
+  return oss.str();
+}