summaryrefslogtreecommitdiff
path: root/Examples/chicken/class/runme-tinyclos.scm
blob: 5ba1d6adb1428c204dcac170206c16ac5ef03998 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
;; This file illustrates the proxy C++ interface generated
;; by SWIG.

(load-library 'example "class_proxy.so")
(declare (uses example))
(declare (uses tinyclos))

;; ----- Object creation -----

(display "Creating some objects:\n")
(define c (make <Circle> 10.0))
(display "    Created circle ")
(display c)
(display "\n")
(define s (make <Square> 10.0))
(display "    Created square ")
(display s)
(display "\n")

;; ----- Access a static member -----

(display "\nA total of ")
(display (Shape-nshapes))
(display " shapes were created\n")

;; ----- Member data access -----

;; Set the location of the object

(slot-set! c 'x 20.0)
(slot-set! c 'y 30.0)

(slot-set! s 'x -10.0)
(slot-set! s 'y 5.0)

(display "\nHere is their current position:\n")
(display "    Circle = (")
(display (slot-ref c 'x))
(display ", ")
(display (slot-ref c 'y))
(display ")\n")
(display "    Square = (")
(display (slot-ref s 'x))
(display ", ")
(display (slot-ref s 'y))
(display ")\n")

;; ----- Call some methods -----

(display "\nHere are some properties of the shapes:\n")
(let
    ((disp (lambda (o)
             (display "   ")
             (display o)
             (display "\n")
             (display "        area      = ")
             (display (area o))
             (display "\n")
             (display "        perimeter = ")
             (display (perimeter o))
             (display "\n"))))
  (disp c)
  (disp s))

(display "\nGuess I'll clean up now\n")

;; Note: Invoke the virtual destructors by forcing garbage collection
(set! c 77)
(set! s 88)
(gc #t)

(display (Shape-nshapes))
(display " shapes remain\n")
(display "Goodbye\n")

(exit)