Add missing index_tuple.hpp header.
[luabind.git] / luabind / detail / instance_holder.hpp
blob0ab851ed9ede97f4ca59d27edcae3746637e14dc
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>
12 # include <stdexcept>
14 namespace luabind { namespace detail {
16 class instance_holder
18 public:
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;
36 private:
37 bool m_pointee_const;
40 namespace mpl = boost::mpl;
42 inline mpl::false_ check_const_pointer(void*)
44 return mpl::false_();
47 inline mpl::true_ check_const_pointer(void const*)
49 return mpl::true_();
52 template <class T>
53 void release_ownership(std::auto_ptr<T>& p)
55 p.release();
58 template <class P>
59 void release_ownership(P const&)
61 throw std::runtime_error(
62 "luabind: smart pointer does not allow ownership transfer");
65 template <class T>
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
74 public:
75 pointer_holder(
76 P p, class_id dynamic_id, void* dynamic_ptr
78 : instance_holder(check_const_pointer(false ? get_pointer(p) : 0))
79 , p(p)
80 , weak(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)));
93 if (!naked_ptr)
94 return std::pair<void*, int>((void*)0, 0);
96 return casts.cast(
97 naked_ptr
98 , static_class_id(false ? get_pointer(p) : 0)
99 , target
100 , dynamic_id
101 , dynamic_ptr
105 void release()
107 weak = const_cast<void*>(static_cast<void const*>(
108 get_pointer(p)));
109 release_ownership(p);
112 private:
113 mutable P 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.
117 void* weak;
118 class_id dynamic_id;
119 void* dynamic_ptr;
122 }} // namespace luabind::detail
124 #endif // LUABIND_INSTANCE_HOLDER_081024_HPP