diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2009-08-18 20:56:02 +0000 |
---|---|---|
committer | Lorry <lorry@roadtrain.codethink.co.uk> | 2012-09-25 16:59:08 +0000 |
commit | 9f8a09ed743cedd9547bf0661d518647966ab114 (patch) | |
tree | 9c7803d3b27a8ec22e91792ac7f7932efa128b20 /Examples/ruby/free_function | |
download | swig-tarball-master.tar.gz |
Imported from /srv/lorry/lorry-area/swig-tarball/swig-1.3.40.tar.gz.HEADswig-1.3.40master
Diffstat (limited to 'Examples/ruby/free_function')
-rw-r--r-- | Examples/ruby/free_function/Makefile | 19 | ||||
-rw-r--r-- | Examples/ruby/free_function/example.cxx | 67 | ||||
-rw-r--r-- | Examples/ruby/free_function/example.dsp | 154 | ||||
-rw-r--r-- | Examples/ruby/free_function/example.h | 49 | ||||
-rw-r--r-- | Examples/ruby/free_function/example.i | 41 | ||||
-rw-r--r-- | Examples/ruby/free_function/runme.rb | 46 |
6 files changed, 376 insertions, 0 deletions
diff --git a/Examples/ruby/free_function/Makefile b/Examples/ruby/free_function/Makefile new file mode 100644 index 0000000..56c84c6 --- /dev/null +++ b/Examples/ruby/free_function/Makefile @@ -0,0 +1,19 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +LIBS = -lm + +all:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' ruby_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_cpp_static + +clean:: + $(MAKE) -f $(TOP)/Makefile ruby_clean + +check: all diff --git a/Examples/ruby/free_function/example.cxx b/Examples/ruby/free_function/example.cxx new file mode 100644 index 0000000..402a947 --- /dev/null +++ b/Examples/ruby/free_function/example.cxx @@ -0,0 +1,67 @@ +#include "example.h" + +Animal::Animal(const char* name) : name_(name) +{ +} + +Animal::~Animal() +{ +} + +/* Return the animal's name */ +const char* Animal::get_name() const +{ + return name_.c_str(); +} + +Zoo::Zoo() +{ +} + +Zoo::~Zoo() +{ + IterType iter = this->animals.begin(); + IterType end = this->animals.end(); + + for(; iter != end; ++iter) + { + Animal* animal = *iter; + delete animal; + } +} + +/* Create a new animal. */ +Animal* Zoo::create_animal(const char* name) +{ + return new Animal(name); +} + +/* Add a new animal to the zoo. */ +void Zoo::add_animal(Animal* animal) +{ + animals.push_back(animal); +} + +Animal* Zoo::remove_animal(size_t i) +{ + /* Note a production implementation should check + for out of range errors. */ + Animal* result = this->animals[i]; + IterType iter = this->animals.begin(); + std::advance(iter, i); + this->animals.erase(iter); + + return result; +} + +/* Return the number of animals in the zoo. */ +size_t Zoo::get_num_animals() const +{ + return animals.size(); +} + +/* Return a pointer to the ith animal */ +Animal* Zoo::get_animal(size_t i) const +{ + return animals[i]; +} diff --git a/Examples/ruby/free_function/example.dsp b/Examples/ruby/free_function/example.dsp new file mode 100644 index 0000000..2adab78 --- /dev/null +++ b/Examples/ruby/free_function/example.dsp @@ -0,0 +1,154 @@ +# Microsoft Developer Studio Project File - Name="example" - Package Owner=<4>
+# Microsoft Developer Studio Generated Build File, Format Version 6.00
+# ** DO NOT EDIT **
+
+# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
+
+CFG=example - Win32 Release
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,
+!MESSAGE use the Export Makefile command and run
+!MESSAGE
+!MESSAGE NMAKE /f "example.mak".
+!MESSAGE
+!MESSAGE You can specify a configuration when running NMAKE
+!MESSAGE by defining the macro CFG on the command line. For example:
+!MESSAGE
+!MESSAGE NMAKE /f "example.mak" CFG="example - Win32 Release"
+!MESSAGE
+!MESSAGE Possible choices for configuration are:
+!MESSAGE
+!MESSAGE "example - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
+!MESSAGE "example - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
+!MESSAGE
+
+# Begin Project
+# PROP AllowPerConfigDependencies 0
+# PROP Scc_ProjName ""
+# PROP Scc_LocalPath ""
+CPP=cl.exe
+MTL=midl.exe
+RSC=rc.exe
+
+!IF "$(CFG)" == "example - Win32 Debug"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 1
+# PROP BASE Output_Dir "Debug"
+# PROP BASE Intermediate_Dir "Debug"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 1
+# PROP Output_Dir "Debug"
+# PROP Intermediate_Dir "Debug"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /GZ /c
+# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /I "$(RUBY_INCLUDE)" /D "_DEBUG" /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /D NT=1 /D "IMPORT" /YX /FD /GZ /c
+# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
+# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
+# ADD BASE RSC /l 0x809 /d "_DEBUG"
+# ADD RSC /l 0x809 /d "_DEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib "$(RUBY_LIB)" /nologo /dll /debug /machine:I386 /out:"example.dll" /pdbtype:sept
+# SUBTRACT LINK32 /pdb:none
+
+!ELSEIF "$(CFG)" == "example - Win32 Release"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "Release"
+# PROP BASE Intermediate_Dir "Release"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "Release"
+# PROP Intermediate_Dir "Release"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /c
+# ADD CPP /nologo /MT /W3 /GX /O2 /I "$(RUBY_INCLUDE)" /D "NDEBUG" /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /D NT=1 /D "IMPORT" /YX /FD /c
+# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
+# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
+# ADD BASE RSC /l 0x809 /d "NDEBUG"
+# ADD RSC /l 0x809 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib /nologo /dll /machine:I386
+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib "$(RUBY_LIB)" /nologo /dll /machine:I386 /out:"example.dll"
+# SUBTRACT LINK32 /pdb:none
+
+!ENDIF
+
+# Begin Target
+
+# Name "example - Win32 Debug"
+# Name "example - Win32 Release"
+# Begin Group "Source Files"
+
+# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
+# Begin Source File
+
+SOURCE=.\example.cxx
+# End Source File
+# Begin Source File
+
+SOURCE=.\example_wrap.cxx
+# End Source File
+# End Group
+# Begin Group "Header Files"
+
+# PROP Default_Filter "h;hpp;hxx;hm;inl"
+# Begin Source File
+
+SOURCE=.\example.h
+# End Source File
+# End Group
+# Begin Group "Resource Files"
+
+# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
+# End Group
+# Begin Source File
+
+SOURCE=.\example.i
+
+!IF "$(CFG)" == "example - Win32 Debug"
+
+# Begin Custom Build
+InputPath=.\example.i
+InputName=example
+
+"$(InputName)_wrap.cxx" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
+ echo In order to function correctly, please ensure the following environment variables are correctly set:
+ echo RUBY_INCLUDE: %RUBY_INCLUDE%
+ echo RUBY_LIB: %RUBY_LIB%
+ echo on
+ ..\..\..\swig.exe -c++ -ruby $(InputPath)
+
+# End Custom Build
+
+!ELSEIF "$(CFG)" == "example - Win32 Release"
+
+# Begin Custom Build
+InputPath=.\example.i
+InputName=example
+
+"$(InputName)_wrap.cxx" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
+ echo In order to function correctly, please ensure the following environment variables are correctly set:
+ echo RUBY_INCLUDE: %RUBY_INCLUDE%
+ echo RUBY_LIB: %RUBY_LIB%
+ echo on
+ ..\..\..\swig.exe -c++ -ruby $(InputPath)
+
+# End Custom Build
+
+!ENDIF
+
+# End Source File
+# End Target
+# End Project
diff --git a/Examples/ruby/free_function/example.h b/Examples/ruby/free_function/example.h new file mode 100644 index 0000000..933bb36 --- /dev/null +++ b/Examples/ruby/free_function/example.h @@ -0,0 +1,49 @@ +#ifndef _EXAMPLE_H_ +#define _EXAMPLE_H_ + +#include <vector> +#include <string> + +class Animal +{ +protected: + std::string name_; +public: + // Construct an animal with a name + Animal(const char* name); + + // Destruct an animal + ~Animal(); + + // Return the animal's name + const char* get_name() const; +}; + +class Zoo +{ +private: + typedef std::vector<Animal*> AnimalsType; + typedef AnimalsType::iterator IterType; +protected: + AnimalsType animals; +public: + Zoo(); + ~Zoo(); + + /* Create a new animal */ + static Animal* create_animal(const char* name); + + /* Add a new animal to the zoo */ + void add_animal(Animal* animal); + + /* Remove an animal from the zoo */ + Animal* remove_animal(size_t i); + + /* Return the number of animals in the zoo */ + size_t get_num_animals() const; + + /* Return a pointer to the ith animal */ + Animal* get_animal(size_t i) const; +}; + +#endif /*_EXAMPLE_H_*/ diff --git a/Examples/ruby/free_function/example.i b/Examples/ruby/free_function/example.i new file mode 100644 index 0000000..a446b3f --- /dev/null +++ b/Examples/ruby/free_function/example.i @@ -0,0 +1,41 @@ +%module example + +%{ +#include "example.h" +%} + +/* Specify that ownership is transferred to the zoo + when calling add_animal */ +%apply SWIGTYPE *DISOWN { Animal* animal }; + +/* Track objects */ +%trackobjects; + +/* Specify the mark function */ +%freefunc Zoo "free_Zoo"; + +%include "example.h" + +%header %{ + static void free_Zoo(void* ptr) { + Zoo* zoo = (Zoo*) ptr; + + /* Loop over each object and call SWIG_RubyRemoveTracking */ + int count = zoo->get_num_animals(); + + for(int i = 0; i < count; ++i) { + /* Get an animal */ + Animal* animal = zoo->get_animal(i); + /* Unlink the Ruby object from the C++ object */ + SWIG_RubyUnlinkObjects(animal); + /* Now remove the tracking for this animal */ + SWIG_RubyRemoveTracking(animal); + } + + /* Now call SWIG_RubyRemoveTracking for the zoo */ + SWIG_RubyRemoveTracking(ptr); + + /* Now free the zoo which will free the animals it contains */ + delete zoo; + } +%} diff --git a/Examples/ruby/free_function/runme.rb b/Examples/ruby/free_function/runme.rb new file mode 100644 index 0000000..1d88b6f --- /dev/null +++ b/Examples/ruby/free_function/runme.rb @@ -0,0 +1,46 @@ +require 'example' + +begin + begin + # Create an animal and zoo + tiger1 = Example::Animal.new("tiger1") + zoo = Example::Zoo.new + + # At the animal to the zoo - this will transfer ownership + # of the underlying C++ object to the C++ zoo object + zoo.add_animal(tiger1) + + # get the id of the tiger + id1 = tiger1.object_id + + # Unset the tiger + tiger1 = nil + end + + # Force a gc + GC.start + + # Get the tiger and its id + tiger2 = zoo.get_animal(0) + id2 = tiger2.object_id + + # The ids should not be the same + if id1==id2 + raise RuntimeError, "Id's should not be the same" + end + + zoo = nil +end + +GC.start + +# This method is no longer valid since the zoo freed the underlying +# C++ object +ok = false +begin + puts tiger2.get_name +rescue ObjectPreviouslyDeleted => error + ok = true +end + +raise(RuntimeError, "Incorrect exception raised - should be ObjectPreviouslyDeleted") unless ok |