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;
80 std::size_t BigMemoryObject::getHeapMemorySize() const
82 std::size_t ret(getHeapMemorySizeWithoutChildren());
83 std::vector<const BigMemoryObject *> v(getDirectChildren());
84 std::set<const BigMemoryObject *> s1,s2(v.begin(),v.end());
85 return ret+GetHeapMemoryOfSet(s1,s2);
89 * This method returns all the progeny of \a this (this is \b not included in returned vector).
90 * All the progeny means all the subobjects (children), subsubobjects (little children), ... of \a this.
91 * The elements in returned array are reported only once even if they appear several times in the progeny of \a this.
93 std::vector<const BigMemoryObject *> BigMemoryObject::getAllTheProgeny() const
95 std::vector<const BigMemoryObject *> s1(getDirectChildren());
96 std::vector<const BigMemoryObject *> ret;
99 ret.insert(ret.end(),s1.begin(),s1.end());
100 std::vector<const BigMemoryObject *> s3;
101 for(std::vector<const BigMemoryObject *>::const_iterator it0=s1.begin();it0!=s1.end();it0++)
103 std::vector<const BigMemoryObject *> s2;
105 s2=(*it0)->getDirectChildren();
106 for(std::vector<const BigMemoryObject *>::const_iterator it1=s2.begin();it1!=s2.end();it1++)
109 if(std::find(ret.begin(),ret.end(),*it1)==ret.end())
119 * This method scan all the progeny of \a this (\a this excluded) to see if \a obj is part of it.
120 * If obj is NULL false is returned.
121 * \sa BigMemoryObject::getAllTheProgeny
123 bool BigMemoryObject::isObjectInTheProgeny(const BigMemoryObject *obj) const
127 std::vector<const BigMemoryObject *> objs(getAllTheProgeny());
128 return std::find(objs.begin(),objs.end(),obj)!=objs.end();
131 std::size_t BigMemoryObject::GetHeapMemorySizeOfObjs(const std::vector<const BigMemoryObject *>& objs)
134 std::set<const BigMemoryObject *> s1,s2;
135 for(std::vector<const BigMemoryObject *>::const_iterator it0=objs.begin();it0!=objs.end();it0++)
138 if(s1.find(*it0)==s1.end())
140 std::vector<const BigMemoryObject *> vTmp((*it0)->getDirectChildren());
141 s2.insert(vTmp.begin(),vTmp.end());
142 ret+=(*it0)->getHeapMemorySizeWithoutChildren();
146 return ret+GetHeapMemoryOfSet(s1,s2);
149 std::size_t BigMemoryObject::GetHeapMemoryOfSet(std::set<const BigMemoryObject *>& s1, std::set<const BigMemoryObject *>& s2)
154 std::set<const BigMemoryObject *> s3;
155 for(std::set<const BigMemoryObject *>::const_iterator it=s2.begin();it!=s2.end();it++)
157 if(s1.find(*it)==s1.end())
159 ret+=(*it)->getHeapMemorySizeWithoutChildren();
161 std::vector<const BigMemoryObject *> v2((*it)->getDirectChildren());
162 for(std::vector<const BigMemoryObject *>::const_iterator it2=v2.begin();it2!=v2.end();it2++)
163 if(s1.find(*it2)==s1.end())
172 std::string BigMemoryObject::getHeapMemorySizeStr() const
174 static const char *UNITS[4]={"B","kB","MB","GB"};
175 std::size_t m(getHeapMemorySize());
176 std::ostringstream oss; oss.precision(3);
177 std::size_t remain(0);
186 std::ostringstream oss2; oss2 << std::fixed << ((double)remain)/1024.;
187 std::string s(oss2.str());
189 std::size_t pos(s.find_last_not_of('0'));
193 oss << s.substr(0,pos+1);
195 oss << " " << UNITS[i];
208 oss << m << " " << UNITS[3];
212 std::vector<const BigMemoryObject *> BigMemoryObject::getDirectChildren() const
214 std::vector<const BigMemoryObject *> ret;
215 std::vector<const BigMemoryObject *> retWithNull(getDirectChildrenWithNull());
216 for(std::vector<const BigMemoryObject *>::const_iterator it=retWithNull.begin();it!=retWithNull.end();it++)
222 BigMemoryObject::~BigMemoryObject()
228 RefCountObjectOnly::RefCountObjectOnly():_cnt(1)
232 RefCountObjectOnly::RefCountObjectOnly(const RefCountObjectOnly& other):_cnt(1)
236 bool RefCountObjectOnly::decrRef() const
238 bool ret=((--_cnt)==0);
244 void RefCountObjectOnly::incrRef() const
249 int RefCountObjectOnly::getRCValue() const
254 RefCountObjectOnly::~RefCountObjectOnly()
259 * Do nothing here ! It is not a bug ( I hope :) ) because all subclasses that
260 * copies using operator= should not copy the ref counter of \a other !
262 RefCountObjectOnly& RefCountObjectOnly::operator=(const RefCountObjectOnly& other)
269 RefCountObject::RefCountObject()
273 RefCountObject::RefCountObject(const RefCountObject& other):RefCountObjectOnly(other)
277 RefCountObject::~RefCountObject()
283 GlobalDict *GlobalDict::GetInstance()
286 UNIQUE_INSTANCE=new GlobalDict;
287 return UNIQUE_INSTANCE;
290 bool GlobalDict::hasKey(const std::string& key) const
292 std::map<std::string, std::string>::const_iterator it(_my_map.find(key));
293 return it!=_my_map.end();
296 std::string GlobalDict::value(const std::string& key) const
298 std::map<std::string, std::string>::const_iterator it(_my_map.find(key));
299 if(it==_my_map.end())
301 std::ostringstream oss;
302 oss << "GlobalDict::value : key \"" << key << "\" is not in map !";
303 throw INTERP_KERNEL::Exception(oss.str().c_str());
308 std::vector<std::string> GlobalDict::keys() const
310 std::vector<std::string> ret;
311 for(std::map<std::string, std::string>::const_iterator it=_my_map.begin();it!=_my_map.end();it++)
312 ret.push_back((*it).first);
316 void GlobalDict::erase(const std::string& key)
318 std::map<std::string, std::string>::iterator it(_my_map.find(key));
319 if(it==_my_map.end())
321 std::ostringstream oss;
322 oss << "GlobalDict::erase : key \"" << key << "\" is not in map !";
323 throw INTERP_KERNEL::Exception(oss.str().c_str());
328 void GlobalDict::clear()
333 void GlobalDict::setKeyValue(const std::string& key, const std::string& val)
335 std::map<std::string, std::string>::const_iterator it(_my_map.find(key));
336 if(it!=_my_map.end())
338 std::ostringstream oss;
339 oss << "GlobalDict::setKeyValue : key \"" << key << "\" already exists !";
340 throw INTERP_KERNEL::Exception(oss.str().c_str());
345 void GlobalDict::setKeyValueForce(const std::string& key, const std::string& val)
350 std::string GlobalDict::printSelf() const
352 std::ostringstream oss;
353 for(std::map<std::string, std::string>::const_iterator it=_my_map.begin();it!=_my_map.end();it++)
355 oss << "(" << (*it).first << "," << (*it).second << ")" << std::endl;