revised scope documentation and added smart pointer documentation.
[luabind.git] / test / test_policies.cpp
blob7c7a58994c0c94b11a1cb678c36dd7be5340acfa
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>("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))
71 .commit(L)
74 function(L, "out_val", &out_val, pure_out_value(_1));
75 function(L, "copy_val", &copy_val, copy(result));
76 function(L, "secret", &secret, discard_result);
78 feedback = 1;
80 if (dostring(L, "a = secret()")) return false;
82 // copy
83 if (dostring(L, "a = copy_val()")) return false;
84 if (dostring(L, "a = nil")) return false;
85 if (dostring(L, "collectgarbage(0)")) return false;
86 if (feedback != 0) return false;
88 // out_value
89 if (dostring(L, "a = out_val()")) return false;
91 // return_reference_to
92 if (dostring(L, "a = test()")) return false;
93 if (feedback != 1) return false;
94 if (dostring(L, "b = a:self_ref()")) return false;
95 if (dostring(L, "a = nil")) return false;
96 if (dostring(L, "collectgarbage(0)")) return false;
97 if (feedback != 1) return false;
98 if (dostring(L, "b = nil")) return false;
99 if (dostring(L, "collectgarbage(0)")) return false;
100 if (feedback != 0) return false;
102 // dependency
103 if (dostring(L, "a = test()")) return false;
104 if (feedback != 1) return false;
105 if (dostring(L, "b = a:internal_ref()")) return false;
106 if (dostring(L, "a = nil")) return false;
107 if (dostring(L, "collectgarbage(0)")) return false;
108 if (feedback != 1) return false;
109 if (dostring(L, "b = nil")) return false;
110 if (dostring(L, "collectgarbage(0)")) return false; // two gc-cycles because dependency-table won't be collected in the same cycle
111 if (dostring(L, "collectgarbage(0)")) return false; // as the object_rep
112 if (feedback != 0) return false;
114 // adopt
115 if (dostring(L, "a = test()")) return false;
116 if (feedback != 1) return false;
117 if (dostring(L, "b = a:make('tjosan')")) return false;
118 if (feedback != 2) return false;
119 if (dostring(L, "a:f(b)")) return false;
121 if (top != lua_gettop(L)) return false;
123 c.release();
124 if (feedback != 0) return false;
126 catch(...)
128 return false;
130 return true;