Overview Clamps Packages CM Dictionary Clamps Dictionary Fomus
Next: cl-sfz , Previous: Defining relations , Up: cl-refs , Home: General

Clamps Packages

The bang-object class

A bang-object is an extension to a ref-object by adding the option to trigger it. The inspiration for a bang object is an object which behaves like a button in a gui or on a hardware controller: In addition to the state of the button (on, off, flashing, etc.) which is captured in the value of its ref-object part, a button can be pressed. A button-press is realized by calling the trigger function on an instance of the bang-object. Note that the triggered function is called without arguments and any value it returns is ignored by trigger. It's called for its side-effect only.

creation function

A bang-object gets created with the function make-bang. The optional arguments of the function are:

  1. A function of no arguments to be executed when calling trigger with the created instance as argument.
  2. The initial value of the object, exactly like the argument to make-ref.

Here is an example:

(defparameter b1 (make-bang (lambda () (msg :warn "Bang object b1 triggered")) 1)) ; => b1

b1 ; => #<bang 1>

(get-val b1) ; => 1
(set-val b1 0) ; => 0
(get-val b1) ; => 0

(trigger b1) ; => nil
;; Output in the REPL:
;; warn: Bang object b1 triggered

When supplying nil as the trigger function argument to make-bang, no action is taken when calling trigger on the created instance.

add-trigger-fn takes at least two arguments: An instance of a bang-object and one or more functions of no argument, to be executed when the object is triggered.

Like watch for the value part of a ref-object, add-trigger-fn returns a function of no arguments, which can be called to remove the added trigger-fn from the bang-object:

(defparameter b1 (make-bang nil 0)) ; => b1

b1 ; => #<bang 1>

(trigger b1) ; => nil
;; No output in the REPL

(defparameter *bang-unwatch-fn* nil)

(setf *bang-unwatch-fn* (add-trigger-fn b1 (lambda () (msg :warn "Bang object b1 triggered"))))
;;  => #<function (lambda () :in add-trigger-fn) {100B72647B}>

(trigger b1) ; => nil
;; Output in the REPL
;; warn: Bang object b1 triggered

(funcall *bang-unwatch-fn*) ; => nil

(trigger b1) ; => nil
;; No output in the REPL