home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / TCL / ITCL / _ITCL.TAR / usr / lib / itcl / demos / counters / counter.tcl
Encoding:
Text File  |  1994-03-21  |  2.9 KB  |  91 lines

  1. # ----------------------------------------------------------------------
  2. #  PURPOSE:  Handling counter operations via [incr Tcl].
  3. #
  4. #   AUTHOR:  Michael J. McLennan       Phone: (610)712-2842
  5. #            AT&T Bell Laboratories   E-mail: michael.mclennan@att.com
  6. #
  7. #      RCS:  counter.tcl,v 1.1.1.1 1994/03/21 22:09:45 mmc Exp
  8. # ----------------------------------------------------------------------
  9. #               Copyright (c) 1993  AT&T Bell Laboratories
  10. # ======================================================================
  11. # Permission to use, copy, modify, and distribute this software and its
  12. # documentation for any purpose and without fee is hereby granted,
  13. # provided that the above copyright notice appear in all copies and that
  14. # both that the copyright notice and warranty disclaimer appear in
  15. # supporting documentation, and that the names of AT&T Bell Laboratories
  16. # any of their entities not be used in advertising or publicity
  17. # pertaining to distribution of the software without specific, written
  18. # prior permission.
  19. #
  20. # AT&T disclaims all warranties with regard to this software, including
  21. # all implied warranties of merchantability and fitness.  In no event
  22. # shall AT&T be liable for any special, indirect or consequential
  23. # damages or any damages whatsoever resulting from loss of use, data or
  24. # profits, whether in an action of contract, negligence or other
  25. # tortuous action, arising out of or in connection with the use or
  26. # performance of this software.
  27. # ======================================================================
  28.  
  29. # ----------------------------------------------------------------------
  30. #  Simple "counter" class
  31. # ----------------------------------------------------------------------
  32. itcl_class counter {
  33.     constructor {config} {}
  34.     method config {config} {}
  35.  
  36.     method value {} {
  37.         return $count
  38.     }
  39.     method ++ {{val unspec}} {
  40.         if {$val == "unspec"} {set val $by}
  41.         return [+= $val]
  42.     }
  43.     method -- {{val unspec}} {
  44.         if {$val == "unspec"} {set val $by}
  45.         return [-= $val]
  46.     }
  47.     method += {val} {
  48.         return [incr count $val]
  49.     }
  50.     method -= {val} {
  51.         return [incr count [expr -1*$val]]
  52.     }
  53.     public by 1
  54.     protected count 0
  55. }
  56.  
  57. # ----------------------------------------------------------------------
  58. #  Add multiplying behavior to "counters"
  59. # ----------------------------------------------------------------------
  60. itcl_class multiplier {
  61.     inherit counter
  62.  
  63.     constructor {config} {}
  64.     method config {config} {}
  65.  
  66.     method ** {{val unspec}} {
  67.         if {$val == "unspec"} {set val $by}
  68.         return [*= $val]
  69.     }
  70.     method *= {val} {
  71.         set count [expr $count*$val]
  72.         return $count
  73.     }
  74. }
  75.  
  76. # ----------------------------------------------------------------------
  77. #  Example usages
  78. # ----------------------------------------------------------------------
  79.  
  80. counter c -by 2
  81. c ++
  82. c += 2
  83. c config -by 1
  84. c ++
  85.  
  86. multiplier m -by 2
  87. m ++
  88. m **
  89. m *= 3
  90. m --
  91.