NAME

TUI::Manual::09 - Streams and Persistence in TUI::Vision

STREAMS AND PERSISTENCE

Turbo Vision uses streams to separate the persistent representation of an object from its in-memory representation. TUI::Vision follows the same model: persistent framework objects describe their state through write and read, while TStream provides the sequential transport used by these methods.

This distinction is important for object structures such as views, groups and windows. Persistence does not simply copy their Perl data structures. The framework's write and read methods decide which state is saved, and during restoration they rebuild the relationships required by the TUI::Vision object model.

Why Streams Exist

A persistent object often contains more than independent scalar values. Objects can contain other objects, refer to objects owned elsewhere in the view hierarchy, and share references with other parts of the object graph.

Serializing the Perl representation directly would make the persistence format depend on implementation details of the object. It would also bypass the reconstruction performed by the object's read method.

TStream therefore provides a small sequential interface. An object writes its persistent state in a defined order. The following excerpt from TView stores the geometry, state and option fields of a view:

sub write :method {    # void ($os)
    ...
    my $saveState =
        $self->{state} & ~( sfActive | sfSelected | sfFocused | sfExposed );

    $os->put(
        $self->{origin},   $self->{size},     $self->{cursor},
        $self->{growMode}, $self->{dragMode}, $self->{helpCtx},
        $saveState,        $self->{options},  $self->{eventMask},
    );

    return;
}

The transient flags are cleared before the state is stored. They describe the running session, not the persistent layout of the view.

The corresponding read method consumes the values in the same order:

sub read :method {    # $self ($is)
    ...
    $self->{origin}    = $is->get();
    $self->{size}      = $is->get();
    $self->{cursor}    = $is->get();
    $self->{growMode}  = $is->get();
    $self->{dragMode}  = $is->get();
    $self->{helpCtx}   = $is->get();
    $self->{state}     = $is->get();
    $self->{options}   = $is->get();
    $self->{eventMask} = $is->get();
    $self->owner( undef );
    ...
}

The read order must match the write order exactly. The signature handling and the assertions are omitted from both excerpts.

The object's write and read implementations therefore define the persistent state of the framework classes, not their internal Perl representation.

Streamable Objects

Objects that participate in object streaming perform the TStreamable role.

A TStreamable class provides two operations:

  • write

    Writes the persistent state of the object to a TStream.

  • read

    Restores the persistent state of the object from a stream.

TStream recognizes objects performing this role when they are passed to put. Ordinary values retain normal value semantics, while streamable objects participate in object identity tracking.

The Perl implementation does not call write and read from put and get directly. A streamable object is serialized with Storable.

Framework classes such as TView and TCollection connect their write and read methods to this mechanism through the STORABLE_freeze and STORABLE_attach hooks. The freeze hook writes the object into a nested stream; the attach hook builds an empty object and calls read on it.

A class that performs the role without these hooks is serialized as its plain Perl structure. The role does not enforce the write/read contract, so classes that rely on it must provide the hooks.

This corresponds to the distinction made by the original Turbo Vision object streaming system between ordinary streamed values and streamable objects.

Object Identity

Persistence must distinguish between an object's contents and its identity.

Consider an object referenced twice, as the frame of a window is (see "Object Graphs"). Writing the frame twice must not cause two independent frame objects to appear after restoration.

When TStream encounters a TStreamable object for the first time, it records the object's identity together with its persistent representation. If the same object is encountered again, the stream records a reference to the previously written object instead of another independent representation.

On reading, the first occurrence reconstructs the object and associates it with the recorded identity. Later references resolve to that reconstructed object.

The identifiers and registration structures used to implement this behavior are internal to the stream implementation. Applications should rely only on the resulting identity semantics.

In Turbo Vision, the record of written objects belongs to each stream. The Perl implementation keeps the registries of written and restored objects process-wide, so identity is shared by all streams in the process. A stream that contains only a reference to an object is therefore resolved only if that object was already restored in the same process. Restore the stream that contains the full object before reading a stream that refers to it.

The lifetime of these entries follows the objects they describe. An entry in the registry of written objects is removed when the object is destroyed. The registry of restored objects keeps the identifier, but its value is a weak reference, so it no longer keeps the object alive. No public API clears either registry; an application cannot remove individual entries.

Object Graphs

Object registration allows the restored TUI::Vision object graph to preserve shared references.

For example, a TWindow stores its frame in the frame field, and the constructor inserts the same frame into the window's subview ring. TWindow is a TGroup, so TWindow::write calls SUPER::write to write the subviews, and it also stores $self->{frame} itself. Both references point to one frame object, and restoration must not produce two.

After persistence and restoration, the required relationship is therefore the following. The diagram shows runtime references after restoration, not inheritance or Z-order:

restored window->{frame} ──┐
                           │
                           ├──▶ restored frame
                           │
restored subview ──────────┘

The restored window and its subview must refer to one frame, not to two independently reconstructed frames.

This is especially important for TUI::Vision structures. Relationships among groups, views and windows are reconstructed according to their read implementations. The stream preserves object identity while those methods rebuild the framework-specific structure.

Consequently, persistence should be understood as reconstruction of the TUI::Vision object graph, not as a direct copy of the complete in-memory Perl reference graph.

Conclusion

TUI::Vision separates persistence into three responsibilities:

  • Persistent objects define their state through write and read. For the framework classes, the Storable hooks call these methods during serialization and restoration.

  • TStreamable identifies objects that participate in object streaming.

  • TStream transports values and preserves the identity of shared streamable objects while the TUI::Vision object graph is reconstructed. In the Perl implementation, this identity registry is shared by all streams in the process.

This keeps the persistent state of framework objects in write and read, independent of their internal Perl representation, while the relationships required by the TUI::Vision object model are preserved. Resource files are not ported; see Porting.

The next chapter covers advanced topics, including the differences from Turbo Vision C++ and Pascal.