Salome HOME
Porting python3
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingRefCountObject.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDCouplingRefCountObject.hxx"
22 #include "MEDCoupling_version.h"
23
24 #include "InterpKernelException.hxx"
25
26 #include <sstream>
27 #include <algorithm>
28
29 using namespace MEDCoupling;
30
31 GlobalDict *GlobalDict::UNIQUE_INSTANCE=0;
32
33 const char *MEDCoupling::MEDCouplingVersionStr()
34 {
35   return MEDCOUPLING_VERSION_STR;
36 }
37
38 int MEDCoupling::MEDCouplingVersion()
39 {
40   return MEDCOUPLING_VERSION;
41 }
42
43 void MEDCoupling::MEDCouplingVersionMajMinRel(int& maj, int& minor, int& releas)
44 {
45   int ver=MEDCOUPLING_VERSION;
46   maj=(ver & 0xFF0000) >> 16;
47   minor=(ver & 0xFF00) >> 8;
48   releas=(ver & 0xFF);
49 }
50
51 int MEDCoupling::MEDCouplingSizeOfVoidStar()
52 {
53   return 8*sizeof(std::size_t);
54 }
55
56 /*!
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.
60  */
61 bool MEDCoupling::MEDCouplingByteOrder()
62 {
63   unsigned int x(1);
64   unsigned char *xc(reinterpret_cast<unsigned char *>(&x));
65   return xc[0]==1;
66 }
67
68 const char *MEDCoupling::MEDCouplingByteOrderStr()
69 {
70   static const char LITTLEENDIAN_STR[]="LittleEndian";
71   static const char BIGENDIAN_STR[]="BigEndian";
72   if(MEDCouplingByteOrder())
73     return LITTLEENDIAN_STR;
74   else
75     return BIGENDIAN_STR;
76 }
77
78 bool MEDCoupling::IsCXX11Compiled()
79 {
80   return true;
81 }
82
83 //=
84
85 std::size_t BigMemoryObject::getHeapMemorySize() const
86 {
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);
91 }
92
93 /*!
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.
97  */
98 std::vector<const BigMemoryObject *> BigMemoryObject::getAllTheProgeny() const
99 {
100   std::vector<const BigMemoryObject *> s1(getDirectChildren());
101   std::vector<const BigMemoryObject *> ret;
102   while(!s1.empty())
103     {
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++)
107         {
108           std::vector<const BigMemoryObject *> s2;
109           if(*it0)
110             s2=(*it0)->getDirectChildren();
111           for(std::vector<const BigMemoryObject *>::const_iterator it1=s2.begin();it1!=s2.end();it1++)
112             {
113               if(*it1)
114                 if(std::find(ret.begin(),ret.end(),*it1)==ret.end())
115                   s3.push_back(*it1);
116             }
117         }
118       s1=s3;
119     }
120   return ret;
121 }
122
123 /*!
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
127  */
128 bool BigMemoryObject::isObjectInTheProgeny(const BigMemoryObject *obj) const
129 {
130   if(!obj)
131     return false;
132   std::vector<const BigMemoryObject *> objs(getAllTheProgeny());
133   return std::find(objs.begin(),objs.end(),obj)!=objs.end();
134 }
135
136 std::size_t BigMemoryObject::GetHeapMemorySizeOfObjs(const std::vector<const BigMemoryObject *>& objs)
137 {
138   std::size_t ret(0);
139   std::set<const BigMemoryObject *> s1,s2;
140   for(std::vector<const BigMemoryObject *>::const_iterator it0=objs.begin();it0!=objs.end();it0++)
141     {
142       if(*it0)
143         if(s1.find(*it0)==s1.end())
144           {
145             std::vector<const BigMemoryObject *> vTmp((*it0)->getDirectChildren());
146             s2.insert(vTmp.begin(),vTmp.end());
147             ret+=(*it0)->getHeapMemorySizeWithoutChildren();
148             s1.insert(*it0);
149           }
150     }
151   return ret+GetHeapMemoryOfSet(s1,s2);
152 }
153
154 std::size_t BigMemoryObject::GetHeapMemoryOfSet(std::set<const BigMemoryObject *>& s1, std::set<const BigMemoryObject *>& s2)
155 {
156   std::size_t ret(0);
157   while(!s2.empty())
158     {
159       std::set<const BigMemoryObject *> s3;
160       for(std::set<const BigMemoryObject *>::const_iterator it=s2.begin();it!=s2.end();it++)
161         {
162           if(s1.find(*it)==s1.end())
163             {
164               ret+=(*it)->getHeapMemorySizeWithoutChildren();
165               s1.insert(*it);
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())
169                   s3.insert(*it2);
170             }
171         }
172       s2=s3;
173     }
174   return ret;
175 }
176
177 std::string BigMemoryObject::getHeapMemorySizeStr() const
178 {
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);
183   int i(0);
184   for(;i<4;i++)
185     {
186       if(m<1024)
187         {
188           oss << m;
189           if(remain!=0)
190             {
191               std::ostringstream oss2; oss2 << std::fixed << ((double)remain)/1024.;
192               std::string s(oss2.str());
193               s=s.substr(1,4);
194               std::size_t pos(s.find_last_not_of('0'));
195               if(pos==4)
196                 oss << s;
197               else
198                 oss << s.substr(0,pos+1);
199             }
200           oss << " " << UNITS[i];
201           break;
202         }
203       else
204         {
205           if(i!=3)
206             {
207               remain=(m%1024);
208               m/=1024;
209             }
210         }
211     }
212   if(i==4)
213     oss << m << " " << UNITS[3];
214   return oss.str();
215 }
216
217 std::vector<const BigMemoryObject *> BigMemoryObject::getDirectChildren() const
218 {
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++)
222     if(*it)
223       ret.push_back(*it);
224   return ret;
225 }
226
227 BigMemoryObject::~BigMemoryObject()
228 {
229 }
230
231 //=
232
233 RefCountObjectOnly::RefCountObjectOnly():_cnt(1)
234 {
235 }
236
237 RefCountObjectOnly::RefCountObjectOnly(const RefCountObjectOnly& other):_cnt(1)
238 {
239 }
240
241 bool RefCountObjectOnly::decrRef() const
242 {
243   bool ret=((--_cnt)==0);
244   if(ret)
245     delete this;
246   return ret;
247 }
248
249 void RefCountObjectOnly::incrRef() const
250 {
251   _cnt++;
252 }
253
254 int RefCountObjectOnly::getRCValue() const
255 {
256   return _cnt;
257 }
258
259 RefCountObjectOnly::~RefCountObjectOnly()
260 {
261 }
262
263 /*!
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 !
266  */
267 RefCountObjectOnly& RefCountObjectOnly::operator=(const RefCountObjectOnly& other)
268 {
269   return *this;
270 }
271
272 //=
273
274 RefCountObject::RefCountObject()
275 {
276 }
277
278 RefCountObject::RefCountObject(const RefCountObject& other):RefCountObjectOnly(other)
279 {
280 }
281
282 RefCountObject::~RefCountObject()
283 {
284 }
285
286 //=
287
288 GlobalDict *GlobalDict::GetInstance()
289 {
290   if(!UNIQUE_INSTANCE)
291     UNIQUE_INSTANCE=new GlobalDict;
292   return UNIQUE_INSTANCE;
293 }
294
295 bool GlobalDict::hasKey(const std::string& key) const
296 {
297   std::map<std::string, std::string>::const_iterator it(_my_map.find(key));
298   return it!=_my_map.end();
299 }
300
301 std::string GlobalDict::value(const std::string& key) const
302 {
303   std::map<std::string, std::string>::const_iterator it(_my_map.find(key));
304   if(it==_my_map.end())
305     {
306       std::ostringstream oss;
307       oss << "GlobalDict::value : key \"" << key << "\" is not in map !";
308       throw INTERP_KERNEL::Exception(oss.str().c_str());
309     }
310   return (*it).second;
311 }
312
313 std::vector<std::string> GlobalDict::keys() const
314 {
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);
318   return ret;
319 }
320
321 void GlobalDict::erase(const std::string& key)
322 {
323   std::map<std::string, std::string>::iterator it(_my_map.find(key));
324   if(it==_my_map.end())
325     {
326       std::ostringstream oss;
327       oss << "GlobalDict::erase : key \"" << key << "\" is not in map !";
328       throw INTERP_KERNEL::Exception(oss.str().c_str());
329     }
330   _my_map.erase(it);
331 }
332
333 void GlobalDict::clear()
334 {
335   _my_map.clear();
336 }
337
338 void GlobalDict::setKeyValue(const std::string& key, const std::string& val)
339 {
340   std::map<std::string, std::string>::const_iterator it(_my_map.find(key));
341   if(it!=_my_map.end())
342     {
343       std::ostringstream oss;
344       oss << "GlobalDict::setKeyValue : key \"" << key << "\" already exists !";
345       throw INTERP_KERNEL::Exception(oss.str().c_str());
346     }
347   _my_map[key]=val;
348 }
349
350 void GlobalDict::setKeyValueForce(const std::string& key, const std::string& val)
351 {
352   _my_map[key]=val;
353 }
354
355 std::string GlobalDict::printSelf() const
356 {
357   std::ostringstream oss;
358   for(std::map<std::string, std::string>::const_iterator it=_my_map.begin();it!=_my_map.end();it++)
359     {
360       oss << "(" << (*it).first << "," << (*it).second << ")" << std::endl;
361     }
362   return oss.str();
363 }