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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
|
# tclOOScript.h --
#
# This file contains support scripts for TclOO. They are defined here so
# that the code can be definitely run even in safe interpreters; TclOO's
# core setup is safe.
#
# Copyright (c) 2012-2019 Donal K. Fellows
# Copyright (c) 2013 Andreas Kupries
# Copyright (c) 2017 Gerald Lester
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.
::namespace eval ::oo {
::namespace path {}
#
# Commands that are made available to objects by default.
#
namespace eval Helpers {
namespace path {}
# ------------------------------------------------------------------
#
# callback, mymethod --
#
# Create a script prefix that calls a method on the current
# object. Same operation, two names.
#
# ------------------------------------------------------------------
proc callback {method args} {
list [uplevel 1 {::namespace which my}] $method {*}$args
}
# Make the [callback] command appear as [mymethod] too.
namespace export callback
namespace eval tmp {namespace import ::oo::Helpers::callback}
namespace export -clear
rename tmp::callback mymethod
namespace delete tmp
# ------------------------------------------------------------------
#
# classvariable --
#
# Link to a variable in the class of the current object.
#
# ------------------------------------------------------------------
proc classvariable {name args} {
# Get a reference to the class's namespace
set ns [info object namespace [uplevel 1 {self class}]]
# Double up the list of variable names
foreach v [list $name {*}$args] {
if {[string match *(*) $v]} {
set reason "can't create a scalar variable that looks like an array element"
return -code error -errorcode {TCL UPVAR LOCAL_ELEMENT} \
[format {bad variable name "%s": %s} $v $reason]
}
if {[string match *::* $v]} {
set reason "can't create a local variable with a namespace separator in it"
return -code error -errorcode {TCL UPVAR INVERTED} \
[format {bad variable name "%s": %s} $v $reason]
}
lappend vs $v $v
}
# Lastly, link the caller's local variables to the class's variables
tailcall namespace upvar $ns {*}$vs
}
# ------------------------------------------------------------------
#
# link --
#
# Make a command that invokes a method on the current object.
# The name of the command and the name of the method match by
# default.
#
# ------------------------------------------------------------------
proc link {args} {
set ns [uplevel 1 {::namespace current}]
foreach link $args {
if {[llength $link] == 2} {
lassign $link src dst
} elseif {[llength $link] == 1} {
lassign $link src
set dst $src
} else {
return -code error -errorcode {TCLOO CMDLINK FORMAT} \
"bad link description; must only have one or two elements"
}
if {![string match ::* $src]} {
set src [string cat $ns :: $src]
}
interp alias {} $src {} ${ns}::my $dst
trace add command ${ns}::my delete [list \
::oo::UnlinkLinkedCommand $src]
}
return
}
}
# ----------------------------------------------------------------------
#
# UnlinkLinkedCommand --
#
# Callback used to remove linked command when the underlying mechanism
# that supports it is deleted.
#
# ----------------------------------------------------------------------
proc UnlinkLinkedCommand {cmd args} {
if {[namespace which $cmd] ne {}} {
rename $cmd {}
}
}
# ----------------------------------------------------------------------
#
# DelegateName --
#
# Utility that gets the name of the class delegate for a class. It's
# trivial, but makes working with them much easier as delegate names are
# intentionally hard to create by accident.
#
# ----------------------------------------------------------------------
proc DelegateName {class} {
string cat [info object namespace $class] {:: oo ::delegate}
}
# ----------------------------------------------------------------------
#
# MixinClassDelegates --
#
# Support code called *after* [oo::define] inside the constructor of a
# class that patches in the appropriate class delegates.
#
# ----------------------------------------------------------------------
proc MixinClassDelegates {class} {
if {![info object isa class $class]} {
return
}
set delegate [DelegateName $class]
if {![info object isa class $delegate]} {
return
}
foreach c [info class superclass $class] {
set d [DelegateName $c]
if {![info object isa class $d]} {
continue
}
define $delegate ::oo::define::superclass -append $d
}
objdefine $class ::oo::objdefine::mixin -append $delegate
}
# ----------------------------------------------------------------------
#
# UpdateClassDelegatesAfterClone --
#
# Support code that is like [MixinClassDelegates] except for when a
# class is cloned.
#
# ----------------------------------------------------------------------
proc UpdateClassDelegatesAfterClone {originObject targetObject} {
# Rebuild the class inheritance delegation class
set originDelegate [DelegateName $originObject]
set targetDelegate [DelegateName $targetObject]
if {
[info object isa class $originDelegate]
&& ![info object isa class $targetDelegate]
} then {
copy $originDelegate $targetDelegate
objdefine $targetObject ::oo::objdefine::mixin -set \
{*}[lmap c [info object mixin $targetObject] {
if {$c eq $originDelegate} {set targetDelegate} {set c}
}]
}
}
# ----------------------------------------------------------------------
#
# oo::define::classmethod --
#
# Defines a class method. See define(n) for details.
#
# Note that the ::oo::define namespace is semi-public and a bit weird
# anyway, so we don't regard the namespace path as being under control:
# fully qualified names are used for everything.
#
# ----------------------------------------------------------------------
proc define::classmethod {name {args {}} {body {}}} {
# Create the method on the class if the caller gave arguments and body
::set argc [::llength [::info level 0]]
::if {$argc == 3} {
::return -code error -errorcode {TCL WRONGARGS} [::format \
{wrong # args: should be "%s name ?args body?"} \
[::lindex [::info level 0] 0]]
}
::set cls [::uplevel 1 self]
::if {$argc == 4} {
::oo::define [::oo::DelegateName $cls] method $name $args $body
}
# Make the connection by forwarding
::tailcall forward $name myclass $name
}
# ----------------------------------------------------------------------
#
# oo::define::initialise, oo::define::initialize --
#
# Do specific initialisation for a class. See define(n) for details.
#
# Note that the ::oo::define namespace is semi-public and a bit weird
# anyway, so we don't regard the namespace path as being under control:
# fully qualified names are used for everything.
#
# ----------------------------------------------------------------------
proc define::initialise {body} {
::set clsns [::info object namespace [::uplevel 1 self]]
::tailcall apply [::list {} $body $clsns]
}
# Make the [initialise] definition appear as [initialize] too
namespace eval define {
::namespace export initialise
::namespace eval tmp {::namespace import ::oo::define::initialise}
::namespace export -clear
::rename tmp::initialise initialize
::namespace delete tmp
}
# ----------------------------------------------------------------------
#
# Slot --
#
# The class of slot operations, which are basically lists at the low
# level of TclOO; this provides a more consistent interface to them.
#
# ----------------------------------------------------------------------
define Slot {
# ------------------------------------------------------------------
#
# Slot Get --
#
# Basic slot getter. Retrieves the contents of the slot.
# Particular slots must provide concrete non-erroring
# implementation.
#
# ------------------------------------------------------------------
method Get {} {
return -code error -errorcode {TCLOO ABSTRACT_SLOT} "unimplemented"
}
# ------------------------------------------------------------------
#
# Slot Set --
#
# Basic slot setter. Sets the contents of the slot. Particular
# slots must provide concrete non-erroring implementation.
#
# ------------------------------------------------------------------
method Set list {
return -code error -errorcode {TCLOO ABSTRACT_SLOT} "unimplemented"
}
# ------------------------------------------------------------------
#
# Slot Resolve --
#
# Helper that lets a slot convert a list of arguments of a
# particular type to their canonical forms. Defaults to doing
# nothing (suitable for simple strings).
#
# ------------------------------------------------------------------
method Resolve list {
return $list
}
# ------------------------------------------------------------------
#
# Slot -set, -append, -clear, --default-operation --
#
# Standard public slot operations. If a slot can't figure out
# what method to call directly, it uses --default-operation.
#
# ------------------------------------------------------------------
method -set args {
set my [namespace which my]
set args [lmap a $args {uplevel 1 [list $my Resolve $a]}]
tailcall my Set $args
}
method -append args {
set my [namespace which my]
set args [lmap a $args {uplevel 1 [list $my Resolve $a]}]
set current [uplevel 1 [list $my Get]]
tailcall my Set [list {*}$current {*}$args]
}
method -clear {} {tailcall my Set {}}
method -prepend args {
set my [namespace which my]
set args [lmap a $args {uplevel 1 [list $my Resolve $a]}]
set current [uplevel 1 [list $my Get]]
tailcall my Set [list {*}$args {*}$current]
}
method -remove args {
set my [namespace which my]
set args [lmap a $args {uplevel 1 [list $my Resolve $a]}]
set current [uplevel 1 [list $my Get]]
tailcall my Set [lmap val $current {
if {$val in $args} continue else {set val}
}]
}
# Default handling
forward --default-operation my -append
method unknown {args} {
set def --default-operation
if {[llength $args] == 0} {
tailcall my $def
} elseif {![string match -* [lindex $args 0]]} {
tailcall my $def {*}$args
}
next {*}$args
}
# Set up what is exported and what isn't
export -set -append -clear -prepend -remove
unexport unknown destroy
}
# Set the default operation differently for these slots
objdefine define::superclass forward --default-operation my -set
objdefine define::mixin forward --default-operation my -set
objdefine objdefine::mixin forward --default-operation my -set
# ----------------------------------------------------------------------
#
# oo::object <cloned> --
#
# Handler for cloning objects that clones basic bits (only!) of the
# object's namespace. Non-procedures, traces, sub-namespaces, etc. need
# more complex (and class-specific) handling.
#
# ----------------------------------------------------------------------
define object method <cloned> {originObject} {
# Copy over the procedures from the original namespace
foreach p [info procs [info object namespace $originObject]::*] {
set args [info args $p]
set idx -1
foreach a $args {
if {[info default $p $a d]} {
lset args [incr idx] [list $a $d]
} else {
lset args [incr idx] [list $a]
}
}
set b [info body $p]
set p [namespace tail $p]
proc $p $args $b
}
# Copy over the variables from the original namespace
foreach v [info vars [info object namespace $originObject]::*] {
upvar 0 $v vOrigin
namespace upvar [namespace current] [namespace tail $v] vNew
if {[info exists vOrigin]} {
if {[array exists vOrigin]} {
array set vNew [array get vOrigin]
} else {
set vNew $vOrigin
}
}
}
# General commands, sub-namespaces and advancd variable config (traces,
# etc) are *not* copied over. Classes that want that should do it
# themselves.
}
# ----------------------------------------------------------------------
#
# oo::class <cloned> --
#
# Handler for cloning classes, which fixes up the delegates.
#
# ----------------------------------------------------------------------
define class method <cloned> {originObject} {
next $originObject
# Rebuild the class inheritance delegation class
::oo::UpdateClassDelegatesAfterClone $originObject [self]
}
# ----------------------------------------------------------------------
#
# oo::singleton --
#
# A metaclass that is used to make classes that only permit one instance
# of them to exist. See singleton(n).
#
# ----------------------------------------------------------------------
class create singleton {
superclass class
variable object
unexport create createWithNamespace
method new args {
if {![info exists object] || ![info object isa object $object]} {
set object [next {*}$args]
::oo::objdefine $object {
method destroy {} {
::return -code error -errorcode {TCLOO SINGLETON} \
"may not destroy a singleton object"
}
method <cloned> {originObject} {
::return -code error -errorcode {TCLOO SINGLETON} \
"may not clone a singleton object"
}
}
}
return $object
}
}
# ----------------------------------------------------------------------
#
# oo::abstract --
#
# A metaclass that is used to make classes that can't be directly
# instantiated. See abstract(n).
#
# ----------------------------------------------------------------------
class create abstract {
superclass class
unexport create createWithNamespace new
}
# ----------------------------------------------------------------------
#
# oo::configuresupport --
#
# Namespace that holds all the implementation details of TIP #558.
# Also includes the commands:
#
# * readableproperties
# * writableproperties
# * objreadableproperties
# * objwritableproperties
#
# Those are all slot implementations that provide access to the C layer
# of property support (i.e., very fast cached lookup of property names).
#
# ----------------------------------------------------------------------
::namespace eval configuresupport {
namespace path ::tcl
# ------------------------------------------------------------------
#
# oo::configuresupport --
#
# A metaclass that is used to make classes that can be configured.
#
# ------------------------------------------------------------------
proc PropertyImpl {readslot writeslot args} {
for {set i 0} {$i < [llength $args]} {incr i} {
# Parse the property name
set prop [lindex $args $i]
if {[string match "-*" $prop]} {
return -code error -level 2 \
-errorcode {TCLOO PROPERTY_FORMAT} \
"bad property name \"$prop\": must not begin with -"
}
if {$prop ne [list $prop]} {
return -code error -level 2 \
-errorcode {TCLOO PROPERTY_FORMAT} \
"bad property name \"$prop\": must be a simple word"
}
if {[string first "::" $prop] != -1} {
return -code error -level 2 \
-errorcode {TCLOO PROPERTY_FORMAT} \
"bad property name \"$prop\": must not contain namespace separators"
}
if {[string match {*[()]*} $prop]} {
return -code error -level 2 \
-errorcode {TCLOO PROPERTY_FORMAT} \
"bad property name \"$prop\": must not contain parentheses"
}
set realprop [string cat "-" $prop]
set getter [format {::set [my varname %s]} $prop]
set setter [format {::set [my varname %s] $value} $prop]
set kind readwrite
# Parse the extra options
while {[set next [lindex $args [expr {$i + 1}]]
string match "-*" $next]} {
set arg [lindex $args [incr i 2]]
switch [prefix match -error [list -level 2 -errorcode \
[list TCL LOOKUP INDEX option $next]] {-get -kind -set} $next] {
-get {
if {$i >= [llength $args]} {
return -code error -level 2 \
-errorcode {TCL WRONGARGS} \
"missing body to go with -get option"
}
set getter $arg
}
-set {
if {$i >= [llength $args]} {
return -code error -level 2 \
-errorcode {TCL WRONGARGS} \
"missing body to go with -set option"
}
set setter $arg
}
-kind {
if {$i >= [llength $args]} {
return -code error -level 2\
-errorcode {TCL WRONGARGS} \
"missing kind value to go with -kind option"
}
set kind [prefix match -message "kind" -error [list \
-level 2 \
-errorcode [list TCL LOOKUP INDEX kind $arg]] {
readable readwrite writable
} $arg]
}
}
}
# Install the option
set reader <ReadProp$realprop>
set writer <WriteProp$realprop>
switch $kind {
readable {
uplevel 2 [list $readslot -append $realprop]
uplevel 2 [list $writeslot -remove $realprop]
uplevel 2 [list method $reader -unexport {} $getter]
}
writable {
uplevel 2 [list $readslot -remove $realprop]
uplevel 2 [list $writeslot -append $realprop]
uplevel 2 [list method $writer -unexport {value} $setter]
}
readwrite {
uplevel 2 [list $readslot -append $realprop]
uplevel 2 [list $writeslot -append $realprop]
uplevel 2 [list method $reader -unexport {} $getter]
uplevel 2 [list method $writer -unexport {value} $setter]
}
}
}
}
# ------------------------------------------------------------------
#
# oo::configuresupport::configurableclass,
# oo::configuresupport::configurableobject --
#
# Namespaces used as implementation vectors for oo::define and
# oo::objdefine when the class/instance is configurable.
#
# ------------------------------------------------------------------
namespace eval configurableclass {
::proc property args {
::oo::configuresupport::PropertyImpl \
::oo::configuresupport::readableproperties \
::oo::configuresupport::writableproperties {*}$args
}
# Plural alias just in case; deliberately NOT documented!
::proc properties args {::tailcall property {*}$args}
::namespace path ::oo::define
::namespace export property
}
namespace eval configurableobject {
::proc property args {
::oo::configuresupport::PropertyImpl \
::oo::configuresupport::objreadableproperties \
::oo::configuresupport::objwritableproperties {*}$args
}
# Plural alias just in case; deliberately NOT documented!
::proc properties args {::tailcall property {*}$args}
::namespace path ::oo::objdefine
::namespace export property
}
# ------------------------------------------------------------------
#
# oo::configuresupport::ReadAll --
#
# The implementation of [$o configure] with no extra arguments.
#
# ------------------------------------------------------------------
proc ReadAll {object my} {
set result {}
foreach prop [info object properties $object -all -readable] {
try {
dict set result $prop [$my <ReadProp$prop>]
} on error {msg opt} {
dict set opt -level 2
return -options $opt $msg
} on return {msg opt} {
dict incr opt -level 2
return -options $opt $msg
} on break {} {
return -code error -level 2 -errorcode {TCLOO SHENANIGANS} \
"property getter for $prop did a break"
} on continue {} {
return -code error -level 2 -errorcode {TCLOO SHENANIGANS} \
"property getter for $prop did a continue"
}
}
return $result
}
# ------------------------------------------------------------------
#
# oo::configuresupport::ReadOne --
#
# The implementation of [$o configure -prop] with that single
# extra argument.
#
# ------------------------------------------------------------------
proc ReadOne {object my propertyName} {
set props [info object properties $object -all -readable]
try {
set prop [prefix match -message "property" $props $propertyName]
} on error {msg} {
catch {
set wps [info object properties $object -all -writable]
set wprop [prefix match $wps $propertyName]
set msg "property \"$wprop\" is write only"
}
return -code error -level 2 -errorcode [list \
TCL LOOKUP INDEX property $propertyName] $msg
}
try {
set value [$my <ReadProp$prop>]
} on error {msg opt} {
dict set opt -level 2
return -options $opt $msg
} on return {msg opt} {
dict incr opt -level 2
return -options $opt $msg
} on break {} {
return -code error -level 2 -errorcode {TCLOO SHENANIGANS} \
"property getter for $prop did a break"
} on continue {} {
return -code error -level 2 -errorcode {TCLOO SHENANIGANS} \
"property getter for $prop did a continue"
}
return $value
}
# ------------------------------------------------------------------
#
# oo::configuresupport::WriteMany --
#
# The implementation of [$o configure -prop val ?-prop val...?].
#
# ------------------------------------------------------------------
proc WriteMany {object my setterMap} {
set props [info object properties $object -all -writable]
foreach {prop value} $setterMap {
try {
set prop [prefix match -message "property" $props $prop]
} on error {msg} {
catch {
set rps [info object properties $object -all -readable]
set rprop [prefix match $rps $prop]
set msg "property \"$rprop\" is read only"
}
return -code error -level 2 -errorcode [list \
TCL LOOKUP INDEX property $prop] $msg
}
try {
$my <WriteProp$prop> $value
} on error {msg opt} {
dict set opt -level 2
return -options $opt $msg
} on return {msg opt} {
dict incr opt -level 2
return -options $opt $msg
} on break {} {
return -code error -level 2 -errorcode {TCLOO SHENANIGANS} \
"property setter for $prop did a break"
} on continue {} {
return -code error -level 2 -errorcode {TCLOO SHENANIGANS} \
"property setter for $prop did a continue"
}
}
return
}
# ------------------------------------------------------------------
#
# oo::configuresupport::configurable --
#
# The class that contains the implementation of the actual
# 'configure' method (mixed into actually configurable classes).
# Great care needs to be taken in these methods as they are
# potentially used in classes where the current namespace is set
# up very strangely.
#
# ------------------------------------------------------------------
::oo::class create configurable {
private variable my
#
# configure --
# Method for providing client access to the property mechanism.
# Has a user-facing API similar to that of [chan configure].
#
method configure args {
::if {![::info exists my]} {
::set my [::namespace which my]
}
::if {[::llength $args] == 0} {
# Read all properties
::oo::configuresupport::ReadAll [self] $my
} elseif {[::llength $args] == 1} {
# Read a single property
::oo::configuresupport::ReadOne [self] $my \
[::lindex $args 0]
} elseif {[::llength $args] % 2 == 0} {
# Set properties, one or several
::oo::configuresupport::WriteMany [self] $my $args
} else {
# Invalid call
::return -code error -errorcode {TCL WRONGARGS} \
[::format {wrong # args: should be "%s"} \
"[self] configure ?-option value ...?"]
}
}
definitionnamespace -instance configurableobject
definitionnamespace -class configurableclass
}
}
# ----------------------------------------------------------------------
#
# oo::configurable --
#
# A metaclass that is used to make classes that can be configured in
# their creation phase (and later too). All the metaclass itself does is
# arrange for the class created to have a 'configure' method and for
# oo::define and oo::objdefine (on the class and its instances) to have
# a property definition for setting things up for 'configure'.
#
# ----------------------------------------------------------------------
class create configurable {
superclass class
constructor {{definitionScript ""}} {
next {mixin ::oo::configuresupport::configurable}
next $definitionScript
}
definitionnamespace -class configuresupport::configurableclass
}
}
# Local Variables:
# mode: tcl
# c-basic-offset: 4
# fill-column: 78
# End:
|