Skip to content
Tcl Source Code Artifact [31bb2c29a6]
Login

Artifact [31bb2c29a6]

Artifact 31bb2c29a6c963383f56973b5ef869ac15da4641e5f4966914632b50efa9d204:


# cstruct.tcl --
# FR : les types Tcl qui décrivent les structures de cstruct.c, et les
#      commandes de confort qui passent un produit typé à C.
# EN : the Tcl types describing cstruct.c's structs, and the convenience
#      commands that hand a typed product to C.
#
#   Le contrat : « -align natural » = la disposition du compilateur C.
#   The contract: "-align natural" = the C compiler's layout.

namespace eval cs {variable dir [file dirname [file normalize [info script]]]}

proc cs::init {{lib ""}} {
    variable dir
    if {$lib eq ""} {set lib [file join $dir cstruct[info sharedlibextension]]}
    ::load $lib Cstruct
    # Types globaux (::Sample) : ils portent le nom des structs C, et le
    # programme qui charge cstruct s'en sert sous ce nom. Sans "::", ils
    # seraient ::cs::Sample (v16u : un type appartient à l'espace de noms
    # où il est défini).
    # struct Sample { uint32_t id; double value; uint8_t flags; };
    type def ::Sample {{u32le}id; {f64le}value; {u8}flags}
    # struct Batch  { uint32_t n; Sample items[]; };
    type def ::Batch {{u32le}n; {list($n).Sample}items}
    # struct Summary { uint32_t count; double min, max, mean; };
    type def ::Summary {{u32le}count; {f64le}min; {f64le}max; {f64le}mean}
}

# FR : voie rapide : le produit est encodé selon la disposition C et lu tel quel
#      par C ; le résultat revient en produit Summary annoté.
# EN : fast path: the product is encoded in the C layout and read as is by C;
#      the result comes back as an annotated Summary product.
proc cs::stats {batch} {
    set bytes [type binary encode Batch $batch -align natural]
    return [type binary decode Summary [cs::stats_raw $bytes] -align natural]
}

# FR : voie « objet » : C reçoit la valeur annotée elle-même.
# EN : "object" path: C receives the annotated value itself.
proc cs::stats_typed {batch} {
    return [type binary decode Summary [cs::stats_obj $batch] -align natural]
}

# FR : C fabrique un Batch ; Tcl le relit comme produit annoté.
# EN : C builds a Batch; Tcl reads it back as an annotated product.
proc cs::make {n seed} {
    return [type binary decode Batch [cs::generate $n $seed] -align natural]
}