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
This commit is contained in:
parent
b65ba2a8db
commit
2d518c638c
22 changed files with 802 additions and 18 deletions
18
Examples/java/nested/Makefile
Normal file
18
Examples/java/nested/Makefile
Normal file
|
|
@ -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
|
||||
62
Examples/java/nested/example.cxx
Normal file
62
Examples/java/nested/example.cxx
Normal file
|
|
@ -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;
|
||||
}
|
||||
162
Examples/java/nested/example.dsp
Normal file
162
Examples/java/nested/example.dsp
Normal file
|
|
@ -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
|
||||
48
Examples/java/nested/example.h
Normal file
48
Examples/java/nested/example.h
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#include <string>
|
||||
|
||||
/** 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;
|
||||
};
|
||||
13
Examples/java/nested/example.i
Normal file
13
Examples/java/nested/example.i
Normal file
|
|
@ -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 <std_string.i>
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
%include "example.h"
|
||||
|
||||
32
Examples/java/nested/runme.java
Normal file
32
Examples/java/nested/runme.java
Normal file
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue