]> SALOME platform Git repositories - tools/yacsgen.git/blob - module_generator/cpp_tmpl.py
Salome HOME
updated copyright message
[tools/yacsgen.git] / module_generator / cpp_tmpl.py
1 # Copyright (C) 2009-2023  EDF
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, or (at your option) any later version.
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
20 try:
21   from string import Template
22 except:
23   from module_generator.compat import Template,set
24
25 cxxCompo="""
26 #include "${component}.hxx"
27 #include <string>
28 #include <unistd.h>
29
30 #include <Calcium.hxx>
31 #include <CalciumException.hxx>
32 ${CalciumInterface}
33 #include <signal.h>
34 #include <SALOME_Embedded_NamingService_Client.hxx>
35 #include <SALOME_NamingService.hxx>
36 #include <Utils_SALOME_Exception.hxx>
37 #include <pthread.h>
38 #include <execinfo.h>
39
40 #define BUILD_EXE ${exe}
41
42 typedef void (*sighandler_t)(int);
43 sighandler_t setsig(int sig, sighandler_t handler)
44 {
45   struct sigaction context, ocontext;
46   context.sa_handler = handler;
47   sigemptyset(&context.sa_mask);
48   context.sa_flags = 0;
49   if (sigaction(sig, &context, &ocontext) == -1)
50     return SIG_ERR;
51   return ocontext.sa_handler;
52 }
53
54 static void AttachDebugger()
55 {
56   void *array[20];
57   size_t size=20;
58   char **strings;
59   size_t i;
60   std::string _what;
61   size = backtrace (array, size);
62   strings = backtrace_symbols (array, size);
63   for (i = 0; i < size; i++)
64      _what=_what+strings[i]+ '\\n';
65   free (strings);
66
67   std::cerr << pthread_self() << std::endl;
68   std::cerr << _what << std::endl;
69
70   if(getenv ("DEBUGGER"))
71     {
72       std::stringstream exec;
73 #if BUILD_EXE
74       exec << "$$DEBUGGER " << "${exe_path} " << getpid() << "&";
75 #else
76       exec << "$$DEBUGGER SALOME_Container " << getpid() << "&";
77 #endif
78       std::cerr << exec.str() << std::endl;
79       system(exec.str().c_str());
80       while(1);
81     }
82 }
83
84 static void THandler(int theSigId)
85 {
86   std::cerr << "SIGSEGV: "  << std::endl;
87   AttachDebugger();
88   //to exit or not to exit
89   _exit(1);
90 }
91
92 static void terminateHandler(void)
93 {
94   std::cerr << "Terminate: not managed exception !"  << std::endl;
95   AttachDebugger();
96   throw SALOME_Exception("Terminate: not managed exception !");
97 }
98
99 static void unexpectedHandler(void)
100 {
101   std::cerr << "Unexpected: unexpected exception !"  << std::endl;
102   AttachDebugger();
103   throw SALOME_Exception("Unexpected: unexpected exception !");
104 }
105
106
107 #define  _(A,B)   A##B
108 #ifdef _WIN32
109 #define F_FUNC(lname,uname) __stdcall uname
110 #define F_CALL(lname,uname) uname
111 #define STR_PSTR(str)       char *str, int _(Len,str)
112 #define STR_PLEN(str)
113 #define STR_PTR(str)        str
114 #define STR_LEN(str)        _(Len,str)
115 #define STR_CPTR(str)        str,strlen(str)
116 #define STR_CLEN(str)
117 #else
118 #define F_FUNC(lname,uname) _(lname,_)        /* Fortran function name */
119 #define F_CALL(lname,uname) _(lname,_)        /* Fortran function call */
120 #define STR_PSTR(str)       char *str         /* fortran string arg pointer */
121 #define STR_PLEN(str)       , int _(Len,str)  /* fortran string arg length */
122 #define STR_PTR(str)        str               /* fortran string pointer */
123 #define STR_LEN(str)        _(Len,str)        /* fortran string length */
124 #define STR_CPTR(str)        str              /* fortran string calling arg pointer */
125 #define STR_CLEN(str)       , strlen(str)     /* fortran string calling arg length */
126 #endif
127
128 //DEFS
129 ${servicesdef}
130 //ENDDEF
131
132 #include <calcium.h>
133
134 extern "C" void cp_exit(int err);
135
136 extern "C" void F_FUNC(cpexit,CPEXIT)(int *err)
137 {
138   if(*err==-1)
139     _exit(-1);
140   else
141     cp_exit(*err);
142 }
143
144 using namespace std;
145
146 //! Constructor for component "${component}" instance
147 /*!
148  *
149  */
150 ${component}_i::${component}_i(CORBA::ORB_ptr orb,
151                      PortableServer::POA_ptr poa,
152                      PortableServer::ObjectId * contId,
153                      const char *instanceName,
154                      const char *interfaceName)
155           : Superv_Component_i(orb, poa, contId, instanceName, interfaceName)
156 {
157 #if BUILD_EXE
158   setsig(SIGSEGV,&THandler);
159   set_terminate(&terminateHandler);
160   set_unexpected(&unexpectedHandler);
161 #endif
162   _thisObj = this ;
163   _id = _poa->activate_object(_thisObj);
164 }
165
166 ${component}_i::${component}_i(CORBA::ORB_ptr orb,
167                      PortableServer::POA_ptr poa,
168                      Engines::Container_ptr container,
169                      const char *instanceName,
170                      const char *interfaceName)
171           : Superv_Component_i(orb, poa, container, instanceName, interfaceName)
172 {
173 #if BUILD_EXE
174   setsig(SIGSEGV,&THandler);
175   set_terminate(&terminateHandler);
176   set_unexpected(&unexpectedHandler);
177 #endif
178   _thisObj = this ;
179   _id = _poa->activate_object(_thisObj);
180 }
181
182 //! Destructor for component "${component}" instance
183 ${component}_i::~${component}_i()
184 {
185 }
186
187 void ${component}_i::destroy()
188 {
189 #if BUILD_EXE
190   _remove_ref();
191   if(!CORBA::is_nil(_orb))
192     _orb->shutdown(0);
193 #else
194   Engines_Component_i::destroy();
195 #endif
196 }
197
198 //! Register datastream ports for a component service given its name
199 /*!
200  *  \param service_name : service name
201  *  \\return true if port registering succeeded, false if not
202  */
203 CORBA::Boolean
204 ${component}_i::init_service(const char * service_name) {
205   CORBA::Boolean rtn = false;
206   string s_name(service_name);
207 ${initservice}
208   return rtn;
209 }
210
211 ${servicesimpl}
212
213 extern "C"
214 {
215   PortableServer::ObjectId * ${component}Engine_factory( CORBA::ORB_ptr orb,
216                                                     PortableServer::POA_ptr poa,
217                                                     PortableServer::ObjectId * contId,
218                                                     const char *instanceName,
219                                                     const char *interfaceName)
220   {
221     MESSAGE("PortableServer::ObjectId * ${component}Engine_factory()");
222     ${component}_i * myEngine = new ${component}_i(orb, poa, contId, instanceName, interfaceName);
223     return myEngine->getId() ;
224   }
225   void yacsinit()
226   {
227     int argc=0;
228     char *argv=0;
229     CORBA::ORB_var orb = CORBA::ORB_init( argc , &argv ) ;
230     PortableServer::POAManager_var pman;
231     CORBA::Object_var obj;
232     try
233       {
234         obj = orb->resolve_initial_references("RootPOA");
235         PortableServer::POA_var  poa = PortableServer::POA::_narrow(obj);
236         PortableServer::POAManager_var pman = poa->the_POAManager();
237         std::string containerName(getenv("SALOME_CONTAINERNAME"));
238         std::string instanceName(getenv("SALOME_INSTANCE"));
239         obj=orb->string_to_object(getenv("SALOME_CONTAINER"));
240         Engines::Container_var container = Engines::Container::_narrow(obj);
241         ${component}_i * myEngine = new ${component}_i(orb, poa, container, instanceName.c_str(), "${component}");
242         pman->activate();
243         obj=myEngine->POA_${module}_ORB::${component}::_this();
244         Engines::EngineComponent_var component = Engines::EngineComponent::_narrow(obj);
245         string component_registerName = containerName + "/" + instanceName;
246         Engines::EmbeddedNamingService_var embedded_NS = container->get_embedded_NS_if_ssl();
247         if( CORBA::is_nil(embedded_NS))
248         {
249           // Use the salome session to get the naming service
250           SALOME_NamingService salome_NS(orb);
251           salome_NS.Register(component,component_registerName.c_str());
252         }
253         else
254         {
255           SALOME_Embedded_NamingService_Client ns_client(embedded_NS);
256           ns_client.Register(component,component_registerName.c_str());
257         }
258         orb->run();
259         orb->destroy();
260       }
261     catch(CORBA::Exception&)
262       {
263         std::cerr << "Caught CORBA::Exception."<< std::endl;
264       }
265     catch(std::exception& exc)
266       {
267         std::cerr << "Caught std::exception - "<<exc.what() << std::endl;
268       }
269     catch(...)
270       {
271         std::cerr << "Caught unknown exception." << std::endl;
272       }
273   }
274
275   void F_FUNC(yacsinit,YACSINIT)()
276   {
277     yacsinit();
278   }
279 }
280 """
281 cxxCompo=Template(cxxCompo)
282
283 hxxCompo="""
284 #ifndef _${component}_HXX_
285 #define _${component}_HXX_
286
287 #include <SALOME_Component.hh>
288 #include "Superv_Component_i.hxx"
289 #include "${module}.hh"
290
291 //COMPODEFS
292 ${compodefs}
293 //ENDDEF
294
295 class ${component}_i: public virtual POA_${module}_ORB::${component},
296                       ${inheritedclass} public virtual Superv_Component_i
297 {
298   public:
299     ${component}_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa,
300               PortableServer::ObjectId * contId,
301               const char *instanceName, const char *interfaceName);
302     ${component}_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa,
303               Engines::Container_ptr container,
304               const char *instanceName, const char *interfaceName);
305     virtual ~${component}_i();
306     void destroy();
307     CORBA::Boolean init_service(const char * service_name);
308 ${servicesdef}
309 };
310
311 extern "C"
312 {
313     PortableServer::ObjectId * ${component}Engine_factory( CORBA::ORB_ptr orb,
314                                                       PortableServer::POA_ptr poa,
315                                                       PortableServer::ObjectId * contId,
316                                                       const char *instanceName,
317                                                       const char *interfaceName);
318     void yacsinit();
319 }
320 #endif
321
322 """
323 hxxCompo=Template(hxxCompo)
324
325 cxxService="""
326 void ${component}_i::${service}(${parameters})
327 {
328   beginService("${component}_i::${service}");
329   Superv_Component_i * component = dynamic_cast<Superv_Component_i*>(this);
330   //char       nom_instance[INSTANCE_LEN];
331   //int info = cp_cd(component,nom_instance);
332   try
333     {
334 //BODY
335 ${body}
336 //ENDBODY
337       //cp_fin(component,CP_ARRET);
338     }
339   catch ( const CalciumException & ex)
340     {
341       std::cerr << ex.what() << std::endl;
342       //cp_fin(component,CP_ARRET);
343       SALOME::ExceptionStruct es;
344       es.text=CORBA::string_dup(ex.what());
345       es.type=SALOME::INTERNAL_ERROR;
346       throw SALOME::SALOME_Exception(es);
347     }
348   catch ( const SALOME_Exception & ex)
349     {
350       //cp_fin(component,CP_ARRET);
351       SALOME::ExceptionStruct es;
352       es.text=CORBA::string_dup(ex.what());
353       es.type=SALOME::INTERNAL_ERROR;
354       throw SALOME::SALOME_Exception(es);
355     }
356   catch ( const SALOME::SALOME_Exception & ex)
357     {
358       //cp_fin(component,CP_ARRET);
359       throw;
360     }
361   catch ( const std::exception& ex)
362     {
363       //std::cerr << typeid(ex).name() << std::endl;
364       SALOME::ExceptionStruct es;
365       es.text=CORBA::string_dup(ex.what());
366       es.type=SALOME::INTERNAL_ERROR;
367       throw SALOME::SALOME_Exception(es);
368     }
369   catch (...)
370     {
371       std::cerr << "unknown exception" << std::endl;
372 #if BUILD_EXE
373       _exit(-1);
374 #endif
375       //cp_fin(component,CP_ARRET);
376       SALOME::ExceptionStruct es;
377       es.text=CORBA::string_dup(" unknown exception");
378       es.type=SALOME::INTERNAL_ERROR;
379       throw SALOME::SALOME_Exception(es);
380     }
381   endService("${component}_i::${service}");
382 }
383
384 """
385 cxxService=Template(cxxService)
386
387 initService="""
388   if (s_name == "${service}")
389     {
390       try
391         {
392           //initialization CALCIUM ports IN
393 ${instream}
394           //initialization CALCIUM ports OUT
395 ${outstream}
396         }
397       catch(const PortAlreadyDefined& ex)
398         {
399           std::cerr << "${component}: " << ex.what() << std::endl;
400           //Ports already created : we use them
401         }
402       catch ( ... )
403         {
404           std::cerr << "${component}: unknown exception" << std::endl;
405         }
406       rtn = true;
407     }
408 """
409 initService=Template(initService)
410
411 exeCPP="""#!/bin/sh
412
413 export SALOME_CONTAINER=$$1
414 export SALOME_CONTAINERNAME=$$2
415 export SALOME_INSTANCE=$$3
416
417 ${compoexe}
418 """
419 exeCPP=Template(exeCPP)
420
421 # Makefile
422
423 # CMakeLists.txt in src/<component>
424 # template parameters:
425 #   module : module name
426 #   component : component name
427 #   componentlib : name of the target library
428 #   includes : additional headers, separated by spaces or \n. can be empty
429 #   sources : additional sources, separated by spaces or \n. can be empty
430 #   libs : additional libraries
431 #   find_libs : find_library commands
432 #   target_properties : set_target_properties commands
433 cmake_src_compo_cpp = """
434 # --- options ---
435 # additional include directories
436 INCLUDE_DIRECTORIES(
437   $${KERNEL_INCLUDE_DIRS}
438   $${OMNIORB_INCLUDE_DIR}
439   $${PROJECT_BINARY_DIR}
440   $${PROJECT_BINARY_DIR}/idl
441   ${includes}
442 )
443
444 # --- definitions ---
445 ADD_DEFINITIONS(
446   $${OMNIORB_DEFINITIONS}
447 )
448
449 # find additional libraries
450 ${find_libs}
451
452 # libraries to link to
453 SET(_link_LIBRARIES
454   $${OMNIORB_LIBRARIES}
455   $${KERNEL_SalomeIDLKernel}
456   $${KERNEL_OpUtil}
457   $${KERNEL_SalomeContainer}
458   SalomeIDL${module}
459   ${libs}
460 )
461
462 # --- headers ---
463
464 # header files / no moc processing
465
466 SET(${module}_HEADERS
467   ${component}.hxx
468 )
469
470 # --- sources ---
471
472 # sources / static
473 SET(${module}_SOURCES
474   ${component}.cxx
475   ${sources}
476 )
477
478 # --- rules ---
479
480 ADD_LIBRARY(${componentlib} $${${module}_SOURCES})
481 TARGET_LINK_LIBRARIES(${componentlib} $${_link_LIBRARIES} )
482 ${target_properties}
483 INSTALL(TARGETS ${componentlib} EXPORT $${PROJECT_NAME}TargetGroup DESTINATION $${SALOME_INSTALL_LIBS})
484
485 INSTALL(FILES $${${module}_HEADERS} DESTINATION $${SALOME_INSTALL_HEADERS})
486 """
487 cmake_src_compo_cpp = Template(cmake_src_compo_cpp)