import source from 1.3.40
[external/swig.git] / Examples / ruby / free_function / runme.rb
1 require 'example'
2
3 begin
4   begin
5     # Create an animal and zoo
6     tiger1 = Example::Animal.new("tiger1")
7     zoo = Example::Zoo.new
8   
9     # At the animal to the zoo - this will transfer ownership
10     # of the underlying C++ object to the C++ zoo object
11     zoo.add_animal(tiger1)
12
13     # get the id of the tiger
14     id1 = tiger1.object_id
15
16     # Unset the tiger
17     tiger1 = nil
18   end
19
20   # Force a gc
21   GC.start
22
23   # Get the tiger and its id
24   tiger2 = zoo.get_animal(0)
25   id2 = tiger2.object_id
26
27   # The ids should not be the same
28   if id1==id2
29     raise RuntimeError, "Id's should not be the same"
30   end
31
32   zoo = nil
33 end
34
35 GC.start
36
37 # This method is no longer valid since the zoo freed the underlying
38 # C++ object
39 ok = false
40 begin
41   puts tiger2.get_name
42 rescue ObjectPreviouslyDeleted => error
43   ok = true
44 end
45
46 raise(RuntimeError, "Incorrect exception raised - should be ObjectPreviouslyDeleted") unless ok