New inheritance graph code.
[luabind.git] / luabind / adopt_policy.hpp
blob0dadb721a8e841fd9fd510fbed033b64efebd096
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_ADOPT_POLICY_HPP_INCLUDED
25 #define LUABIND_ADOPT_POLICY_HPP_INCLUDED
27 #include <luabind/config.hpp>
28 #include <luabind/wrapper_base.hpp>
29 #include <luabind/detail/policy.hpp>
30 #include <luabind/detail/implicit_cast.hpp>
31 #include <luabind/back_reference_fwd.hpp>
32 #include <boost/type_traits/is_polymorphic.hpp>
34 namespace luabind { namespace detail
36 template <class T>
37 void adjust_backref_ownership(T* ptr, mpl::true_)
39 if (wrap_base* p = dynamic_cast<wrap_base*>(ptr))
41 wrapped_self_t& wrapper = wrap_access::ref(*p);
42 wrapper.get(wrapper.state());
43 wrapper.m_strong_ref.set(wrapper.state());
47 inline void adjust_backref_ownership(void*, mpl::false_)
50 template<class Direction = lua_to_cpp>
51 struct adopt_pointer : pointer_converter
53 typedef adopt_pointer type;
55 int const consumed_args(...)
57 return 1;
60 template<class T>
61 T* apply(lua_State* L, by_pointer<T>, int index)
63 T* ptr = pointer_converter::apply(
64 L, LUABIND_DECORATE_TYPE(T*), index);
66 object_rep* obj = static_cast<object_rep*>(
67 lua_touserdata(L, index));
68 obj->release();
70 adjust_backref_ownership(ptr, boost::is_polymorphic<T>());
72 return ptr;
75 template<class T>
76 int match(lua_State* L, by_pointer<T>, int index)
78 return pointer_converter::match(
79 L, LUABIND_DECORATE_TYPE(T*), index);
82 template<class T>
83 void converter_postcall(lua_State*, T, int) {}
86 template<>
87 struct adopt_pointer<cpp_to_lua>
89 typedef adopt_pointer type;
91 template<class T>
92 void apply(lua_State* L, T* ptr)
94 if (ptr == 0)
96 lua_pushnil(L);
97 return;
100 // if there is a back_reference, then the
101 // ownership will be removed from the
102 // back reference and put on the lua stack.
103 if (luabind::move_back_reference(L, ptr))
104 return;
106 make_instance(L, std::auto_ptr<T>(ptr));
110 template<int N>
111 // struct adopt_policy : converter_policy_tag
112 struct adopt_policy : conversion_policy<N>
114 // BOOST_STATIC_CONSTANT(int, index = N);
116 static void precall(lua_State*, const index_map&) {}
117 static void postcall(lua_State*, const index_map&) {}
119 struct only_accepts_nonconst_pointers {};
121 template<class T, class Direction>
122 struct apply
124 typedef luabind::detail::is_nonconst_pointer<T> is_nonconst_p;
125 typedef typename boost::mpl::if_<is_nonconst_p, adopt_pointer<Direction>, only_accepts_nonconst_pointers>::type type;
131 namespace luabind
133 template<int N>
134 detail::policy_cons<detail::adopt_policy<N>, detail::null_type>
135 adopt(LUABIND_PLACEHOLDER_ARG(N))
137 return detail::policy_cons<detail::adopt_policy<N>, detail::null_type>();
141 #endif // LUABIND_ADOPT_POLICY_HPP_INCLUDE