/trunk/Examples/ruby/free_function/example.i

# · Swig · 41 lines · 21 code · 10 blank · 10 comment · 0 complexity · ea0d7f322373c8b3465a7828324f24a7 MD5 · raw file

  1. %module example
  2. %{
  3. #include "example.h"
  4. %}
  5. /* Specify that ownership is transferred to the zoo
  6. when calling add_animal */
  7. %apply SWIGTYPE *DISOWN { Animal* animal };
  8. /* Track objects */
  9. %trackobjects;
  10. /* Specify the mark function */
  11. %freefunc Zoo "free_Zoo";
  12. %include "example.h"
  13. %header %{
  14. static void free_Zoo(void* ptr) {
  15. Zoo* zoo = (Zoo*) ptr;
  16. /* Loop over each object and call SWIG_RubyRemoveTracking */
  17. int count = zoo->get_num_animals();
  18. for(int i = 0; i < count; ++i) {
  19. /* Get an animal */
  20. Animal* animal = zoo->get_animal(i);
  21. /* Unlink the Ruby object from the C++ object */
  22. SWIG_RubyUnlinkObjects(animal);
  23. /* Now remove the tracking for this animal */
  24. SWIG_RubyRemoveTracking(animal);
  25. }
  26. /* Now call SWIG_RubyRemoveTracking for the zoo */
  27. SWIG_RubyRemoveTracking(ptr);
  28. /* Now free the zoo which will free the animals it contains */
  29. delete zoo;
  30. }
  31. %}