Salome HOME
#24735 [CEA][Windows] HELLO compilation issue
[samples/hello.git] / src / HELLO / HELLO.cxx
1 // Copyright (C) 2007-2021  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, or (at your option) any later version.
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
23 #include "HELLO.hxx"
24 #include "HELLO_version.h"
25
26 #include <SALOME_KernelServices.hxx>
27 #include <SALOMEconfig.h>
28 #include CORBA_CLIENT_HEADER(SALOMEDS)
29 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
30
31 #include <Utils_ExceptHandlers.hxx>
32
33 #include <string>
34
35 #ifdef GetObject
36 #undef GetObject
37 #endif
38
39 namespace {
40   static std::string studyName( const std::string& name )
41   {
42     std::string fullName = "/Hello/";
43     fullName += name;
44     return fullName;
45   }
46 }
47 /*!
48   \brief Constructor
49
50   Creates an instance of the HELLO component engine
51   
52   \param orb reference to the ORB
53   \param poa reference to the POA
54   \param contId CORBA object ID, pointing to the owner SALOME container
55   \param instanceName SALOME component instance name
56   \param interfaceName SALOME component interface name
57 */
58 HELLO_Abstract::HELLO_Abstract( CORBA::ORB_ptr orb,
59               PortableServer::POA_ptr poa,
60               PortableServer::ObjectId* contId, 
61               const char* instanceName, 
62               const char* interfaceName,
63         bool withRegistry)
64   : Engines_Component_i( orb, poa, contId, instanceName, interfaceName, false, withRegistry)
65 {
66   _thisObj = this;
67   _id = _poa->activate_object( _thisObj ); // register and activate this servant object
68 }
69
70 /*!
71   \brief Destructor
72
73   Clean up allocated resources
74 */
75 HELLO_Abstract::~HELLO_Abstract()
76 {
77   // nothing to do
78 }
79
80 /*!
81   \brief Say hello to \a name
82   \param name person's name
83   \return operation status
84 */
85 HELLO_ORB::status HELLO_Abstract::hello( const char* name )
86 {
87   // set exception handler to catch unexpected CORBA exceptions
88   Unexpect aCatch(SALOME_SalomeException);
89   
90   // set result status to error initially
91   HELLO_ORB::status result = HELLO_ORB::OP_ERR_UNKNOWN;
92
93   SALOMEDS::Study_var aStudy = this->getStudyServant();
94   // check if reference to study is valid
95   if ( !CORBA::is_nil( aStudy ) ) {
96     // get full object path
97     std::string fullName = studyName( name );
98     // check if the object with the same name is already registered in the study
99     SALOMEDS::SObject_var sobj = aStudy->FindObjectByPath( fullName.c_str() );
100     if ( !CORBA::is_nil( sobj ) ) {
101       // person is already registered in the study -> ERROR
102       result = HELLO_ORB::OP_ERR_ALREADY_MET;
103     }
104     else {
105       // person is not registered yet -> register
106       SALOMEDS::StudyBuilder_var     studyBuilder   = aStudy->NewBuilder();          // study builder
107       SALOMEDS::UseCaseBuilder_var   useCaseBuilder = aStudy->GetUseCaseBuilder();   // use case builder
108
109       // find HELLO component; create it if not found
110       SALOMEDS::SComponent_var father = aStudy->FindComponent( "HELLO" );
111       if ( CORBA::is_nil( father ) ) {
112         // create component
113         father = studyBuilder->NewComponent( "HELLO" ); 
114         // set name attribute
115         father->SetAttrString( "AttributeName", "Hello" );
116         // set icon attribute
117         father->SetAttrString( "AttributePixMap", "ICON_HELLO" );
118         // register component in the study
119         studyBuilder->DefineComponentInstance( father, HELLO_Gen::_this() );
120         // add component to the use case tree
121         // (to support tree representation customization and drag-n-drop)
122         useCaseBuilder->SetRootCurrent();
123         useCaseBuilder->Append( father ); // component object is added as the top level item
124       }
125
126       // create new sub-object, as a child of the component object
127       sobj = studyBuilder->NewObject( father );
128       sobj->SetAttrString( "AttributeName", name );
129       // add object to the use case tree
130       // (to support tree representation customization and drag-n-drop)
131       useCaseBuilder->AppendTo( father, sobj );
132
133       // cleanup
134       father->UnRegister();
135       sobj->UnRegister();
136
137       // set operation status
138       result = HELLO_ORB::OP_OK;
139     }
140   }
141   
142   // return result of the operation
143   return result;
144 }
145
146 /*!
147   \brief Say goodbye to \a name
148   \param name person's name
149   \return operation status
150 */
151 HELLO_ORB::status HELLO_Abstract::goodbye( const char* name )
152 {
153   // set exception handler to catch unexpected CORBA exceptions
154   Unexpect aCatch(SALOME_SalomeException);
155   
156   // set result status to error initially
157   HELLO_ORB::status result = HELLO_ORB::OP_ERR_UNKNOWN;
158
159   SALOMEDS::Study_var aStudy = this->getStudyServant();
160   // check if reference to study is valid
161   if ( !CORBA::is_nil( aStudy ) ) {
162     // get full object path
163     std::string fullName = studyName( name );
164
165     // initially set error status: person is not registered
166     result = HELLO_ORB::OP_ERR_DID_NOT_MEET;
167
168     // check if the object with the same name is registered in the study
169     // find all objects with same name
170     SALOMEDS::StudyBuilder_var studyBuilder = aStudy->NewBuilder();            // study builder
171     SALOMEDS::UseCaseBuilder_var useCaseBuilder = aStudy->GetUseCaseBuilder(); // use case builder
172     SALOMEDS::SObject_var sobj = aStudy->FindObjectByPath( fullName.c_str() );
173     while ( !CORBA::is_nil( sobj ) ) {
174       std::list<SALOMEDS::SObject_var> toRemove;
175       toRemove.push_back( sobj );
176       SALOMEDS::UseCaseIterator_var useCaseIt = useCaseBuilder->GetUseCaseIterator( sobj ); // use case iterator
177       for ( useCaseIt->Init( true ); useCaseIt->More(); useCaseIt->Next() )
178         toRemove.push_back( useCaseIt->Value() );
179       // perform removing of all found objects (recursively with children)
180       std::list<SALOMEDS::SObject_var>::const_iterator it;
181       for( it = toRemove.begin(); it != toRemove.end(); ++it ) {
182         sobj = *it;
183         // remove object from the study
184         // - normally it's enough to call RemoveObject() method
185         // RemoveObject`WithChildren() also removes all children recursively if there are any
186         // - it's not necessary to remove it from use case builder, it is done automatically
187         studyBuilder->RemoveObjectWithChildren( sobj );
188         
189         // cleanup
190         sobj->UnRegister();
191         
192         // set operation status to OK as at least one object is removed
193         result = HELLO_ORB::OP_OK;
194       }
195       sobj = aStudy->FindObjectByPath( fullName.c_str() );
196     }
197   }
198   
199   // return result of the operation
200   return result;
201 }
202
203 /*!
204   \brief Copy or move objects to the specified position
205   
206   This function is used in the drag-n-drop functionality.
207   
208   \param what objects being copied/moved
209   \param where parent object where objects are copied/moved to
210   \param row position in the parent object's children list at which objects are copied/moved
211   \param isCopy \c true if object are copied or \c false otherwise
212 */
213 void HELLO_Abstract::copyOrMove( const HELLO_ORB::object_list& what,
214                         SALOMEDS::SObject_ptr where,
215                         CORBA::Long row, CORBA::Boolean isCopy )
216 {
217   if ( CORBA::is_nil( where ) ) return; // bad parent
218
219   SALOMEDS::Study_var aStudy = this->getStudyServant();
220   SALOMEDS::StudyBuilder_var studyBuilder = aStudy->NewBuilder();               // study builder
221   SALOMEDS::UseCaseBuilder_var useCaseBuilder = aStudy->GetUseCaseBuilder();    // use case builder
222   SALOMEDS::SComponent_var father = where->GetFatherComponent();               // father component
223   std::string dataType = father->ComponentDataType();
224   if ( dataType != "HELLO" ) return; // not a HELLO component
225   
226   SALOMEDS::SObject_var objAfter;
227   if ( row >= 0 && useCaseBuilder->HasChildren( where ) ) {
228     // insert at given row -> find insertion position
229     SALOMEDS::UseCaseIterator_var useCaseIt = useCaseBuilder->GetUseCaseIterator( where );
230     int i;
231     for ( i = 0; i < row && useCaseIt->More(); i++, useCaseIt->Next() );
232     if ( i == row && useCaseIt->More() ) {
233       objAfter = useCaseIt->Value();
234     }
235   }
236   
237   for ( int i = 0; i < what.length(); i++ ) {
238     SALOMEDS::SObject_var sobj = what[i];
239     if ( CORBA::is_nil( sobj ) ) continue; // skip bad object
240     if ( isCopy ) {
241       // copying is performed
242       // get name of the object
243       CORBA::String_var name = sobj->GetName();
244       // create new object, as a child of the component object
245       SALOMEDS::SObject_var new_sobj = studyBuilder->NewObject( father );
246       new_sobj->SetAttrString( "AttributeName", name.in() );
247       sobj = new_sobj;
248     }
249     // insert the object or its copy to the use case tree
250     if ( !CORBA::is_nil( objAfter ) )
251       useCaseBuilder->InsertBefore( sobj, objAfter ); // insert at given row
252     else
253       useCaseBuilder->AppendTo( where, sobj );        // append to the end of list
254   }
255 }
256
257 // Version information
258 char* HELLO_Abstract::getVersion()
259 {
260 #if defined(HELLO_DEVELOPMENT)
261   return CORBA::string_dup(HELLO_VERSION_STR"dev");
262 #else
263   return CORBA::string_dup(HELLO_VERSION_STR);
264 #endif
265 }
266
267 SALOMEDS::Study_var HELLO_Session::getStudyServant()
268 {
269   return KERNEL::getStudyServant();
270 }
271
272 #include "SALOMEDS_Study_i.hxx"
273
274 SALOMEDS::Study_var HELLO_No_Session::getStudyServant()
275 {
276   return SALOMEDS::Study::_duplicate(KERNEL::getStudyServantSA());
277 }
278
279 extern "C"
280 {
281   /*!
282     \brief Exportable factory function: create an instance of the HELLO component engine
283     \param orb reference to the ORB
284     \param poa reference to the POA
285     \param contId CORBA object ID, pointing to the owner SALOME container
286     \param instanceName SALOME component instance name
287     \param interfaceName SALOME component interface name
288     \return CORBA object identifier of the registered servant
289   */
290   PortableServer::ObjectId* HELLOEngine_factory( CORBA::ORB_ptr orb,
291                                                  PortableServer::POA_ptr poa, 
292                                                  PortableServer::ObjectId* contId,
293                                                  const char* instanceName, 
294                                                  const char* interfaceName )
295   {
296     HELLO_Session* component = new HELLO_Session( orb, poa, contId, instanceName, interfaceName );
297     return component->getId();
298   }
299 }