Author Topic: %fileImports% problem  (Read 1322 times)

Henry Wiechert

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
%fileImports% problem
« on: April 01, 2005, 08:04:18 am »
Hi,

when generating C++ code for a class, an association by reference leads to the inclusion of the header file of the referenced class (%fileImports%). But in this case a forward declaration would be sufficient. Especially if two classes referencing each other the #include method does not work.

Example:
Code: [Select]
// A.h generated by EA
#include "B.h"
class A { B *b; };


Code: [Select]
// A.h how I need it
class B;
class A { B *b; };


I know that I can add my own statements in the fileImports section in the CodeGen Dialog. But the #include statements will be added by EA anyway.
How can I avoid this ?

Thanx,
Henry

Simon M

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 6453
  • Karma: +55/-6
    • View Profile
Re: %fileImports% problem
« Reply #1 on: April 03, 2005, 03:29:44 pm »
The solution is to not use the %fileImports% macro.

Instead you can create an Import template to generate the #include statements and another to generate the forward declarations.  In the Import Section template call a list over each of the templates with the conditions that you want.  In the Import Section Impl template list over the #include import for anything necessary.  Look in the help file for the substitution macros that are available for import objects.  (http://www.sparxsystems.com.au/EAUserGuide/index.html?fieldsubstitutionmacros.htm)

You could also look at the PHP templates for an example of how a filename based import is generated.  (PHP, Java and C# have their import statements (of whatever form) generated this way since the upgrade to support newer versions of them)

The only problem to this solution is that you lose the functionality to add your own statements into the CodeGen dialog.

Simon
Simon

support@sparxsystems.com

Henry Wiechert

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: %fileImports% problem
« Reply #2 on: April 04, 2005, 01:42:47 pm »
Thank you for your help.
I created the Import templates and it works fine.

But your last statement points out the remaining problem. I'm no longer able to add my own statements into the CodeGen dialog :(
I like the highly customisable code generator very much. But why is the user defined input in the CodeGen dialog and the automaically generated imports mixed together in the %fileImports% macro ?
It would be more flexible to have a new kind of %userImports% which expands ONLY the user input.

Henry

Simon M

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 6453
  • Karma: +55/-6
    • View Profile
Re: %fileImports% problem
« Reply #3 on: April 04, 2005, 03:33:20 pm »
It's currently that way for backward compatibility.

Here's another workaround.  Try using something like this as your import section template (with filling in the conditions that you're using)

Code: [Select]
$imports = %fileImports%
$pos = %FIND($imports,"//End User Imports")%
%if $pos == "-1"%
$imports = ""
%else%
$imports = %LEFT($imports,$pos)%
%endIf%
$imports += %list="Import_HashInclude" @separator="\n"%
$imports += %list="Import_ForwardDeclare" @separator="\n"%
%REMOVE_DUPLICATES($imports,"\n")%

Simon
Simon

support@sparxsystems.com

Henry Wiechert

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: %fileImports% problem
« Reply #4 on: April 05, 2005, 04:40:57 am »
This is what I was looking for!!
I did not considered the string functions until now ...

Regarding backward compatibility a solution could be to have two new macros. One for user input only - and the other for automatically generated imports only. The %fileImports% macro could stay as it is (even though for C++ it is only partly useful).

Thank you very much for your help and keep on the good work on EA.

Henry