*** empty log message ***
[luabind.git] / test / test_policies.cpp
blob27e4a0e3507fe53bedcdc5da99e6817e80ea226b
1 #include "test.h"
3 #include <luabind/out_value_policy.hpp>
4 #include <luabind/return_reference_to_policy.hpp>
5 #include <luabind/copy_policy.hpp>
7 namespace
10 LUABIND_ANONYMOUS_FIX int feedback = 0;
12 struct policies_test_class
14 std::string name_;
16 policies_test_class() { feedback++; }
17 policies_test_class(const char* name): name_(name) { feedback++; }
18 ~policies_test_class() { feedback--; }
20 policies_test_class* make(const char* name) const
22 return new policies_test_class(name);
25 void f(policies_test_class* p) { delete p; }
27 const policies_test_class* internal_ref() { return this; }
29 policies_test_class* self_ref()
31 return this;
34 // private:
35 policies_test_class(const policies_test_class&) {}
38 policies_test_class global;
40 void out_val(float* f) { *f = 3.f; }
41 policies_test_class* copy_val() { return &global; }
43 struct secret_type {};
45 secret_type sec_;
47 secret_type* secret() { return &sec_; }
49 } // anonymous namespace
51 bool test_policies()
53 try
55 using namespace luabind;
57 lua_State* L = lua_open();
58 lua_closer c(L);
59 lua_baselibopen(L);
61 int top = lua_gettop(L);
63 luabind::open(L);
65 class_<policies_test_class>(L, "test")
66 .def(constructor<>())
67 .def("f", &policies_test_class::f, adopt(_1))
68 .def("make", &policies_test_class::make, adopt(return_value))
69 .def("internal_ref", &policies_test_class::internal_ref, dependency(return_value, self))
70 .def("self_ref", &policies_test_class::self_ref, return_reference_to(self))
73 function(L, "out_val", &out_val, pure_out_value(_1));
74 function(L, "copy_val", &copy_val, copy(result));
75 function(L, "secret", &secret, discard_result);
77 feedback = 1;
79 if (dostring(L, "a = secret()")) return false;
81 // copy
82 if (dostring(L, "a = copy_val()")) return false;
83 if (dostring(L, "a = nil")) return false;
84 if (dostring(L, "collectgarbage(0)")) return false;
85 if (feedback != 0) return false;
87 // out_value
88 if (dostring(L, "a = out_val()")) return false;
90 // return_reference_to
91 if (dostring(L, "a = test()")) return false;
92 if (feedback != 1) return false;
93 if (dostring(L, "b = a:self_ref()")) return false;
94 if (dostring(L, "a = nil")) return false;
95 if (dostring(L, "collectgarbage(0)")) return false;
96 if (feedback != 1) return false;
97 if (dostring(L, "b = nil")) return false;
98 if (dostring(L, "collectgarbage(0)")) return false;
99 if (feedback != 0) return false;
101 // dependency
102 if (dostring(L, "a = test()")) return false;
103 if (feedback != 1) return false;
104 if (dostring(L, "b = a:internal_ref()")) return false;
105 if (dostring(L, "a = nil")) return false;
106 if (dostring(L, "collectgarbage(0)")) return false;
107 if (feedback != 1) return false;
108 if (dostring(L, "b = nil")) return false;
109 if (dostring(L, "collectgarbage(0)")) return false; // two gc-cycles because dependency-table won't be collected in the same cycle
110 if (dostring(L, "collectgarbage(0)")) return false; // as the object_rep
111 if (feedback != 0) return false;
113 // adopt
114 if (dostring(L, "a = test()")) return false;
115 if (feedback != 1) return false;
116 if (dostring(L, "b = a:make('tjosan')")) return false;
117 if (feedback != 2) return false;
118 if (dostring(L, "a:f(b)")) return false;
120 if (top != lua_gettop(L)) return false;
122 c.release();
123 if (feedback != 0) return false;
125 catch(...)
127 return false;
129 return true;