Remove instance_holder dependency on class_rep.
[luabind.git] / luabind / detail / instance_holder.hpp
blobd577c09573909e2b1275088665f417c573976ed7
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>
13 # include <stdexcept>
15 namespace luabind { namespace detail {
17 class instance_holder
19 public:
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;
37 private:
38 bool m_pointee_const;
41 namespace mpl = boost::mpl;
43 inline mpl::false_ check_const_pointer(void*)
45 return mpl::false_();
48 inline mpl::true_ check_const_pointer(void const*)
50 return mpl::true_();
53 template <class T>
54 void release_ownership(std::auto_ptr<T>& p)
56 p.release();
59 template <class P>
60 void release_ownership(P const&)
62 throw std::runtime_error(
63 "luabind: smart pointer does not allow ownership transfer");
66 template <class T>
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
75 public:
76 pointer_holder(
77 P p, class_id dynamic_id, void* dynamic_ptr
79 : instance_holder(check_const_pointer(false ? get_pointer(p) : 0))
80 , p(p)
81 , weak(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)));
94 if (!naked_ptr)
95 return std::pair<void*, int>(0, 0);
97 return casts.cast(
98 naked_ptr
99 , static_class_id(false ? get_pointer(p) : 0)
100 , target
101 , dynamic_id
102 , dynamic_ptr
106 void release()
108 weak = const_cast<void*>(static_cast<void const*>(
109 get_pointer(p)));
110 release_ownership(p);
113 private:
114 mutable P 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.
118 void* weak;
119 class_id dynamic_id;
120 void* dynamic_ptr;
123 }} // namespace luabind::detail
125 #endif // LUABIND_INSTANCE_HOLDER_081024_HPP