;;; -*- mode: lisp -*-
;;; $Id: methcall.poplisp,v 1.0 2002/05/08 11:15:00 dada Exp $
;; OO with CLOS
(proclaim '(optimize (speed 3)(safety 0)(space 0)(debug 0)(compilation-speed 0)))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defstruct (toggle (:constructor make-toggle ()))
(state t :type boolean)))
(defmethod activate ((this toggle))
(setf (toggle-state this) (not (toggle-state this)))
this)
(eval-when (:compile-toplevel :load-toplevel :execute)
(defstruct (nth-toggle (:include toggle)
(:constructor make-nth-toggle (count-max)))
(count-max 1 :type fixnum)
(counter 0 :type fixnum)))
(defmethod activate ((this nth-toggle))
(incf (nth-toggle-counter this))
(cond ((>= (nth-toggle-counter this)
(nth-toggle-count-max this))
(setf (toggle-state this) (not (toggle-state this)))
(setf (nth-toggle-counter this) 0)))
this)
(defun print-bool (b)
(format t (if b "true~%" "false~%")))
(let ((n (parse-integer (or (car pop11::poparglist) "1")))
(val))
(declare (fixnum n val))
(let ((tog (make-toggle)))
(dotimes (i n)
(declare (fixnum i))
(setq val (toggle-state (activate tog))))
(print-bool (toggle-state tog))
(let ((ntog (make-nth-toggle 3)))
(dotimes (i n)
(declare (fixnum i))
(setq val (toggle-state (activate ntog))))
(print-bool (toggle-state ntog)))))