Version 4 of persistent itcl

Updated 2002-08-18 12:36:43

The page Persistent incr-Tcl objects got me thinking about persistence.

The code presented there requires too much user intervention, I want something which handles more of the details automatically.

Here's a function which constructs a script which, when evaluated in global scope, will do what I think I want. (Note, for testing purposes, this code converts all the values to uppercase - remove the string toupper to fix it.)

  # serialize extracts all variables and their current values into a script
  # such that `eval [serialize obj]' will set obj to those values
  proc serialize {obj} {
      set result ""
      foreach var [$obj info variable] {
          foreach {type name} [$obj info variable $var -type -name] break
          if {([namespace tail $name] != "this") && ($type != "common")} {
              set name "@itcl $obj $name"
              if {[catch {
                  append result "set \"${name}\" [string toupper [set $name]]\n"
              } err]} {
                  puts stderr "err: $err"
              }
          }
      }
      return $result
  }

Here's a small test:

  class test_persist {
      common com com_val
      private variable priv priv_val
      public variable pub pub_val
      protected variable prot prot_val

      constructor {} {
          set priv priv_v
          set pub pub_v
          set prot prot_v
      }
  }

  class test_p1 {
      inherit test_persist
      public variable sub_pub sub_pub
      private variable sub_priv sub_priv

      method test {} {
          foreach var [$this info variable] {
              foreach {type name val} [$this info variable $var -type -name -value] break
              puts "$name = $val"
          }
      }

      constructor {} {
          # needs to be here
      }
  }

  test_p1 t
  puts "Vars: [t info variable]"
  set dump [serialize t]
  puts "DUMP: $dump"
  eval $dump
  puts [serialize t]
  t test

CMcC


Persistence and Distribution

This approach raises some interesting possibilities for persistence via metakit:

* Each class instance can be represented by a metakit row * Tequila could be adapted to attach instance variables (or whole objects) in addition to simple arrays