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/inheritance.hpp>
9 # include <luabind/get_pointer.hpp>
10 # include <luabind/typeid.hpp>
11 # include <boost/type_traits/is_polymorphic.hpp>
14 namespace luabind
{ namespace detail
{
19 instance_holder(bool pointee_const
)
20 : m_pointee_const(pointee_const
)
23 virtual ~instance_holder()
26 virtual std::pair
<void*, int> get(
27 cast_graph
const& casts
, class_id target
) const = 0;
29 virtual void release() = 0;
31 bool pointee_const() const
33 return m_pointee_const
;
40 namespace mpl
= boost::mpl
;
42 inline mpl::false_
check_const_pointer(void*)
47 inline mpl::true_
check_const_pointer(void const*)
53 void release_ownership(std::auto_ptr
<T
>& p
)
59 void release_ownership(P
const&)
61 throw std::runtime_error(
62 "luabind: smart pointer does not allow ownership transfer");
66 class_id
static_class_id(T
*)
68 return registered_class
<T
>::id
;
71 template <class P
, class Pointee
= void const>
72 class pointer_holder
: public instance_holder
76 P p
, class_id dynamic_id
, void* dynamic_ptr
78 : instance_holder(check_const_pointer(false ? get_pointer(p
) : 0))
81 , dynamic_id(dynamic_id
)
82 , dynamic_ptr(dynamic_ptr
)
85 std::pair
<void*, int> get(cast_graph
const& casts
, class_id target
) const
87 if (target
== registered_class
<P
>::id
)
88 return std::pair
<void*, int>(&this->p
, 0);
90 void* naked_ptr
= const_cast<void*>(static_cast<void const*>(
91 weak
? weak
: get_pointer(p
)));
94 return std::pair
<void*, int>((void*)0, 0);
98 , static_class_id(false ? get_pointer(p
) : 0)
107 weak
= const_cast<void*>(static_cast<void const*>(
109 release_ownership(p
);
114 // weak will hold a possibly stale pointer to the object owned
115 // by p once p has released it's owership. This is a workaround
116 // to make adopt() work with virtual function wrapper classes.
122 }} // namespace luabind::detail
124 #endif // LUABIND_INSTANCE_HOLDER_081024_HPP