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.
25 #include <luabind/out_value_policy.hpp>
26 #include <luabind/return_reference_to_policy.hpp>
27 #include <luabind/copy_policy.hpp>
28 #include <luabind/adopt_policy.hpp>
29 #include <luabind/discard_result_policy.hpp>
30 #include <luabind/dependency_policy.hpp>
31 #include <luabind/luabind.hpp>
36 struct policies_test_class
: counted_type
<policies_test_class
>
38 policies_test_class(const char* name
): name_(name
) {}
39 policies_test_class() {}
42 policies_test_class
* make(const char* name
) const
44 return new policies_test_class(name
);
47 void f(policies_test_class
* p
) { delete p
; }
48 const policies_test_class
* internal_ref() { return this; }
49 policies_test_class
* self_ref()
53 policies_test_class(const policies_test_class
&) {}
56 policies_test_class global
;
58 void out_val(float* f
) { *f
= 3.f
; }
59 policies_test_class
* copy_val() { return &global
; }
61 struct secret_type
{};
65 secret_type
* secret() { return &sec_
; }
67 } // anonymous namespace
73 using namespace luabind
;
77 class_
<policies_test_class
>("test")
79 .def("f", &policies_test_class::f
, adopt(_1
))
80 .def("make", &policies_test_class::make
, adopt(return_value
))
81 .def("internal_ref", &policies_test_class::internal_ref
, dependency(return_value
, self
))
82 .def("self_ref", &policies_test_class::self_ref
, return_reference_to(self
)),
84 def("out_val", &out_val
, pure_out_value(_1
)),
85 def("copy_val", ©_val
, copy(result
)),
86 def("secret", &secret
, discard_result
)
94 "collectgarbage()\n");
96 BOOST_CHECK(policies_test_class::count
== 1);
99 DOSTRING(L
, "a = out_val()\n");
101 // return_reference_to
108 BOOST_CHECK(policies_test_class::count
== 2);
114 BOOST_CHECK(policies_test_class::count
== 1);
120 BOOST_CHECK(policies_test_class::count
== 2);
123 "b = a:internal_ref()\n"
127 BOOST_CHECK(policies_test_class::count
== 2);
129 // two gc-cycles because dependency-table won't be collected in the
130 // same cycle as the object_rep
136 BOOST_CHECK(policies_test_class::count
== 1);
143 BOOST_CHECK(policies_test_class::count
== 2);
146 "b = a:make('tjosan')\n"
149 BOOST_CHECK(policies_test_class::count
== 3);
156 BOOST_CHECK(policies_test_class::count
== 1);