PageRenderTime 94ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/lua/owner/runme.lua

#
Lua | 104 lines | 93 code | 6 blank | 5 comment | 7 complexity | a669d48d34cfcf543f7ec90ee4755bc2 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. -- Operator overloading example
  2. ---- importing ----
  3. if string.sub(_VERSION,1,7)=='Lua 5.0' then
  4. -- lua5.0 doesnt have a nice way to do this
  5. lib=loadlib('example.dll','luaopen_example') or loadlib('example.so','luaopen_example')
  6. assert(lib)()
  7. else
  8. -- lua 5.1 does
  9. require('example')
  10. end
  11. print "ok, lets test Lua's ownership of C++ objects"
  12. print("Currently there are",example.Shape_nshapes,"shapes (there should be 0)")
  13. print "\nLets make a couple"
  14. a=example.Square(10)
  15. b=example.Circle(1)
  16. print("Currently there are",example.Shape_nshapes,"shapes (there should be 2)")
  17. print "\nNote lets use the createX functions"
  18. c=example.createCircle(5)
  19. d=example.createSquare(3)
  20. print("Currently there are",example.Shape_nshapes,"shapes (there should be 4)")
  21. print "\nWe will run the garbage collector & see if they are till here"
  22. collectgarbage()
  23. print("Currently there are",example.Shape_nshapes,"shapes (there should be 4)")
  24. print "\nLets get rid of them all, collect garbage & see if they are till here"
  25. a,b,c,d=nil,nil,nil,nil
  26. collectgarbage()
  27. print("Currently there are",example.Shape_nshapes,"shapes (there should be 0)")
  28. print "\nLets start putting stuff into the ShapeOwner"
  29. print "The ShapeOwner now owns the shapes, but Lua still has pointers to them"
  30. o=example.ShapeOwner()
  31. a=example.Square(10)
  32. b=example.Circle(1)
  33. o:add(a)
  34. o:add(b)
  35. o:add(example.createSquare(5))
  36. print("Currently there are",example.Shape_nshapes,"shapes (there should be 3)")
  37. print "\nWe will nil our references,run the garbage collector & see if they are till here"
  38. print "they should be, as the ShapeOwner owns them"
  39. a,b=nil,nil
  40. collectgarbage()
  41. print("Currently there are",example.Shape_nshapes,"shapes (there should be 3)")
  42. print "\nWe will access them and check that they are still valid"
  43. a=o:get(0)
  44. b=o:get(1)
  45. print(" Area's are",a:area(),b:area(),o:get(2):area())
  46. collectgarbage()
  47. print("Currently there are",example.Shape_nshapes,"shapes (there should be 3)")
  48. print "\nWe will remove one from the C++ owner & pass its ownership to Lua,"
  49. print " then check that they are still unchanged"
  50. a,b=nil,nil
  51. a=o:remove(0) -- a now owns it
  52. collectgarbage()
  53. print("Currently there are",example.Shape_nshapes,"shapes (there should be 3)")
  54. print "\nDelete the ShapeOwner (this should destroy two shapes),"
  55. print " but we have one left in Lua"
  56. o=nil
  57. collectgarbage()
  58. print("Currently there are",example.Shape_nshapes,"shapes (there should be 1)")
  59. print "\nFinal tidy up "
  60. a=nil
  61. collectgarbage()
  62. print("Currently there are",example.Shape_nshapes,"shapes (there should be 0)")
  63. print "Final test, we will create some Shapes & pass them around like mad"
  64. print "If there is any memory leak, you will see it in the memory usage"
  65. io.flush()
  66. sh={}
  67. -- make some objects
  68. for i=0,10 do
  69. a=example.Circle(i)
  70. b=example.Square(i)
  71. sh[a]=true
  72. sh[b]=true
  73. end
  74. o=example.ShapeOwner()
  75. for i=0,10000 do
  76. for k,_ in pairs(sh) do
  77. o:add(k)
  78. end
  79. sh={} -- clear it
  80. while true do
  81. a=o:remove(0)
  82. if a==nil then break end
  83. sh[a]=true
  84. end
  85. if i%100==0 then collectgarbage() end
  86. end
  87. print "done"
  88. o,sh=nil,nil
  89. collectgarbage()
  90. print("Currently there are",example.Shape_nshapes,"shapes (there should be 0)")
  91. print "thats all folks!"