My Project
instance_holder.hpp
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)
4 
5 #ifndef LUABIND_INSTANCE_HOLDER_081024_HPP
6 # define LUABIND_INSTANCE_HOLDER_081024_HPP
7 
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>
13 
14 namespace luabind { namespace detail {
15 
17 {
18 public:
19  instance_holder(bool pointee_const)
20  : m_pointee_const(pointee_const)
21  {}
22 
23  virtual ~instance_holder()
24  {}
25 
26  virtual std::pair<void*, int> get(
27  cast_graph const& casts, class_id target) const = 0;
28 
29  virtual void release() = 0;
30 
31  bool pointee_const() const
32  {
33  return m_pointee_const;
34  }
35 
36 private:
37  bool m_pointee_const;
38 };
39 
40 namespace mpl = boost::mpl;
41 
42 inline mpl::false_ check_const_pointer(void*)
43 {
44  return mpl::false_();
45 }
46 
47 inline mpl::true_ check_const_pointer(void const*)
48 {
49  return mpl::true_();
50 }
51 
52 template <class T>
53 void release_ownership(std::auto_ptr<T>& p)
54 {
55  p.release();
56 }
57 
58 template <class P>
59 void release_ownership(P const&)
60 {
61  throw std::runtime_error(
62  "luabind: smart pointer does not allow ownership transfer");
63 }
64 
65 template <class T>
66 class_id static_class_id(T*)
67 {
69 }
70 
71 template <class P, class Pointee = void const>
73 {
74 public:
76  P p, class_id dynamic_id, void* dynamic_ptr
77  )
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)
83  {}
84 
85  std::pair<void*, int> get(cast_graph const& casts, class_id target) const
86  {
87  if (target == registered_class<P>::id)
88  return std::pair<void*, int>(&this->p, 0);
89 
90  void* naked_ptr = const_cast<void*>(static_cast<void const*>(
91  weak ? weak : get_pointer(p)));
92 
93  if (!naked_ptr)
94  return std::pair<void*, int>((void*)0, 0);
95 
96  return casts.cast(
97  naked_ptr
98  , static_class_id(false ? get_pointer(p) : 0)
99  , target
100  , dynamic_id
101  , dynamic_ptr
102  );
103  }
104 
105  void release()
106  {
107  weak = const_cast<void*>(static_cast<void const*>(
108  get_pointer(p)));
109  release_ownership(p);
110  }
111 
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;
120 };
121 
122 }} // namespace luabind::detail
123 
124 #endif // LUABIND_INSTANCE_HOLDER_081024_HPP
Definition: instance_holder.hpp:72
Definition: inheritance.hpp:153
Definition: PEtypes.h:507
Definition: inheritance.hpp:25
Definition: instance_holder.hpp:16