shared_ptr_converter.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright Daniel Wallin 2009. Use, modification and distribution is
  2. // subject to the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #ifndef LUABIND_SHARED_PTR_CONVERTER_090211_HPP
  5. # define LUABIND_SHARED_PTR_CONVERTER_090211_HPP
  6. # include <luabind/get_main_thread.hpp>
  7. # include <luabind/handle.hpp>
  8. # include <luabind/detail/policy.hpp>
  9. # include <boost/shared_ptr.hpp>
  10. namespace luabind {
  11. namespace detail
  12. {
  13. struct shared_ptr_deleter
  14. {
  15. shared_ptr_deleter(lua_State* L, int index)
  16. : life_support(get_main_thread(L), L, index)
  17. {}
  18. void operator()(void const*)
  19. {
  20. handle().swap(life_support);
  21. }
  22. handle life_support;
  23. };
  24. } // namespace detail
  25. template <class T>
  26. struct default_converter<boost::shared_ptr<T> >
  27. : default_converter<T*>
  28. {
  29. typedef boost::mpl::false_ is_native;
  30. template <class U>
  31. int match(lua_State* L, U, int index)
  32. {
  33. return default_converter<T*>::match(
  34. L, LUABIND_DECORATE_TYPE(T*), index);
  35. }
  36. template <class U>
  37. boost::shared_ptr<T> apply(lua_State* L, U, int index)
  38. {
  39. T* raw_ptr = default_converter<T*>::apply(
  40. L, LUABIND_DECORATE_TYPE(T*), index);
  41. if (!raw_ptr)
  42. return boost::shared_ptr<T>();
  43. return boost::shared_ptr<T>(
  44. raw_ptr, detail::shared_ptr_deleter(L, index));
  45. }
  46. void apply(lua_State* L, boost::shared_ptr<T> const& p)
  47. {
  48. if (detail::shared_ptr_deleter* d =
  49. boost::get_deleter<detail::shared_ptr_deleter>(p))
  50. {
  51. d->life_support.push(L);
  52. }
  53. else
  54. {
  55. detail::value_converter().apply(L, p);
  56. }
  57. }
  58. template <class U>
  59. void converter_postcall(lua_State*, U const&, int)
  60. {}
  61. };
  62. template <class T>
  63. struct default_converter<boost::shared_ptr<T> const&>
  64. : default_converter<boost::shared_ptr<T> >
  65. {};
  66. } // namespace luabind
  67. #endif // LUABIND_SHARED_PTR_CONVERTER_090211_HPP