From 5cfef34ec9fc67df3149436a83873ce527d4af9d Mon Sep 17 00:00:00 2001 From: mescalinum Date: Mon, 31 Aug 2009 13:42:50 +0000 Subject: add usage examples and help svn path=/trunk/externals/tclpd/; revision=12154 --- dynroute.tcl | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 dynroute.tcl (limited to 'dynroute.tcl') diff --git a/dynroute.tcl b/dynroute.tcl new file mode 100644 index 0000000..ef8553c --- /dev/null +++ b/dynroute.tcl @@ -0,0 +1,56 @@ +source pdlib.tcl + +# dynroute: dynamically route messages based on first element +# non-matching arguments are sent to last inlet +# constructor: specify the number of outlets (default: 1) +# send commands to the right inlet +# available commands: +# add route selector to output number +# remove remove previously created routing +# clear + +pd::class dynroute { + constructor { + pd::add_inlet $self list + + set @num_outlets [pd::arg 0 int] + if {$@num_outlets < 0} {set @num_outlets 2} + + for {set i 0} {$i < $@num_outlets} {incr i} { + pd::add_outlet $self list + } + + set @routing {} + } + + 0_list { + set sel [pd::arg 0 any] + set out [expr {$@num_outlets-1}] + catch {set out [dict get $@routing $sel]} + pd::outlet $self $out list $args + } + + 1_add { + set sel [pd::arg 0 any] + set out [pd::arg 1 int] + if {$out < 0 || $out >= $@num_outlets} { + pd::post "error: add: outlet number out of range" + return + } + dict set @routing $sel $out + } + + 1_remove { + set sel [pd::arg 0 any] + set out [pd::arg 1 int] + if {$out < 0 || $out >= $@num_outlets} { + pd::post "error: add: outlet number out of range" + return + } + catch {dict unset @routing $sel $out} + } + + 1_clear { + set @routing {} + } +} -- cgit v1.2.1