Book a Demo

Author Topic: C++ namespaces generation with packages  (Read 4376 times)

grand.titus

  • EA Novice
  • *
  • Posts: 15
  • Karma: +0/-0
    • View Profile
C++ namespaces generation with packages
« on: October 06, 2006, 07:42:56 am »
Hi

I have a very simple project:
---Class Model [set as Namespace Root]
------Package1 [NOT set as Namespace Root]
---------Class1 with an attribute c2:Class2
------Package2 [NOT set as Namespace Root]
---------Class2

When I generate the C++ code, I expect to get a file Class1.h with something like:
Code: [Select]

using namespace Package2;

namespace Package1
{
  class Class1
  {
     public:
        Class1();
        virtual ~Class1();
     private:
        Class2 c2;
  };
}

But what I really get is:
Code: [Select]

#include "Class2.h"

class Class1
{
  public:
     Class1();
     virtual ~Class1();
  private:
     Class2 c2;
};


In fact, EA packages are not transformed into C++ namespaces.

Thanks in advance

PS: EA version : 6.5.797
« Last Edit: October 06, 2006, 07:44:12 am by grand.titus »

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8626
  • Karma: +259/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: C++ namespaces generation with packages
« Reply #1 on: October 06, 2006, 04:24:31 pm »
Is Class Model really a Class?  If so, then that's likely to be the problem (although I can't see how you can set a Class to a Namespace Root).

Also check that both packages have the context menu item Set as Namespace Root (and not Clear Namespace Root).

Finally check the Settings|Namespaces... entries.

HTH,
Paolo
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: C++ namespaces generation with packages
« Reply #2 on: October 08, 2006, 03:10:44 pm »
Open the options dialog (Tools | Options) to the C++ page.

You'll find an option 'Generate Namespaces' that will be false.

grand.titus

  • EA Novice
  • *
  • Posts: 15
  • Karma: +0/-0
    • View Profile
Re: C++ namespaces generation with packages
« Reply #3 on: October 09, 2006, 12:40:06 am »
Thanks, I've not seen this option. Now it works.

I get the code:
Code: [Select]

#include "Class2.h"
namespace Package1
{
class Class1
{
public:
Class1();
virtual ~Class1();
Package2::Class2 get();
private:
Package2::Class2 c2;
};
}

I am glad with that, but is it possible to use the instruction "using namespace ...;"?
In particular, can I put "using namespace std;" at the head of ech file?



I also wonder if it is possible to generate sources into a directory "src" rather than "Class Model"? In order to get this hierarchy:
Code: [Select]

-src
.+--Package1
.|..+--Class1.h
.|..+--Class1.cpp
.+--Package2
....+--Class2.h
....+--Class2.cpp

rather than:
Code: [Select]

-Class Diagram
.+--Package1
.|..+--Class1.h
.|..+--Class1.cpp
.+--Package2
....+--Class2.h
....+--Class2.cpp
« Last Edit: October 09, 2006, 07:59:06 am by grand.titus »

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: C++ namespaces generation with packages
« Reply #4 on: October 09, 2006, 03:04:48 pm »
Quote
I am glad with that, but is it possible to use the instruction "using namespace ...;"?
In particular, can I put "using namespace std;" at the head of ech file?

Either add it to each class headers field, or add it to the Import Section template.

Quote
I also wonder if it is possible to generate sources into a directory "src" rather than "Class Model"? In order to get this hierarchy:
Code: [Select]
-src
.+--Package1
.|..+--Class1.h
.|..+--Class1.cpp
.+--Package2
....+--Class2.h
....+--Class2.cpp
rather than:
Code: [Select]
-Class Diagram
.+--Package1
.|..+--Class1.h
.|..+--Class1.cpp
.+--Package2
....+--Class2.h
....+--Class2.cpp

I guess you're doing a package generation, and auto-generating the files.  As the top level package is included in the path being generated you have two options.
Rename "Class Model" to "src" so that when generating you get that package, or generate each of the subpackages separately into the src directory.

grand.titus

  • EA Novice
  • *
  • Posts: 15
  • Karma: +0/-0
    • View Profile
Re: C++ namespaces generation with packages
« Reply #5 on: October 10, 2006, 12:53:11 am »
Quote
Either add it to each class headers field, or add it to the Import Section template.

Finally, I have added:

  • "using namespace std;" into the Import Section template
  • "using namespace Package2;" into the headers and imports fields of the Generate Code dialog box of the class Class1.

These two instructions are added in the right place in *.h and *.cpp. But EA doesn't take care of "using namespace Package2;" and continues to add "Package2::" before "Class2". Is it possible to get round this problem?
« Last Edit: October 10, 2006, 12:54:16 am by grand.titus »

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: C++ namespaces generation with packages
« Reply #6 on: October 10, 2006, 03:04:16 pm »
Only by further modification of the templates.  They assume the need to include the namespace qualifier (as the using statement isn't generated by default) so you'll need to replace the %RESOLVE_QUALIFIED_TYPE("::", "int")% call with %attType% (or opReturnType etc)

grand.titus

  • EA Novice
  • *
  • Posts: 15
  • Karma: +0/-0
    • View Profile
Re: C++ namespaces generation with packages
« Reply #7 on: October 13, 2006, 08:13:33 am »
Thanks for your answer.
Unfortunately manipulating template transformation code seem to be complex for me.