Initial commit, includes Lua with broken Luabind as a backup for branching purposes
[terrastrategy.git] / include / luabind / back_reference.hpp
blob56036c239290610a29c386d8e64a1cce00727bfe
1 // Copyright (c) 2004 Daniel Wallin and Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
23 #ifndef LUABIND_BACK_REFERENCE_040510_HPP
24 #define LUABIND_BACK_REFERENCE_040510_HPP
26 #include <luabind/lua_include.hpp>
27 #include <luabind/wrapper_base.hpp>
28 #include <luabind/detail/has_get_pointer.hpp>
29 #include <luabind/get_pointer.hpp>
30 #include <boost/type_traits/is_polymorphic.hpp>
31 #include <boost/type_traits/is_const.hpp>
32 #include <boost/mpl/if.hpp>
34 namespace luabind {
36 namespace detail
38 namespace mpl = boost::mpl;
40 template<class T>
41 wrap_base const* get_back_reference_aux0(T const* p, mpl::true_)
43 return dynamic_cast<wrap_base const*>(p);
46 template<class T>
47 wrap_base const* get_back_reference_aux0(T const* p, mpl::false_)
49 return 0;
52 template<class T>
53 wrap_base const* get_back_reference_aux1(T const* p)
55 return get_back_reference_aux0(p, boost::is_polymorphic<T>());
58 template<class T>
59 wrap_base const* get_back_reference_aux2(T const& x, mpl::true_)
61 return get_back_reference_aux1(get_pointer(x));
64 template<class T>
65 wrap_base const* get_back_reference_aux2(T const& x, mpl::false_)
67 return get_back_reference_aux1(&x);
70 template<class T>
71 wrap_base const* get_back_reference(T const& x)
73 return detail::get_back_reference_aux2(
75 , has_get_pointer<T>()
79 } // namespace detail
81 template<class T>
82 bool get_back_reference(lua_State* L, T const& x)
84 #ifndef LUABIND_NO_RTTI
85 if (wrap_base const* w = detail::get_back_reference(x))
87 detail::wrap_access::ref(*w).get(L);
88 return true;
90 #endif
91 return false;
94 template<class T>
95 bool move_back_reference(lua_State* L, T const& x)
97 #ifndef LUABIND_NO_RTTI
98 if (wrap_base* w = const_cast<wrap_base*>(detail::get_back_reference(x)))
100 assert(detail::wrap_access::ref(*w).m_strong_ref.is_valid());
101 detail::wrap_access::ref(*w).get(L);
102 detail::wrap_access::ref(*w).m_strong_ref.reset();
103 return true;
105 #endif
106 return false;
109 } // namespace luabind
111 #endif // LUABIND_BACK_REFERENCE_040510_HPP