Fixing documentation. Adding an example.
This commit is contained in:
parent
736c6b953e
commit
6b18d20979
6 changed files with 110 additions and 8 deletions
|
|
@ -1346,15 +1346,20 @@ int module_variable = 9;
|
||||||
namespace MyWorld {
|
namespace MyWorld {
|
||||||
class World {
|
class World {
|
||||||
public:
|
public:
|
||||||
|
World():
|
||||||
|
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 {
|
||||||
enum Gender { MALE, FEMALE };
|
public:
|
||||||
static int populate_cave() { return 19; }
|
Dweller():
|
||||||
int create_cave() { return 13; }
|
food_count(11) {}
|
||||||
int food_count; // = 11
|
enum Gender { MALE = 0, FEMALE = 1 };
|
||||||
|
static int populate_cave() { return 19; }
|
||||||
|
int create_cave() { return 13; }
|
||||||
|
int food_count; // = 11
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1364,15 +1369,13 @@ Now, from Lua usage is as follows:
|
||||||
> print(example.module_function())
|
> print(example.module_function())
|
||||||
7
|
7
|
||||||
> print(example.module_variable)
|
> print(example.module_variable)
|
||||||
8
|
9
|
||||||
> print(example.MyWorld.World():create_world())
|
> print(example.MyWorld.World():create_world())
|
||||||
17
|
17
|
||||||
> print(example.MyWorld.World.world_max_count)
|
> print(example.MyWorld.World.world_max_count)
|
||||||
9
|
9
|
||||||
> print(example.MyWorld.Nested.Dweller.MALE)
|
> print(example.MyWorld.Nested.Dweller.MALE)
|
||||||
0
|
0
|
||||||
> print(example.MyWorld.Nested.Dweller().food_count)
|
|
||||||
11
|
|
||||||
>
|
>
|
||||||
</pre></div>
|
</pre></div>
|
||||||
<H4> Backward compatibility </H4>
|
<H4> Backward compatibility </H4>
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ funcptr3
|
||||||
functest
|
functest
|
||||||
functor
|
functor
|
||||||
import
|
import
|
||||||
|
nspace
|
||||||
owner
|
owner
|
||||||
pointer
|
pointer
|
||||||
simple
|
simple
|
||||||
|
|
|
||||||
19
Examples/lua/nspace/Makefile
Normal file
19
Examples/lua/nspace/Makefile
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
TOP = ../..
|
||||||
|
SWIG = $(TOP)/../preinst-swig
|
||||||
|
CXXSRCS =
|
||||||
|
TARGET = example
|
||||||
|
INTERFACE = example.i
|
||||||
|
|
||||||
|
check: build
|
||||||
|
$(MAKE) -f $(TOP)/Makefile lua_run
|
||||||
|
|
||||||
|
build:
|
||||||
|
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||||
|
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' lua_cpp
|
||||||
|
|
||||||
|
static:
|
||||||
|
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||||
|
TARGET='mylua' INTERFACE='$(INTERFACE)' lua_cpp_static
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(MAKE) -f $(TOP)/Makefile lua_clean
|
||||||
28
Examples/lua/nspace/example.h
Normal file
28
Examples/lua/nspace/example.h
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
#ifndef _example_guardian_
|
||||||
|
#define _example_guardian_
|
||||||
|
|
||||||
|
int module_function() { return 7; }
|
||||||
|
int module_variable = 9;
|
||||||
|
|
||||||
|
namespace MyWorld {
|
||||||
|
class World {
|
||||||
|
public:
|
||||||
|
World():
|
||||||
|
world_max_count(9) {}
|
||||||
|
int create_world() { return 17; }
|
||||||
|
const int world_max_count; // = 9
|
||||||
|
};
|
||||||
|
namespace Nested {
|
||||||
|
class Dweller {
|
||||||
|
public:
|
||||||
|
Dweller():
|
||||||
|
food_count(11) {}
|
||||||
|
enum Gender { MALE = 0, FEMALE = 1 };
|
||||||
|
static int populate_cave() { return 19; }
|
||||||
|
int create_cave() { return 13; }
|
||||||
|
int food_count; // = 11
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
10
Examples/lua/nspace/example.i
Normal file
10
Examples/lua/nspace/example.i
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
%module example
|
||||||
|
|
||||||
|
%{
|
||||||
|
#include "example.h"
|
||||||
|
%}
|
||||||
|
|
||||||
|
%nspace MyWorld::Nested::Dweller;
|
||||||
|
%nspace MyWorld::World;
|
||||||
|
|
||||||
|
%include "example.h"
|
||||||
41
Examples/lua/nspace/runme.lua
Normal file
41
Examples/lua/nspace/runme.lua
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
-- file: runme.lua
|
||||||
|
|
||||||
|
-- This file illustrates class C++ interface generated
|
||||||
|
-- by SWIG.
|
||||||
|
|
||||||
|
---- importing ----
|
||||||
|
if string.sub(_VERSION,1,7)=='Lua 5.0' then
|
||||||
|
-- lua5.0 doesnt have a nice way to do this
|
||||||
|
lib=loadlib('example.dll','luaopen_example') or loadlib('example.so','luaopen_example')
|
||||||
|
assert(lib)()
|
||||||
|
else
|
||||||
|
-- lua 5.1 does
|
||||||
|
require('example')
|
||||||
|
end
|
||||||
|
|
||||||
|
ex = example
|
||||||
|
|
||||||
|
-- Calling a module function ( aka global function )
|
||||||
|
assert( ex.module_function() == 7 )
|
||||||
|
print("example.module_function(): ", ex.module_function())
|
||||||
|
|
||||||
|
-- Accessing a module (aka global) variable
|
||||||
|
assert( ex.module_variable == 9 )
|
||||||
|
print("example.module_variable: ", ex.module_variable)
|
||||||
|
|
||||||
|
-- Creating an instance of the class
|
||||||
|
w1 = ex.MyWorld.World()
|
||||||
|
print("Creating class instance: w1 = ex.MyWorld.World(): ", w1)
|
||||||
|
|
||||||
|
-- Accessing class members
|
||||||
|
assert( ex.MyWorld.World():create_world() == 17 )
|
||||||
|
print( "w1:create_world() = ", w1:create_world() )
|
||||||
|
assert( w1:create_world() == 17 )
|
||||||
|
print( "w1:world_max_count = ", w1.world_max_count )
|
||||||
|
assert( w1.world_max_count == 9 )
|
||||||
|
|
||||||
|
-- Accessing enums from class within namespace
|
||||||
|
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.FEMALE == 1 )
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue