Salome HOME
CCAR: move genericobj management in SALOMEDSImpl from Study to AttributeIOR
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeIOR.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   : SALOMEDSImpl_AttributeIOR.cxx
23 //  Author : Sergey RUIN
24 //  Module : SALOME
25 //
26 #include "SALOMEDSImpl_AttributeIOR.hxx"
27 #include "SALOMEDSImpl_Study.hxx"
28
29 using namespace std;
30
31 //to disable automatic genericobj management comment the following line
32 #define WITHGENERICOBJ
33
34 #ifdef WITHGENERICOBJ
35 #include "SALOME_GenericObj_i.hh"
36
37 static CORBA::ORB_var getORB()
38 {
39   int argc=0;
40   return CORBA::ORB_init(argc,0);
41 }
42
43 void IORGenericObjDecref(const std::string& anIOR)
44 {
45   CORBA::Object_var obj;
46   SALOME::GenericObj_var gobj;
47   try
48     {
49        obj = getORB()->string_to_object(anIOR.c_str());
50        gobj = SALOME::GenericObj::_narrow(obj);
51        if(! CORBA::is_nil(gobj) )
52          {
53            gobj->Destroy();
54          }
55     }
56   catch(const CORBA::Exception& e)
57     {
58     }
59 }
60
61 void IORGenericObjIncref(const std::string& anIOR)
62 {
63   CORBA::Object_var obj;
64   SALOME::GenericObj_var gobj;
65   try
66     {
67       obj = getORB()->string_to_object(anIOR.c_str());
68       gobj = SALOME::GenericObj::_narrow(obj);
69       if(! CORBA::is_nil(gobj) )
70         {
71           gobj->Register();
72         }
73     }
74   catch(const CORBA::Exception& e)
75     {
76     }
77 }
78 #endif
79
80 //=======================================================================
81 //function : GetID
82 //purpose  : 
83 //=======================================================================
84
85 const std::string& SALOMEDSImpl_AttributeIOR::GetID () 
86 {
87   static std::string SALOMEDSImpl_AttributeIORID ("92888E01-7074-11d5-A690-0800369C8A03");
88   return SALOMEDSImpl_AttributeIORID;
89 }
90
91
92
93 //=======================================================================
94 //function : Set
95 //purpose  : 
96 //=======================================================================
97
98 SALOMEDSImpl_AttributeIOR* SALOMEDSImpl_AttributeIOR::Set (const DF_Label& L,
99                                                            const std::string& S) 
100 {
101   SALOMEDSImpl_AttributeIOR* A = NULL;
102   if (!(A=(SALOMEDSImpl_AttributeIOR*)L.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
103     A = new  SALOMEDSImpl_AttributeIOR(); 
104     L.AddAttribute(A);
105   }
106
107   A->SetValue(S); 
108   return A;
109 }
110
111 //=======================================================================
112 //function : SetValue
113 //purpose  : 
114 //=======================================================================
115 void SALOMEDSImpl_AttributeIOR::SetValue(const std::string& theValue)
116 {
117   CheckLocked();
118
119   SALOMEDSImpl_Study* study=SALOMEDSImpl_Study::GetStudy(Label());
120
121   Backup();
122   //remove IOR entry in study
123   if(theValue != myString)
124     {
125       IORGenericObjIncref(theValue);
126       IORGenericObjDecref(myString);
127       study->DeleteIORLabelMapItem(myString);
128     }
129
130   myString = theValue;
131
132   //add IOR entry in study
133   SALOMEDSImpl_Study::IORUpdated(this);
134 }
135
136 //=======================================================================
137 //function : Value
138 //purpose  : 
139 //=======================================================================
140 std::string SALOMEDSImpl_AttributeIOR::Value() const
141 {
142   return myString;
143 }
144
145 //=======================================================================
146 //function : constructor
147 //purpose  : 
148 //=======================================================================
149 SALOMEDSImpl_AttributeIOR::SALOMEDSImpl_AttributeIOR()
150 :SALOMEDSImpl_GenericAttribute("AttributeIOR")
151 {
152 }
153
154 SALOMEDSImpl_AttributeIOR::~SALOMEDSImpl_AttributeIOR()
155 {
156   IORGenericObjDecref(myString);
157 }
158
159 //=======================================================================
160 //function : ID
161 //purpose  : 
162 //=======================================================================
163
164 const std::string& SALOMEDSImpl_AttributeIOR::ID () const { return GetID(); }
165
166
167 //=======================================================================
168 //function : NewEmpty
169 //purpose  : 
170 //=======================================================================
171
172 DF_Attribute* SALOMEDSImpl_AttributeIOR::NewEmpty () const
173 {  
174   return new SALOMEDSImpl_AttributeIOR(); 
175 }
176
177 //=======================================================================
178 //function : Restore
179 //purpose  : 
180 //=======================================================================
181
182 void SALOMEDSImpl_AttributeIOR::Restore( DF_Attribute* with) 
183 {
184   myString = dynamic_cast<SALOMEDSImpl_AttributeIOR*>(with)->Value();
185   return;
186 }
187
188 //=======================================================================
189 //function : Paste
190 //purpose  : 
191 //=======================================================================
192
193 void SALOMEDSImpl_AttributeIOR::Paste (DF_Attribute* into)
194 {
195   dynamic_cast<SALOMEDSImpl_AttributeIOR*>(into)->SetValue(myString);
196 }
197