FILE ViewRecExampleClient.Mesa
last changed by Spreitzer at December 12, 1983 9:53 pm
A sample use of ViewRec
DIRECTORY Rope, IO, ViewerIO, ViewRec, Process;
ViewRecExampleClient:
PROGRAM
IMPORTS IO, ViewerIO, ViewRec, Process =
BEGIN
Wasnt: ERROR = CODE;
ROPE: TYPE = Rope.ROPE;
State: TYPE = {x, Y, z};
Foo: TYPE = {xyz, abc, lmn};
FuBar: TYPE = {abc, de, f, g, hu, i, j, k, l, m};
Handle: TYPE = REF Rep;
Rep:
TYPE =
RECORD [
state: State ← Y,
push: PROC[inst: Instance, number: LONG CARDINAL],
pokes: LONG INTEGER ← 1,
foo: LONG CARDINAL ← 7,
fb: FuBar ← de,
char: CHAR ← '\n,
bool: BOOLEAN ← FALSE,
f: Foo ← abc,
oof: RECORD [a: REAL, b: INTEGER] ← [-47, 17701],
name: Rope.ROPE ← "fu",
SetState: PROC [h: Handle, to: State] RETURNS [was: State],
MustBe: PROC [h: Handle, what: State ← z, eq: BOOLEAN],
Allowable: PROC [h: Handle, which: ARRAY State OF BOOLEAN ← [FALSE, TRUE, FALSE]],
Negate: PROC [h: Handle],
SetName: PROC [h: Handle, newName: ROPE],
Delay: PROC [milliseconds: CARDINAL],
stuff: SEQUENCE length: CARDINAL OF ARRAY BOOLEAN OF REAL
];
Instance: TYPE = REF InstanceBody;
InstanceBody: TYPE = RECORD[stream: IO.STREAM];
count: CARDINAL ← 0;
SetState:
PROC [h: Handle, to: State]
RETURNS [was: State] =
BEGIN
was ← h.state;
h.state ← to;
h.pokes ← -(h.pokes + (IF h.pokes < 0 THEN -1 ELSE 1));
END;
MustBe:
PROC [h: Handle, what: State ← z, eq:
BOOLEAN] =
BEGIN
IF (h.state = what) # eq THEN ERROR Wasnt;
END;
Allowable:
PROC [h: Handle, which:
ARRAY State
OF
BOOLEAN ← [
FALSE,
TRUE,
FALSE]] =
{IF NOT which[h.state] THEN ERROR};
Push: PROC[inst: Instance, number: LONG CARDINAL ← 20] = {inst.stream.PutF["given %g\n", IO.card[number]]};
Negate: PROC [h: Handle] = {h.pokes ← - h.pokes};
SetName: PROC [h: Handle, newName: ROPE] = {Delay[5000]; h.name ← newName};
Delay:
PROC [milliseconds:
CARDINAL] =
{Process.Pause[Process.MsecToTicks[milliseconds]]};
Doit:
PROC
RETURNS [h: Handle] =
BEGIN
instance: Instance ← NEW[InstanceBody ← [ViewerIO.CreateViewerStreams[name: IO.PutFR["foo#%g", IO.card[count ← count+1]]].out]];
h ← NEW [Rep[3]];
h.state ← Y;
h.pokes ← 1;
h.foo ← 7;
h.fb ← de;
h.bool ← FALSE;
h.f ← abc;
h.oof ← [-47, 17701];
h.name ← "fu";
h.SetState ← SetState;
h.push ← Push;
h.MustBe ← MustBe;
h.Allowable ← Allowable;
h.Negate ← Negate;
h.SetName ← SetName;
h.Delay ← Delay;
FOR i: CARDINAL IN [0 .. h.length) DO h[i] ← [6.9, 2.7] ENDLOOP;
[] ← ViewRec.ViewRef[agg: h,
specs: ViewRec.BindAllOfATypeFromRefs[h,
NEW [Instance ← instance]].
BindingListAppend[ViewRec.BindAllOfATypeFromRefs[h, NEW[Handle ← h]]],
viewerInit: [iconic: FALSE, name: IO.PutFR["Test#%g", IO.card[count]]],
paint: TRUE];
END;
END.