[Acceleo] In search of a clean solution for namespace/block generation [message #632220] |
Tue, 12 October 2010 03:34  |
Eclipse User |
|
|
|
Hello,
I am searching for the best way for generating code like the following in Acceleo. Unfortunately, I haven't found a good solution/example:
namespace N1
{
namespace N2
{
namespace N3
{
...specific-code...
}
}
}
Especially concerning reusability of the "namespace generation", a perfect indentation and clean code, I want to use a construct as follows. Unfortunately, something like this, does not exist in Acceleo (I think?)
[generateNamespaces(baseClass)]
...specific-code...
[generateNamespaces /]
Has anybody some advice? How to solve my use case in a good way?
Thanks!
Regards,
Kirsten
|
|
|
Re: [Acceleo] In search of a clean solution for namespace/block generation [message #632229 is a reply to message #632220] |
Tue, 12 October 2010 04:26   |
Eclipse User |
|
|
|
Hi Kirsten,
You won't be able to have a perfect indentation in both generated code and template, you'll have to make compromises .
The best way of handling this problem is if your namespaces and "baseclass" are of two different kind. For example if your namespaces are "EPackage"s and baseclass are "EClass"es, you could then use code like this :
[template public generate(element : EObject)]
[comment this will not generate anything/]
[/template]
[template public generate(pack : EPackage)]
namespace [name/]
{
[pack.eContents().generate()/]
}
[/template]
[template public generate(class : EClass)]
...specific code...
[/template]
The three templates share the same name, so Acceleo will check the actual runtime type of the parameter in order to know which it should call. As you start your generation from the namespace (package "N1"), it will first generate the "namespace N1" part, then as it goes down the content encountering more packages or classes, generate the remainder. As soon as we find anything than a package or class, the placeholder "generate(EObject)" will be called and won't generate a thing.
There are more than a single way to do this, you could also iterate over the result of "baseClass.ancestors(EPackage)->reverse()" and generate all namespaces at once from this list ... You'll have to experiment to see which you prefer.
Regards,
Laurent Goubet
Obeo
|
|
|
|
Powered by
FUDForum. Page generated in 0.28477 seconds