added finalizers
[luabind.git] / luabind / dependency_policy.hpp
blob25ba523b29c6d66d25cc26304d6f5c857a796e21
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 #ifndef LUABIND_DEPENDENCY_POLICY_HPP_INCLUDED
25 #define LUABIND_DEPENDENCY_POLICY_HPP_INCLUDED
27 #include <luabind/config.hpp>
28 #include <luabind/detail/policy.hpp>
30 namespace luabind { namespace detail
32 // makes A dependent on B, meaning B will outlive A.
33 // internally A stores a reference to B
34 template<int A, int B>
35 struct dependency_policy
37 static void postcall(lua_State* L, const index_map& indices)
39 int nurse_index = indices[A];
40 int patient = indices[B];
42 object_rep* nurse = static_cast<object_rep*>(lua_touserdata(L, nurse_index));
43 assert((nurse != 0) && "internal error, please report"); // internal error
45 nurse->add_dependency(L, patient);
51 namespace luabind
53 // most absurd workaround of all time?
54 namespace detail
56 template<int N>
57 struct size_char_array
59 char storage[N + 2];
62 template<int N>
63 size_char_array<N> deduce_size(boost::arg<N>);
65 template<class T>
66 struct get_index_workaround
68 static T t;
69 BOOST_STATIC_CONSTANT(int, value = sizeof(deduce_size(t)) - 2);
73 template<class A, class B>
74 detail::policy_cons<detail::dependency_policy<detail::get_index_workaround<A>::value, detail::get_index_workaround<B>::value>, detail::null_type> dependency(A,B) { return detail::policy_cons<detail::dependency_policy<detail::get_index_workaround<A>::value, detail::get_index_workaround<B>::value>, detail::null_type>(); }
76 template<class A>
77 detail::policy_cons<detail::dependency_policy<0, detail::get_index_workaround<A>::value>, detail::null_type>
78 return_internal_reference(A)
79 { return detail::policy_cons<detail::dependency_policy<0, detail::get_index_workaround<A>::value>, detail::null_type>(); }
82 #endif // LUABIND_DEPENDENCY_POLICY_HPP_INCLUDED