Sparx Systems Forum
Enterprise Architect => General Board => Topic started by: grand.titus on October 14, 2006, 04:07:23 pm
-
Hello everyboby
I would like EA takes care of attribute initial values during C++ code generation.
For exemple, I want to set the 4 attributes of this class to 1:
class Class1 {
public:
Class1();
virtual ~Class1();
private:
static const int stat_const_att;
static int stat_att;
const int dyn_const_att;
int dyn_att;
};
So I would like EA to generate something like:
// Class1.h
class Class1 {
public:
Class1();
virtual ~Class1();
private:
static const int stat_const_att=1;
static int stat_att;
const int dyn_const_att;
int dyn_att;
};
//Class1.cpp
#include "Class1.h"
int Class1::stat_att =1;
Class1::Class1() : dyn_att(1),dyn_const_att(1){
}
I search a solution on this forum and I found this post:
I did it sir,
So u need create new template Attribute__Implement
$COMMENT = "Its script implement initialization for constructors"
%PI=" "%
%if attStatic==""%
$COMMENT = "Not static initialization"
$type=%attQualType%
$type=%REPLACE($type,".","::")%
$qualType=%packagePath% + "::" + %attType%
%PI=""%
%attContainment=="By Reference" ? "&" : ""%
%attName%( %attInitial% )
%else%
$COMMENT = "Static initialization"
%attQualType% %className%::%attName% = %attInitial%;
%endIf%
Replace Class Body Implement template to following code
$templateArgs=%list="ClassParameter" @separator=", "%
%if $templateArgs != ""%
$templateArgs="<" + $templateArgs + ">"
$templ="template" + $templateArgs
%endIf%
$consPrefix=$templ + "\n" + %classQualName% + "::"
%if classStereotype != "struct"%
%PI=" "%
%if genOptGenConstructor == "T" and genOptGenConstructorInline != "T" and classHasConstructor != "T"%
$defaults = %list="Attribute__Implement" @separator=", " attInitial !=""%
$consPrefix%className%()
%if $defaults != ""%
: $defaults
%endIf%
\n{\n\n}
%endIf%
%PI="\n\n"%
%if genOptGenDestructor == "T" and genOptGenDestructorInline != "T" and classHasDestructor != "T"%
$destructor=$consPrefix+"~"+%className%+"()\n{\n\n}"
$destructor
%endIf%
%if genOptGenCopyConstructor == "T" and genOptGenCopyConstructorInline != "T" and classHasCopyConstructor != "T"%
$consPrefix%className%( const %className%& the%className% )\n{\n\n}
%endIf%
%endIf%
%list="OperationImpl" @separator="\n\n\n"%
%list="InnerClassImpl" @separator=""%
I checked - its works
Unfortunately, it doesn't work. It generates something like:
// Class1.h
class Class1 {
public:
Class1();
virtual ~Class1();
private:
static const int stat_const_att;
static int stat_att;
const int dyn_const_att;
int dyn_att;
};
//Class1.cpp
#include "Class1.h"
Class1::Class1() : int Class1::stat_att =1;int Class1::stat_const_att =1;dyn_att(1),dyn_const_att(1){
}
I am not confortable with code template editor. Is there is somebody for correcting the (nice) work of golyshkin?
Thanks for your help.
-
You'll actually find that the answer is in the previous post. You'll see that he's put the following in.
%PI=" "%
%if genOptGenConstructor == "T" and genOptGenConstructorInline != "T" and classHasConstructor != "T"%
$defaults = %list="Attribute__Implement" @separator=", " attInitial !=""%
$consPrefix%className%()
%if $defaults != ""%
: $defaults
%endIf%
\n{\n\n}
%endIf%
While I originally said that the list should look like the following.
$defaults = %list="Attribute__Default" @separator=", " attStatic != "T" and attDefault != ""%
%if $defaults != ""%
: $defaults
Adding 'attStatic != "T" and' to the list generates the correct initializer.
Also, his following post provides the edit for static members.
-
OK but what about initialisation of
-const static attribut ("=value" in the header)
-non-const static attribut ("type class::att=value;" in the class body)
-
Hi,
One solution is to change the Attribute Declaration template
like this:
Original:
%RESOLVE_QUALIFIED_TYPE("::", "int")%$ptr
%PI=""%
%attName%
%if attCollection == "T" and attContainerType != ""%
%attContainerType%
%endIf%
;
to following:
%RESOLVE_QUALIFIED_TYPE("::", "int")%$ptr
%PI=""%
%attName%
%if attStatic=="T" and attConst=="T"%
= %attInitial%
%endIf%
%if attCollection == "T" and attContainerType != ""%
%attContainerType%
%endIf%
;
This works for me in version 6.5.804 (Build:804) :)