Book a Demo

Author Topic: C++ Smart pointer forward engineering  (Read 4907 times)

jmhaapasa

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
C++ Smart pointer forward engineering
« on: April 19, 2005, 04:56:37 am »
I haven't seen this addressed before, so I'm going ahead:

There is a nice facility in EA to define collection classes for associations, along the lines of std::vector<#TYPE#> that generate the appropriate member type in code for an association with (ordered / qualified / other) multiplicity 0..* (or similar).

Would it be difficult to extend this functionality to handle multiplicities of 0..1 and 1? This would be useful in case you want to specify an aggregation using a smart pointer, like boost::shared_ptr<#TYPE#> for example. Or is there another way to get the same result in the generated code?

The types should probably take into account association type as well, so that we could have composition = scoped pointer, aggregation = shared pointer and simple directed association = weak pointer (for another boost example).

Reverse engineering of the most common templated smart pointers would be very nice as well, even if a generic,  customizable member-type-to-association reverse engineering would probably be very difficult to implement in the presence of arbitrary templated types. Perhaps the code generation association member types could be used in parametrizing
the reverse engineering process as well?

We use a lot of shared pointers, and the default EA pointer and reference associations do not really help us much. If we could at least generate smart pointer associations, that would be great. Reversing them: bliss.

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: C++ Smart pointer forward engineering
« Reply #1 on: April 19, 2005, 03:30:17 pm »
You could edit the C++ Linked Attribute Declaration template to achieve this.

A snippet of the template may look like this.
Code: [Select]
%if linkCard == "0..1"%
$type = "boost::shared_ptr<" + %linkAttName% + ">"
%elseIf linkAttCollectionClass != "" and linkCard != "" and linkCard != "0" and linkCard != "1"%
$type=%linkAttCollectionClass%
%endIf%

Don't think there is currently access to what the association type is in the templates, but %linkStereotype% may come in handy.

That should get you to good, if not great.  Bliss is a lot further off though.

Simon
« Last Edit: April 19, 2005, 03:31:04 pm by simonm »

jmhaapasa

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Re: C++ Smart pointer forward engineering
« Reply #2 on: April 19, 2005, 11:16:10 pm »
Thank you. The modification works very well for us, and is really kind of obvious (sheepish grin). We also use %linkStereotype% values of "shared", "scoped" and "weak" to refine pointer type selection, as per your suggestion.

jmhaapasa

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Re: C++ Smart pointer forward engineering
« Reply #3 on: April 19, 2005, 11:58:37 pm »
For the record, here is our new stereotyped override for the Linked Attribute Declaration template and our new "shared" stereotype:

Code: [Select]

%PI=" "%
$type=%REPLACE(linkAttQualName,".","::")%
$qualType=%packagePath% + "::" + %linkAttName%
%if genOptCPPGenNamespace == "T" and $type == $qualType%
$type="boost::shared_ptr<"+%linkAttName%+">"
%endIf%
%if linkAttCollectionClass != "" and linkCard != "" and linkCard != "0" and linkCard != "0..1" and linkCard != "1"%
$type=%REPLACE(linkAttCollectionClass,"$TYPE",$type)%
%endIf%
$type
%if linkAttRole != ""%
%linkAttRole%;
%else%
%REPLACE(genOptDefaultAssocAttName,"$LinkClass",linkAttName)%;
%endIf%


This handles all the multiplicities, and integrates nicely with the collection classes, as long as you use $TYPE instead of #TYPE# in the collection class definition in the code generation options. So, we have smart pointers or, at our pleasure, containers of smart pointers. Just repeat for stereotypes "weak" and "scoped".

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: C++ Smart pointer forward engineering
« Reply #4 on: April 20, 2005, 03:51:57 pm »
Good to hear that it's working for you.

Just curious?  What is the reason for using $TYPE instead of #TYPE#?

Simon

jmhaapasa

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Re: C++ Smart pointer forward engineering
« Reply #5 on: April 20, 2005, 08:54:23 pm »
$TYPE allows us to use the REPLACE in the above template more easily (it's just an arbitrary token). If we used #TYPE#, EA would perform the substitution before providing the linkAttCollectionClass macro to the template. This way we can use the normal EA collection classes with the type substitution parameter intact.

Of course, now the non-overridden Link Attribute Declaration template (the one without smart pointers) does have to be modified as well, to perform the type substitution in the template, since we are no longer using #TYPE# in collection classes, and lose the automatic substitution. It's all a bit of a hack, of course, but does seem to work.