;;; -*- mode: lisp -*-
;;; $Id: objinst.poplisp,v 1.0 2002/05/08 11:16: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"))))
(declare (fixnum n))
(let ((tog (make-toggle)))
  (dotimes (i 5)
(declare (fixnum i))
(print-bool (toggle-state (activate tog)))))
(dotimes (i n)
  (make-toggle))

(format t "~%")

(let ((ntog (make-nth-toggle 3)))
  (dotimes (i 8)
(declare (fixnum i))
(print-bool (toggle-state (activate ntog)))))
(dotimes (i n)
  (declare (fixnum i))
  (make-nth-toggle 3)))