Test object identity with shared_ptr_converter.
[luabind.git] / luabind / detail / instance_holder.hpp
blob9267b764af3046d147b973a9bca4aa52c5690cc3
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>
14 # include <stdexcept>
16 namespace luabind { namespace detail {
18 class instance_holder
20 public:
21 instance_holder(class_rep* cls, bool pointee_const)
22 : m_cls(cls)
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
35 return m_cls;
38 bool pointee_const() const
40 return m_pointee_const;
43 private:
44 class_rep* m_cls;
45 bool m_pointee_const;
48 namespace mpl = boost::mpl;
50 inline mpl::false_ check_const_pointer(void*)
52 return mpl::false_();
55 inline mpl::true_ check_const_pointer(void const*)
57 return mpl::true_();
60 template <class T>
61 void release_ownership(std::auto_ptr<T>& p)
63 p.release();
66 template <class P>
67 void release_ownership(P const&)
69 throw std::runtime_error(
70 "luabind: smart pointer does not allow ownership transfer");
73 template <class T>
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
82 public:
83 pointer_holder(
84 P p, class_id dynamic_id, void* dynamic_ptr, class_rep* cls
86 : instance_holder(cls, check_const_pointer(get_pointer(p)))
87 , p(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*>(
98 get_pointer(p)));
100 if (!naked_ptr)
101 return std::pair<void*, int>(0, 0);
103 return get_class()->casts().cast(
104 naked_ptr
105 , static_class_id(get_pointer(p))
106 , target
107 , dynamic_id
108 , dynamic_ptr
112 void release()
114 release_ownership(p);
117 private:
118 mutable P p;
119 class_id dynamic_id;
120 void* dynamic_ptr;
123 }} // namespace luabind::detail
125 #endif // LUABIND_INSTANCE_HOLDER_081024_HPP