Salome HOME
CCAR: add definition parameter "sources" to C++, F77, Python components.
[tools/yacsgen.git] / module_generator / cpp_tmpl.py
1 try:
2   from string import Template
3 except:
4   from compat import Template,set
5
6 cxxCompo="""
7 #include "${component}.hxx"
8 #include <string>
9 #include <unistd.h>
10
11 #include <Calcium.hxx>
12 #include <calcium.h>
13 #include <signal.h>
14 #include <SALOME_NamingService.hxx>
15 #include <Utils_SALOME_Exception.hxx>
16
17 typedef void (*sighandler_t)(int);
18 sighandler_t setsig(int sig, sighandler_t handler)
19 {
20   struct sigaction context, ocontext;
21   context.sa_handler = handler;
22   sigemptyset(&context.sa_mask);
23   context.sa_flags = 0;
24   if (sigaction(sig, &context, &ocontext) == -1)
25     return SIG_ERR;
26   return ocontext.sa_handler;
27 }
28
29 static void AttachDebugger()
30 {
31   if(getenv ("DEBUGGER"))
32     {
33       std::stringstream exec;
34 #if ${exe}
35       exec << "$$DEBUGGER " << "${exe_path} " << getpid() << "&";
36 #else
37       exec << "$$DEBUGGER SALOME_Container " << getpid() << "&";
38 #endif
39       std::cerr << exec.str() << std::endl;
40       system(exec.str().c_str());
41       while(1);
42     }
43 }
44
45 static void THandler(int theSigId)
46 {
47   std::cerr << "SIGSEGV: "  << std::endl;
48   AttachDebugger();
49   //to exit or not to exit
50   _exit(1);
51 }
52
53 static void terminateHandler(void)
54 {
55   std::cerr << "Terminate: not managed exception !"  << std::endl;
56   AttachDebugger();
57   throw SALOME_Exception("Terminate: not managed exception !");
58 }
59
60 static void unexpectedHandler(void)
61 {
62   std::cerr << "Unexpected: unexpected exception !"  << std::endl;
63   AttachDebugger();
64   throw SALOME_Exception("Unexpected: unexpected exception !");
65 }
66
67
68 #define  _(A,B)   A##B
69 #ifdef _WIN32
70 #define F_FUNC(lname,uname) __stdcall uname
71 #define F_CALL(lname,uname) uname
72 #define STR_PSTR(str)       char *str, int _(Len,str)
73 #define STR_PLEN(str)
74 #define STR_PTR(str)        str
75 #define STR_LEN(str)        _(Len,str)
76 #define STR_CPTR(str)        str,strlen(str)
77 #define STR_CLEN(str)
78 #else
79 #define F_FUNC(lname,uname) _(lname,_)        /* Fortran function name */
80 #define F_CALL(lname,uname) _(lname,_)        /* Fortran function call */
81 #define STR_PSTR(str)       char *str         /* fortran string arg pointer */
82 #define STR_PLEN(str)       , int _(Len,str)  /* fortran string arg length */
83 #define STR_PTR(str)        str               /* fortran string pointer */
84 #define STR_LEN(str)        _(Len,str)        /* fortran string length */
85 #define STR_CPTR(str)        str              /* fortran string calling arg pointer */
86 #define STR_CLEN(str)       , strlen(str)     /* fortran string calling arg length */
87 #endif
88
89 //DEFS
90 ${servicesdef}
91 //ENDDEF
92
93 extern "C" void cp_exit(int err);
94
95 extern "C" void F_FUNC(cpexit,CPEXIT)(int err)
96 {
97   if(err==-1)
98     _exit(-1);
99   else
100     cp_exit(err);
101 }
102
103 using namespace std;
104
105 //! Constructor for component "${component}" instance
106 /*!
107  *
108  */
109 ${component}_i::${component}_i(CORBA::ORB_ptr orb,
110                      PortableServer::POA_ptr poa,
111                      PortableServer::ObjectId * contId,
112                      const char *instanceName,
113                      const char *interfaceName)
114           : Superv_Component_i(orb, poa, contId, instanceName, interfaceName)
115 {
116   std::cerr << "create component" << std::endl;
117 #if ${exe}
118   setsig(SIGSEGV,&THandler);
119   set_terminate(&terminateHandler);
120   set_unexpected(&unexpectedHandler);
121 #endif
122   _thisObj = this ;
123   _id = _poa->activate_object(_thisObj);
124 }
125
126 ${component}_i::${component}_i(CORBA::ORB_ptr orb,
127                      PortableServer::POA_ptr poa,
128                      Engines::Container_ptr container,
129                      const char *instanceName,
130                      const char *interfaceName)
131           : Superv_Component_i(orb, poa, container, instanceName, interfaceName)
132 {
133 #if ${exe}
134   setsig(SIGSEGV,&THandler);
135   set_terminate(&terminateHandler);
136   set_unexpected(&unexpectedHandler);
137 #endif
138   _thisObj = this ;
139   _id = _poa->activate_object(_thisObj);
140 }
141
142 //! Destructor for component "${component}" instance
143 ${component}_i::~${component}_i()
144 {
145 }
146
147 void ${component}_i::destroy()
148 {
149   Engines_Component_i::destroy();
150 #if ${exe}
151   if(!CORBA::is_nil(_orb))
152     _orb->shutdown(0);
153 #endif
154 }
155
156 //! Register datastream ports for a component service given its name
157 /*!
158  *  \param service_name : service name
159  *  \\return true if port registering succeeded, false if not
160  */
161 CORBA::Boolean
162 ${component}_i::init_service(const char * service_name) {
163   CORBA::Boolean rtn = false;
164   string s_name(service_name);
165 ${initservice}
166   return rtn;
167 }
168
169 ${servicesimpl}
170
171 extern "C"
172 {
173   PortableServer::ObjectId * ${component}Engine_factory( CORBA::ORB_ptr orb,
174                                                     PortableServer::POA_ptr poa,
175                                                     PortableServer::ObjectId * contId,
176                                                     const char *instanceName,
177                                                     const char *interfaceName)
178   {
179     MESSAGE("PortableServer::ObjectId * ${component}Engine_factory()");
180     ${component}_i * myEngine = new ${component}_i(orb, poa, contId, instanceName, interfaceName);
181     return myEngine->getId() ;
182   }
183   void yacsinit()
184   {
185     int argc=0;
186     char *argv=0;
187     CORBA::ORB_var orb = CORBA::ORB_init( argc , &argv ) ;
188     PortableServer::POAManager_var pman;
189     CORBA::Object_var obj;
190     try
191       {
192         SALOME_NamingService * salomens = new SALOME_NamingService(orb);
193         obj = orb->resolve_initial_references("RootPOA");
194         PortableServer::POA_var  poa = PortableServer::POA::_narrow(obj);
195         PortableServer::POAManager_var pman = poa->the_POAManager();
196         std::string containerName(getenv("SALOME_CONTAINERNAME"));
197         std::string instanceName(getenv("SALOME_INSTANCE"));
198         obj=orb->string_to_object(getenv("SALOME_CONTAINER"));
199         Engines::Container_var container = Engines::Container::_narrow(obj);
200         ${component}_i * myEngine = new ${component}_i(orb, poa, container, instanceName.c_str(), "${component}");
201         pman->activate();
202         obj=myEngine->_this();
203         Engines::Component_var component = Engines::Component::_narrow(obj);
204         string component_registerName = containerName + "/" + instanceName;
205         salomens->Register(component,component_registerName.c_str());
206         orb->run();
207         orb->destroy();
208       }
209     catch(CORBA::Exception&)
210       {
211         std::cerr << "Caught CORBA::Exception."<< std::endl;
212       }
213     catch(std::exception& exc)
214       {
215         std::cerr << "Caught std::exception - "<<exc.what() << std::endl;
216       }
217     catch(...)
218       {
219         std::cerr << "Caught unknown exception." << std::endl;
220       }
221   }
222
223   void F_FUNC(yacsinit,YACSINIT)()
224   {
225     yacsinit();
226   }
227 }
228 """
229 cxxCompo=Template(cxxCompo)
230
231 hxxCompo="""
232 #ifndef _${component}_HXX_
233 #define _${component}_HXX_
234
235 #include "Superv_Component_i.hxx"
236 #include "${module}.hh"
237
238 class ${component}_i:
239   public virtual POA_${module}::${component},
240   public virtual Superv_Component_i
241 {
242   public:
243     ${component}_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa,
244               PortableServer::ObjectId * contId,
245               const char *instanceName, const char *interfaceName);
246     ${component}_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa,
247               Engines::Container_ptr container,
248               const char *instanceName, const char *interfaceName);
249     virtual ~${component}_i();
250     void destroy();
251     CORBA::Boolean init_service(const char * service_name);
252 ${servicesdef}
253 };
254
255 extern "C"
256 {
257     PortableServer::ObjectId * ${component}Engine_factory( CORBA::ORB_ptr orb,
258                                                       PortableServer::POA_ptr poa,
259                                                       PortableServer::ObjectId * contId,
260                                                       const char *instanceName,
261                                                       const char *interfaceName);
262     void yacsinit();
263 }
264 #endif
265
266 """
267 hxxCompo=Template(hxxCompo)
268
269 cxxService="""
270 void ${component}_i::${service}(${parameters})
271 {
272   std::cerr << "${component}_i::${service}" << std::endl;
273   beginService("${component}_i::${service}");
274   Superv_Component_i * component = dynamic_cast<Superv_Component_i*>(this);
275   char       nom_instance[INSTANCE_LEN];
276   int info = cp_cd(component,nom_instance);
277   try
278     {
279 //BODY
280 ${body}
281 //ENDBODY
282       cp_fin(component,CP_ARRET);
283     }
284   catch ( const CalciumException & ex)
285     {
286       std::cerr << ex.what() << std::endl;
287       cp_fin(component,CP_ARRET);
288       SALOME::ExceptionStruct es;
289       es.text=CORBA::string_dup(ex.what());
290       es.type=SALOME::INTERNAL_ERROR;
291       throw SALOME::SALOME_Exception(es);
292     }
293   catch ( const SALOME_Exception & ex)
294     {
295       cp_fin(component,CP_ARRET);
296       SALOME::ExceptionStruct es;
297       es.text=CORBA::string_dup(ex.what());
298       es.type=SALOME::INTERNAL_ERROR;
299       throw SALOME::SALOME_Exception(es);
300     }
301   catch ( const SALOME::SALOME_Exception & ex)
302     {
303       cp_fin(component,CP_ARRET);
304       throw;
305     }
306   catch (...)
307     {
308       std::cerr << "unknown exception" << std::endl;
309 #if ${exe}
310       _exit(-1);
311 #endif
312       cp_fin(component,CP_ARRET);
313       SALOME::ExceptionStruct es;
314       es.text=CORBA::string_dup(" unknown exception");
315       es.type=SALOME::INTERNAL_ERROR;
316       throw SALOME::SALOME_Exception(es);
317     }
318   endService("${component}_i::${service}");
319   std::cerr << "end of ${component}_i::${service}" << std::endl;
320 }
321
322 """
323 cxxService=Template(cxxService)
324
325 initService="""
326   if (s_name == "${service}")
327     {
328       try
329         {
330           //initialization CALCIUM ports IN
331 ${instream}
332           //initialization CALCIUM ports OUT
333 ${outstream}
334         }
335       catch(const PortAlreadyDefined& ex)
336         {
337           std::cerr << "${component}: " << ex.what() << std::endl;
338           //Ports already created : we use them
339         }
340       catch ( ... )
341         {
342           std::cerr << "${component}: unknown exception" << std::endl;
343         }
344       rtn = true;
345     }
346 """
347 initService=Template(initService)
348
349 exeCPP="""#!/bin/sh
350
351 export SALOME_CONTAINER=$$1
352 export SALOME_CONTAINERNAME=$$2
353 export SALOME_INSTANCE=$$3
354
355 ${compoexe}
356 """
357 exeCPP=Template(exeCPP)
358
359 # Makefile
360
361 compoMakefile="""
362 include $$(top_srcdir)/adm_local/make_common_starter.am
363
364 AM_CFLAGS=$$(KERNEL_INCLUDES) -fexceptions
365
366 lib_LTLIBRARIES = lib${component}Engine.la
367 lib${component}Engine_la_SOURCES      = ${component}.cxx ${sources}
368 nodist_lib${component}Engine_la_SOURCES =
369 lib${component}Engine_la_CXXFLAGS = -I$$(top_builddir)/idl  $$(KERNEL_INCLUDES) ${includes}
370 lib${component}Engine_la_FFLAGS = $$(KERNEL_INCLUDES) -fexceptions ${includes}
371 lib${component}Engine_la_LIBADD   = -L$$(top_builddir)/idl -l${module} $$(FLIBS) ${libs}
372 lib${component}Engine_la_LDFLAGS = ${rlibs}
373 salomeinclude_HEADERS = ${component}.hxx
374 """
375 compoMakefile=Template(compoMakefile)
376
377 compoEXEMakefile="""
378 include $$(top_srcdir)/adm_local/make_common_starter.am
379
380 AM_CFLAGS=$$(KERNEL_INCLUDES) -fexceptions
381
382 lib_LTLIBRARIES = lib${component}Exelib.la
383 lib${component}Exelib_la_SOURCES      = ${component}.cxx
384 nodist_lib${component}Exelib_la_SOURCES =
385 lib${component}Exelib_la_CXXFLAGS = -I$$(top_builddir)/idl  $$(KERNEL_INCLUDES) ${includes}
386 lib${component}Exelib_la_FFLAGS = $$(KERNEL_INCLUDES) -fexceptions ${includes}
387 lib${component}Exelib_la_LIBADD   = -L$$(top_builddir)/idl -l${module} $$(FLIBS) ${libs}
388 lib${component}Exelib_la_LDFLAGS = ${rlibs}
389 salomeinclude_HEADERS = ${component}.hxx
390 # These files are executable scripts
391 dist_salomescript_SCRIPTS= ${component}.exe
392 """
393 compoEXEMakefile=Template(compoEXEMakefile)
394