Lua documentation tweaks and make nspace example more concise.

This commit is contained in:
William S Fulton 2014-03-04 07:54:37 +00:00
commit c99417ab13
4 changed files with 21 additions and 31 deletions

View file

@ -510,13 +510,14 @@ Enums are exported into a class table. For example, given some enums:
enum Days { SUNDAY = 0, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }; enum Days { SUNDAY = 0, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
struct Test { struct Test {
enum { TEST1 = 10, TEST2 = 20 }; enum { TEST1 = 10, TEST2 = 20 };
#ifdef __cplusplus // There are no static members in C language #ifdef __cplusplus // There are no static members in C
static const int ICONST = 12; static const int ICONST = 12;
#endif #endif
}; };
</pre></div> </pre></div>
<p> <p>
There is a slight difference in behaviour in C mode and C++ model. In C++ mode this is 'effectively' converted into the following Lua code: There is a slight difference in behaviour wrapping C and C++ code due to the different scoping rules of C and C++.
The wrapped C++ code is used as follows from Lua code:
</p> </p>
<div class="targetlang"><pre> <div class="targetlang"><pre>
&gt; print(example.SUNDAY) &gt; print(example.SUNDAY)
@ -527,7 +528,7 @@ There is a slight difference in behaviour in C mode and C++ model. In C++ mode t
12 12
</pre></div> </pre></div>
<p>In C mode enums from structs are exported into global namespace (due to C Standard). See below:</p> <p>Enums within a C struct are in the global namespace and are used as follows from Lua</p>
<div class="targetlang"><pre> <div class="targetlang"><pre>
&gt; print(example.SUNDAY) &gt; print(example.SUNDAY)
0 0
@ -538,7 +539,8 @@ There is a slight difference in behaviour in C mode and C++ model. In C++ mode t
<p> <p>
<b>Compatibility Note:</b> Versions of SWIG prior to SWIG-3.0.0 did not generate the class table members above. <b>Compatibility Note:</b> Versions of SWIG prior to SWIG-3.0.0 did not generate the class table members above.
The following code was the only way to access these constants/enums: There is no change in the C wrappers, but
the following code was the only way to access these constants/enums when wrapping C++ member constants:
</p> </p>
<div class="targetlang"><pre> <div class="targetlang"><pre>
&gt; print(example.Test_TEST1) &gt; print(example.Test_TEST1)
@ -551,15 +553,6 @@ The old-style bindings are still generated in addition to the new ones.
If the <tt>-no-old-metatable-bindings</tt> option is used, then these old-style bindings are not generated. If the <tt>-no-old-metatable-bindings</tt> option is used, then these old-style bindings are not generated.
</p> </p>
<p> <p>
However, in C mode, prefixed names of enums are not exported. There is no sense in having both Test_TEST1 and TEST1 in global namespace.
</p>
<div class="targetlang"><pre>
&gt; print(example.TEST1)
10
&gt; print(example.Test_TEST1)
nil
</pre></div>
<p>
It is worth mentioning, that <tt>example.Test.TEST1</tt> and <tt>example.Test_TEST1</tt> are different entities and changing one does not change the other. It is worth mentioning, that <tt>example.Test.TEST1</tt> and <tt>example.Test_TEST1</tt> are different entities and changing one does not change the other.
Given the fact that these are constantes and they are not supposed to be changed, it is up to you to avoid such issues. Given the fact that these are constantes and they are not supposed to be changed, it is up to you to avoid such issues.
</p> </p>
@ -749,7 +742,7 @@ public:
}; };
</pre></div> </pre></div>
<p> <p>
In Lua, the static members can be accessed as follows: In Lua, C++ static members can be accessed as follows:
</p> </p>
<div class="code"><pre> <div class="code"><pre>
&gt; example.Spam.foo() -- calling Spam::foo() &gt; example.Spam.foo() -- calling Spam::foo()
@ -1357,20 +1350,15 @@ int module_variable = 9;
namespace MyWorld { namespace MyWorld {
class World { class World {
public: public:
World(): World() : world_max_count(9) {}
world_max_count(9) {}
int create_world() { return 17; } int create_world() { return 17; }
const int world_max_count; // = 9 const int world_max_count; // = 9
}; };
namespace Nested { namespace Nested {
class Dweller { class Dweller {
public: public:
Dweller():
food_count(11) {}
enum Gender { MALE = 0, FEMALE = 1 }; enum Gender { MALE = 0, FEMALE = 1 };
static int populate_cave() { return 19; } static int count() { return 19; }
int create_cave() { return 13; }
int food_count; // = 11
}; };
} }
} }
@ -1387,6 +1375,8 @@ Now, from Lua usage is as follows:
9 9
&gt; print(example.MyWorld.Nested.Dweller.MALE) &gt; print(example.MyWorld.Nested.Dweller.MALE)
0 0
&gt; print(example.MyWorld.Nested.Dweller.count())
19
&gt; &gt;
</pre></div> </pre></div>
<H4> Compatibility Note </H4> <H4> Compatibility Note </H4>
@ -1397,7 +1387,7 @@ If SWIG is running in backward compatible way, i.e. without <tt>-no-old-metatabl
9 9
&gt; print(example.MyWorld.Nested.Dweller_MALE) &gt; print(example.MyWorld.Nested.Dweller_MALE)
0 0
&gt; print(example.MyWorld.Nested.Dweller_populate_cave()) &gt; print(example.MyWorld.Nested.Dweller_count())
11 11
&gt; &gt;
</pre></div> </pre></div>

View file

@ -7,20 +7,15 @@ int module_variable = 9;
namespace MyWorld { namespace MyWorld {
class World { class World {
public: public:
World(): World() : world_max_count(9) {}
world_max_count(9) {}
int create_world() { return 17; } int create_world() { return 17; }
const int world_max_count; // = 9 const int world_max_count; // = 9
}; };
namespace Nested { namespace Nested {
class Dweller { class Dweller {
public: public:
Dweller():
food_count(11) {}
enum Gender { MALE = 0, FEMALE = 1 }; enum Gender { MALE = 0, FEMALE = 1 };
static int populate_cave() { return 19; } static int count() { return 19; }
int create_cave() { return 13; }
int food_count; // = 11
}; };
} }
} }

View file

@ -4,7 +4,7 @@
#include "example.h" #include "example.h"
%} %}
%nspace MyWorld::Nested::Dweller; %nspace MyWorld::Nested::Dweller;
%nspace MyWorld::World; %nspace MyWorld::World;
%include "example.h" %include "example.h"

View file

@ -37,5 +37,10 @@ assert( w1.world_max_count == 9 )
-- Accessing enums from class within namespace -- Accessing enums from class within namespace
print( "Accessing enums: ex.MyWorld.Nested.Dweller.MALE = ", ex.MyWorld.Nested.Dweller.MALE ) print( "Accessing enums: ex.MyWorld.Nested.Dweller.MALE = ", ex.MyWorld.Nested.Dweller.MALE )
assert( ex.MyWorld.Nested.Dweller.MALE == 0 ) assert( ex.MyWorld.Nested.Dweller.MALE == 0 )
print( "Accessing enums: ex.MyWorld.Nested.Dweller.FEMALE = ", ex.MyWorld.Nested.Dweller.FEMALE )
assert( ex.MyWorld.Nested.Dweller.FEMALE == 1 ) assert( ex.MyWorld.Nested.Dweller.FEMALE == 1 )
-- Accessing static member function
print( "Accessing static member function: ex.MyWorld.Nested.Dweller.count() = ", ex.MyWorld.Nested.Dweller.count() )
assert( ex.MyWorld.Nested.Dweller.count() == 19 )