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/detail/class_rep.hpp> // TODO
11 # include <luabind/get_pointer.hpp>
12 # include <luabind/typeid.hpp>
13 # include <boost/type_traits/is_polymorphic.hpp>
16 namespace luabind
{ namespace detail
{
21 instance_holder(class_rep
* cls
, bool pointee_const
)
23 , m_pointee_const(pointee_const
)
26 virtual ~instance_holder()
29 virtual std::pair
<void*, int> get(class_id target
) const = 0;
31 virtual void release() = 0;
33 class_rep
* get_class() const
38 bool pointee_const() const
40 return m_pointee_const
;
48 namespace mpl
= boost::mpl
;
50 inline mpl::false_
check_const_pointer(void*)
55 inline mpl::true_
check_const_pointer(void const*)
61 void release_ownership(std::auto_ptr
<T
>& p
)
67 void release_ownership(P
const&)
69 throw std::runtime_error(
70 "luabind: smart pointer does not allow ownership transfer");
74 class_id
static_class_id(T
*)
76 return registered_class
<T
>::id
;
79 template <class P
, class Pointee
= void const>
80 class pointer_holder
: public instance_holder
84 P p
, class_id dynamic_id
, void* dynamic_ptr
, class_rep
* cls
86 : instance_holder(cls
, check_const_pointer(get_pointer(p
)))
88 , dynamic_id(dynamic_id
)
89 , dynamic_ptr(dynamic_ptr
)
92 std::pair
<void*, int> get(class_id target
) const
94 if (target
== registered_class
<P
>::id
)
95 return std::pair
<void*, int>(&this->p
, 0);
97 void* naked_ptr
= const_cast<void*>(static_cast<void const*>(
101 return std::pair
<void*, int>(0, 0);
103 return get_class()->casts().cast(
105 , static_class_id(get_pointer(p
))
114 release_ownership(p
);
123 }} // namespace luabind::detail
125 #endif // LUABIND_INSTANCE_HOLDER_081024_HPP