*** empty log message ***
[luabind.git] / test / test_policies.cpp
blob187856a63411a2e90e9605d8f00d5801e8cb935b
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 module(L)
67 class_<policies_test_class>("test")
68 .def(constructor<>())
69 .def("f", &policies_test_class::f, adopt(_1))
70 .def("make", &policies_test_class::make, adopt(return_value))
71 .def("internal_ref", &policies_test_class::internal_ref, dependency(return_value, self))
72 .def("self_ref", &policies_test_class::self_ref, return_reference_to(self)),
74 def("out_val", &out_val, pure_out_value(_1)),
75 def("copy_val", &copy_val, copy(result)),
76 def("secret", &secret, discard_result)
79 feedback = 1;
81 if (dostring(L, "a = secret()")) return false;
83 // copy
84 if (dostring(L, "a = copy_val()")) return false;
85 if (dostring(L, "a = nil")) return false;
86 if (dostring(L, "collectgarbage(0)")) return false;
87 if (feedback != 0) return false;
89 // out_value
90 if (dostring(L, "a = out_val()")) return false;
92 // return_reference_to
93 if (dostring(L, "a = test()")) return false;
94 if (feedback != 1) return false;
95 if (dostring(L, "b = a:self_ref()")) return false;
96 if (dostring(L, "a = nil")) return false;
97 if (dostring(L, "collectgarbage(0)")) return false;
98 if (feedback != 1) return false;
99 if (dostring(L, "b = nil")) return false;
100 if (dostring(L, "collectgarbage(0)")) return false;
101 if (feedback != 0) return false;
103 // dependency
104 if (dostring(L, "a = test()")) return false;
105 if (feedback != 1) return false;
106 if (dostring(L, "b = a:internal_ref()")) return false;
107 if (dostring(L, "a = nil")) return false;
108 if (dostring(L, "collectgarbage(0)")) return false;
109 if (feedback != 1) return false;
110 if (dostring(L, "b = nil")) return false;
111 if (dostring(L, "collectgarbage(0)")) return false; // two gc-cycles because dependency-table won't be collected in the same cycle
112 if (dostring(L, "collectgarbage(0)")) return false; // as the object_rep
113 if (feedback != 0) return false;
115 // adopt
116 if (dostring(L, "a = test()")) return false;
117 if (feedback != 1) return false;
118 if (dostring(L, "b = a:make('tjosan')")) return false;
119 if (feedback != 2) return false;
120 if (dostring(L, "a:f(b)")) return false;
122 if (top != lua_gettop(L)) return false;
124 c.release();
125 if (feedback != 0) return false;
127 catch(...)
129 return false;
131 return true;