Salome HOME
CCAR: add definition parameter "sources" to C++, F77, Python components.
[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, sources=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                              sources=sources)
16
17   def validate(self):
18     """ validate component definition parameters"""
19     Component.validate(self)
20     kinds = ("lib", "exe")
21     if self.kind not in kinds:
22       raise Invalid("kind must be one of %s" % kinds)
23
24     if self.kind == "exe" :
25       if not self.exe_path:
26         raise Invalid("exe_path must be defined for component %s" % self.name)
27
28   def makeCompo(self, gen):
29     """generate files for C++ component
30        
31        return a dict where key is the file name and value is the content of the file
32     """
33     cxxfile = "%s.cxx" % self.name
34     hxxfile = "%s.hxx" % self.name
35     if self.kind == "lib":
36       sources = " ".join(self.sources)
37       return {"Makefile.am":compoMakefile.substitute(module=gen.module.name, 
38                                                      component=self.name,
39                                                      libs=self.libs, 
40                                                      rlibs=self.rlibs,
41                                                      sources=sources,
42                                                      includes=self.includes),
43               cxxfile:self.makecxx(gen), hxxfile:self.makehxx(gen)}
44     if self.kind == "exe":
45       return {"Makefile.am":compoEXEMakefile.substitute(module=gen.module.name, 
46                                                         component=self.name,
47                                                         libs=self.libs, 
48                                                         rlibs=self.rlibs,
49                                                         includes=self.includes),
50               self.name+".exe":exeCPP.substitute(compoexe=self.exe_path),
51               cxxfile:self.makecxx(gen, 1), hxxfile:self.makehxx(gen)}
52
53   def makehxx(self, gen):
54     """return a string that is the content of .hxx file
55     """
56     services = []
57     for serv in self.services:
58       service = "    void %s(" % serv.name
59       service = service+gen.makeArgs(serv)+");"
60       services.append(service)
61     servicesdef = "\n".join(services)
62     return hxxCompo.substitute(component=self.name, module=gen.module.name, 
63                                servicesdef=servicesdef)
64
65   def makecxx(self, gen, exe=0):
66     """return a string that is the content of .cxx file
67     """
68     services = []
69     inits = []
70     defs = []
71     for serv in self.services:
72       defs.append(serv.defs)
73       service = cxxService.substitute(component=self.name, service=serv.name, 
74                                       parameters=gen.makeArgs(serv),
75                                       body=serv.body, exe=exe)
76       streams = []
77       for name, typ, dep in serv.instream:
78         streams.append('          create_calcium_port(this,"%s","%s","IN","%s");'% (name, typ, dep))
79       instream = "\n".join(streams)
80       streams = []
81       for name, typ, dep in serv.outstream:
82         streams.append('          create_calcium_port(this,"%s","%s","OUT","%s");'% (name, typ, dep))
83       outstream = "\n".join(streams)
84
85       init = initService.substitute(component=self.name, service=serv.name,
86                                     instream=instream, outstream=outstream)
87       services.append(service)
88       inits.append(init)
89     return cxxCompo.substitute(component=self.name, module=gen.module.name, 
90                                exe=exe, exe_path=self.exe_path, 
91                                servicesdef="\n".join(defs), 
92                                servicesimpl="\n".join(services), 
93                                initservice='\n'.join(inits))
94