1 // Copyright Daniel Wallin 2008. 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)
5 #ifndef LUABIND_INSTANCE_HOLDER_081024_HPP
6 # define LUABIND_INSTANCE_HOLDER_081024_HPP
8 # include <luabind/detail/implicit_cast.hpp>
9 # include <luabind/detail/inheritance.hpp>
10 # include <luabind/get_pointer.hpp>
11 # include <luabind/typeid.hpp>
12 # include <boost/type_traits/is_polymorphic.hpp>
15 namespace luabind
{ namespace detail
{
20 instance_holder(bool pointee_const
)
21 : m_pointee_const(pointee_const
)
24 virtual ~instance_holder()
27 virtual std::pair
<void*, int> get(
28 cast_graph
const& casts
, class_id target
) const = 0;
30 virtual void release() = 0;
32 bool pointee_const() const
34 return m_pointee_const
;
41 namespace mpl
= boost::mpl
;
43 inline mpl::false_
check_const_pointer(void*)
48 inline mpl::true_
check_const_pointer(void const*)
54 void release_ownership(std::auto_ptr
<T
>& p
)
60 void release_ownership(P
const&)
62 throw std::runtime_error(
63 "luabind: smart pointer does not allow ownership transfer");
67 class_id
static_class_id(T
*)
69 return registered_class
<T
>::id
;
72 template <class P
, class Pointee
= void const>
73 class pointer_holder
: public instance_holder
77 P p
, class_id dynamic_id
, void* dynamic_ptr
79 : instance_holder(check_const_pointer(false ? get_pointer(p
) : 0))
82 , dynamic_id(dynamic_id
)
83 , dynamic_ptr(dynamic_ptr
)
86 std::pair
<void*, int> get(cast_graph
const& casts
, class_id target
) const
88 if (target
== registered_class
<P
>::id
)
89 return std::pair
<void*, int>(&this->p
, 0);
91 void* naked_ptr
= const_cast<void*>(static_cast<void const*>(
92 weak
? weak
: get_pointer(p
)));
95 return std::pair
<void*, int>(0, 0);
99 , static_class_id(false ? get_pointer(p
) : 0)
108 weak
= const_cast<void*>(static_cast<void const*>(
110 release_ownership(p
);
115 // weak will hold a possibly stale pointer to the object owned
116 // by p once p has released it's owership. This is a workaround
117 // to make adopt() work with virtual function wrapper classes.
123 }} // namespace luabind::detail
125 #endif // LUABIND_INSTANCE_HOLDER_081024_HPP