|
Re: Dynamic Storage [message #1768997 is a reply to message #1768905] |
Tue, 25 July 2017 14:05 |
Ernesto Posse Messages: 438 Registered: March 2011 |
Senior Member |
|
|
Hi.
The short answer is that this is a target language issue, rather than a UML-RT issue, so it's up to whatever you can do in C++.
It is possible, but not with a direct link to the attribute's multiplicity. When you declare the multiplicity of an attribute to be greater than one, the code generator will declare an array (not a vector), which in C++ is always of fixed size, i.e. it's size must be known statically by the C++ compiler. You can actually declare the multiplicity of the attribute to be a symbolic constant (e.g. SIZE) but this symbolic constant must be evaluated at generation time and its value obtained from the same class, an enclosing namespace or another namespace if the constant is given with a fully qualified name (e.g. Model1::Class1::SIZE). It can even be an arithmetic expression, but in any case, this expression must be evaluated at generation time, and therefore it is static and not useful for dynamic allocation.
Nevertheless, there is a way to get dynamically allocated arrays, i.e. vectors. Instead of declaring the attribute as having multiplicity greater than one, leave it to the default value. Then in the Model Explorer, select the attribute, then in the Properties view select "Profile", and in the "Applied Stereotypes" field click the plus icon [+] to add the "AttributeProperties" stereotype. When you add it, unfold it and you should see several properties, including one called "type" of type String. Select it and in the type text box, declare the type you want to use. For example "std::vector<int>", where in this case, you can use the 'vector' class from the standard library. Aside from this, you must add the header where this class is defined. To do that, select the class or capsule that contains your attribute, then in the Properties view select "Profile" and click [+] in the "Applied Stereotypes" field. Select "CapsuleProperties", or "PassiveClassProperties", unfold it and select "headerPreface". In the headerPreface field add the include statement:
#include <vector>
Then you can generate code which should compile. The attribute will be a vector with storage automatically managed.
If you want your own class for manage storage, you can do that and follow steps similar to the ones above but with your own class name and header.
|
|
|
Powered by
FUDForum. Page generated in 0.03586 seconds