DataObject
¶
DataObject is a node in a tree of data.
All data within the data context is described as a tree of data objects. The class provides access and look-up functionality of the contained data objects.
The interface for navigating and modifying the tree of data objects mirrors the same interface as in Node2D and Node3D.
To create a data object call a create function that returns one of the data objects.
Those functions are DataObject:create, DataObjectInt:create, DataObjectBool:create, etc.
-- Create a DataObjectString. Note that an owning reference is created.
local dataObjectStringRef = DataObjectString:create("Name", "Value")
-- It can be used in the same way non-owning references are used.
assertEq(dataObjectStringRef:getName(), "Name")
Kanzi arranges data objects in a tree that you can think of as a directed acyclic graph with no loops. Each data object can have multiple children but only one parent.
You can manipulate the data object tree by adding children:
-- Add the `dataObjectSpeed` data object to the `root` data object, which is a root of some data source:
local dataObjectSpeed = DataObjectInt:create("Root.Speed", 80)
root:addChild(dataObjectSpeed)
You can remove child data objects from a parent data object.
-- Remove the previously added child data objects from the `root` data object.
root:removeChild(dataObjectSpeed)
Parents take ownership, and hold a reference to the children.
-- Release a Lua reference. Kanzi passes the ownership to the parent data object.
dataObjectSpeed = nil
-- The reference is kept alive by Lua until garbage collected. You can optionally force Lua garbage collection.
collectgarbage()
You can iterate over the data object children by using iterators.
for child in dataObjectSpeed:iterateChildren() do
print(child:getName())
end
While you are iterating over a collection, modifying it is not safe.
for child in dataObjectSpeed:iterateChildren() do
dataObjectSpeed:removeAllChildren() -- Bad. Will crash Kanzi Engine.
end
Usually you should search for data objects from DataSource through DataContext interface:
local speedObject = dataSource:lookupDataContext("vehicle.cluster.speed")
If lookup fails, Kanzi returns nil.
Inherits properties and message types from DataObjectMetadata.
| create() | Creates an instance of DataObject |
| getName() | Retrieves the name of the object |
| setName() | Sets the name of the data object |
| getType() | Allows to retrieve the type of the object |
| setParent() | Assigns a parent data object to this object |
| getParent() | Retrieves the parent data object of this object |
| getChildCount() | Retrieves the count of child objects in this data object |
| getChild() | Retrieves a child object at the given index |
| getChildIndex() | Retrieves the index of a child data object |
| findChild() | Tries to find a child with the given name |
| hasChild() | Checks whether or not the child data object exists |
| addChild() | Adds the data object as a child of this object |
| insertChild() | Inserts the data object as a child of this object, allows to specify index of the object |
| removeChildAtIndex() | Removes a child object at the given index |
| removeChild() | Removes a child object from the list of children |
| removeAllChildren() | Removes all children |
| iterateChildren() | Returns an iterator used to iterate over children of the data object |
Creates an instance of DataObject.
| objectName | (string) | The name of the object. |
Retrieves the name of the object.
| (string) | The name of the object. |
Sets the name of the data object.
| name | (string) | The name. |
Allows to retrieve the type of the object.
| (DataObjectType) | Returns the type of the object. |
Assigns a parent data object to this object.
| parent | (DataObject) | Parent DataObject to assign. |
Retrieves the parent data object of this object.
| (DataObject) | The parent DataObject of this DataObject. |
Retrieves the count of child objects in this data object.
| (number) | Child object count. |
Retrieves a child object at the given index.
| index | (number) | Index of the child. |
| (DataObject) | Child data object at the given index. |
Retrieves the index of a child data object.
| child | (DataObject) | Child DataObject which index to get. |
| (number) | Index of the item in question. |
Tries to find a child with the given name.
| (DataObject or nil) | Child object that was found, or nil if there is no such child. |
Checks whether or not the child data object exists.
| child | (DataObject) | Child to check. |
| (boolean) | true if there is such child, otherwise false. |
Adds the data object as a child of this object.
| child | (DataObject) | Child object to add. |
Inserts the data object as a child of this object, allows to specify index of the object.
| index | (number) | Index where to add the given object. |
| child | (DataObject) | Child object to add. |
Removes a child object at the given index.
| index | (number) | Index of the child object to remove. |
Removes a child object from the list of children.
| child | (DataObject) | Child object to remove. |
Removes all children.
Returns an iterator used to iterate over children of the data object.