Salome HOME
b2374b980bddca7ab85176de6c96def3a7bdd007
[modules/med.git] / src / MEDCoupling / MEDCouplingRefCountObject.cxx
1 // Copyright (C) 2007-2013  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.
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 <set>
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 RefCountObject::RefCountObject():_cnt(1)
74 {
75 }
76
77 RefCountObject::RefCountObject(const RefCountObject& other):_cnt(1)
78 {
79 }
80
81 /*!
82  * Do nothing here ! It is not a bug ( I hope :) ) because all subclasses that
83  * copies using operator= should not copy the ref counter of \a other !
84  */
85 RefCountObject& RefCountObject::operator=(const RefCountObject& other)
86 {
87   return *this;
88 }
89
90 std::size_t RefCountObject::getHeapMemorySize() const
91 {
92   std::size_t ret(getHeapMemorySizeWithoutChildren());
93   std::vector<RefCountObject *> v(getDirectChildren());
94   std::set<RefCountObject *> s1,s2(v.begin(),v.end());
95   while(!s2.empty())
96     {
97       std::set<RefCountObject *> s3;
98       for(std::set<RefCountObject *>::const_iterator it=s2.begin();it!=s2.end();it++)
99         {
100           if(s1.find(*it)==s1.end())
101             {
102               ret+=(*it)->getHeapMemorySizeWithoutChildren();
103               s1.insert(*it);
104               std::vector<RefCountObject *> v2((*it)->getDirectChildren());
105               for(std::vector<RefCountObject *>::const_iterator it2=v2.begin();it2!=v2.end();it2++)
106                 if(s1.find(*it2)==s1.end())
107                   s3.insert(*it2);
108             }
109         }
110       s2=s3;
111     }
112   return ret;
113 }
114
115 bool RefCountObject::decrRef() const
116 {
117   bool ret=((--_cnt)==0);
118   if(ret)
119     delete this;
120   return ret;
121 }
122
123 void RefCountObject::incrRef() const
124 {
125   _cnt++;
126 }
127
128 int RefCountObject::getRCValue() const
129 {
130   return _cnt;
131 }
132
133 RefCountObject::~RefCountObject()
134 {
135 }