From 2d518c638c008d12a7cf7e93a7711df34bd60859 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 29 Nov 2013 07:46:48 +0000 Subject: [PATCH] Add C++ nested class example This also reverts the nested class additions to the Java/C# 'class' example so that the 'class' example remains identical across different language modules --- Examples/csharp/check.list | 1 + Examples/csharp/class/example.cxx | 2 +- Examples/csharp/class/example.h | 8 +- Examples/csharp/class/runme.cs | 6 +- Examples/csharp/nested/Makefile | 19 +++ Examples/csharp/nested/example-cs.csproj | 94 +++++++++++++ Examples/csharp/nested/example-vc.vcproj | 158 ++++++++++++++++++++++ Examples/csharp/nested/example.cxx | 62 +++++++++ Examples/csharp/nested/example.h | 48 +++++++ Examples/csharp/nested/example.i | 13 ++ Examples/csharp/nested/example.sln | 30 +++++ Examples/csharp/nested/runme.cs | 27 ++++ Examples/java/check.list | 1 + Examples/java/class/example.cxx | 2 +- Examples/java/class/example.h | 8 +- Examples/java/class/runme.java | 6 +- Examples/java/nested/Makefile | 18 +++ Examples/java/nested/example.cxx | 62 +++++++++ Examples/java/nested/example.dsp | 162 +++++++++++++++++++++++ Examples/java/nested/example.h | 48 +++++++ Examples/java/nested/example.i | 13 ++ Examples/java/nested/runme.java | 32 +++++ 22 files changed, 802 insertions(+), 18 deletions(-) create mode 100644 Examples/csharp/nested/Makefile create mode 100644 Examples/csharp/nested/example-cs.csproj create mode 100644 Examples/csharp/nested/example-vc.vcproj create mode 100644 Examples/csharp/nested/example.cxx create mode 100644 Examples/csharp/nested/example.h create mode 100644 Examples/csharp/nested/example.i create mode 100644 Examples/csharp/nested/example.sln create mode 100644 Examples/csharp/nested/runme.cs create mode 100644 Examples/java/nested/Makefile create mode 100644 Examples/java/nested/example.cxx create mode 100644 Examples/java/nested/example.dsp create mode 100644 Examples/java/nested/example.h create mode 100644 Examples/java/nested/example.i create mode 100644 Examples/java/nested/runme.java diff --git a/Examples/csharp/check.list b/Examples/csharp/check.list index 5454d8531..a530a4b42 100644 --- a/Examples/csharp/check.list +++ b/Examples/csharp/check.list @@ -5,6 +5,7 @@ class enum extend funcptr +nested reference simple template diff --git a/Examples/csharp/class/example.cxx b/Examples/csharp/class/example.cxx index 9b23ea4e6..1e8e203dd 100644 --- a/Examples/csharp/class/example.cxx +++ b/Examples/csharp/class/example.cxx @@ -9,7 +9,7 @@ void Shape::move(double dx, double dy) { y += dy; } -int Shape::Counter::nshapes = 0; +int Shape::nshapes = 0; double Circle::area(void) { return M_PI*radius*radius; diff --git a/Examples/csharp/class/example.h b/Examples/csharp/class/example.h index 430cf47dc..46d901361 100644 --- a/Examples/csharp/class/example.h +++ b/Examples/csharp/class/example.h @@ -2,19 +2,17 @@ class Shape { public: - struct Counter{ - static int nshapes; - }; Shape() { - Counter::nshapes++; + nshapes++; } virtual ~Shape() { - Counter::nshapes--; + nshapes--; }; double x, y; void move(double dx, double dy); virtual double area(void) = 0; virtual double perimeter(void) = 0; + static int nshapes; }; class Circle : public Shape { diff --git a/Examples/csharp/class/runme.cs b/Examples/csharp/class/runme.cs index 2b500da5c..9088031d6 100644 --- a/Examples/csharp/class/runme.cs +++ b/Examples/csharp/class/runme.cs @@ -17,9 +17,9 @@ public class runme Console.WriteLine( " Created circle " + c ); Console.WriteLine( " Created square " + s ); - // ----- Access a static member of a nested class ----- + // ----- Access a static member ----- - Console.WriteLine( "\nA total of " + Shape.Counter.nshapes + " shapes were created" ); + Console.WriteLine( "\nA total of " + Shape.nshapes + " shapes were created" ); // ----- Member data access ----- @@ -60,7 +60,7 @@ public class runme // Note: when this using scope is exited the C# Dispose() methods // are called which in turn call the C++ destructors - Console.WriteLine( Shape.Counter.nshapes + " shapes remain" ); + Console.WriteLine( Shape.nshapes + " shapes remain" ); Console.WriteLine( "Goodbye" ); } } diff --git a/Examples/csharp/nested/Makefile b/Examples/csharp/nested/Makefile new file mode 100644 index 000000000..bc3ce8ce8 --- /dev/null +++ b/Examples/csharp/nested/Makefile @@ -0,0 +1,19 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +SWIGOPT = +CSHARPSRCS = *.cs +CSHARPFLAGS= -nologo -out:runme.exe + +check: build + $(MAKE) -f $(TOP)/Makefile csharp_run + +build: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' csharp_cpp + $(MAKE) -f $(TOP)/Makefile CSHARPSRCS='$(CSHARPSRCS)' CSHARPFLAGS='$(CSHARPFLAGS)' csharp_compile + +clean: + $(MAKE) -f $(TOP)/Makefile csharp_clean diff --git a/Examples/csharp/nested/example-cs.csproj b/Examples/csharp/nested/example-cs.csproj new file mode 100644 index 000000000..8004780fb --- /dev/null +++ b/Examples/csharp/nested/example-cs.csproj @@ -0,0 +1,94 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/Examples/csharp/nested/example-vc.vcproj b/Examples/csharp/nested/example-vc.vcproj new file mode 100644 index 000000000..5788bc9c7 --- /dev/null +++ b/Examples/csharp/nested/example-vc.vcproj @@ -0,0 +1,158 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Examples/csharp/nested/example.cxx b/Examples/csharp/nested/example.cxx new file mode 100644 index 000000000..03bb74d9e --- /dev/null +++ b/Examples/csharp/nested/example.cxx @@ -0,0 +1,62 @@ +#include "example.h" + +int MotorCar::DesignOpinion::AceDesignCount = 0; +int MotorCar::DesignOpinion::TotalDesignCount = 0; + +int MotorCar::DesignOpinion::PercentScore() { + return AceDesignCount*100/TotalDesignCount; +} + +MotorCar::Wheels::Wheels(Shape shape, size_t count) : shape(shape), count(count) {} + +MotorCar::WindScreen::WindScreen(bool opaque) : opaque(opaque) {} + +MotorCar::MotorCar(const std::string &name, const Wheels &wheels, const WindScreen &windscreen) : name(name), wheels(wheels), windscreen(windscreen) {} + +MotorCar MotorCar::DesignFromComponents(const std::string &name, const Wheels &wheels, const WindScreen &windscreen) { + MotorCar car = MotorCar(name, wheels, windscreen); + DesignOpinion::TotalDesignCount++; + if (car.wheels.Opinion().itrocks && car.windscreen.Opinion().itrocks) + DesignOpinion::AceDesignCount++; + return car; +} + +MotorCar::DesignOpinion MotorCar::Wheels::Opinion() { + DesignOpinion opinion; + opinion.itrocks = true; + if (shape == Square) { + opinion.itrocks = false; + opinion.reason = "you'll have a few issues with wheel rotation"; + } + if (count <= 2) { + opinion.reason += opinion.itrocks ? "" : " and "; + opinion.itrocks = false; + opinion.reason += "a few more wheels are needed for stability"; + } + if (opinion.itrocks) + opinion.reason = "your choice of wheels was top notch"; + + return opinion; +} + +MotorCar::DesignOpinion MotorCar::WindScreen::Opinion() { + DesignOpinion opinion; + opinion.itrocks = !opaque; + opinion.reason = opinion.itrocks ? "the driver will have a commanding view out the window" : "you can't see out the windscreen"; + return opinion; +} + +std::string MotorCar::WillItWork() { + DesignOpinion wh = wheels.Opinion(); + DesignOpinion ws = windscreen.Opinion(); + std::string willit; + if (wh.itrocks && ws.itrocks) { + willit = "Great car design because " + wh.reason + " and " + ws.reason; + } else { + willit = "You need a rethink because "; + willit += wh.itrocks ? "" : wh.reason; + willit += (!wh.itrocks && !ws.itrocks) ? " and " : ""; + willit += ws.itrocks ? "" : ws.reason; + } + return willit; +} diff --git a/Examples/csharp/nested/example.h b/Examples/csharp/nested/example.h new file mode 100644 index 000000000..4fb107cb5 --- /dev/null +++ b/Examples/csharp/nested/example.h @@ -0,0 +1,48 @@ +#include + +/** Design a motor car from various components */ +struct MotorCar { + + /** Information about an opinion of the design of a car component */ + struct DesignOpinion { + bool itrocks; + std::string reason; + static int AceDesignCount; + static int TotalDesignCount; + static int PercentScore(); + }; + + /** Wheels component */ + struct Wheels { + enum Shape { Round, Square }; + Wheels(Shape shape, size_t count); + DesignOpinion Opinion(); + private: + Shape shape; + size_t count; + }; + + /** Windscreen component */ + struct WindScreen { + WindScreen(bool opaque); + DesignOpinion Opinion(); + private: + bool opaque; + }; + + /** Factory method for creating a car */ + static MotorCar DesignFromComponents(const std::string &name, const Wheels &wheels, const WindScreen &windscreen); + + std::string Name() { + return name; + } + + /** Get an overall opinion on the car design */ + std::string WillItWork(); + +private: + MotorCar(const std::string &name, const Wheels &wheels, const WindScreen &windscreen); + std::string name; + Wheels wheels; + WindScreen windscreen; +}; diff --git a/Examples/csharp/nested/example.i b/Examples/csharp/nested/example.i new file mode 100644 index 000000000..c07c1521a --- /dev/null +++ b/Examples/csharp/nested/example.i @@ -0,0 +1,13 @@ +%module example + +// This example shows how wrappers for numerous aspects of C++ nested classes work: +// Nested static and instance variables and methods and nested enums + +%include + +%{ +#include "example.h" +%} + +%include "example.h" + diff --git a/Examples/csharp/nested/example.sln b/Examples/csharp/nested/example.sln new file mode 100644 index 000000000..88995ffd3 --- /dev/null +++ b/Examples/csharp/nested/example.sln @@ -0,0 +1,30 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "example-cs", "example-cs.csproj", "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + ProjectSection(ProjectDependencies) = postProject + {C2302635-D489-4678-96B4-70F5309DCBE6} = {C2302635-D489-4678-96B4-70F5309DCBE6} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example-vc", "example-vc.vcproj", "{C2302635-D489-4678-96B4-70F5309DCBE6}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + Debug = Debug + Release = Release + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.ActiveCfg = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.Build.0 = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.ActiveCfg = Release|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.Build.0 = Release|.NET + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.ActiveCfg = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.Build.0 = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.ActiveCfg = Release|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/Examples/csharp/nested/runme.cs b/Examples/csharp/nested/runme.cs new file mode 100644 index 000000000..facaefdb7 --- /dev/null +++ b/Examples/csharp/nested/runme.cs @@ -0,0 +1,27 @@ +// This example illustrates how C++ classes can be used from C# using SWIG. +// The C# class gets mapped onto the C++ class and behaves as if it is a C# class. + +using System; + +public class runme +{ + static void Main() + { + MotorCar car1 = MotorCar.DesignFromComponents("Bumpy", new MotorCar.Wheels(MotorCar.Wheels.Shape.Square, 4), new MotorCar.WindScreen(false)); + MotorCar car2 = MotorCar.DesignFromComponents("Wobbly", new MotorCar.Wheels(MotorCar.Wheels.Shape.Round, 2), new MotorCar.WindScreen(false)); + MotorCar car3 = MotorCar.DesignFromComponents("Batty", new MotorCar.Wheels(MotorCar.Wheels.Shape.Round, 4), new MotorCar.WindScreen(true)); + MotorCar car4 = MotorCar.DesignFromComponents("Spiffing", new MotorCar.Wheels(MotorCar.Wheels.Shape.Round, 4), new MotorCar.WindScreen(false)); + + Console.WriteLine("Expert opinion on " + car1.Name() + " : \n " + car1.WillItWork()); + Console.WriteLine("Expert opinion on " + car2.Name() + " : \n " + car2.WillItWork()); + Console.WriteLine("Expert opinion on " + car3.Name() + " : \n " + car3.WillItWork()); + Console.WriteLine("Expert opinion on " + car4.Name() + " : \n " + car4.WillItWork()); + + int count = MotorCar.DesignOpinion.AceDesignCount; + int total = MotorCar.DesignOpinion.TotalDesignCount; + int percent = MotorCar.DesignOpinion.PercentScore(); + Console.WriteLine("Overall opinion rating on car design is " + count + "/" + total + " = " + percent + "%"); + + Console.WriteLine("Single square wheel thoughts: " + new MotorCar.Wheels(MotorCar.Wheels.Shape.Square, 1).Opinion().reason); + } +} diff --git a/Examples/java/check.list b/Examples/java/check.list index 9728342f2..825d04a6d 100644 --- a/Examples/java/check.list +++ b/Examples/java/check.list @@ -7,6 +7,7 @@ extend funcptr multimap native +nested pointer reference simple diff --git a/Examples/java/class/example.cxx b/Examples/java/class/example.cxx index 9b23ea4e6..1e8e203dd 100644 --- a/Examples/java/class/example.cxx +++ b/Examples/java/class/example.cxx @@ -9,7 +9,7 @@ void Shape::move(double dx, double dy) { y += dy; } -int Shape::Counter::nshapes = 0; +int Shape::nshapes = 0; double Circle::area(void) { return M_PI*radius*radius; diff --git a/Examples/java/class/example.h b/Examples/java/class/example.h index 430cf47dc..46d901361 100644 --- a/Examples/java/class/example.h +++ b/Examples/java/class/example.h @@ -2,19 +2,17 @@ class Shape { public: - struct Counter{ - static int nshapes; - }; Shape() { - Counter::nshapes++; + nshapes++; } virtual ~Shape() { - Counter::nshapes--; + nshapes--; }; double x, y; void move(double dx, double dy); virtual double area(void) = 0; virtual double perimeter(void) = 0; + static int nshapes; }; class Circle : public Shape { diff --git a/Examples/java/class/runme.java b/Examples/java/class/runme.java index 90844ba23..e1ea0d71c 100644 --- a/Examples/java/class/runme.java +++ b/Examples/java/class/runme.java @@ -21,9 +21,9 @@ public class runme { Square s = new Square(10); System.out.println( " Created square " + s ); - // ----- Access a static member of a nested class ----- + // ----- Access a static member ----- - System.out.println( "\nA total of " + Shape.Counter.getNshapes() + " shapes were created" ); + System.out.println( "\nA total of " + Shape.getNshapes() + " shapes were created" ); // ----- Member data access ----- @@ -64,7 +64,7 @@ public class runme { c.delete(); s.delete(); - System.out.println( Shape.Counter.getNshapes() + " shapes remain" ); + System.out.println( Shape.getNshapes() + " shapes remain" ); System.out.println( "Goodbye" ); } } diff --git a/Examples/java/nested/Makefile b/Examples/java/nested/Makefile new file mode 100644 index 000000000..8f274e7cb --- /dev/null +++ b/Examples/java/nested/Makefile @@ -0,0 +1,18 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +SWIGOPT = +JAVASRCS = *.java + +check: build + $(MAKE) -f $(TOP)/Makefile java_run + +build: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' java_cpp + $(MAKE) -f $(TOP)/Makefile JAVASRCS='$(JAVASRCS)' JAVAFLAGS='$(JAVAFLAGS)' java_compile + +clean: + $(MAKE) -f $(TOP)/Makefile java_clean diff --git a/Examples/java/nested/example.cxx b/Examples/java/nested/example.cxx new file mode 100644 index 000000000..03bb74d9e --- /dev/null +++ b/Examples/java/nested/example.cxx @@ -0,0 +1,62 @@ +#include "example.h" + +int MotorCar::DesignOpinion::AceDesignCount = 0; +int MotorCar::DesignOpinion::TotalDesignCount = 0; + +int MotorCar::DesignOpinion::PercentScore() { + return AceDesignCount*100/TotalDesignCount; +} + +MotorCar::Wheels::Wheels(Shape shape, size_t count) : shape(shape), count(count) {} + +MotorCar::WindScreen::WindScreen(bool opaque) : opaque(opaque) {} + +MotorCar::MotorCar(const std::string &name, const Wheels &wheels, const WindScreen &windscreen) : name(name), wheels(wheels), windscreen(windscreen) {} + +MotorCar MotorCar::DesignFromComponents(const std::string &name, const Wheels &wheels, const WindScreen &windscreen) { + MotorCar car = MotorCar(name, wheels, windscreen); + DesignOpinion::TotalDesignCount++; + if (car.wheels.Opinion().itrocks && car.windscreen.Opinion().itrocks) + DesignOpinion::AceDesignCount++; + return car; +} + +MotorCar::DesignOpinion MotorCar::Wheels::Opinion() { + DesignOpinion opinion; + opinion.itrocks = true; + if (shape == Square) { + opinion.itrocks = false; + opinion.reason = "you'll have a few issues with wheel rotation"; + } + if (count <= 2) { + opinion.reason += opinion.itrocks ? "" : " and "; + opinion.itrocks = false; + opinion.reason += "a few more wheels are needed for stability"; + } + if (opinion.itrocks) + opinion.reason = "your choice of wheels was top notch"; + + return opinion; +} + +MotorCar::DesignOpinion MotorCar::WindScreen::Opinion() { + DesignOpinion opinion; + opinion.itrocks = !opaque; + opinion.reason = opinion.itrocks ? "the driver will have a commanding view out the window" : "you can't see out the windscreen"; + return opinion; +} + +std::string MotorCar::WillItWork() { + DesignOpinion wh = wheels.Opinion(); + DesignOpinion ws = windscreen.Opinion(); + std::string willit; + if (wh.itrocks && ws.itrocks) { + willit = "Great car design because " + wh.reason + " and " + ws.reason; + } else { + willit = "You need a rethink because "; + willit += wh.itrocks ? "" : wh.reason; + willit += (!wh.itrocks && !ws.itrocks) ? " and " : ""; + willit += ws.itrocks ? "" : ws.reason; + } + return willit; +} diff --git a/Examples/java/nested/example.dsp b/Examples/java/nested/example.dsp new file mode 100644 index 000000000..f52544b95 --- /dev/null +++ b/Examples/java/nested/example.dsp @@ -0,0 +1,162 @@ +# 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 "$(JAVA_INCLUDE)" /I "$(JAVA_INCLUDE)\win32" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /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 /nologo /dll /debug /machine:I386 /out:"example.dll" /pdbtype:sept +# Begin Special Build Tool +SOURCE="$(InputPath)" +PostBuild_Desc=Java compile post-build step +PostBuild_Cmds=echo on "%JAVA_BIN%\javac" *.java +# End Special Build Tool + +!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 "$(JAVA_INCLUDE)" /I "$(JAVA_INCLUDE)\win32" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /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 /nologo /dll /machine:I386 /out:"example.dll" +# Begin Special Build Tool +SOURCE="$(InputPath)" +PostBuild_Desc=Java compile post-build step +PostBuild_Cmds=echo on "%JAVA_BIN%\javac" *.java +# End Special Build Tool + +!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 JAVA_INCLUDE: %JAVA_INCLUDE% + echo JAVA_BIN: %JAVA_BIN% + echo on + ..\..\..\swig.exe -c++ -java "$(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 JAVA_INCLUDE: %JAVA_INCLUDE% + echo JAVA_BIN: %JAVA_BIN% + echo on + ..\..\..\swig.exe -c++ -java "$(InputPath)" + +# End Custom Build + +!ENDIF + +# End Source File +# End Target +# End Project diff --git a/Examples/java/nested/example.h b/Examples/java/nested/example.h new file mode 100644 index 000000000..4fb107cb5 --- /dev/null +++ b/Examples/java/nested/example.h @@ -0,0 +1,48 @@ +#include + +/** Design a motor car from various components */ +struct MotorCar { + + /** Information about an opinion of the design of a car component */ + struct DesignOpinion { + bool itrocks; + std::string reason; + static int AceDesignCount; + static int TotalDesignCount; + static int PercentScore(); + }; + + /** Wheels component */ + struct Wheels { + enum Shape { Round, Square }; + Wheels(Shape shape, size_t count); + DesignOpinion Opinion(); + private: + Shape shape; + size_t count; + }; + + /** Windscreen component */ + struct WindScreen { + WindScreen(bool opaque); + DesignOpinion Opinion(); + private: + bool opaque; + }; + + /** Factory method for creating a car */ + static MotorCar DesignFromComponents(const std::string &name, const Wheels &wheels, const WindScreen &windscreen); + + std::string Name() { + return name; + } + + /** Get an overall opinion on the car design */ + std::string WillItWork(); + +private: + MotorCar(const std::string &name, const Wheels &wheels, const WindScreen &windscreen); + std::string name; + Wheels wheels; + WindScreen windscreen; +}; diff --git a/Examples/java/nested/example.i b/Examples/java/nested/example.i new file mode 100644 index 000000000..c07c1521a --- /dev/null +++ b/Examples/java/nested/example.i @@ -0,0 +1,13 @@ +%module example + +// This example shows how wrappers for numerous aspects of C++ nested classes work: +// Nested static and instance variables and methods and nested enums + +%include + +%{ +#include "example.h" +%} + +%include "example.h" + diff --git a/Examples/java/nested/runme.java b/Examples/java/nested/runme.java new file mode 100644 index 000000000..855dbea91 --- /dev/null +++ b/Examples/java/nested/runme.java @@ -0,0 +1,32 @@ +// This example illustrates wrapping of nested C++ classes + +public class runme { + static { + try { + System.loadLibrary("example"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); + System.exit(1); + } + } + + public static void main(String argv[]) + { + MotorCar car1 = MotorCar.DesignFromComponents("Bumpy", new MotorCar.Wheels(MotorCar.Wheels.Shape.Square, 4), new MotorCar.WindScreen(false)); + MotorCar car2 = MotorCar.DesignFromComponents("Wobbly", new MotorCar.Wheels(MotorCar.Wheels.Shape.Round, 2), new MotorCar.WindScreen(false)); + MotorCar car3 = MotorCar.DesignFromComponents("Batty", new MotorCar.Wheels(MotorCar.Wheels.Shape.Round, 4), new MotorCar.WindScreen(true)); + MotorCar car4 = MotorCar.DesignFromComponents("Spiffing", new MotorCar.Wheels(MotorCar.Wheels.Shape.Round, 4), new MotorCar.WindScreen(false)); + + System.out.println("Expert opinion on " + car1.Name() + " : \n " + car1.WillItWork()); + System.out.println("Expert opinion on " + car2.Name() + " : \n " + car2.WillItWork()); + System.out.println("Expert opinion on " + car3.Name() + " : \n " + car3.WillItWork()); + System.out.println("Expert opinion on " + car4.Name() + " : \n " + car4.WillItWork()); + + int count = MotorCar.DesignOpinion.getAceDesignCount(); + int total = MotorCar.DesignOpinion.getTotalDesignCount(); + int percent = MotorCar.DesignOpinion.PercentScore(); + System.out.println("Overall opinion rating on car design is " + count + "/" + total + " = " + percent + "%"); + + System.out.println("Single square wheel thoughts: " + new MotorCar.Wheels(MotorCar.Wheels.Shape.Square, 1).Opinion().getReason()); + } +}