/tags/rel-1-3-26/SWIG/Examples/test-suite/ruby/track_objects.i

# · Swig · 95 lines · 66 code · 19 blank · 10 comment · 0 complexity · d56a77fbe33dd01b743b8dcabc809061 MD5 · raw file

  1. %module track_objects
  2. %include typemaps.i
  3. %trackobjects Foo;
  4. %newobject Bar::get_new_foo;
  5. %typemap(in, numinputs=0) Foo** foo (Foo *temp) {
  6. /* %typemap(in, numinputs=0) Foo** foo */
  7. $1 = &temp;
  8. }
  9. %typemap(argout) Foo** foo {
  10. /* %typemap(argout) Foo** foo */
  11. $result = SWIG_NewPointerObj((void *) *$1, $*1_descriptor, 0);
  12. }
  13. %apply SWIGTYPE *DISOWN {Foo* ownedFoo};
  14. %inline %{
  15. class Foo
  16. {
  17. public:
  18. Foo() {}
  19. ~Foo() {}
  20. /* Helper method that can be called from Ruby that checks
  21. that two Ruby objects are pointing to the same underlying
  22. C++ object */
  23. bool cpp_equal(const Foo* other)
  24. {
  25. return (this == other);
  26. }
  27. /* Just a simple method to call on Foo*/
  28. char* say_hello()
  29. {
  30. return "Hello";
  31. }
  32. };
  33. class Bar
  34. {
  35. private:
  36. Foo* owned_;
  37. Foo* unowned_;
  38. public:
  39. Bar(): owned_(new Foo), unowned_(0)
  40. {
  41. }
  42. ~Bar()
  43. {
  44. delete owned_;
  45. }
  46. /* Test that track objects works with %newobject */
  47. static Foo* get_new_foo()
  48. {
  49. return new Foo;
  50. }
  51. /* Test the same foo Ruby object is created each time */
  52. Foo* get_owned_foo()
  53. {
  54. return owned_;
  55. }
  56. /* Test that track objects works with argout parameters.*/
  57. void get_owned_foo_by_argument(Foo** foo)
  58. {
  59. *foo = owned_;
  60. }
  61. /* Test that track objects works with the DISOWN typemap.*/
  62. void set_owned_foo(Foo* ownedFoo)
  63. {
  64. delete owned_;
  65. owned_ = ownedFoo;
  66. }
  67. Foo* get_unowned_foo()
  68. {
  69. return unowned_;
  70. }
  71. void set_unowned_foo(Foo* foo)
  72. {
  73. unowned_ = foo;
  74. }
  75. };
  76. %}