1 // Copyright (c) 2003 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.
24 #include <luabind/luabind.hpp>
28 struct operator_tester
: counted_type
<operator_tester
>
30 int operator+(int a
) const
35 float operator-() const
40 float operator()() const
45 float operator()(int a
) const
50 float operator()(int a
)
57 int operator+(int a
, const operator_tester
&)
62 struct operator_tester2
: counted_type
<operator_tester2
>
66 float operator+(const operator_tester
&, const operator_tester2
&)
71 struct operator_tester3
: operator_tester
, counted_type
<operator_tester3
> {};
73 const operator_tester
* make_const_test()
75 static operator_tester o
;
79 std::ostream
& operator<<(std::ostream
& os
, const operator_tester
&)
81 os
<< "operator_tester"; return os
;
84 operator_tester
* clone(const operator_tester
* p
)
85 { return new operator_tester(*p
); }
87 } // anonymous namespace
91 COUNTER_GUARD(operator_tester
);
92 COUNTER_GUARD(operator_tester2
);
93 COUNTER_GUARD(operator_tester3
);
97 using namespace luabind
;
101 class_
<operator_tester
>("operator_tester")
102 .def(constructor
<>())
103 .def(tostring(const_self
))
105 .def(other
<int>() + self
)
108 .def(const_self(int()))
110 // .def("clone", &clone, adopt(return_value)),
112 class_
<operator_tester2
>("operator_tester2")
113 .def(constructor
<>())
114 .def(other
<const operator_tester
&>() + self
),
116 class_
<operator_tester3
, bases
<operator_tester
> >("operator_tester3")
117 .def(constructor
<>()),
119 def("make_const_test", &make_const_test
)
122 DOSTRING(L
, "test = operator_tester()");
123 DOSTRING(L
, "test2 = operator_tester2()");
124 DOSTRING(L
, "test3 = operator_tester3()");
125 DOSTRING(L
, "const_test = make_const_test()");
127 DOSTRING(L
, "assert(test() == 3.5)");
128 DOSTRING(L
, "assert(test(5) == 2.5 + 5)");
129 DOSTRING(L
, "assert(const_test(7) == 3.5 + 7)");
131 DOSTRING(L
, "assert(-test == 4.6)");
132 DOSTRING(L
, "assert(test + test2 == 7.3)");
133 DOSTRING(L
, "assert(2 + test == 2 + 2)");
134 DOSTRING(L
, "assert(test + 2 == 1 + 2)");
135 DOSTRING(L
, "assert(test3 + 6 == 1 + 6)");
136 DOSTRING(L
, "assert(test3 + test2 == 7.3)");
137 DOSTRING(L
, "assert(tostring(test) == 'operator_tester')");
141 "function my_class:__add(lhs)\n"
142 " return my_class(self.val + lhs.val)\n"
144 "function my_class:__init(a)\n"
147 "function my_class:__sub(v)\n"
148 " if (type(self) == 'number') then\n"
149 " return my_class(self - v.val)\n"
150 " elseif (type(v) == 'number') then\n"
151 " return my_class(self.val - v)\n"
153 " return my_class(self.val - v.val)\n"
164 DOSTRING(L
, "assert(c == 10)");
165 DOSTRING(L
, "assert(d == 2)");