Salome HOME
CCAR: split file __init__.py into several files : templates + implementation specific
[tools/yacsgen.git] / module_generator / cppcompo.py
1 """
2   Module that defines CPPComponent for SALOME components implemented in C++
3 """
4
5 from gener import Component, Invalid
6 from cpp_tmpl import initService, cxxService, hxxCompo, cxxCompo
7 from cpp_tmpl import compoEXEMakefile, compoMakefile, exeCPP
8
9 class CPPComponent(Component):
10   def __init__(self, name, services=None, libs="", rlibs="", includes="", 
11                      kind="lib", exe_path=None):
12     self.exe_path = exe_path
13     Component.__init__(self, name, services, impl="CPP", libs=libs, 
14                              rlibs=rlibs, includes=includes, kind=kind)
15
16   def validate(self):
17     """ validate component definition parameters"""
18     Component.validate(self)
19     kinds = ("lib", "exe")
20     if self.kind not in kinds:
21       raise Invalid("kind must be one of %s" % kinds)
22
23     if self.kind == "exe" :
24       if not self.exe_path:
25         raise Invalid("exe_path must be defined for component %s" % self.name)
26
27   def makeCompo(self, gen):
28     """generate files for C++ component
29        
30        return a dict where key is the file name and value is the content of the file
31     """
32     cxxfile = "%s.cxx" % self.name
33     hxxfile = "%s.hxx" % self.name
34     if self.kind == "lib":
35       return {"Makefile.am":compoMakefile.substitute(module=gen.module.name, 
36                                                      component=self.name,
37                                                      libs=self.libs, 
38                                                      rlibs=self.rlibs,
39                                                      includes=self.includes),
40               cxxfile:self.makecxx(gen), hxxfile:self.makehxx(gen)}
41     if self.kind == "exe":
42       return {"Makefile.am":compoEXEMakefile.substitute(module=gen.module.name, 
43                                                         component=self.name,
44                                                         libs=self.libs, 
45                                                         rlibs=self.rlibs,
46                                                         includes=self.includes),
47               self.name+".exe":exeCPP.substitute(compoexe=self.exe_path),
48               cxxfile:self.makecxx(gen, 1), hxxfile:self.makehxx(gen)}
49
50   def makehxx(self, gen):
51     """return a string that is the content of .hxx file
52     """
53     services = []
54     for serv in self.services:
55       service = "    void %s(" % serv.name
56       service = service+gen.makeArgs(serv)+");"
57       services.append(service)
58     servicesdef = "\n".join(services)
59     return hxxCompo.substitute(component=self.name, module=gen.module.name, 
60                                servicesdef=servicesdef)
61
62   def makecxx(self, gen, exe=0):
63     """return a string that is the content of .cxx file
64     """
65     services = []
66     inits = []
67     defs = []
68     for serv in self.services:
69       defs.append(serv.defs)
70       service = cxxService.substitute(component=self.name, service=serv.name, 
71                                       parameters=gen.makeArgs(serv),
72                                       body=serv.body, exe=exe)
73       streams = []
74       for name, typ, dep in serv.instream:
75         streams.append('          create_calcium_port(this,"%s","%s","IN","%s");'% (name, typ, dep))
76       instream = "\n".join(streams)
77       streams = []
78       for name, typ, dep in serv.outstream:
79         streams.append('          create_calcium_port(this,"%s","%s","OUT","%s");'% (name, typ, dep))
80       outstream = "\n".join(streams)
81
82       init = initService.substitute(component=self.name, service=serv.name,
83                                     instream=instream, outstream=outstream)
84       services.append(service)
85       inits.append(init)
86     return cxxCompo.substitute(component=self.name, module=gen.module.name, 
87                                exe=exe, exe_path=self.exe_path, 
88                                servicesdef="\n".join(defs), 
89                                servicesimpl="\n".join(services), 
90                                initservice='\n'.join(inits))
91