new example: hello_world
[luabind.git] / luabind / dependency_policy.hpp
blob00aba1e7fbb0f0c8553e7120c7ed3eba74e95b62
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 #if defined (BOOST_MSVC) && (BOOST_MSVC <= 1200)
53 namespace luabind
55 // most absurd workaround of all time?
56 namespace detail
58 template<int N>
59 struct size_char_array
61 char storage[N + 2];
64 template<int N>
65 size_char_array<N> deduce_size(boost::arg<N>);
67 template<class T>
68 struct get_index_workaround
70 static T t;
71 BOOST_STATIC_CONSTANT(int, value = sizeof(deduce_size(t)) - 2);
75 template<class A, class B>
76 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>(); }
78 template<class A>
79 detail::policy_cons<detail::dependency_policy<0, detail::get_index_workaround<A>::value>, detail::null_type>
80 return_internal_reference(A)
81 { return detail::policy_cons<detail::dependency_policy<0, detail::get_index_workaround<A>::value>, detail::null_type>(); }
84 #else
86 namespace luabind
88 template<int A, int B>
89 detail::policy_cons<detail::dependency_policy<A, B>, detail::null_type>
90 dependency(boost::arg<A>, boost::arg<B>)
92 return detail::policy_cons<detail::dependency_policy<A, B>, detail::null_type>();
95 template<int A>
96 detail::policy_cons<detail::dependency_policy<0, A>, detail::null_type>
97 return_internal_reference(boost::arg<A>)
99 return detail::policy_cons<detail::dependency_policy<0, A>, detail::null_type>();
103 #endif
105 #endif // LUABIND_DEPENDENCY_POLICY_HPP_INCLUDED