Document new C++11 strongly typed enumerations support.
This commit is contained in:
parent
5b480847dd
commit
8c77d4be89
2 changed files with 38 additions and 35 deletions
|
|
@ -5,6 +5,9 @@ See the RELEASENOTES file for a summary of changes in each release.
|
||||||
Version 3.0.3 (in progress)
|
Version 3.0.3 (in progress)
|
||||||
===========================
|
===========================
|
||||||
|
|
||||||
|
2014-12-07: wsfulton
|
||||||
|
Add support for C++11 strongly typed enumerations.
|
||||||
|
|
||||||
2014-11-21: wsfulton
|
2014-11-21: wsfulton
|
||||||
[Java C#] Fix multiply defined error when using %rename of enum items when using the "simple enum" wrappers.
|
[Java C#] Fix multiply defined error when using %rename of enum items when using the "simple enum" wrappers.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -495,59 +495,59 @@ struct DerivedStruct : BaseStruct {
|
||||||
<H3><a name="CPlusPlus11_strongly_typed_enumerations"></a>7.2.13 Strongly typed enumerations</H3>
|
<H3><a name="CPlusPlus11_strongly_typed_enumerations"></a>7.2.13 Strongly typed enumerations</H3>
|
||||||
|
|
||||||
|
|
||||||
<p>SWIG parses the new <tt>enum class</tt> syntax and forward declarator for the enums:</p>
|
<p>SWIG supports strongly typed enumerations and parses the new <tt>enum class</tt> syntax and forward declarator for the enums, such as:</p>
|
||||||
<div class="code"><pre>
|
<div class="code"><pre>
|
||||||
enum class MyEnum : unsigned int;
|
enum class MyEnum : unsigned int;
|
||||||
</pre></div>
|
</pre></div>
|
||||||
|
|
||||||
<p>The strongly typed enumerations are treated the same as the ordinary and anonymous enums.
|
<p>
|
||||||
This is because the required nested class support in SWIG is new and has not yet been incorporated into the wrapping of these strongly typed enum classes.
|
Strongly typed enums are often used to avoid name clashes such as the following:
|
||||||
This is usually not a problem, however,
|
</p>
|
||||||
there may be some name clashes. For example, the following code:</p>
|
|
||||||
|
|
||||||
<div class="code"><pre>
|
<div class="code"><pre>
|
||||||
struct Color {
|
struct Color {
|
||||||
enum class PrintingColors : unsigned int {
|
enum class RainbowColors : unsigned int {
|
||||||
Cyan, Magenta, Yellow, Black
|
Red, Orange, Yellow, Green, Blue, Indigo, Violet
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class BasicColors {
|
enum class WarmColors {
|
||||||
Red, Green, Blue
|
Yellow, Orange, Red
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class AllColors {
|
|
||||||
// produces warnings because of duplicate names
|
|
||||||
Yellow, Orange, Red, Magenta, Blue, Cyan, Green, Pink, Black, White
|
|
||||||
};
|
|
||||||
};
|
|
||||||
</pre></div>
|
|
||||||
|
|
||||||
<p>A workaround is to write these as a series of separate classes containing anonymous enums:</p>
|
// Note normal enum
|
||||||
|
enum PrimeColors {
|
||||||
<div class="code"><pre>
|
Red=100, Green, Blue
|
||||||
struct PrintingColors {
|
|
||||||
enum : unsigned int {
|
|
||||||
Cyan, Magenta, Yellow, Black
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
struct BasicColors {
|
|
||||||
enum : unsigned int {
|
|
||||||
Red, Green, Blue
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AllColors {
|
|
||||||
enum : unsigned int {
|
|
||||||
Yellow, Orange, Red, Magenta, Blue, Cyan, Green, Pink, Black, White
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
</pre></div>
|
</pre></div>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Expect to see this improved in a future version of SWIG.
|
There are various ways that the target languages handle enums, so it is not possible to precisely state how they are handled in this section.
|
||||||
|
However, generally, most scripting languages mangle in the strongly typed enumeration's class name,
|
||||||
|
but do not use any additional mangling for normal enumerations. For example, in Python, the following code
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
<div class="targetlang"><pre>
|
||||||
|
print Color.RainbowColors_Red, Color.WarmColors_Red, Color.Red
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
results in
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<div class="shell"><pre>
|
||||||
|
0 2 100
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
The strongly typed languages often wrap normal enums into an enum class and so treat normal enums and strongly typed enums the same.
|
||||||
|
The equivalent in Java is:
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<div class="targetlang"><pre>
|
||||||
|
System.out.println(Color.RainbowColors.Red.swigValue() + " " + Color.WarmColors.Red.swigValue() + " " + Color.PrimeColors.Red.swigValue());
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
<H3><a name="CPlusPlus11_double_angle_brackets"></a>7.2.14 Double angle brackets</H3>
|
<H3><a name="CPlusPlus11_double_angle_brackets"></a>7.2.14 Double angle brackets</H3>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue