1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #include "MEDCouplingRefCountObject.hxx"
22 #include "MEDCoupling_version.h"
25 #include "InterpKernelException.hxx"
30 using namespace MEDCoupling;
32 GlobalDict *GlobalDict::UNIQUE_INSTANCE=0;
34 const char *MEDCoupling::MEDCouplingVersionStr()
36 return MEDCOUPLING_VERSION_STR;
39 int MEDCoupling::MEDCouplingVersion()
41 return MEDCOUPLING_VERSION;
44 void MEDCoupling::MEDCouplingVersionMajMinRel(int& maj, int& minor, int& releas)
46 int ver=MEDCOUPLING_VERSION;
47 maj=(ver & 0xFF0000) >> 16;
48 minor=(ver & 0xFF00) >> 8;
52 int MEDCoupling::MEDCouplingSizeOfVoidStar()
54 return 8*sizeof(std::size_t);
57 std::size_t MEDCoupling::MEDCouplingSizeOfIDs()
59 return 8*sizeof(mcIdType);
63 * If true is returned it is a LittleEndian machine.
64 * If false it is a BigEndian machine.
65 * \return the coding mode of integers of the machine.
67 bool MEDCoupling::MEDCouplingByteOrder()
70 unsigned char *xc(reinterpret_cast<unsigned char *>(&x));
74 const char *MEDCoupling::MEDCouplingByteOrderStr()
76 static const char LITTLEENDIAN_STR[]="LittleEndian";
77 static const char BIGENDIAN_STR[]="BigEndian";
78 if(MEDCouplingByteOrder())
79 return LITTLEENDIAN_STR;
84 bool MEDCoupling::IsCXX11Compiled()
91 std::size_t BigMemoryObject::getHeapMemorySize() const
93 std::size_t ret(getHeapMemorySizeWithoutChildren());
94 std::vector<const BigMemoryObject *> v(getDirectChildren());
95 std::set<const BigMemoryObject *> s1,s2(v.begin(),v.end());
96 return ret+GetHeapMemoryOfSet(s1,s2);
100 * This method returns all the progeny of \a this (this is \b not included in returned vector).
101 * All the progeny means all the subobjects (children), subsubobjects (little children), ... of \a this.
102 * The elements in returned array are reported only once even if they appear several times in the progeny of \a this.
104 std::vector<const BigMemoryObject *> BigMemoryObject::getAllTheProgeny() const
106 std::vector<const BigMemoryObject *> s1(getDirectChildren());
107 std::vector<const BigMemoryObject *> ret;
110 ret.insert(ret.end(),s1.begin(),s1.end());
111 std::vector<const BigMemoryObject *> s3;
112 for(std::vector<const BigMemoryObject *>::const_iterator it0=s1.begin();it0!=s1.end();it0++)
114 std::vector<const BigMemoryObject *> s2;
116 s2=(*it0)->getDirectChildren();
117 for(std::vector<const BigMemoryObject *>::const_iterator it1=s2.begin();it1!=s2.end();it1++)
120 if(std::find(ret.begin(),ret.end(),*it1)==ret.end())
130 * This method scan all the progeny of \a this (\a this excluded) to see if \a obj is part of it.
131 * If obj is NULL false is returned.
132 * \sa BigMemoryObject::getAllTheProgeny
134 bool BigMemoryObject::isObjectInTheProgeny(const BigMemoryObject *obj) const
138 std::vector<const BigMemoryObject *> objs(getAllTheProgeny());
139 return std::find(objs.begin(),objs.end(),obj)!=objs.end();
142 std::size_t BigMemoryObject::GetHeapMemorySizeOfObjs(const std::vector<const BigMemoryObject *>& objs)
145 std::set<const BigMemoryObject *> s1,s2;
146 for(std::vector<const BigMemoryObject *>::const_iterator it0=objs.begin();it0!=objs.end();it0++)
149 if(s1.find(*it0)==s1.end())
151 std::vector<const BigMemoryObject *> vTmp((*it0)->getDirectChildren());
152 s2.insert(vTmp.begin(),vTmp.end());
153 ret+=(*it0)->getHeapMemorySizeWithoutChildren();
157 return ret+GetHeapMemoryOfSet(s1,s2);
160 std::size_t BigMemoryObject::GetHeapMemoryOfSet(std::set<const BigMemoryObject *>& s1, std::set<const BigMemoryObject *>& s2)
165 std::set<const BigMemoryObject *> s3;
166 for(std::set<const BigMemoryObject *>::const_iterator it=s2.begin();it!=s2.end();it++)
168 if(s1.find(*it)==s1.end())
170 ret+=(*it)->getHeapMemorySizeWithoutChildren();
172 std::vector<const BigMemoryObject *> v2((*it)->getDirectChildren());
173 for(std::vector<const BigMemoryObject *>::const_iterator it2=v2.begin();it2!=v2.end();it2++)
174 if(s1.find(*it2)==s1.end())
183 std::string BigMemoryObject::getHeapMemorySizeStr() const
185 static const char *UNITS[4]={"B","kB","MB","GB"};
186 std::size_t m(getHeapMemorySize());
187 std::ostringstream oss; oss.precision(3);
188 std::size_t remain(0);
197 std::ostringstream oss2; oss2 << std::fixed << ((double)remain)/1024.;
198 std::string s(oss2.str());
200 std::size_t pos(s.find_last_not_of('0'));
204 oss << s.substr(0,pos+1);
206 oss << " " << UNITS[i];
219 oss << m << " " << UNITS[3];
223 std::vector<const BigMemoryObject *> BigMemoryObject::getDirectChildren() const
225 std::vector<const BigMemoryObject *> ret;
226 std::vector<const BigMemoryObject *> retWithNull(getDirectChildrenWithNull());
227 for(std::vector<const BigMemoryObject *>::const_iterator it=retWithNull.begin();it!=retWithNull.end();it++)
233 BigMemoryObject::~BigMemoryObject()
239 RefCountObjectOnly::RefCountObjectOnly():_cnt(1)
243 RefCountObjectOnly::RefCountObjectOnly(const RefCountObjectOnly& other):_cnt(1)
247 bool RefCountObjectOnly::decrRef() const
249 bool ret=((--_cnt)==0);
255 void RefCountObjectOnly::incrRef() const
260 int RefCountObjectOnly::getRCValue() const
265 RefCountObjectOnly::~RefCountObjectOnly()
270 * Do nothing here ! It is not a bug ( I hope :) ) because all subclasses that
271 * copies using operator= should not copy the ref counter of \a other !
273 RefCountObjectOnly& RefCountObjectOnly::operator=(const RefCountObjectOnly& other)
280 RefCountObject::RefCountObject()
284 RefCountObject::RefCountObject(const RefCountObject& other):RefCountObjectOnly(other)
288 RefCountObject::~RefCountObject()
294 GlobalDict *GlobalDict::GetInstance()
297 UNIQUE_INSTANCE=new GlobalDict;
298 return UNIQUE_INSTANCE;
301 bool GlobalDict::hasKey(const std::string& key) const
303 std::map<std::string, std::string>::const_iterator it(_my_map.find(key));
304 return it!=_my_map.end();
307 std::string GlobalDict::value(const std::string& key) const
309 std::map<std::string, std::string>::const_iterator it(_my_map.find(key));
310 if(it==_my_map.end())
312 std::ostringstream oss;
313 oss << "GlobalDict::value : key \"" << key << "\" is not in map !";
314 throw INTERP_KERNEL::Exception(oss.str().c_str());
319 std::vector<std::string> GlobalDict::keys() const
321 std::vector<std::string> ret;
322 for(std::map<std::string, std::string>::const_iterator it=_my_map.begin();it!=_my_map.end();it++)
323 ret.push_back((*it).first);
327 void GlobalDict::erase(const std::string& key)
329 std::map<std::string, std::string>::iterator it(_my_map.find(key));
330 if(it==_my_map.end())
332 std::ostringstream oss;
333 oss << "GlobalDict::erase : key \"" << key << "\" is not in map !";
334 throw INTERP_KERNEL::Exception(oss.str().c_str());
339 void GlobalDict::clear()
344 void GlobalDict::setKeyValue(const std::string& key, const std::string& val)
346 std::map<std::string, std::string>::const_iterator it(_my_map.find(key));
347 if(it!=_my_map.end())
349 std::ostringstream oss;
350 oss << "GlobalDict::setKeyValue : key \"" << key << "\" already exists !";
351 throw INTERP_KERNEL::Exception(oss.str().c_str());
356 void GlobalDict::setKeyValueForce(const std::string& key, const std::string& val)
361 std::string GlobalDict::printSelf() const
363 std::ostringstream oss;
364 for(std::map<std::string, std::string>::const_iterator it=_my_map.begin();it!=_my_map.end();it++)
366 oss << "(" << (*it).first << "," << (*it).second << ")" << std::endl;