Book a Demo

Author Topic: More newbie code generation question  (Read 4031 times)

Steve Blackwell

  • EA Novice
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
More newbie code generation question
« on: March 06, 2014, 06:00:39 am »
EA newbie still trying to get the hang of code generation.

I have a class, hello, that uses std::string to write "Hello world".
I have a second class, TopLevel, that creates an instance of hello and I have a dependency link from TopLevel to hello marked with a <<use>> stereotype.

The 2 classes and the class diagram are in the hello package in the Project Brower.

If I go to "Generate Package Source Code..." with the "Root Package" set to hello, click the "Include all Child Packages" checkbox and click the Generate button, I get a .h and a .cpp file for hello and for TopLevel.

However, the hello.cpp does not include <iostream> and the TopLevel.cpp does not include "hello.h". If I right click on the hello class in the Project Browser and select "Generate Code..." there is a place to add the #include <iostream> but it does not appear to be persistent.

So, 2 questions,
1) How do I tell the hello class that it needs to include <iostream>?
2) How do I get the dependency to cause #include "hello.h" to be included in the TopLevel.cpp code?

Make that 3 questions: Can EA even do this?

Thanks,
Steve

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: More newbie code generation question
« Reply #1 on: March 06, 2014, 09:02:07 am »
1. There are two fields in the generate class dialog that allow you to add #includes that aren't represented in the model.
2. By default the templates don't generate a #include for a dependency. You can do it, but the C++ import section doesn't use the list of imports by default. If you look at the PHP Import section template that should give you enough to work out how to do it.
3. Yes, it can.

Steve Blackwell

  • EA Novice
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Re: More newbie code generation question
« Reply #2 on: March 08, 2014, 02:37:53 am »
I mentioned in my original message that I had seen that generate class dialog. I also said that what you enter there does not persist which makes its use impractical. Say I have 100 classes, then every time I want to generate code, I would have to go to each class and add the appropriate includes? How would I do an automated build? This is probably a question for another topic.

I tried your suggestion and added to the C++ Import Section template but when I try to generate code I get a stack overflow error and EA crashes Even just adding this either before or after the existing %fileImports% line:
Code: [Select]
$imports = %list="Import" @separator="\n" importFromDependency=="T"%causes a crash.

Steve

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: More newbie code generation question
« Reply #3 on: March 11, 2014, 08:44:02 am »
1. Not sure why you're not seeing it persisted. It does for me, maybe a Version Control or other lock on the element.
2. I just tried.

Import Section Template
Code: [Select]
%list="Import" @separator="\n" importFromDependency=="T"%
%fileImports%

Import Template
Code: [Select]
#include "%importFileName%"
It did exactly the right thing.

What version and edition are you using?
« Last Edit: March 11, 2014, 08:44:26 am by simonm »

Steve Blackwell

  • EA Novice
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Re: More newbie code generation question
« Reply #4 on: March 12, 2014, 03:02:17 am »
I'm using version 9.3.935 which I know is old.

Well, that's odd - today it is not crashing. I don't know what changed but now it is working.

Also I found (don't know how I didn't see it in the beginning) the Save button in the class code generation dialog that makes the includes persistant.

I have a few more questions.
1) Is it possible to further limit the files imported to only dependencies with a particular stereotype? Something like:

Code: [Select]
%list="Import @seperator="\n" importFromDependency=="T" && steroetype="use"%
Perhaps a better question is: Where can I find some documentation on code generation templates? I have not found the help to be particularly useful.

2) Can EA generate a main method or in fact any global method outside of a class. This is again using C++ as my language.

Thanks,
Steve

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: More newbie code generation question
« Reply #5 on: March 13, 2014, 08:48:10 am »
1. No. For the full list of macros supported in the import template do a search in the help for importFromDependency and open the page that shows.

2. Well, you add a stereotype override for a class so that it doesn't generate a declaration or code block. Instead only generating the contents. It won't round trip and generation will always think it's new and duplicate the contents. The only other way I can think of is to create a class and set the language to C. As long as the filename is different to your C++ classes it should manage it.