|
| 1 | +// Copyright (c) 2003 Daniel Wallin and Arvid Norberg |
| 2 | + |
| 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: |
| 9 | + |
| 10 | +// The above copyright notice and this permission notice shall be included |
| 11 | +// in all copies or substantial portions of the Software. |
| 12 | + |
| 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. |
| 22 | + |
| 23 | + |
| 24 | +#ifndef LUABIND_ADOPT_POLICY_HPP_INCLUDED |
| 25 | +#define LUABIND_ADOPT_POLICY_HPP_INCLUDED |
| 26 | + |
| 27 | +#include <luabind/config.hpp> |
| 28 | +#include <luabind/detail/policy.hpp> |
| 29 | +#include <luabind/detail/implicit_cast.hpp> |
| 30 | +#include <boost/mpl/bool.hpp> |
| 31 | +#include <luabind/back_reference_fwd.hpp> |
| 32 | + |
| 33 | +namespace luabind { namespace detail |
| 34 | +{ |
| 35 | + template<class Direction = lua_to_cpp> |
| 36 | + struct adopt_pointer |
| 37 | + { |
| 38 | + typedef boost::mpl::bool_<false> is_value_converter; |
| 39 | + typedef adopt_pointer type; |
| 40 | + |
| 41 | + template<class T> |
| 42 | + T* apply(lua_State* L, by_pointer<T>, int index) |
| 43 | + { |
| 44 | + // preconditions: |
| 45 | + // lua_isuserdata(L, index); |
| 46 | + // getmetatable().__lua_class is true |
| 47 | + // object_rep->flags() & object_rep::constant == 0 |
| 48 | + |
| 49 | + int offset = 0; |
| 50 | + object_rep* obj = static_cast<object_rep*>(lua_touserdata(L, index)); |
| 51 | + assert((obj != 0) && "internal error, please report"); |
| 52 | + const class_rep* crep = obj->crep(); |
| 53 | + |
| 54 | + int steps = implicit_cast(crep, LUABIND_TYPEID(T), offset); |
| 55 | + (void)steps; |
| 56 | + |
| 57 | + assert((steps >= 0) && "adopt_pointer used with type that cannot be converted"); |
| 58 | + obj->remove_ownership(); |
| 59 | + T* ptr = reinterpret_cast<T*>(obj->ptr(offset)); |
| 60 | + |
| 61 | + return ptr; |
| 62 | + } |
| 63 | + |
| 64 | + template<class T> |
| 65 | + static int match(lua_State* L, by_pointer<T>, int index) |
| 66 | + { |
| 67 | + object_rep* obj = is_class_object(L, index); |
| 68 | + if (obj == 0) return -1; |
| 69 | + // cannot cast a constant object to nonconst |
| 70 | + if (obj->flags() & object_rep::constant) return -1; |
| 71 | + if (!(obj->flags() & object_rep::owner)) return -1; |
| 72 | + int d; |
| 73 | + return implicit_cast(obj->crep(), LUABIND_TYPEID(T), d); |
| 74 | + } |
| 75 | + |
| 76 | + template<class T> |
| 77 | + void converter_postcall(lua_State*, T, int) {} |
| 78 | + }; |
| 79 | + |
| 80 | + template<> |
| 81 | + struct adopt_pointer<cpp_to_lua> |
| 82 | + { |
| 83 | + typedef boost::mpl::bool_<false> is_value_converter; |
| 84 | + typedef adopt_pointer type; |
| 85 | + |
| 86 | + template<class T> |
| 87 | + void apply(lua_State* L, T* ptr) |
| 88 | + { |
| 89 | + if (ptr == 0) |
| 90 | + { |
| 91 | + lua_pushnil(L); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + // if there is a back_reference, then the |
| 96 | + // ownership will be removed from the |
| 97 | + // back reference and put on the lua stack. |
| 98 | + if (luabind::move_back_reference(L, ptr)) |
| 99 | + { |
| 100 | + object_rep* obj = static_cast<object_rep*>(lua_touserdata(L, -1)); |
| 101 | + obj->set_flags(obj->flags() | object_rep::owner); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + class_registry* registry = class_registry::get_registry(L); |
| 106 | + class_rep* crep = registry->find_class(LUABIND_TYPEID(T)); |
| 107 | + |
| 108 | +/* // create the struct to hold the object |
| 109 | + void* obj = lua_newuserdata(L, sizeof(object_rep)); |
| 110 | + // we send 0 as destructor since we know it will never be called |
| 111 | + new(obj) object_rep(ptr, crep, object_rep::owner, delete_s<T>::apply);*/ |
| 112 | + |
| 113 | + void* obj; |
| 114 | + void* held; |
| 115 | + |
| 116 | + boost::tie(obj,held) = crep->allocate(L); |
| 117 | + |
| 118 | + new(obj) object_rep(ptr, crep, object_rep::owner, delete_s<T>::apply); |
| 119 | + |
| 120 | + // set the meta table |
| 121 | + detail::getref(L, crep->metatable_ref()); |
| 122 | + lua_setmetatable(L, -2); |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + template<int N> |
| 127 | +// struct adopt_policy : converter_policy_tag |
| 128 | + struct adopt_policy : conversion_policy<N> |
| 129 | + { |
| 130 | +// BOOST_STATIC_CONSTANT(int, index = N); |
| 131 | + |
| 132 | + static void precall(lua_State*, const index_map&) {} |
| 133 | + static void postcall(lua_State*, const index_map&) {} |
| 134 | + |
| 135 | + struct only_accepts_nonconst_pointers {}; |
| 136 | + |
| 137 | + template<class T, class Direction> |
| 138 | + struct apply |
| 139 | + { |
| 140 | + typedef luabind::detail::is_nonconst_pointer<T> is_nonconst_p; |
| 141 | + typedef typename boost::mpl::if_<is_nonconst_p, adopt_pointer<Direction>, only_accepts_nonconst_pointers>::type type; |
| 142 | + }; |
| 143 | + }; |
| 144 | + |
| 145 | +}} |
| 146 | + |
| 147 | +namespace luabind |
| 148 | +{ |
| 149 | + template<int N> |
| 150 | + detail::policy_cons<detail::adopt_policy<N>, detail::null_type> |
| 151 | + adopt(LUABIND_PLACEHOLDER_ARG(N)) |
| 152 | + { |
| 153 | + return detail::policy_cons<detail::adopt_policy<N>, detail::null_type>(); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +#endif // LUABIND_ADOPT_POLICY_HPP_INCLUDE |
| 158 | + |
0 commit comments