Book a Demo

Author Topic: Code Generation of friend Dependency for C++ (MDG)  (Read 2835 times)

sparxstevebe

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Code Generation of friend Dependency for C++ (MDG)
« on: June 24, 2014, 12:31:55 am »
Hello,

I want to generate C++ code from my UML models. I'm using the Software Engineering Variant of EA 10. Especially I'm looking for a means to generate type friend relationship. To generate operation friend relationship is not scope of this question.

As the documentation states there is no built-in friend-type-generation code template. Opposite to that for operations the stereotype is used. So I decided to create one, but without any success.

The model is as follows:
  • class HasFriend with private members telNum:int, getTelNum():int
  • struct IsFriend with public member doCall():void that calls getTelNum()
  • friend dependency pointing from HasFriend to IsFriend

My expectation would be, that HasFriend would be generated including that line:
Code: [Select]
friend struct IsFriend;
But it doesn't. Only the friend must be able to retrieve the telephone number. Using interface abstraction is not possible for me due to common restrictions (note: example is simplification of the problem).

Tried the following:
  • could not find the right code template for dependencies to override it
  • modeled the friendship as association with stereotype <<friend>> and created a stereotyped version of the LinkAttributeDeclaration code template: now I'm not able to retrieve the stereotype of the attribute type (struct,enum,union,class)

:-/ Maybe I'm looking in the wrong direction. But it's desired for me to be able to generate the friend relationship to another type (class).

Does anybody know, how to achieve this goal.

Many thanks in advance and best regards,
/me

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Code Generation of friend Dependency for C++ (
« Reply #1 on: June 24, 2014, 09:38:00 am »
None of the default templates loop over dependency connectors.

You can add a custom template for type of Connector and filter for only dependencies with a given stereotype when calling it.

Connector__Friend
Code: [Select]
friend struct %connectorDestElemName%
Somewhere inside Class Body
Code: [Select]
%list="Connector__Friend" @separator="\n\n" @indent="\t" classGUID==connectorSourceElemGUID and connectorType=="Dependency" and connectorStereotype=="friend"%
Note: it won't be synchonized, I don't remember what it will do when reverse engineering and I hate friend declarations.

sparxstevebe

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: Code Generation of friend Dependency for C++ (
« Reply #2 on: June 24, 2014, 08:26:07 pm »
Hello Simon,

thank you very much for this quick response. The way you showed is obviously way more generic and clear. I started to try it out and it was a good beginning, since the type spec was hardcoded.

So I added some more lines to the proposed template Connector__Friend, that I want to give here for example to others.

Notes:
  • in EA10 the macro connectorDestElemStereotype is not listed but defined; it is only refered so as "connectorDestElem*: A set of macros..."
  • the type spec for class and enum can not be taken from the stereotype of the destination element
  • PI needs to be changed (first) to get a one-liner and (second) to prevent unnecessary spaces at end of line

Connector__Friend
Code: [Select]
%PI=" "%

friend
$spec = %connectorDestElemStereotype%
%if $spec==""%
$spec="class"
%elseIf $spec=="enumeration"%
$spec="enum"
%endIf%
$spec

%PI=""%
%connectorDestElemName%
;

So I'm pleased to say you helped me to solve my problem.

Simon M wrote:
Quote
and I hate friend declarations

I'm glad to see that's the declaration you hate not the relationship. ;-)

Best regards and praise for that forum,
/me
« Last Edit: June 27, 2014, 05:58:37 am by sparxstevebe »