How to add a template class as a member of another class

General discussions about code engineering

Moderator: Moderators

How to add a template class as a member of another class

Postby bobo1@hotmail.com » Fri May 22, 2015 10:35 pm

I am modeling some interactions that rely on template classes. I have the template classes modeled within MagicDraw, but don't see how to invoke the templates from another class. For what I am doing, I need to use the model to generate C++ code. Does anyone know how to do this?

For example, if I modeled a template class called "TemplateClass", which has template parameter "TClass", how do I then include the template class as a member of another class (named "AClass") or as a parameter in a method (named "SimpleFunction") to generate the following C++ code?
Code: Select all
class AClass
{   
public:
    SimpleFunction( TemplateClass<TClass> & myTemplateClassRef );
    TemplateClass<TClass> handler;   
};

One solution is to create a "dummy" class called "TemplateClass<TClass>" and then add the ""dummy" class as the attribute to "AClass" and as the parameter to "SimpleFunction", but that creates a disconnect between the "dummy" and the true template class. Is this a bug in MagicDraw or is there something else I am missing?
bobo1@hotmail.com
Forum Newbie
Forum Newbie
 
Posts: 5
Posts Rating:0
Joined: Fri May 22, 2015 10:32 pm

Re: How to add a template class as a member of another class

Postby bhoomchai » Tue May 26, 2015 10:45 pm

This is quite a complex model. Hope the image below can explain everything.

TemplateBinding.png


Best Regards,
No Magic Customer Support
You do not have the required permissions to view the files attached to this post.
bhoomchai
Customer Support
Customer Support
 
Posts: 36
Posts Rating:5
Joined: Thu Jan 21, 2010 12:38 am

Re: How to add a template class as a member of another class

Postby bobo1@hotmail.com » Wed May 27, 2015 8:56 am

Wow! I never would have guessed that approach. I agree that the solution is "quite complex" given the simple goal, but it seems to work. Any chance No Magic could simplify the interface?

Many thanks for the help.
bobo1@hotmail.com
Forum Newbie
Forum Newbie
 
Posts: 5
Posts Rating:0
Joined: Fri May 22, 2015 10:32 pm

Re: How to add a template class as a member of another class

Postby bobo1@hotmail.com » Wed May 27, 2015 9:43 am

I have a closely related question on this topic. In order to invoke the "AClass" object, I added a "TClass" template parameter to the "AClass" object. (Easy enough.)

Next I want to create a class owning an "AClass" attribute of a specific type. (I used "int" as the specific type to create an "IntClass" object with an AClass<int> attribute.) So the updated "AClass" code looks like this...

Code: Select all
template <class TClass> //The change to the model adds this line
class AClass
{
   
public:
    TemplateClass<TClass> handler;
   
    void SimpleFunction( const TemplateClass<TClass> & myTemplateClassRef );
   
};


And the code for the IntClass should look something like this...
Code: Select all
class IntClass
{
   
public:
    AClass<int> genericHandler;
   
};


Only problem is that when I bind the "IntClass" to the "AClass", the IntClass .cpp and .hpp files are not generated and aren't added when I add the IntClass object to the code engineering set. Am I missing something?
bobo1@hotmail.com
Forum Newbie
Forum Newbie
 
Posts: 5
Posts Rating:0
Joined: Fri May 22, 2015 10:32 pm

Re: How to add a template class as a member of another class

Postby bobo1@hotmail.com » Wed May 27, 2015 9:53 am

Okay I figured it out.

After I bind the IntClass to the AClass I have to create another class that has an IntClass as an attribute of the new class. Then I can add the new class to the code engineering set.

Again, it works...but wow! Seems like a lot of steps to reach a simple goal.
bobo1@hotmail.com
Forum Newbie
Forum Newbie
 
Posts: 5
Posts Rating:0
Joined: Fri May 22, 2015 10:32 pm


Return to Code engineering

Who is online

Users browsing this forum: No registered users and 1 guest