Salome HOME
Merge branch 'abn/fix_orient' into V7_main
[modules/med.git] / src / MEDCoupling / MEDCouplingRefCountObject.cxx
1 // Copyright (C) 2007-2014  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
26 using namespace ParaMEDMEM;
27
28 const char *ParaMEDMEM::MEDCouplingVersionStr()
29 {
30   return SALOMEMED_VERSION_STR;
31 }
32
33 int ParaMEDMEM::MEDCouplingVersion()
34 {
35   return SALOMEMED_VERSION;
36 }
37
38 void ParaMEDMEM::MEDCouplingVersionMajMinRel(int& maj, int& minor, int& releas)
39 {
40   int ver=SALOMEMED_VERSION;
41   maj=(ver & 0xFF0000) >> 16;
42   minor=(ver & 0xFF00) >> 8;
43   releas=(ver & 0xFF);
44 }
45
46 int ParaMEDMEM::MEDCouplingSizeOfVoidStar()
47 {
48   return 8*sizeof(std::size_t);
49 }
50
51 /*!
52  * If true is returned it is a LittleEndian machine.
53  * If false it is a BigEndian machine.
54  * \return the coding mode of integers of the machine.
55  */
56 bool ParaMEDMEM::MEDCouplingByteOrder()
57 {
58   unsigned int x(1);
59   unsigned char *xc(reinterpret_cast<unsigned char *>(&x));
60   return xc[0]==1;
61 }
62
63 const char *ParaMEDMEM::MEDCouplingByteOrderStr()
64 {
65   static const char LITTLEENDIAN_STR[]="LittleEndian";
66   static const char BIGENDIAN_STR[]="BigEndian";
67   if(MEDCouplingByteOrder())
68     return LITTLEENDIAN_STR;
69   else
70     return BIGENDIAN_STR;
71 }
72
73 //=
74
75 std::size_t BigMemoryObject::getHeapMemorySize() const
76 {
77   std::size_t ret(getHeapMemorySizeWithoutChildren());
78   std::vector<const BigMemoryObject *> v(getDirectChildren());
79   std::set<const BigMemoryObject *> s1,s2(v.begin(),v.end());
80   return ret+GetHeapMemoryOfSet(s1,s2);
81 }
82
83 std::size_t BigMemoryObject::GetHeapMemorySizeOfObjs(const std::vector<const BigMemoryObject *>& objs)
84 {
85   std::size_t ret(0);
86   std::set<const BigMemoryObject *> s1,s2;
87   for(std::vector<const BigMemoryObject *>::const_iterator it0=objs.begin();it0!=objs.end();it0++)
88     {
89       if(*it0)
90         if(s1.find(*it0)==s1.end())
91           {
92             std::vector<const BigMemoryObject *> vTmp((*it0)->getDirectChildren());
93             s2.insert(vTmp.begin(),vTmp.end());
94             ret+=(*it0)->getHeapMemorySizeWithoutChildren();
95             s1.insert(*it0);
96           }
97     }
98   return ret+GetHeapMemoryOfSet(s1,s2);
99 }
100
101 std::size_t BigMemoryObject::GetHeapMemoryOfSet(std::set<const BigMemoryObject *>& s1, std::set<const BigMemoryObject *>& s2)
102 {
103   std::size_t ret(0);
104   while(!s2.empty())
105     {
106       std::set<const BigMemoryObject *> s3;
107       for(std::set<const BigMemoryObject *>::const_iterator it=s2.begin();it!=s2.end();it++)
108         {
109           if(s1.find(*it)==s1.end())
110             {
111               ret+=(*it)->getHeapMemorySizeWithoutChildren();
112               s1.insert(*it);
113               std::vector<const BigMemoryObject *> v2((*it)->getDirectChildren());
114               for(std::vector<const BigMemoryObject *>::const_iterator it2=v2.begin();it2!=v2.end();it2++)
115                 if(s1.find(*it2)==s1.end())
116                   s3.insert(*it2);
117             }
118         }
119       s2=s3;
120     }
121   return ret;
122 }
123
124 std::string BigMemoryObject::getHeapMemorySizeStr() const
125 {
126   static const char *UNITS[4]={"B","kB","MB","GB"};
127   std::size_t m(getHeapMemorySize());
128   std::ostringstream oss; oss.precision(3);
129   std::size_t remain(0);
130   int i(0);
131   for(;i<4;i++)
132     {
133       if(m<1024)
134         {
135           oss << m;
136           if(remain!=0)
137             {
138               std::ostringstream oss2; oss2 << std::fixed << ((double)remain)/1024.;
139               std::string s(oss2.str());
140               s=s.substr(1,4);
141               std::size_t pos(s.find_last_not_of('0'));
142               if(pos==4)
143                 oss << s;
144               else
145                 oss << s.substr(0,pos+1);
146             }
147           oss << " " << UNITS[i];
148           break;
149         }
150       else
151         {
152           if(i!=3)
153             {
154               remain=(m%1024);
155               m/=1024;
156             }
157         }
158     }
159   if(i==4)
160     oss << m << " " << UNITS[3];
161   return oss.str();
162 }
163
164 BigMemoryObject::~BigMemoryObject()
165 {
166 }
167
168 //=
169
170 RefCountObjectOnly::RefCountObjectOnly():_cnt(1)
171 {
172 }
173
174 RefCountObjectOnly::RefCountObjectOnly(const RefCountObjectOnly& other):_cnt(1)
175 {
176 }
177
178 bool RefCountObjectOnly::decrRef() const
179 {
180   bool ret=((--_cnt)==0);
181   if(ret)
182     delete this;
183   return ret;
184 }
185
186 void RefCountObjectOnly::incrRef() const
187 {
188   _cnt++;
189 }
190
191 int RefCountObjectOnly::getRCValue() const
192 {
193   return _cnt;
194 }
195
196 RefCountObjectOnly::~RefCountObjectOnly()
197 {
198 }
199
200 /*!
201  * Do nothing here ! It is not a bug ( I hope :) ) because all subclasses that
202  * copies using operator= should not copy the ref counter of \a other !
203  */
204 RefCountObjectOnly& RefCountObjectOnly::operator=(const RefCountObjectOnly& other)
205 {
206   return *this;
207 }
208
209 //=
210
211 RefCountObject::RefCountObject()
212 {
213 }
214
215 RefCountObject::RefCountObject(const RefCountObject& other):RefCountObjectOnly(other)
216 {
217 }
218
219 RefCountObject::~RefCountObject()
220 {
221 }