serial
Short summary
Method to perform a given action on all objects inside the collection.
Objects are invoked with the applier in order until done, only then is the next invoked.
Order is defined by the default iterator.
If at any point the applier returns an error the foreach stops and returns an error.
Action to perform needs to implement IApplier.
Attention: This method is single cycle, what means the call waits for all appliers to finish. Therefore none of the appliers should wait for anything (Events/IO/...)
Example:
You have a list of numbers: 1 2 3 and a ConcreteIncrementApplier that can take a number and increment it.
Calling list.forEach(ConcreteIncrementApplier) will increment all numbers in the list, becoming: 2 3 4.
Return: SUCCESS: Alle appliers returned with SUCCESS
ERROR: The applier returned error for at least one object, or any other error occured
- Return type: CNM_ReturnTypes.SingleExecutionResult
Parameters
| Name | Type | Comment | Kind |
|---|---|---|---|
| applier | CNM_CollectionInterfaces.IApplier | the applier to be used on all objects | input |
Code
Declaration
METHOD serial :CNM_ReturnTypes.SingleExecutionResult
VAR_INPUT
(*the applier to be used on all objects*)
applier :CNM_CollectionInterfaces.IApplier;
END_VAR
VAR
continueForeach :BOOL;
objectState :CNM_ReturnTypes.SingleExecutionState;
object : CNM_AbstractObject.IObject;
applierIndex :UDINT;
currentApplier :CNM_CollectionInterfaces.IApplier;
END_VAR
VAR CONSTANT
NULL : __XWORD := 0;
END_VAR
Implementation
applierIndex := 0;
currentApplier := 0;
IF ((
THIS^.isObjectNull(THIS^.serialIterator)
) AND_THEN (
THIS^.usedCollection.createNewIterator(iterator => THIS^.serialIterator)
<> CNM_ReturnTypes.SingleExecutionResult.SUCCESS
)
) THEN
serial := CNM_ReturnTypes.SingleExecutionResult.ERROR;
RETURN;
END_IF
WHILE THIS^.selectApplier(applier, applierIndex, currentApplier) DO
THIS^.serialIterator.iterate(execute := FALSE);
WHILE (THIS^.serialIterator.iterate(execute := TRUE, object => object) = CNM_ReturnTypes.SingleExecutionState.BUSY) DO
IF currentApplier.accept(object) <> CNM_ReturnTypes.SingleExecutionResult.SUCCESS THEN
serial := CNM_ReturnTypes.SingleExecutionResult.ERROR;
RETURN;
END_IF
currentApplier.apply(FALSE);
WHILE TRUE DO
objectState := currentApplier.apply(TRUE);
CASE objectState OF
CNM_ReturnTypes.SingleExecutionState.SUCCESS:
EXIT;
CNM_ReturnTypes.SingleExecutionState.BUSY:
;
ELSE
serial := CNM_ReturnTypes.SingleExecutionResult.ERROR;
RETURN;
END_CASE
END_WHILE
END_WHILE
END_WHILE
serial := CNM_ReturnTypes.SingleExecutionResult.SUCCESS;