clone
Short summary
This method is used to create a new instance
of the object which has the same internal state as the object.
If the object is a container class, the content objects
are not cloned, for this there is the method ICloneable.deepClone.
If an object does not support cloning the return value is
CNM_ReturnTypes.CloneResult.CLONE_IS_NOT_SUPPORTED.
If there is not enough memory or the cloning fails, because the
attribute enable dynamic creation was forgotten, the return value is
CNM_ReturnTypes.CloneResult.FAILED.
Only if the return value is CNM_ReturnTypes.CloneResult.SUCCESS
the output clonedObject points to the new object
otherwise clonedObject is NULL.
Return: CNM_ReturnTypes.CloneResult.SUCCESS: new object clone was created,
CNM_ReturnTypes.CloneResult.FAILED: object clone failed
CNM_ReturnTypes.CloneResult.CLONE_IS_NOT_SUPPORTED: object does not support cloning
- Return type: CNM_AbstractObject.CNM_ReturnTypes.CloneResult
Parameters
| Name | Type | Comment | Kind |
|---|---|---|---|
| clonedObject | CNM_AbstractObject.IObject | new object instance or NULL if somthing went wrong | output |
Code
Declaration
METHOD clone :CNM_AbstractObject.CNM_ReturnTypes.CloneResult
VAR_OUTPUT
(* new object instance or NULL if somthing went wrong *)
clonedObject :CNM_AbstractObject.IObject;
END_VAR
VAR
newTreeNode :POINTER TO BalancedBinarySearchTreeNode;
newChildObject :CNM_AbstractObject.IObject;
newChildNode :CNM_CollectionInterfaces.IBinaryTreeNode;
END_VAR
VAR CONSTANT
OBJECT_NOT_REFERENCED :__XWORD := 0;
END_VAR
Implementation
newTreeNode := __NEW(
BalancedBinarySearchTreeNode(
value := THIS^.object,
left := OBJECT_NOT_REFERENCED,
right := OBJECT_NOT_REFERENCED
)
);
IF (newTreeNode = 0) THEN
clone := CNM_ReturnTypes.CloneResult.FAILED;
RETURN;
ELSIF THIS^.isObjectValid(newTreeNode^) THEN
clone := CNM_ReturnTypes.CloneResult.SUCCESS;
IF THIS^.isObjectValid(THIS^.left) THEN
clone := THIS^.left.clone(clonedObject => newChildObject);
__QUERYINTERFACE(newCHildObject,newChildNode);
newTreeNode^.leftChild := newChildNode;
END_IF
IF THIS^.isObjectValid(THIS^.right) AND clone = CNM_ReturnTypes.CloneResult.SUCCESS THEN
clone := THIS^.right.clone(clonedObject => newChildObject);
__QUERYINTERFACE(newCHildObject,newChildNode);
newTreeNode^.rightChild := newChildNode;
END_IF
clonedObject := newTreeNode^;
RETURN(clone = CNM_ReturnTypes.CloneResult.SUCCESS);
ELSE
clone := CNM_ReturnTypes.CloneResult.FAILED;
END_IF
//clone wasn't success, clean up
IF THIS^.isObjectValid(newTreeNode^) THEN
THIS^.destructNodeAndAllChilds(newTreeNode^);
clonedObject := OBJECT_NOT_REFERENCED;
END_IF