Book a Demo

Author Topic: Generate C++ code for attribute default value  (Read 3223 times)

grand.titus

  • EA Novice
  • *
  • Posts: 15
  • Karma: +0/-0
    • View Profile
Generate C++ code for attribute default value
« 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:
Code: [Select]

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:
Code: [Select]

// 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:
Quote
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:
Code: [Select]

// 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.

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Generate C++ code for attribute default value
« Reply #1 on: October 15, 2006, 03:30:35 pm »
You'll actually find that the answer is in the previous post.  You'll see that he's put the following in.

Code: [Select]
%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.

Code: [Select]
$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.


grand.titus

  • EA Novice
  • *
  • Posts: 15
  • Karma: +0/-0
    • View Profile
Re: Generate C++ code for attribute default value
« Reply #2 on: October 15, 2006, 11:44:28 pm »
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)

Lersiken

  • EA Novice
  • *
  • Posts: 1
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Generate C++ code for attribute default value
« Reply #3 on: March 30, 2007, 02:52:39 am »
 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)  :)