PageRenderTime 36ms CodeModel.GetById 22ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

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