Delphi Topics
This page discusses some topics specific to the Delphi version of the Iceoryx2 API and OOP-style library.
Error Handling
Most API's return a generic type TResult<T, E>, which contains either the result of the API or an error value (similar to Rust's Result type).
You can choose to handle the result yourself by checking the IsOk or IsErr properties before you access the Value (or the Error) property. Or you can just use the Value property without checking for validity first, in which case an exception will be raised in case the result contains an error.
To check for errors yourself:
var NodeResult := TNodeBuilder.New.Create(TServiceType.Ipc);
if (NodeResult.IsOk) then
begin
var Node := NodeResult.Value;
{ ... continue using Node ... }
end
else
{ Handle error yourself }
HandleError(NodeResult.Error);
To raise an exception in case of an error:
{ Accessing .Value without checking first may raise an exception }
var Node := TNodeBuilder.New.Create(TServiceType.Ipc).Value;
Errors are always of an enumerated type, depending on context, such as TNodeCreationFailure when creating a node failed. When an exception is raised, it contains the string version of the enumerated value.
Memory Management
All Iceoryx2 "classes" are wrapped inside Delphi records. Almost all memory management is automatic: any Iceoryx2 "objects" you create are released automatically when those objects go out of scope. This is accomplished through the use of Custom Managed Records.
Most of the time, you can treat this records as classes: they have methods and properties like regular classes do. You can also check against nil to see of the record is assigned, like you would with regular objects.
There are a couple of situations, most notably when using TWaitSetGuard, where you need to release the object manually before any dependent objects go out of scope. In those cases, you should call the Release method (eg. TWaitSetGuard.Release).
Static Strings and Vectors
You cannot communicate Delphi strings or dynamic arrays across processes since these types are basically pointers to dynamically allocated memory. Instead, you can use the TStaticString<> and TStaticVector<> types from the Neslib.Iceoryx2.Containers unit.
The TStaticString<> and TStaticVector<> types are compatible across Delphi, C++ and Rust, and any other language binding that follows the original Rust specification. However, using them in Delphi is a bit more complicated since Delphi doesn't support "generic value parameters". For example, unlike C++ and Rust, in Delphi you cannot declare a static string like this:
Instead you need to define a storage type that is 256 bytes in size:
Or use one of the pre-defined storage types like T256Bytes.
For TStaticVector<>, the storage specifies the number of bytes, not the number of elements in the vector. So if you want to create a static vector that can hold at most 8 values of type Double, than you need 64 bytes of storage since a Double is 8 bytes in size:
Declaring a two dimensional vector (eg. a nested vector or matrix) gets a bit more complicated since you need know how much storage you need for a single row in the matrix. You cannot just multiple the number of rows by the number of columns by the size of each element, since TStaticVector<> itself also adds some overhead. So you need to declare a data type for the storage of the matrix first:
You cannot declare
TRowandTMatrixStoragein the sametypesection. Doing so will result in a compilation error.
And then you can declare a matrix like this:
If you don't define the storage types correctly, then you will get an "Incompatible Types" error when trying to communicate with a service written in a language other than Delphi.