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"
24 #include "InterpKernelException.hxx"
29 using namespace MEDCoupling;
31 GlobalDict *GlobalDict::UNIQUE_INSTANCE=0;
33 const char *MEDCoupling::MEDCouplingVersionStr()
35 return MEDCOUPLING_VERSION_STR;
38 int MEDCoupling::MEDCouplingVersion()
40 return MEDCOUPLING_VERSION;
43 void MEDCoupling::MEDCouplingVersionMajMinRel(int& maj, int& minor, int& releas)
45 int ver=MEDCOUPLING_VERSION;
46 maj=(ver & 0xFF0000) >> 16;
47 minor=(ver & 0xFF00) >> 8;
51 int MEDCoupling::MEDCouplingSizeOfVoidStar()
53 return 8*sizeof(std::size_t);
57 * If true is returned it is a LittleEndian machine.
58 * If false it is a BigEndian machine.
59 * \return the coding mode of integers of the machine.
61 bool MEDCoupling::MEDCouplingByteOrder()
64 unsigned char *xc(reinterpret_cast<unsigned char *>(&x));
68 const char *MEDCoupling::MEDCouplingByteOrderStr()
70 static const char LITTLEENDIAN_STR[]="LittleEndian";
71 static const char BIGENDIAN_STR[]="BigEndian";
72 if(MEDCouplingByteOrder())
73 return LITTLEENDIAN_STR;
78 bool MEDCoupling::IsCXX11Compiled()
85 std::size_t BigMemoryObject::getHeapMemorySize() const
87 std::size_t ret(getHeapMemorySizeWithoutChildren());
88 std::vector<const BigMemoryObject *> v(getDirectChildren());
89 std::set<const BigMemoryObject *> s1,s2(v.begin(),v.end());
90 return ret+GetHeapMemoryOfSet(s1,s2);
94 * This method returns all the progeny of \a this (this is \b not included in returned vector).
95 * All the progeny means all the subobjects (children), subsubobjects (little children), ... of \a this.
96 * The elements in returned array are reported only once even if they appear several times in the progeny of \a this.
98 std::vector<const BigMemoryObject *> BigMemoryObject::getAllTheProgeny() const
100 std::vector<const BigMemoryObject *> s1(getDirectChildren());
101 std::vector<const BigMemoryObject *> ret;
104 ret.insert(ret.end(),s1.begin(),s1.end());
105 std::vector<const BigMemoryObject *> s3;
106 for(std::vector<const BigMemoryObject *>::const_iterator it0=s1.begin();it0!=s1.end();it0++)
108 std::vector<const BigMemoryObject *> s2;
110 s2=(*it0)->getDirectChildren();
111 for(std::vector<const BigMemoryObject *>::const_iterator it1=s2.begin();it1!=s2.end();it1++)
114 if(std::find(ret.begin(),ret.end(),*it1)==ret.end())
124 * This method scan all the progeny of \a this (\a this excluded) to see if \a obj is part of it.
125 * If obj is NULL false is returned.
126 * \sa BigMemoryObject::getAllTheProgeny
128 bool BigMemoryObject::isObjectInTheProgeny(const BigMemoryObject *obj) const
132 std::vector<const BigMemoryObject *> objs(getAllTheProgeny());
133 return std::find(objs.begin(),objs.end(),obj)!=objs.end();
136 std::size_t BigMemoryObject::GetHeapMemorySizeOfObjs(const std::vector<const BigMemoryObject *>& objs)
139 std::set<const BigMemoryObject *> s1,s2;
140 for(std::vector<const BigMemoryObject *>::const_iterator it0=objs.begin();it0!=objs.end();it0++)
143 if(s1.find(*it0)==s1.end())
145 std::vector<const BigMemoryObject *> vTmp((*it0)->getDirectChildren());
146 s2.insert(vTmp.begin(),vTmp.end());
147 ret+=(*it0)->getHeapMemorySizeWithoutChildren();
151 return ret+GetHeapMemoryOfSet(s1,s2);
154 std::size_t BigMemoryObject::GetHeapMemoryOfSet(std::set<const BigMemoryObject *>& s1, std::set<const BigMemoryObject *>& s2)
159 std::set<const BigMemoryObject *> s3;
160 for(std::set<const BigMemoryObject *>::const_iterator it=s2.begin();it!=s2.end();it++)
162 if(s1.find(*it)==s1.end())
164 ret+=(*it)->getHeapMemorySizeWithoutChildren();
166 std::vector<const BigMemoryObject *> v2((*it)->getDirectChildren());
167 for(std::vector<const BigMemoryObject *>::const_iterator it2=v2.begin();it2!=v2.end();it2++)
168 if(s1.find(*it2)==s1.end())
177 std::string BigMemoryObject::getHeapMemorySizeStr() const
179 static const char *UNITS[4]={"B","kB","MB","GB"};
180 std::size_t m(getHeapMemorySize());
181 std::ostringstream oss; oss.precision(3);
182 std::size_t remain(0);
191 std::ostringstream oss2; oss2 << std::fixed << ((double)remain)/1024.;
192 std::string s(oss2.str());
194 std::size_t pos(s.find_last_not_of('0'));
198 oss << s.substr(0,pos+1);
200 oss << " " << UNITS[i];
213 oss << m << " " << UNITS[3];
217 std::vector<const BigMemoryObject *> BigMemoryObject::getDirectChildren() const
219 std::vector<const BigMemoryObject *> ret;
220 std::vector<const BigMemoryObject *> retWithNull(getDirectChildrenWithNull());
221 for(std::vector<const BigMemoryObject *>::const_iterator it=retWithNull.begin();it!=retWithNull.end();it++)
227 BigMemoryObject::~BigMemoryObject()
233 RefCountObjectOnly::RefCountObjectOnly():_cnt(1)
237 RefCountObjectOnly::RefCountObjectOnly(const RefCountObjectOnly& other):_cnt(1)
241 bool RefCountObjectOnly::decrRef() const
243 bool ret=((--_cnt)==0);
249 void RefCountObjectOnly::incrRef() const
254 int RefCountObjectOnly::getRCValue() const
259 RefCountObjectOnly::~RefCountObjectOnly()
264 * Do nothing here ! It is not a bug ( I hope :) ) because all subclasses that
265 * copies using operator= should not copy the ref counter of \a other !
267 RefCountObjectOnly& RefCountObjectOnly::operator=(const RefCountObjectOnly& other)
274 RefCountObject::RefCountObject()
278 RefCountObject::RefCountObject(const RefCountObject& other):RefCountObjectOnly(other)
282 RefCountObject::~RefCountObject()
288 GlobalDict *GlobalDict::GetInstance()
291 UNIQUE_INSTANCE=new GlobalDict;
292 return UNIQUE_INSTANCE;
295 bool GlobalDict::hasKey(const std::string& key) const
297 std::map<std::string, std::string>::const_iterator it(_my_map.find(key));
298 return it!=_my_map.end();
301 std::string GlobalDict::value(const std::string& key) const
303 std::map<std::string, std::string>::const_iterator it(_my_map.find(key));
304 if(it==_my_map.end())
306 std::ostringstream oss;
307 oss << "GlobalDict::value : key \"" << key << "\" is not in map !";
308 throw INTERP_KERNEL::Exception(oss.str().c_str());
313 std::vector<std::string> GlobalDict::keys() const
315 std::vector<std::string> ret;
316 for(std::map<std::string, std::string>::const_iterator it=_my_map.begin();it!=_my_map.end();it++)
317 ret.push_back((*it).first);
321 void GlobalDict::erase(const std::string& key)
323 std::map<std::string, std::string>::iterator it(_my_map.find(key));
324 if(it==_my_map.end())
326 std::ostringstream oss;
327 oss << "GlobalDict::erase : key \"" << key << "\" is not in map !";
328 throw INTERP_KERNEL::Exception(oss.str().c_str());
333 void GlobalDict::clear()
338 void GlobalDict::setKeyValue(const std::string& key, const std::string& val)
340 std::map<std::string, std::string>::const_iterator it(_my_map.find(key));
341 if(it!=_my_map.end())
343 std::ostringstream oss;
344 oss << "GlobalDict::setKeyValue : key \"" << key << "\" already exists !";
345 throw INTERP_KERNEL::Exception(oss.str().c_str());
350 void GlobalDict::setKeyValueForce(const std::string& key, const std::string& val)
355 std::string GlobalDict::printSelf() const
357 std::ostringstream oss;
358 for(std::map<std::string, std::string>::const_iterator it=_my_map.begin();it!=_my_map.end();it++)
360 oss << "(" << (*it).first << "," << (*it).second << ")" << std::endl;