The mechanism for generating standard operations involves:
In the site.prp file, you must provide a pair of class-level properties for each standard operation. The names must use the following format:
The "op1" part of the name is the string that you are going to use in the StandardOperations property to refer to this standard operation.
The content of the Declaration property will be generated in the relevant .h file. The content of the Definition property will be generated in the relevant .c or .cpp file.
Ordinarily you will use one or more IBM Rational Rhapsody keywords in the value of these properties. For example:
Subject CG
Metaclass Class
Property op1Declaration MultiLine "void $Name;"
Property op1Definition MultiLine "void $Name: $Name() {int x=7;}"
Property op2Declaration MultiLine "void $Name_serialize;"
Property op2Definition MultiLine "void $Name: $Name_serialize{}"
end
end
Since you are likely to want to include manipulation of class members in the code for the standard operation that you are defining, Rational Rhapsody provides keywords for generating code that will be repeated for each attribute, relation, or operation in the class.
To define code that involves all of the attributes in the class:
To define code that involves all of the associations in the class:
To define code that involves all of the operations in the class:
You can also define code that will be repeated for each of the arguments in the class. This includes any event arguments as well as all of the arguments for the operations contained in the class.
To define code that involves all of the arguments:
When defining standard operations for template classes, you can use the keyword $NameWithTemplateParams. If you have a class named ClassA and it is a template this keyword is replaced with "ClassA<params>". If the class is not a template, the keyword is replaced with "ClassA". The following property definitions demonstrate the use of this keyword when defining a copy constructor.
Subject CG
Metaclass Class
Property CopyCtorDeclaration MultiLine "$Name(const $NameWithTemplateParams & x);"
Property CopyCtorDefinition MultiLine "$NameWithTemplateParams::$Name(const $NameWithTemplateParams & x) { /* some code */ }"
end
You can also define code that takes information from base classes that a class extends.
To define such code:
The property CG::Class::StandardOperations takes a comma-separated list of operation references, based on the property names you included in your site.prp file.
If you added class-level properties named op1Declaration, op1Definition, op2Declaration, and op2Definition, then the two standard operations will be generated for a class if you specify op1, op2 as the value of the property StandardOperations for that class.
This example shows how you can use the standard operation feature to implement serialization of events. The approach would be similar to implement unserialization.
To have the operation generated for all the events in a given package:
CG::Event::serializeDeclaration
public:
strstream & serializer(strstream & theStrStream)
const;
CG::Event::serializeDefinition
strstream & $Name::serialize(strstream & theStrStream) {
$Base
$Arguments
return theStrStream;
}
CG::Argument::serialize
theStrStream << $Name;
CG::Event::serializeBase
$Name::serialize(theStrStream);
If you defined an event named VoiceAlarm with two arguments, severity and priority, and VoiceAlarm inherits from Alarm, then the code generated would be as follows:
strstream & VoiceAlarm::serialize(strstream & theStrStream) {
Alarm::serialize(theStrStream);
theStrStream << severity;
theStrStream << priority;
return theStrStream;
}