Salome HOME
CCAR: remove memory leaks in non local SALOMEDS
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_GenericAttribute.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SALOMEDS_GenericAttribute.cxx
23 //  Author : Sergey RUIN
24 //  Module : SALOME
25 //
26 #include <string>
27
28 #include "SALOMEDS_GenericAttribute.hxx"
29 #include "SALOMEDSImpl_SObject.hxx"
30 #include "SALOMEDS_SObject.hxx"
31 #include "SALOMEDS_ClientAttributes.hxx"
32 #include "SALOMEDS.hxx"
33
34 #include "Basics_Utils.hxx"
35
36 #ifdef WIN32
37 #include <process.h>
38 #else
39 #include <sys/types.h>
40 #include <unistd.h>
41 #endif
42
43 using namespace std; 
44
45 SALOMEDS_GenericAttribute::SALOMEDS_GenericAttribute(SALOMEDSImpl_GenericAttribute* theGA)
46 {
47   _isLocal = true;
48   _local_impl = theGA;
49   _corba_impl = SALOMEDS::GenericAttribute::_nil();
50 }
51
52 SALOMEDS_GenericAttribute::SALOMEDS_GenericAttribute(SALOMEDS::GenericAttribute_ptr theGA)
53 {
54 #ifdef WIN32
55   long pid =  (long)_getpid();
56 #else
57   long pid =  (long)getpid();
58 #endif  
59
60   CORBA::LongLong addr = theGA->GetLocalImpl(Kernel_Utils::GetHostname().c_str(), pid, _isLocal);
61   if(_isLocal) {
62     _local_impl = reinterpret_cast<SALOMEDSImpl_GenericAttribute*>(addr);
63     _corba_impl = SALOMEDS::GenericAttribute::_nil();
64   }
65   else {
66     _local_impl = NULL;
67     _corba_impl = SALOMEDS::GenericAttribute::_duplicate(theGA);
68   }
69 }
70
71 SALOMEDS_GenericAttribute::~SALOMEDS_GenericAttribute() 
72 {
73   if (!_isLocal) {
74     _corba_impl->Destroy();
75   }
76 }
77
78 void SALOMEDS_GenericAttribute::CheckLocked() 
79 {
80   if (_isLocal) {
81     SALOMEDS::Locker lock;
82     try {
83       _local_impl->CheckLocked();
84     }
85     catch(...) {
86       throw SALOMEDS::GenericAttribute::LockProtection();
87     }
88   }
89   else {
90     _corba_impl->CheckLocked();
91   }
92 }
93
94 std::string SALOMEDS_GenericAttribute::Type()
95 {
96   std::string aType;
97   if (_isLocal) {
98     SALOMEDS::Locker lock;
99     aType = _local_impl->Type();
100   }
101   else {
102     aType = (CORBA::String_var)_corba_impl->Type();
103   }
104   return aType;
105 }
106
107 std::string SALOMEDS_GenericAttribute::GetClassType()
108 {
109   std::string aType;
110   if (_isLocal) {
111     SALOMEDS::Locker lock;
112     aType = _local_impl->GetClassType();
113   }
114   else {
115     aType = (CORBA::String_var)_corba_impl->GetClassType();
116   }
117   return aType;
118 }
119
120 _PTR(SObject) SALOMEDS_GenericAttribute::GetSObject()
121 {
122   SALOMEDSClient_SObject* aSO = NULL;
123   if (_isLocal) {
124     SALOMEDS::Locker lock;
125     aSO = new SALOMEDS_SObject(_local_impl->GetSObject());
126   }
127   else {
128     aSO = new SALOMEDS_SObject((SALOMEDS::SObject_var)_corba_impl->GetSObject());
129   }
130
131   return _PTR(SObject)(aSO);
132 }
133
134
135 SALOMEDS_GenericAttribute* SALOMEDS_GenericAttribute::CreateAttribute(SALOMEDSImpl_GenericAttribute* theGA)
136 {
137   SALOMEDS::Locker lock;
138
139   SALOMEDS_GenericAttribute* aGA = NULL;
140   if(theGA) {
141      std::string aTypeOfAttribute = theGA->GetClassType();
142       __CreateGenericClientAttributeLocal
143   }
144   return aGA;
145 }
146
147 SALOMEDS_GenericAttribute* SALOMEDS_GenericAttribute::CreateAttribute(SALOMEDS::GenericAttribute_ptr theGA)
148 {
149   SALOMEDS_GenericAttribute* aGA = NULL;
150   if(!CORBA::is_nil(theGA)) {
151       CORBA::String_var astr=theGA->GetClassType();
152       std::string aTypeOfAttribute = astr.in();
153       __CreateGenericClientAttributeCORBA
154   }
155   return aGA;
156 }