doc tweaks
[luabind.git] / test / test_scope.cpp
blob3ca526b99d1142414231c515f2f03b3898390e51
1 // Copyright (c) 2004 Daniel Wallin and Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
23 #include "test.hpp"
24 #include <luabind/luabind.hpp>
26 namespace
28 int f() { return 1; }
29 int f_(int a) { return 2; }
30 int f__(int a) { return 3; }
31 int g() { return 4; }
32 int g_(int) { return 5; }
33 int h() { return 6; }
35 struct test_class : counted_type<test_class>
37 test_class()
38 : test(1)
40 int test;
43 struct test_class2 : counted_type<test_class2>
45 test_class2() {}
46 int string_string(std::string const& s1, std::string const& s2)
47 { return 1; }
52 void test_scope()
54 COUNTER_GUARD(test_class);
55 COUNTER_GUARD(test_class2);
57 lua_state L;
59 using namespace luabind;
61 module(L)
63 class_<test_class2>("test_class2")
64 .def(constructor<>())
65 .def("string_string", &test_class2::string_string)
68 module(L, "test")
70 class_<test_class>("test_class")
71 .def(constructor<>())
72 .def_readonly("test", &test_class::test)
73 // .static_()
75 def("inner_fun", &f)
77 .enum_("vals")
79 value("val1", 1),
80 value("val2", 2)
83 def("f", &f),
84 def("f", &f_),
86 namespace_("inner")
88 def("g", &g),
89 def("f", &f__)
92 namespace_("inner")
94 def("g", &g_)
99 module(L, "test")
101 namespace_("inner")
103 def("h", &h)
108 DOSTRING(L, "assert(test.f() == 1)");
109 DOSTRING(L, "assert(test.f(3) == 2)");
110 DOSTRING(L, "assert(test.test_class.inner_fun() == 1)");
111 DOSTRING(L,
112 "a = test.test_class()\n"
113 "assert(a.test == 1)");
114 DOSTRING(L,
115 "b = test.test_class.val2\n"
116 "assert(b == 2)");
117 DOSTRING(L, "assert(test.inner.g() == 4)");
118 DOSTRING(L, "assert(test.inner.g(7) == 5)");
119 DOSTRING(L, "assert(test.inner.f(4) == 3)");
120 DOSTRING(L, "assert(test.inner.h() == 6)");