Getting parent ConstructorDeclaration from ClassConcept
Greetings!
I'm trying to instantiate generated classes in my code. I've gathered that I need a ConstructorDeclaration for this, and I can get it via node<ClassConcept>.constructors(). However, this does not seem to include the default "autogenerated" constructor. Is there any way I can get access to this method?
Please sign in to leave a comment.
You need DefaultClassCreator to instantiate a class that only has the implicit default constructor.
Vaclav
Hey! Thanks for your incredibly fast answer!
I've created a little sample project to show what my problem is. I've found the DefaultClassCreator concept (see the second $LOOP$ in map_Root), but i can only get it to work if the wrapped code calls a default constructor. Here you see what I'm trying to do. map_C1 only has a default constructor, map_C2 an explicit constructor. I'd like to treat both cases the same (i.e. call implicit no-argument constructor if available, otherwise call default constructor).
Inspector window for C1 reference macro:
Inspector window for C2 reference macro:
Strangely, the type of the second expression is ConstructorDeclaration, and that of the first expression is Classifier.
How do I solve this?
Hi,
I'm afraid you'll need to distinguish the two cases, perhaps with an $IF$ macro. In the "explicit constructor" case, you will need to manually extract the desired constructor from the ClassConcept node through the constructors() behavior method.
Hello!
Thanks for your help, I'll give it a shot.
Seems I was a bit premature in marking this solved.
So, I've managed to create an $IF$ macro and switching between DefaultClassCreator and Constructor.
Unfortunately, it seems I can't access a mapping label within the $IF$ macro. Is there a way around this?
Could you be more specific? How are you trying to access a mapping label?
Ah, sorry. I'll try to demonstrate.
Classes are generated for each "type", and the class concept is saved into a mapping label.
To access the constructor, I use this mapping label in subsequent stages.
When I try something like this:
I get the error "Get output by input and label cannot be used here" when trying to run the generator for a sandbox project. How do I get around this?
You can't access labeled targets from a node macro (here, $IF$). There's no guarantee transformation for EnumeratedIntType -> ClassConcept had happened before evaluation of $IF$, hence Generator can't find output for typeclassMapping label and can't proceed further. genContext.get output... operations are for macros that are executed once structural transformation is over (i.e. reference macro and weave macro). You need another condition for your IF. Is there any information in the input model you can use to find out whether target ClassConcept has a constructor?
No, basically I just want to create a new instance of given class, whether with an explicit or implicit constructor. I could add some behavior to concepts to indicate whether they have an explicit constructor, but this is more work than defining an explicit constructor for every class that I'm trying to instantiate.
Thanks for your help!
Then yes, explicit constructor is the easiest way to go.