Salome HOME
remove src/MEDCalc/doc
[modules/med.git] / src / MEDCoupling / MEDCouplingRefCountObject.cxx
1 // Copyright (C) 2007-2015  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 "MED_version.h"
23
24 #include <sstream>
25 #include <algorithm>
26
27 using namespace ParaMEDMEM;
28
29 const char *ParaMEDMEM::MEDCouplingVersionStr()
30 {
31   return SALOMEMED_VERSION_STR;
32 }
33
34 int ParaMEDMEM::MEDCouplingVersion()
35 {
36   return SALOMEMED_VERSION;
37 }
38
39 void ParaMEDMEM::MEDCouplingVersionMajMinRel(int& maj, int& minor, int& releas)
40 {
41   int ver=SALOMEMED_VERSION;
42   maj=(ver & 0xFF0000) >> 16;
43   minor=(ver & 0xFF00) >> 8;
44   releas=(ver & 0xFF);
45 }
46
47 int ParaMEDMEM::MEDCouplingSizeOfVoidStar()
48 {
49   return 8*sizeof(std::size_t);
50 }
51
52 /*!
53  * If true is returned it is a LittleEndian machine.
54  * If false it is a BigEndian machine.
55  * \return the coding mode of integers of the machine.
56  */
57 bool ParaMEDMEM::MEDCouplingByteOrder()
58 {
59   unsigned int x(1);
60   unsigned char *xc(reinterpret_cast<unsigned char *>(&x));
61   return xc[0]==1;
62 }
63
64 const char *ParaMEDMEM::MEDCouplingByteOrderStr()
65 {
66   static const char LITTLEENDIAN_STR[]="LittleEndian";
67   static const char BIGENDIAN_STR[]="BigEndian";
68   if(MEDCouplingByteOrder())
69     return LITTLEENDIAN_STR;
70   else
71     return BIGENDIAN_STR;
72 }
73
74 //=
75
76 std::size_t BigMemoryObject::getHeapMemorySize() const
77 {
78   std::size_t ret(getHeapMemorySizeWithoutChildren());
79   std::vector<const BigMemoryObject *> v(getDirectChildren());
80   std::set<const BigMemoryObject *> s1,s2(v.begin(),v.end());
81   return ret+GetHeapMemoryOfSet(s1,s2);
82 }
83
84 /*!
85  * This method returns all the progeny of \a this (this is \b not included in returned vector).
86  * All the progeny means all the subobjects (children), subsubobjects (little children), ... of \a this.
87  * The elements in returned array are reported only once even if they appear several times in the progeny of \a this.
88  */
89 std::vector<const BigMemoryObject *> BigMemoryObject::getAllTheProgeny() const
90 {
91   std::vector<const BigMemoryObject *> s1(getDirectChildren());
92   std::vector<const BigMemoryObject *> ret;
93   while(!s1.empty())
94     {
95       ret.insert(ret.end(),s1.begin(),s1.end());
96       std::vector<const BigMemoryObject *> s3;
97       for(std::vector<const BigMemoryObject *>::const_iterator it0=s1.begin();it0!=s1.end();it0++)
98         {
99           std::vector<const BigMemoryObject *> s2;
100           if(*it0)
101             s2=(*it0)->getDirectChildren();
102           for(std::vector<const BigMemoryObject *>::const_iterator it1=s2.begin();it1!=s2.end();it1++)
103             {
104               if(*it1)
105                 if(std::find(ret.begin(),ret.end(),*it1)==ret.end())
106                   s3.push_back(*it1);
107             }
108         }
109       s1=s3;
110     }
111   return ret;
112 }
113
114 /*!
115  * This method scan all the progeny of \a this (\a this excluded) to see if \a obj is part of it.
116  * If obj is NULL false is returned.
117  * \sa BigMemoryObject::getAllTheProgeny
118  */
119 bool BigMemoryObject::isObjectInTheProgeny(const BigMemoryObject *obj) const
120 {
121   if(!obj)
122     return false;
123   std::vector<const BigMemoryObject *> objs(getAllTheProgeny());
124   return std::find(objs.begin(),objs.end(),obj)!=objs.end();
125 }
126
127 std::size_t BigMemoryObject::GetHeapMemorySizeOfObjs(const std::vector<const BigMemoryObject *>& objs)
128 {
129   std::size_t ret(0);
130   std::set<const BigMemoryObject *> s1,s2;
131   for(std::vector<const BigMemoryObject *>::const_iterator it0=objs.begin();it0!=objs.end();it0++)
132     {
133       if(*it0)
134         if(s1.find(*it0)==s1.end())
135           {
136             std::vector<const BigMemoryObject *> vTmp((*it0)->getDirectChildren());
137             s2.insert(vTmp.begin(),vTmp.end());
138             ret+=(*it0)->getHeapMemorySizeWithoutChildren();
139             s1.insert(*it0);
140           }
141     }
142   return ret+GetHeapMemoryOfSet(s1,s2);
143 }
144
145 std::size_t BigMemoryObject::GetHeapMemoryOfSet(std::set<const BigMemoryObject *>& s1, std::set<const BigMemoryObject *>& s2)
146 {
147   std::size_t ret(0);
148   while(!s2.empty())
149     {
150       std::set<const BigMemoryObject *> s3;
151       for(std::set<const BigMemoryObject *>::const_iterator it=s2.begin();it!=s2.end();it++)
152         {
153           if(s1.find(*it)==s1.end())
154             {
155               ret+=(*it)->getHeapMemorySizeWithoutChildren();
156               s1.insert(*it);
157               std::vector<const BigMemoryObject *> v2((*it)->getDirectChildren());
158               for(std::vector<const BigMemoryObject *>::const_iterator it2=v2.begin();it2!=v2.end();it2++)
159                 if(s1.find(*it2)==s1.end())
160                   s3.insert(*it2);
161             }
162         }
163       s2=s3;
164     }
165   return ret;
166 }
167
168 std::string BigMemoryObject::getHeapMemorySizeStr() const
169 {
170   static const char *UNITS[4]={"B","kB","MB","GB"};
171   std::size_t m(getHeapMemorySize());
172   std::ostringstream oss; oss.precision(3);
173   std::size_t remain(0);
174   int i(0);
175   for(;i<4;i++)
176     {
177       if(m<1024)
178         {
179           oss << m;
180           if(remain!=0)
181             {
182               std::ostringstream oss2; oss2 << std::fixed << ((double)remain)/1024.;
183               std::string s(oss2.str());
184               s=s.substr(1,4);
185               std::size_t pos(s.find_last_not_of('0'));
186               if(pos==4)
187                 oss << s;
188               else
189                 oss << s.substr(0,pos+1);
190             }
191           oss << " " << UNITS[i];
192           break;
193         }
194       else
195         {
196           if(i!=3)
197             {
198               remain=(m%1024);
199               m/=1024;
200             }
201         }
202     }
203   if(i==4)
204     oss << m << " " << UNITS[3];
205   return oss.str();
206 }
207
208 std::vector<const BigMemoryObject *> BigMemoryObject::getDirectChildren() const
209 {
210   std::vector<const BigMemoryObject *> ret;
211   std::vector<const BigMemoryObject *> retWithNull(getDirectChildrenWithNull());
212   for(std::vector<const BigMemoryObject *>::const_iterator it=retWithNull.begin();it!=retWithNull.end();it++)
213     if(*it)
214       ret.push_back(*it);
215   return ret;
216 }
217
218 BigMemoryObject::~BigMemoryObject()
219 {
220 }
221
222 //=
223
224 RefCountObjectOnly::RefCountObjectOnly():_cnt(1)
225 {
226 }
227
228 RefCountObjectOnly::RefCountObjectOnly(const RefCountObjectOnly& other):_cnt(1)
229 {
230 }
231
232 bool RefCountObjectOnly::decrRef() const
233 {
234   bool ret=((--_cnt)==0);
235   if(ret)
236     delete this;
237   return ret;
238 }
239
240 void RefCountObjectOnly::incrRef() const
241 {
242   _cnt++;
243 }
244
245 int RefCountObjectOnly::getRCValue() const
246 {
247   return _cnt;
248 }
249
250 RefCountObjectOnly::~RefCountObjectOnly()
251 {
252 }
253
254 /*!
255  * Do nothing here ! It is not a bug ( I hope :) ) because all subclasses that
256  * copies using operator= should not copy the ref counter of \a other !
257  */
258 RefCountObjectOnly& RefCountObjectOnly::operator=(const RefCountObjectOnly& other)
259 {
260   return *this;
261 }
262
263 //=
264
265 RefCountObject::RefCountObject()
266 {
267 }
268
269 RefCountObject::RefCountObject(const RefCountObject& other):RefCountObjectOnly(other)
270 {
271 }
272
273 RefCountObject::~RefCountObject()
274 {
275 }