added events example.

This commit is contained in:
Norayr Chilingarian 2015-05-27 02:56:52 +04:00
parent fbdf562e2e
commit 3665c36d27
3 changed files with 66 additions and 0 deletions

View file

@ -0,0 +1,7 @@
**events example in oberon**
Just a simple example which helps to explain how events are implemented, what is callback procedure, and how easy it is.
Some languages simplify programmer's work, hide details, so that it's not obvious to him what's going on when he defines it's own OnSomething function.
Callbacks are common when dealing with gui events like OnClick but they can be used with any events. I guess code is simple enough, it doesn't need explanation.

55
src/test/events/clb.Mod Normal file
View file

@ -0,0 +1,55 @@
MODULE clb;
IMPORT Console;
TYPE OnSomething = PROCEDURE (x, y : INTEGER);
PROCEDURE ProcessEvents(x, y : INTEGER; onsomething : OnSomething);
BEGIN
IF onsomething # NIL THEN onsomething(x, y)
ELSE
Console.String("didn't happen"); Console.Ln
END;
END ProcessEvents;
PROCEDURE OnEvent(x, y : INTEGER);
BEGIN
Console.String("event happened"); Console.Ln
END OnEvent;
PROCEDURE OnEvent2(x, y : INTEGER);
BEGIN
Console.String("happened"); Console.Ln
END OnEvent2;
PROCEDURE Something;
VAR onsmth : OnSomething;
BEGIN
onsmth := NIL;
ProcessEvents(0, 0, onsmth);
onsmth := OnEvent;
ProcessEvents(0, 0, onsmth);
END Something;
BEGIN
Something;
(*
ProcessEvents(0, 0, NIL);
ProcessEvents(0, 0, OnEvent);
ProcessEvents(0, 0, OnEvent2);
*)
END clb.

4
src/test/events/makefile Normal file
View file

@ -0,0 +1,4 @@
all:
/opt/voc/bin/voc -M clb.Mod