ALF for C++ Code Generation Examples

This document provides selected ALF examples and their corresponding C++ output, automatically generated from JUnit tests marked for documentation. It complements the main ALF code generation guide with verified working examples.

This page discusses:

Generation Off Region

Generation-off Region with an In-line Island

Reference: GenerationOffRegion. Verified by JUnit test testTheRegionIsRenderedAsDocumentationAndTheIslandAsCode.

ALF:

//@generation(off)
this.display.show(x);
/*@inline('C++')
native_show();
*/
this.log();
//@generation(on)

C++:

// @generation(off): ALF simulated by CATIA Magic, not generated
//   this.display.show(x);
native_show();
//   this.log();
// @generation(on)

In Line Statement

In-line Code for the Other Target

Reference: InLineStatement. Verified by JUnit test testInLineForTheOtherTargetIsOmittedWithATrace.

ALF:

/*@inline(C)
other_target_only();
*/

C++:

// @inline(C) omitted for this target

In-line Code for This Target

Reference: InLineStatement. Verified by JUnit test testInLineForThisTargetIsEmittedVerbatim.

ALF:

/*@inline('C++')
native_call(42);
*/

C++:

native_call(42);

In-line Code Among ALF Statements

Reference: InLineStatement. Verified by JUnit test testInLineMixesWithGeneratedAlf.

ALF:

x = x + 1;
/*@inline('C++')
native_call(x);
*/

C++:

x = x + 1;
native_call(x);

Invocation Expression

Virtual Operation Call

References: ThisExpression, InvocationExpression, FeatureInvocationTarget. Verified by JUnit test testMethodCall.

ALF:

this.computeValue(42);

C++:

this->computeValue(42);

WriteLine Call

Reference: InvocationExpression. Verified by JUnit test testWriteLineFunction.

ALF:

WriteLine("Hello");

C++:

tal_printf("Hello\n");

Reception Call

Reference: InvocationExpression. Verified by JUnit test testReceptionCall.

ALF:

light.PressedSignal();

C++:

this->light->PressedSignal();

Do Statement

Do-While Loop with Break

References: DoStatement, BreakStatement. Verified by JUnit test testDoWhileWithBreak.

ALF:

do {
    if (done) {
        break;
    }
} while (running);

C++:

do {
    if (done) {
        break;
    }
} while (running);

Simple Do-While Loop

Reference: DoStatement. Verified by JUnit test testDoWhileLoop.

ALF:

do {
    x--;
} while (x > 0);

C++:

do {
    x--;
} while (x > 0);

If Statement

If / Else

Reference: IfStatement. Verified by JUnit test testIfElseStatement.

ALF:

if (x > 0) {
    y = 1;
} else {
    y = -1;
}

C++:

if (x > 0) {
    y = 1;
} else {
    y = -1;
}

Simple If

Reference: IfStatement. Verified by JUnit test testSimpleIfStatement.

ALF:

if (x > 0) {
    y = 1;
}

C++:

if (x > 0) {
    y = 1;
}

If / Else-If Chain

Reference: IfStatement. Verified by JUnit test testIfElseIfChain.

ALF:

if (x > 0) {
    y = 1;
} else if (x < 0) {
    y = -1;
} else {
    y = 0;
}

C++:

if (x > 0) {
    y = 1;
} else if (x < 0) {
    y = -1;
} else {
    y = 0;
}

Early Return

References: IfStatement, ReturnStatement. Verified by JUnit test testEarlyReturnSimple.

ALF:

if (x) {
    return 0;
}
return 42;

C++:

if (x) {
    return 0;
}
return 42;

For Statement

Collection-Based for

Reference: ForStatement. Verified by JUnit test testForLoopSimple.

ALF:

for (item in items) {
    total = total + item;
}

C++:

for (int item : this->items) {
    total = total + item;
}

Range-Based for

Reference: ForStatement. Verified by JUnit test testForRange.

ALF:

for (i in 1..5) {
    total = total + i;
}

C++:

for (int i = 1; i <= 5; i++) {
    total = total + i;
}

While Statement

Simple While Loop

Reference: WhileStatement. Verified by JUnit test testSimpleWhileLoop.

ALF:

while (x > 0) {
    x--;
}

C++:

while (x > 0) {
    x--;
}

While Loop with Break

References: WhileStatement, BreakStatement. Verified by JUnit test testBreakInWhileLoop.

ALF:

while (true) {
    if (done) {
        break;
    }
}

C++:

while (true) {
    if (done) {
        break;
    }
}