Salome HOME
CCAR: remove some memory leaks in SALOMEDS CORBA interface
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_GenericAttribute_i.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_i.cxx
23 //  Author : Sergey RUIN
24 //  Module : SALOME
25 //
26 #include "utilities.h"
27 #include "SALOMEDS_GenericAttribute_i.hxx"
28 #include "SALOMEDS_Attributes.hxx"
29 #include "SALOMEDS.hxx"
30 #include "SALOMEDSImpl_SObject.hxx"
31 #include "SALOMEDSImpl_Study.hxx"
32 #include "Utils_ExceptHandlers.hxx"
33 #include "Basics_Utils.hxx"
34 #include <map>
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 UNEXPECT_CATCH(GALockProtection, SALOMEDS::GenericAttribute::LockProtection);
46
47 SALOMEDS_GenericAttribute_i::SALOMEDS_GenericAttribute_i(DF_Attribute* theImpl, CORBA::ORB_ptr theOrb)
48 {
49   _orb = CORBA::ORB::_duplicate(theOrb);
50   _impl = theImpl;
51 }
52
53 SALOMEDS_GenericAttribute_i::~SALOMEDS_GenericAttribute_i()
54 {
55 }
56
57 void SALOMEDS_GenericAttribute_i::CheckLocked() throw (SALOMEDS::GenericAttribute::LockProtection) 
58 {
59   SALOMEDS::Locker lock;
60   Unexpect aCatch(GALockProtection);
61
62   if (_impl  && !CORBA::is_nil(_orb)) {
63     try {
64       SALOMEDSImpl_GenericAttribute::Impl_CheckLocked(_impl);
65     }
66     catch (...) {
67       throw SALOMEDS::GenericAttribute::LockProtection();
68     }
69   }
70 }
71
72 SALOMEDS::SObject_ptr SALOMEDS_GenericAttribute_i::GetSObject() 
73 {
74   SALOMEDS::Locker lock;
75   if (!_impl || _impl->Label().IsNull()) return SALOMEDS::SObject::_nil();
76   SALOMEDSImpl_SObject so_impl = SALOMEDSImpl_Study::SObject(_impl->Label());
77   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (so_impl, _orb);
78   return so._retn();
79 }
80
81
82 char* SALOMEDS_GenericAttribute_i::Type() 
83 {
84   SALOMEDS::Locker lock;
85   if (_impl) {
86     string type = SALOMEDSImpl_GenericAttribute::Impl_GetType(_impl);
87     return CORBA::string_dup(type.c_str());
88   }    
89
90   return (char*)"";
91 }
92
93 char* SALOMEDS_GenericAttribute_i::GetClassType()
94 {
95   SALOMEDS::Locker lock;
96   if (_impl) {
97     string class_type = SALOMEDSImpl_GenericAttribute::Impl_GetClassType(_impl);
98     return CORBA::string_dup(class_type.c_str());
99   }
100
101   return (char*)"";
102 }
103
104
105 SALOMEDS::GenericAttribute_ptr SALOMEDS_GenericAttribute_i::CreateAttribute
106                                                          (DF_Attribute* theAttr,
107                                                           CORBA::ORB_ptr theOrb)
108 {
109   SALOMEDS::Locker lock;
110
111   string aClassType = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(theAttr)->GetClassType();
112   char* aTypeOfAttribute = (char*)aClassType.c_str();
113   SALOMEDS::GenericAttribute_var anAttribute;
114   SALOMEDS_GenericAttribute_i* attr_servant = NULL;
115   __CreateGenericCORBAAttribute
116
117   return anAttribute._retn(); 
118 }
119
120 //===========================================================================
121 //   PRIVATE FUNCTIONS
122 //===========================================================================
123 CORBA::LongLong SALOMEDS_GenericAttribute_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
124 {
125 #ifdef WIN32
126   long pid = (long)_getpid();
127 #else
128   long pid = (long)getpid();
129 #endif
130   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
131   return reinterpret_cast<CORBA::LongLong>(_impl);
132 }