Book a Demo

Author Topic: Attribute Initial Value generated code  (Read 6702 times)

ferran

  • EA User
  • **
  • Posts: 26
  • Karma: +0/-0
    • View Profile
Attribute Initial Value generated code
« on: January 09, 2020, 08:11:52 pm »
Dear colleagues,

I have got a problem while generated code and having a attribute defined in a class with a Initial Value.
The issue is that the attribute is generated as a variable but the Initial Value is not generated.

From this other post:https://www.sparxsystems.com/forums/smf/index.php?topic=24567.0
I understand that this can only be modified by editing Code templates.

The problem is that checking the macros for attribute field substitution I can only find the "attInitial" macro as the possible value, but when I edit the code template in order to generate this field, it did not work.

Any ideas on how to get that done?

Thank you very much.

Ferran.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Attribute Initial Value generated code
« Reply #1 on: January 09, 2020, 08:40:28 pm »
IIRC the code gen does generate only stubs and no code. Maybe if you fill the Behavior property you can set some initial value. I'd not go that way. Any IDE can do better than this "code editor" of EA.

q.

ferran

  • EA User
  • **
  • Posts: 26
  • Karma: +0/-0
    • View Profile
Re: Attribute Initial Value generated code
« Reply #2 on: January 10, 2020, 07:28:03 pm »
yeah I know I can do that with an IDE!

The thing is to find a way of doing this by generating the code automatically. When I import the code with an array with a value set, it imports the values correctly.

I do not understand, why it does not work the other way around?


Max H

  • EA User
  • **
  • Posts: 24
  • Karma: +0/-0
    • View Profile
    • SafeCode Consulting
Re: Attribute Initial Value generated code
« Reply #3 on: January 14, 2020, 06:36:19 am »
I do quite a bit of code generation, including behavioral.  I haven't seen this issue.  What language are you trying to generate for?   In the C# templates I know this works, and it is declared within the "Attribute Declaration" template as:

%attName%
%attInitial ? " = " value%

One pitfall I had in the past was expecting to see it generate in an interface class, which would be illegal in C# -- so is not coded into that template.

ferran

  • EA User
  • **
  • Posts: 26
  • Karma: +0/-0
    • View Profile
Re: Attribute Initial Value generated code
« Reply #4 on: January 14, 2020, 07:18:41 pm »
Hi Maxh,

I am generating code in C language.

What i have tried so far in the "Attribute declaration" script is the following 2 options:

1) %attInitial ? " = " value%
2) %attInitial ? value%

None worked for generating the value introduced at the "Initial Value" field.
Has someone faced this same issue?

Kind regards,

Ferran.

Jsc

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Re: Attribute Initial Value generated code
« Reply #5 on: January 14, 2020, 08:57:13 pm »
I have also used the "attInitial" macro und it works good.
you can create a simple new template in CTF to test the functionality of this %attInitial% Macro:

Template File:
%Class%

Template Class:
%list="Attribute" @separator="\n" @indent="  "%

Template Attribute:
%attName%
%AttributeDeclaration%

Template AttributeDeclaration:
%attInitial%

It works in my EA

ferran

  • EA User
  • **
  • Posts: 26
  • Karma: +0/-0
    • View Profile
Re: Attribute Initial Value generated code
« Reply #6 on: January 14, 2020, 11:26:44 pm »
Thanks for the answer, but this is not working for me.

I know it should work but it does not for the "attribute declaration" script.

Max H

  • EA User
  • **
  • Posts: 24
  • Karma: +0/-0
    • View Profile
    • SafeCode Consulting
Re: Attribute Initial Value generated code
« Reply #7 on: January 15, 2020, 08:58:29 am »
I just looked at the stock C templates.  There appears to be issues with them... 

First -- They should be using the Impl templates uniformly to deal with the differences between .h and .c files.  They don't do that for attributes.  In the Attribute Declaration template, they only expand the attInitial in limited circumstances (enum, #define, etc.).  Absolutely correct for a .h file.

There is no way from within the template to know if you should be on the Impl side, so different templates (Attribute Impl & Attribute Declaration Impl) is needed to expand the attInitial on the .c file.

There is also mis-application of the "static" keyword, which should never appear in the .h file, and should be associated with a "Private" attScope rather than attStatic for this language.  Variables should be "extern" in the .h file.  etc.

It looks to me like quite a bit of template modification is needed to get C code that will compile and work as expected.  If C is your language, it will be worthwhile.
« Last Edit: January 15, 2020, 10:18:42 am by maxh »

Max H

  • EA User
  • **
  • Posts: 24
  • Karma: +0/-0
    • View Profile
    • SafeCode Consulting
Re: Attribute Initial Value generated code
« Reply #8 on: January 15, 2020, 11:02:34 am »
For a quick (partial) success, try this:

  • In "Class Body Impl" change in "Attribute" to "AttributeImpl" and eliminate the two  "Private" scope checks at the end of that same statement. Take care not to delete the trailing '%'.
  • Populate the "Attribute Impl" template with the code:
    %AttributeDeclarationImpl%
  • In the "Attribute Declaration" template, replace the line that checks for attStatic with the text "extern" (without quotes).
  • Cut-n-paste the following code into the "Attribute Declaration Impl" template:
    %if attTag:"define" == "true" and attScope != "Public"%
    %PI=""%
    #define %attName%
    %attInitial ? value%
    %endTemplate%

    %PI=" "%

    %attScope=="Private"  ? "static"%
    %attConst=="T" ? "const"%
    %attVolatile=="T" ? "volatile"%

    %if attStereotype=="struct"%
    struct
    %elseIf attStereotype=="union"%
    union
    %elseIf attStereotype=="enumeration"%
    enum
    %endIf%

    %attType%

    %PI=""%
    %attContainment=="By Reference" ? "*" : ""%

    %if attStereotype != "alignment"%
    %attName%
    %attInitial ? " = " value%
    %endIf%

    %if classStereotype == "struct" or classStereotype == "union"%
    %attTag:"bitfield" ? ":" value%
    %endIf%

    %if attCollection == "T" and attContainerType != ""%
    %attContainerType%
    %endIf%
    ;


That worked for me.  Good luck.

Jsc

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Re: Attribute Initial Value generated code
« Reply #9 on: January 15, 2020, 06:37:20 pm »
I have create a simple generate file in C. In *.h file the initial values are generated, but in *.c file are not.
If you want to generate the initial value in *.c file, you can also try this:
in template "AttributeDeclaration" you can find this code fragment:
%if attStereotype != "alignment"%
%attName%
%endIf%

replaced with the following code fragement:

%if attStereotype != "alignment"%
%attName%  %if attInitial !=""% =%attInitial% %endIf%
%endIf%


ferran

  • EA User
  • **
  • Posts: 26
  • Karma: +0/-0
    • View Profile
Re: Attribute Initial Value generated code
« Reply #10 on: January 15, 2020, 07:53:32 pm »
Thanks JSC,

worked at the end with this approach.

Kind regards,

Ferran.