New example to test free_functions. Makes use of tracking objects functionality.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@7522 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Charlie Savage 2005-09-24 22:12:53 +00:00
commit 6c2e628c55
6 changed files with 375 additions and 0 deletions

View file

@ -0,0 +1,19 @@
TOP = ../..
SWIG = $(TOP)/../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

View file

@ -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; 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];
}

View file

@ -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 odbc32.lib odbccp32.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 odbc32.lib odbccp32.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 odbc32.lib odbccp32.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 odbc32.lib odbccp32.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.c" : $(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 -c++ -ruby $(InputPath)
# End Custom Build
!ELSEIF "$(CFG)" == "example - Win32 Release"
# Begin Custom Build
InputPath=.\example.i
InputName=example
"$(InputName)_wrap.c" : $(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 -c++ -ruby $(InputPath)
# End Custom Build
!ENDIF
# End Source File
# End Target
# End Project

View file

@ -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_*/

View file

@ -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_RemoveMapping */
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_RemoveMapping for the zoo */
SWIG_RubyRemoveTracking(ptr);
/* Now free the zoo which will free the animals it contains */
delete zoo;
}
%}

View file

@ -0,0 +1,45 @@
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
begin
puts tiger2.get_name
rescue RuntimeError => error
if not error.message == "This Animal * already released"
raise
end
end