adopt_policy.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
  2. // Permission is hereby granted, free of charge, to any person obtaining a
  3. // copy of this software and associated documentation files (the "Software"),
  4. // to deal in the Software without restriction, including without limitation
  5. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. // and/or sell copies of the Software, and to permit persons to whom the
  7. // Software is furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included
  9. // in all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  11. // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  15. // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  18. // OR OTHER DEALINGS IN THE SOFTWARE.
  19. #ifndef LUABIND_ADOPT_POLICY_HPP_INCLUDED
  20. #define LUABIND_ADOPT_POLICY_HPP_INCLUDED
  21. #include <luabind/config.hpp>
  22. #include <luabind/wrapper_base.hpp>
  23. #include <luabind/detail/policy.hpp>
  24. #include <luabind/back_reference_fwd.hpp>
  25. #include <luabind/wrapper_base.hpp>
  26. #include <boost/type_traits/is_polymorphic.hpp>
  27. namespace luabind { namespace detail
  28. {
  29. template <class T>
  30. void adjust_backref_ownership(T* ptr, mpl::true_)
  31. {
  32. if (wrap_base* p = dynamic_cast<wrap_base*>(ptr))
  33. {
  34. wrapped_self_t& wrapper = wrap_access::ref(*p);
  35. wrapper.get(wrapper.state());
  36. wrapper.m_strong_ref.set(wrapper.state());
  37. }
  38. }
  39. inline void adjust_backref_ownership(void*, mpl::false_)
  40. {}
  41. template <class Pointer, class Direction = lua_to_cpp>
  42. struct adopt_pointer : pointer_converter
  43. {
  44. typedef adopt_pointer type;
  45. int const consumed_args(...)
  46. {
  47. return 1;
  48. }
  49. template<class T>
  50. T* apply(lua_State* L, by_pointer<T>, int index)
  51. {
  52. T* ptr = pointer_converter::apply(
  53. L, LUABIND_DECORATE_TYPE(T*), index);
  54. object_rep* obj = static_cast<object_rep*>(
  55. lua_touserdata(L, index));
  56. obj->release();
  57. adjust_backref_ownership(ptr, boost::is_polymorphic<T>());
  58. return ptr;
  59. }
  60. template<class T>
  61. int match(lua_State* L, by_pointer<T>, int index)
  62. {
  63. return pointer_converter::match(
  64. L, LUABIND_DECORATE_TYPE(T*), index);
  65. }
  66. template<class T>
  67. void converter_postcall(lua_State*, T, int) {}
  68. };
  69. template <class Pointer, class T>
  70. struct pointer_or_default
  71. {
  72. typedef Pointer type;
  73. };
  74. template <class T>
  75. struct pointer_or_default<void, T>
  76. {
  77. typedef std::auto_ptr<T> type;
  78. };
  79. template <class Pointer>
  80. struct adopt_pointer<Pointer, cpp_to_lua>
  81. {
  82. typedef adopt_pointer type;
  83. template<class T>
  84. void apply(lua_State* L, T* ptr)
  85. {
  86. if (ptr == 0)
  87. {
  88. lua_pushnil(L);
  89. return;
  90. }
  91. // if there is a back_reference, then the
  92. // ownership will be removed from the
  93. // back reference and put on the lua stack.
  94. if (luabind::move_back_reference(L, ptr))
  95. return;
  96. typedef typename pointer_or_default<Pointer, T>::type
  97. pointer_type;
  98. make_instance(L, pointer_type(ptr));
  99. }
  100. };
  101. template <int N, class Pointer = void>
  102. struct adopt_policy : conversion_policy<N>
  103. {
  104. // BOOST_STATIC_CONSTANT(int, index = N);
  105. static void precall(lua_State*, const index_map&) {}
  106. static void postcall(lua_State*, const index_map&) {}
  107. struct only_accepts_nonconst_pointers {};
  108. template<class T, class Direction>
  109. struct apply
  110. {
  111. typedef luabind::detail::is_nonconst_pointer<T> is_nonconst_p;
  112. typedef typename boost::mpl::if_<
  113. is_nonconst_p
  114. , adopt_pointer<Pointer, Direction>
  115. , only_accepts_nonconst_pointers
  116. >::type type;
  117. };
  118. };
  119. }}
  120. namespace luabind
  121. {
  122. template<int N>
  123. detail::policy_cons<detail::adopt_policy<N>, detail::null_type>
  124. adopt(LUABIND_PLACEHOLDER_ARG(N))
  125. {
  126. return detail::policy_cons<detail::adopt_policy<N>, detail::null_type>();
  127. }
  128. template <class Pointer, int N>
  129. detail::policy_cons<detail::adopt_policy<N, Pointer>, detail::null_type>
  130. adopt(LUABIND_PLACEHOLDER_ARG(N))
  131. {
  132. return detail::policy_cons<detail::adopt_policy<N, Pointer>, detail::null_type>();
  133. }
  134. }
  135. #endif // LUABIND_ADOPT_POLICY_HPP_INCLUDE