Removed Makefiles.
[luabind.git] / luabind / copy_policy.hpp
blobeb4bafc23b476e84b997c93133d45f7264982898
1 // Copyright (c) 2003 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.
24 #ifndef LUABIND_COPY_POLICY_HPP_INCLUDED
25 #define LUABIND_COPY_POLICY_HPP_INCLUDED
27 #include <luabind/config.hpp>
29 #include <boost/type_traits/is_pointer.hpp>
30 #include <boost/type_traits/is_reference.hpp>
31 #include <luabind/detail/policy.hpp>
33 namespace luabind { namespace detail {
35 struct copy_pointer_to
37 template<class T>
38 void apply(lua_State* L, const T* ptr)
40 if (ptr == 0)
42 lua_pushnil(L);
43 return;
46 class_registry* registry = class_registry::get_registry(L);
48 class_rep* crep = registry->find_class(LUABIND_TYPEID(T));
50 // if you get caught in this assert you are trying
51 // to use an unregistered type
52 assert(crep && "you are trying to use an unregistered type");
54 T* copied_obj = new T(*ptr);
56 // create the struct to hold the object
57 void* obj = lua_newuserdata(L, sizeof(object_rep));
58 // we send 0 as destructor since we know it will never be called
59 new(obj) object_rep(copied_obj, crep, object_rep::owner, delete_s<T>::apply);
61 // set the meta table
62 detail::getref(L, crep->metatable_ref());
63 lua_setmetatable(L, -2);
67 struct copy_reference_to
69 template<class T>
70 void apply(lua_State* L, const T& ref)
72 class_registry* registry = class_registry::get_registry(L);
73 class_rep* crep = registry->find_class(LUABIND_TYPEID(T));
75 // if you get caught in this assert you are trying
76 // to use an unregistered type
77 assert(crep && "you are trying to use an unregistered type");
79 T* copied_obj = new T(ref);
81 // create the struct to hold the object
82 void* obj = lua_newuserdata(L, sizeof(object_rep));
83 // we send 0 as destructor since we know it will never be called
84 new(obj) object_rep(copied_obj, crep, object_rep::owner, delete_s<T>::apply);
86 // set the meta table
87 detail::getref(L, crep->metatable_ref());
88 lua_setmetatable(L, -2);
92 template<int N>
93 struct copy_policy : conversion_policy<N>
95 struct only_accepts_pointers_or_references {};
96 struct only_converts_from_cpp_to_lua {};
98 static void precall(lua_State*, const index_map&) {}
99 static void postcall(lua_State*, const index_map&) {}
101 template<class T, class Direction>
102 struct apply
104 typedef typename boost::mpl::if_<boost::is_same<Direction, cpp_to_lua>
105 , typename boost::mpl::if_<boost::is_pointer<T>
106 , copy_pointer_to
107 , typename boost::mpl::if_<boost::is_reference<T>
108 , copy_reference_to
109 , only_accepts_pointers_or_references>::type>::type
110 , only_converts_from_cpp_to_lua>::type type;
115 namespace luabind
117 template<int N>
118 detail::policy_cons<detail::copy_policy<N>, detail::null_type>
119 copy(LUABIND_PLACEHOLDER_ARG(N))
121 return detail::policy_cons<detail::copy_policy<N>, detail::null_type>();
125 #endif // LUABIND_COPY_POLICY_HPP_INCLUDED