IBM Support

How to use WriterTemplate introduced in Rhapsody 8.0.6 for advance code formatting

Question & Answer


Question

How do you use WriterTemplate feature introduced in IBM Rational Rhapsody 8.0.6 in order to achieve advanced code formatting over multi-lining, spacing, indentation, positioning and more?

Cause

Information regarding WriterTemplate is limited in the Information Center for Rhapsody.

Answer

WriterTemplate was introduced in Rhapsody 8.0.6 to make the task of applying different formatting (e.g. multi-lining, spacing, indentation, positioning, etc) more flexible and customizable.

This feature is not provided to replace conventional formatting options already available in Rhapsody; however, it will allow you to do things exemplified below, which were not easy to achieve in old releases.

Review the General Formatting section in white paper 7039283: Customize code generation using Rational Rhapsody for C++ for details.

  • Example 1: Move comments inside the operation body (click to collapse or expand)
    1. Create a mode as below:


      Note: The comment "Where do you see me?" is generated in the header file.

    2. Set CPP_CG::Operation::GenerateDescriptionInImplementation property for class_0 to "Use SpecificationText".

      You will see the comment show up in the implementation file as below:



    3. Navigate to CPP_CG::WriterTemplates::OperationImp for Operation_0.

    4. Update WriterTemplate as below:



    5. See the comment appear inside the operation body:

  • Example 2: Add 3 spaces in front of visibility keywords (click to collapse or expand)
    1. Create a mode as below:


      Note:
      Visibility keywords (Public/Protected/Private) are placed at the beginning of lines.

    2. Navigate to CPP_CG::WriterTemplates::ProtectionGroup for class_0.

    3. Update WriterTemplate as below:



    4. See the code generated as below:

  • Example 3: Align all lines at the beginning (click to collapse or expand)
    1. Navigate to CPP_CG::WriterTemplates::ClassSpecElement for a class.

    2. Update WriterTemplate as below:



    3. See the code generated will change as below:


    You can also use this feature to insert special keywords, however, it won't be recommended because doing so in CPP_Roundtrip::General::RoundtripScheme=Respect (default) mode would make your code not understandable by Rhapsody, leading to various parsing errors described in technote 1640085: Rhapsody roundtrip reports parsing error if terminator is missing in macro statement inside of preserve code block

    For more information, navigate to Properties tab in the Features dialog of each element, and look at properties defined under the WriterTemplate meta-class.

    Notes:
    • Each WritetTemplate property contains “[[…]]”. Within those brackets there are Code Generation extractor keywords (those keywords will be substituted with proper code).
    • Adding code within these brackets will affect code, only if at least one of the contained keywords is substituted.

    [{"Product":{"code":"SSB2MU","label":"IBM Engineering Systems Design Rhapsody"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Documentation","Platform":[{"code":"PF016","label":"Linux"},{"code":"PF033","label":"Windows"}],"Version":"8.0.6","Edition":"","Line of Business":{"code":"LOB59","label":"Sustainability Software"}}]

    Product Synonym

    Rational Rhapsody

    Document Information

    Modified date:
    27 May 2022

    UID

    swg21670578