runme2.tcl 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # file: runme2.tcl
  2. # This file illustrates the low-level C++ interface
  3. # created by SWIG. In this case, all of our C++ classes
  4. # get converted into function calls.
  5. catch { load ./example[info sharedlibextension] example}
  6. # ----- Object creation -----
  7. puts "Creating some objects:"
  8. set c [new_Circle 10]
  9. puts " Created circle $c"
  10. set s [new_Square 10]
  11. puts " Created square $s"
  12. # ----- Access a static member -----
  13. puts "\nA total of $Shape_nshapes shapes were created"
  14. # ----- Member data access -----
  15. # Set the location of the object
  16. # Note: the base class must be used since that's where x and y
  17. # were declared.
  18. Shape_x_set $c 20
  19. Shape_y_set $c 30
  20. Shape_x_set $s -10
  21. Shape_y_set $s 5
  22. puts "\nHere is their current position:"
  23. puts " Circle = ([Shape_x_get $c], [Shape_y_get $c])"
  24. puts " Square = ([Shape_x_get $s], [Shape_y_get $s])"
  25. # ----- Call some methods -----
  26. puts "\nHere are some properties of the shapes:"
  27. foreach o "$c $s" {
  28. puts " $o"
  29. puts " area = [Shape_area $o]"
  30. puts " perimeter = [Shape_perimeter $o]"
  31. }
  32. # Notice how the Shape_area() and Shape_perimeter() functions really
  33. # invoke the appropriate virtual method on each object.
  34. # ----- Try to cause a type error -----
  35. puts "\nI'm going to try and break the type system"
  36. if { [catch {
  37. # Bad script!
  38. Square_area $c # Try to invoke Square method on a Circle
  39. puts " Bad bad SWIG!"
  40. }]} {
  41. puts " Well, it didn't work. Good SWIG."
  42. }
  43. # ----- Delete everything -----
  44. puts "\nGuess I'll clean up now"
  45. # Note: this invokes the virtual destructor
  46. delete_Shape $c
  47. delete_Shape $s
  48. puts "$Shape_nshapes shapes remain"
  49. puts "Goodbye"