[D] nspace support.

As for C# and Java, this doesn't work for free functions/variables yet.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12534 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
David Nadlinger 2011-03-13 00:32:26 +00:00
parent a63cde3827
commit ce6516fb4c
13 changed files with 757 additions and 164 deletions

View File

@ -28,6 +28,7 @@
<li><a href="#D_directors">D Directors</a>
<li><a href="#D_other_features">Other features</a>
<ul>
<li><a href="#D_nspace">Extended namespace support (<tt>nspace</tt>)</a></li>
<li><a href="#D_native_pointer_support">Native pointer support</a>
<li><a href="#D_operator_overloading">Operator overloading</a>
<li><a href="#D_test_suite">Running the test-suite</a>
@ -187,6 +188,12 @@
<p>There are two other variants available, <tt>$&amp;dclassname</tt> and <tt>$*dclassname</tt>. The former adds a level of indirection, while the latter removes one. For instance, when wrapping <tt>Foo **</tt>, <tt>$*dclassname</tt> would be replaced by the proxy class name corresponding to <tt>Foo *</tt>.</p>
</dd>
<dt><tt>$dclazzname</tt> (C#: <tt>$csclazzname</tt>)</dt>
<dd>
<p>This special variable expands the fully qualified C++ class into the package name, if used by the <a href="SWIGPlus.html#SWIGPlus_nspace"><tt>nspace</tt> feature</a>, and the proxy class name, mangled for use as a function name. For example, <tt>Namespace1::Namespace2::Klass</tt> is expanded into <tt>Namespace1_Namespace2_Klass_</tt>.</p>
<p>This special variable might be useful for calling certain functions in the wrapper layer (e.g. upcast wrappers) which are mangled like this.</p>
</dd>
<dt><tt>$null</tt></dt>
<dd><p>In code inserted into the generated C/C++ wrapper functions, this variable is replaced by either <tt>0</tt> or nothing at all, depending on whether the function has a return value or not. It can be used to bail out early e.g. in case of errors (<tt>return $null;</tt>).</p></dd>
@ -249,7 +256,7 @@ SomeClass bar() {
<p>This macro is only valid inside the <tt><a href="D.html#D_class_code_typemaps">dconstructor</a></tt> typemap and contains the value of the <tt>dconstructor</tt> typemap attribute if the currently wrapped class has directors enabled.</p>
<p>This is how the default <tt>dconstructor</tt> typemap looks like (you usually do not want to specify a custom one):</p>
<div class="code"><pre>
%typemap(dconstructor, excode=SWIGEXCODE,
%typemap(dconstructor, excode=SWIGEXCODE,
directorconnect="\n swigDirectorConnect();") SWIGTYPE {
this($imcall, true);$excode$directorconnect
}
@ -331,7 +338,7 @@ struct A {
<dd><p>Additional code to be emitted to the imports section of the intermediary D module (the <a href="D.html#D_importtype">$importtype</a> macro can be used here). You probably want to use this in conjunction with the <tt>imdmodulecode</tt> pragma.</p></dd>
<dt><tt>%pragma(d) proxydmodulecode</tt></dt>
<dd><p>Just like <tt>proxydmodulecode</tt>, the argument is copied to the proxy D module (if SWIG is in <a href="D.html#D_splitproxy">split proxy mode</a>, it is emitted to the main proxy D module only).</p></dd>
<dd><p>Just like <tt>proxydmodulecode</tt>, the argument is copied to the proxy D module (if SWIG is in <a href="D.html#D_splitproxy">split proxy mode</a> and/or the <tt>nspace</tt> feature is used, it is emitted to the main proxy D module only).</p></dd>
<dt><tt>%pragma(d) globalproxyimports</tt></dt>
<dd>
@ -379,10 +386,13 @@ struct A {
<H2><a name="D_other_features"></a>20.8 Other features</H2>
<p>The <a href="SWIGPlus.html#SWIGPlus_nspace"><tt>nspace</tt></a> feature of SWIG is not yet supported for D - all class modules are written to the same package, regardless of which C++ namespace they are in.</p>
<H3><a name="D_nspace"></a>20.8.1 Extended namespace support (<tt>nspace</tt>)</H3>
<H3><a name="D_native_pointer_support"></a>20.8.1 Native pointer support</H3>
<p>By default, SWIG flattens all C++ namespaces into a single target language namespace, but as for Java and C#, the <a href="SWIGPlus.html#SWIGPlus_nspace"><tt>nspace</tt></a> feature is supported for D. If it is active, C++ namespaces are mapped to D packages/modules. Note, however, that like for the other languages, <em>free</em> variables and functions are not supported yet; currently, they are all allows written to the main proxy D module.</p>
<H3><a name="D_native_pointer_support"></a>20.8.2 Native pointer support</H3>
<p>Contrary to many of the scripting languages supported by SWIG, D fully supports C-style pointers. The D module thus includes a custom mechanism to wrap C pointers directly as D pointers where applicable, that is, if the type that is pointed to is represented the same in C and D (on the bit-level), dubbed a »primtive type« below.</p>
@ -394,7 +404,7 @@ struct A {
<p>To determine if a type should be considered primitive, the <tt>cprimitive</tt> attribute on its <tt>dtype</tt> attribute is used. For example, the <tt>dtype</tt> typemap for <tt>float</tt> has <tt>cprimitive="1"</tt>, so the code from the <tt>nativepointer</tt> attribute is taken into account e.g. for <tt>float **</tt> or the function pointer <tt>float (*)(float *)</tt>.</p>
<H3><a name="D_operator_overloading"></a>20.8.2 Operator overloading</H3>
<H3><a name="D_operator_overloading"></a>20.8.3 Operator overloading</H3>
<p>The D module comes with basic operator overloading support for both D1 and D2. There are, however, a few limitations arising from conceptual differences between C++ and D:</p>
@ -406,7 +416,7 @@ struct A {
<p>There are also some cases where the operators can be translated to D, but the differences in the implementation details are big enough that a rather involved scheme would be required for automatic wrapping them, which has not been implemented yet. This affects, for example, the array subscript operator, <tt>[]</tt>, in combination with assignments - while <tt>operator []</tt> in C++ simply returns a reference which is then written to, D resorts to a separate <tt>opIndexAssign</tt> method -, or implicit casting (which was introduced in D2 via <tt>alias this</tt>). Despite the lack of automatic support, manually handling these cases should be perfectly possible.</p>
<H3><a name="D_test_suite"></a>20.8.3 Running the test-suite</H3>
<H3><a name="D_test_suite"></a>20.8.4 Running the test-suite</H3>
<p>As with any other language, the SWIG test-suite can be built for D using the <tt>*-d-test-suite</tt> targets of the top-level Makefile. By default, D1 is targeted, to build it with D2, use the optional <tt>D_VERSION</tt> variable, e.g. <tt>make check-d-test-suite D_VERSION=2</tt>.</p>

View File

@ -2,7 +2,7 @@ using System;
public class runme
{
static void Main()
static void Main()
{
// constructors and destructors
nspaceNamespace.Outer.Inner1.Color color1 = new nspaceNamespace.Outer.Inner1.Color();
@ -62,7 +62,7 @@ public class runme
throw new ApplicationException("Transmission2 wrong");
// turn feature off / ignoring
nspaceNamespace.Outer.nspace ns = new nspaceNamespace.Outer.nspace();
nspaceNamespace.Outer.namespce ns = new nspaceNamespace.Outer.namespce();
ns.Dispose();
nspaceNamespace.NoNSpacePlease nons = new nspaceNamespace.NoNSpacePlease();
nons.Dispose();

View File

@ -65,13 +65,13 @@ run_testcase = \
cd $*$(VERSIONSUFFIX) && \
$(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile \
DFLAGS='-of$*_runme' \
DSRCS='../$(srcdir)/$(TESTPREFIX)$*$(TESTSUFFIX) $*/*.d' d_compile && \
DSRCS='../$(srcdir)/$(TESTPREFIX)$*$(TESTSUFFIX) `find $* -name *.d`' d_compile && \
env LD_LIBRARY_PATH=".:$$LD_LIBRARY_PATH" $(RUNTOOL) ./$*_runme; \
else \
cd $*$(VERSIONSUFFIX) && \
$(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile \
DFLAGS='-c' \
DSRCS='$*/*.d' d_compile && cd .. ; \
DSRCS='`find $* -name *.d`' d_compile && cd .. ; \
fi
# Clean: remove testcase directories

View File

@ -0,0 +1,32 @@
module nspace_extend_runme;
static import oi1c = nspace_extend.Outer.Inner1.Color;
static import oi2c = nspace_extend.Outer.Inner2.Color;
void main() {
{
// constructors and destructors
scope color1 = new oi1c.Color();
scope color = new oi1c.Color(color1);
// class methods
color.colorInstanceMethod(20.0);
oi1c.Color.colorStaticMethod(20.0);
auto created = oi1c.Color.create();
}
{
// constructors and destructors
scope color2 = new oi2c.Color();
scope color = new oi2c.Color(color2);
// class methods
color.colorInstanceMethod(20.0);
oi2c.Color.colorStaticMethod(20.0);
auto created = oi2c.Color.create();
// Same class different namespaces
auto col1 = new oi1c.Color();
auto col2 = oi2c.Color.create();
col2.colors(col1, col1, col2, col2, col2);
}
}

View File

@ -0,0 +1,32 @@
module nspace_extend_runme;
static import oi1c = nspace_extend.Outer.Inner1.Color;
static import oi2c = nspace_extend.Outer.Inner2.Color;
void main() {
{
// constructors and destructors
scope color1 = new oi1c.Color();
scope color = new oi1c.Color(color1);
// class methods
color.colorInstanceMethod(20.0);
oi1c.Color.colorStaticMethod(20.0);
auto created = oi1c.Color.create();
}
{
// constructors and destructors
scope color2 = new oi2c.Color();
scope color = new oi2c.Color(color2);
// class methods
color.colorInstanceMethod(20.0);
oi2c.Color.colorStaticMethod(20.0);
auto created = oi2c.Color.create();
// Same class different namespaces
auto col1 = new oi1c.Color();
auto col2 = oi2c.Color.create();
col2.colors(col1, col1, col2, col2, col2);
}
}

View File

@ -0,0 +1,87 @@
module nspace_runme;
import nspace.nspace;
static import nspace.NoNSpacePlease;
static import nspace.Outer.namespce;
static import nspace.Outer.Inner1.Channel;
static import oi1c = nspace.Outer.Inner1.Color;
static import nspace.Outer.Inner2.Channel;
static import nspace.Outer.Inner2.Color;
static import nspace.Outer.Inner3.Blue;
static import nspace.Outer.Inner4.Blue;
static import nspace.Outer.SomeClass;
void main() {
// constructors and destructors
auto color1 = new oi1c.Color();
auto color = new oi1c.Color(color1);
// class methods
color.colorInstanceMethod(20.0);
oi1c.Color.colorStaticMethod(20.0);
auto created = oi1c.Color.create();
// class enums
auto someClass = new nspace.Outer.SomeClass.SomeClass();
auto channel = someClass.GetInner1ColorChannel();
if (channel != oi1c.Color.Channel.Transmission) {
throw new Exception("Transmission wrong");
}
// class anonymous enums
int val1 = oi1c.Color.ColorEnumVal1;
int val2 = oi1c.Color.ColorEnumVal2;
if (val1 != 0 || val2 != 0x22) {
throw new Exception("ColorEnumVal wrong");
}
// instance member variables
color.instanceMemberVariable = 123;
if (color.instanceMemberVariable != 123) {
throw new Exception("instance member variable failed");
}
// static member variables
oi1c.Color.staticMemberVariable = 789;
if (oi1c.Color.staticMemberVariable != 789) {
throw new Exception("static member variable failed");
}
if (oi1c.Color.staticConstMemberVariable != 222) {
throw new Exception("static const member variable failed");
}
if (oi1c.Color.staticConstEnumMemberVariable != oi1c.Color.Channel.Transmission) {
throw new Exception("static const enum member variable failed");
}
// check globals in a namespace don't get mangled with the nspace option
nspace.nspace.namespaceFunction(color);
nspace.nspace.namespaceVar = 111;
if (nspace.nspace.namespaceVar != 111) {
throw new Exception("global var failed");
}
// Same class different namespaces
auto col1 = new oi1c.Color();
auto col2 = nspace.Outer.Inner2.Color.Color.create();
col2.colors(col1, col1, col2, col2, col2);
// global enums
auto outerChannel1 = someClass.GetInner1Channel();
if (outerChannel1 != nspace.Outer.Inner1.Channel.Channel.Transmission1) {
throw new Exception("Transmission1 wrong");
}
auto outerChannel2 = someClass.GetInner2Channel();
if (outerChannel2 != nspace.Outer.Inner2.Channel.Channel.Transmission2) {
throw new Exception("Transmission2 wrong");
}
// turn feature off / ignoring
auto ns = new nspace.Outer.namespce.namespce();
auto nons = new nspace.NoNSpacePlease.NoNSpacePlease();
// Derived class
auto blue3 = new nspace.Outer.Inner3.Blue.Blue();
blue3.blueInstanceMethod();
auto blue4 = new nspace.Outer.Inner4.Blue.Blue();
blue4.blueInstanceMethod();
}

View File

@ -0,0 +1,77 @@
module nspace_runme;
import std.exception;
import nspace.nspace;
static import nspace.NoNSpacePlease;
static import nspace.Outer.namespce;
static import nspace.Outer.Inner1.Channel;
static import oi1c = nspace.Outer.Inner1.Color;
static import nspace.Outer.Inner2.Channel;
static import nspace.Outer.Inner2.Color;
static import nspace.Outer.Inner3.Blue;
static import nspace.Outer.Inner4.Blue;
static import nspace.Outer.SomeClass;
void main() {
// constructors and destructors
auto color1 = new oi1c.Color();
auto color = new oi1c.Color(color1);
// class methods
color.colorInstanceMethod(20.0);
oi1c.Color.colorStaticMethod(20.0);
auto created = oi1c.Color.create();
// class enums
auto someClass = new nspace.Outer.SomeClass.SomeClass();
auto channel = someClass.GetInner1ColorChannel();
enforce(channel == oi1c.Color.Channel.Transmission,
"Transmission wrong");
// class anonymous enums
int val1 = oi1c.Color.ColorEnumVal1;
int val2 = oi1c.Color.ColorEnumVal2;
enforce(val1 == 0 && val2 == 0x22, "ColorEnumVal wrong");
// instance member variables
color.instanceMemberVariable = 123;
enforce(color.instanceMemberVariable == 123,
"instance member variable failed");
// static member variables
oi1c.Color.staticMemberVariable = 789;
enforce(oi1c.Color.staticMemberVariable == 789,
"static member variable failed");
enforce(oi1c.Color.staticConstMemberVariable == 222,
"static const member variable failed");
enforce(oi1c.Color.staticConstEnumMemberVariable == oi1c.Color.Channel.Transmission,
"static const enum member variable failed");
// check globals in a namespace don't get mangled with the nspace option
nspace.nspace.namespaceFunction(color);
nspace.nspace.namespaceVar = 111;
enforce(nspace.nspace.namespaceVar == 111, "global var failed");
// Same class different namespaces
auto col1 = new oi1c.Color();
auto col2 = nspace.Outer.Inner2.Color.Color.create();
col2.colors(col1, col1, col2, col2, col2);
// global enums
auto outerChannel1 = someClass.GetInner1Channel();
enforce(outerChannel1 == nspace.Outer.Inner1.Channel.Channel.Transmission1,
"Transmission1 wrong");
auto outerChannel2 = someClass.GetInner2Channel();
enforce(outerChannel2 == nspace.Outer.Inner2.Channel.Channel.Transmission2,
"Transmission2 wrong");
// turn feature off / ignoring
auto ns = new nspace.Outer.namespce.namespce();
auto nons = new nspace.NoNSpacePlease.NoNSpacePlease();
// Derived class
auto blue3 = new nspace.Outer.Inner3.Blue.Blue();
blue3.blueInstanceMethod();
auto blue4 = new nspace.Outer.Inner4.Blue.Blue();
blue4.blueInstanceMethod();
}

View File

@ -68,7 +68,7 @@ public class nspace_runme {
throw new RuntimeException("Transmission2 wrong");
// turn feature off / ignoring
nspacePackage.Outer.nspace ns = new nspacePackage.Outer.nspace();
nspacePackage.Outer.namespce ns = new nspacePackage.Outer.namespce();
nspacePackage.NoNSpacePlease nons = new nspacePackage.NoNSpacePlease();
// Derived class

View File

@ -2,7 +2,7 @@
%module nspace
// nspace feature only supported by these languages
#if defined(SWIGJAVA) || defined(SWIGCSHARP)
#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGD)
%nspace;
%nonspace Outer::Inner2::NoNSpacePlease;
@ -15,7 +15,7 @@
%inline %{
namespace Outer {
class nspace {
class namespce {
};
namespace Inner1 {
enum Channel { Diffuse, Specular = 0x10, Transmission1 };
@ -33,12 +33,12 @@ namespace Outer {
static const Channel staticConstEnumMemberVariable = Transmission;
void colorInstanceMethod(double d) {}
static void colorStaticMethod(double d) {}
}; // Color
}; // Color
int Color::staticMemberVariable = 0;
Color namespaceFunction(Color k) { return k; }
int namespaceVar = 0;
} // Inner1
} // Inner1
namespace Inner2 {
enum Channel { Diffuse, Specular = 0x30, Transmission2 };
@ -56,12 +56,12 @@ namespace Outer {
static const Channel staticConstEnumMemberVariable = Transmission;
void colorInstanceMethod(double d) {}
static void colorStaticMethod(double d) {}
void colors(const Inner1::Color& col1a,
const Outer::Inner1::Color& col1b,
const Color &col2a,
const Inner2::Color& col2b,
void colors(const Inner1::Color& col1a,
const Outer::Inner1::Color& col1b,
const Color &col2a,
const Inner2::Color& col2b,
const Outer::Inner2::Color& col2c) {}
}; // Color
}; // Color
int Color::staticMemberVariable = 0;
class NoNSpacePlease {};
} // Inner2

View File

@ -2,7 +2,7 @@
%module nspace_extend
// nspace feature only supported by these languages
#if defined(SWIGJAVA) || defined(SWIGCSHARP)
#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGD)
%nspace;
@ -39,10 +39,10 @@ namespace Outer {
void colorInstanceMethod(double d) {}
static void colorStaticMethod(double d) {}
void colors(const Inner1::Color& col1a,
const Outer::Inner1::Color& col1b,
const Color &col2a,
const Inner2::Color& col2b,
void colors(const Inner1::Color& col1a,
const Outer::Inner1::Color& col1b,
const Color &col2a,
const Inner2::Color& col2b,
const Outer::Inner2::Color& col2c) {}
}

View File

@ -162,7 +162,7 @@ private void* swigCPtr;
private bool swigCMemOwn;
public this(void* cObject, bool ownCObject) {
super($imdmodule.$dclassnameSmartPtrUpcast(cObject), ownCObject);
super($imdmodule.$dclazznameSmartPtrUpcast(cObject), ownCObject);
swigCPtr = cObject;
swigCMemOwn = ownCObject;
}

View File

@ -84,7 +84,7 @@ mixin $imdmodule.SwigOperatorDefinitions;
private void* swigCPtr;
public this(void* cObject, bool ownCObject) {
super($imdmodule.$dclassnameUpcast(cObject), ownCObject);
super($imdmodule.$dclazznameUpcast(cObject), ownCObject);
swigCPtr = cObject;
}

File diff suppressed because it is too large Load Diff