用於產生標準作業的機制涉及:
在 site.prp 檔案中,必須為每個標準作業提供類別層次內容配對。名稱必須使用以下格式:
名稱的 "op1" 部分是要在 StandardOperations 內容中用來參照此標準作業的字串。
Declaration 內容的內容將在相關 .h 檔案中產生。Definition 內容的內容將在相關 .c 或 .cpp 檔案中產生。
您通常會在這些內容的值中使用一個以上的 IBM Rational Rhapsody 關鍵字。例如:
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
因為您要在將定義之標準作業的程式碼中包括類別成員的操作,所以 Rational Rhapsody 提供了用於產生將針對類別中每個屬性、關係和作業重複的程式碼。
若要定義涉及類別中所有屬性的程式碼:
若要定義涉及類別中所有關聯的程式碼:
若要定義涉及類別中所有作業的程式碼:
您還可以定義針對類別中每個引數重複的程式碼。這包括任何事件引數,以及類別中所包含作業的所有引數。
若要定義涉及所有引數的程式碼:
為範本類別定義標準作業時,可以使用關鍵字 $NameWithTemplateParams。如果您具有名為 ClassA 的類別並且它是範本,則將此關鍵字取代為 "ClassA<params>"。 如果該類別不是範本,則將此關鍵字取代為 "ClassA"。 下列內容定義示範如何在定義複製建構子時使用此關鍵字。
Subject CG
Metaclass Class
Property CopyCtorDeclaration MultiLine "$Name(const $NameWithTemplateParams & x);"
Property CopyCtorDefinition MultiLine "$NameWithTemplateParams::$Name(const $NameWithTemplateParams & x) { /* some code */ }"
end
內容 CG::Class::StandardOperations 採用以逗點區隔的作業參照清單,基於您包括在 site.prp 檔案中的內容名稱。
如果您新增了名為 op1Declaration、op1Definition、op2Declaration 及 op2Definition 的類別層次內容,則在指定 op1, op2 作為某類別之內容 StandardOperations 的值時,將為該類別產生兩個標準作業。
此範例顯示可如何使用標準作業特性來實作事件序列化。實作解除序列化的方法相似。
要為給定套件中的所有事件產生作業:
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);