Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Papyrus for Real Time » Define an attribute typed with a class without the default constructor(Define an attribute typed with a class without the default constructor)
Define an attribute typed with a class without the default constructor [message #1795371] Fri, 21 September 2018 05:09 Go to next message
Mojtaba Bagherzadeh is currently offline Mojtaba BagherzadehFriend
Messages: 36
Registered: April 2016

How can I define an attribute typed with a class without the default constructor? The main problem is that whenever we define an attribute without a default constructor, we have to initialize it inside the capsule constructor which is generated by Papyrus-RT and I am not aware of a way to customize the constructor code of the capsule.

Re: Define an attribute typed with a class without the default constructor [message #1795428 is a reply to message #1795371] Fri, 21 September 2018 17:44 Go to previous message
Ernesto Posse is currently offline Ernesto PosseFriend
Messages: 416
Registered: March 2011
Senior Member
The first question is more of a C++ question than a Papyrus-RT question. Whenever you create a passive class (i.e. a non-capsule), by default it will not generate a default constructor, so, according to C++ semantics, it will have an implicitly declared default constructor. See [1]. So if some capsule (say Capsule1) has an attribute (say 'a') typed by such class (say Class1), when the capsule is instantiated, it will automatically call Class1's implicitly declared default constructor:

class Capsule_Capsule1 : public UMLRTCapsule
    Capsule_Capsule1( const UMLRTCapsuleClass * cd, UMLRTSlot * st, const UMLRTCommsPort * * border, const UMLRTCommsPort * * internal, bool isStat );
// ...
    Class1 a;   // (1)

class Class1
    int n;
    static const UMLRTObject_field fields[];

When Capsule1 is created, space for attribute 'a' declared in line (1) is automatically allocated inside the space for the capsule, and Class1's default constructor (implicit or not) will be called. And this is recursive of course. That's just the standard C++ semantics of constructors. (C++ != Java).

If you need to initialize a's own attributes, then you can do it in Capsule1's initial transition's action.

Alternatively, you can apply the "PassiveClassProperties" stereotype to the class (Class1) and in the stereotype's properties, set "generateDefaultConstructor" to true (although that is the default). You can also control how to generate other things, like the copy constructor, the assignment operator, the equality operator, insertion and extraction operators, etc. With "generateDefaultConstructor" to true, you can then customize the body of the generated explicit default constructor.

Note however, that this works for passive classes only and not for capsules: you cannot override the capsule constructor, and this is by design, because the generated code must ensure that some things are initialized for capsules, calling the RTS's UMLRTCapsule's constructor, and setting certain internal attributes. If the user was allowed to customize this, we could not guarantee anything about the capsule's behaviour, or even its structure. Furthermore, we do not allow things such as assignment, and operations that would allow capsule serialization (copy constructor, insertion/extraction), etc. because the semantics of UML-RT doesn't define if this should be possible at all.

Previous Topic:Unable to add log port in the middle of the capsule
Next Topic:multiplicity at objects?
Goto Forum:

Current Time: Thu Feb 09 00:55:55 GMT 2023

Powered by FUDForum. Page generated in 0.01884 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top