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_PROPERTY_HPP_INCLUDED
25 #define LUABIND_PROPERTY_HPP_INCLUDED
27 #include <luabind/config.hpp>
29 namespace luabind
{ namespace detail
33 template<class R, class T, class Policies>
34 int get(R(T::*f)() const, T* obj, lua_State* L, Policies* policies) { return returns<R>::call(f, obj, L, policies); }
36 template<class R, class T, class Policies>
37 int get(R(*f)(const T*), T* obj, lua_State* L, Policies* policies) { return returns<R>::call(f, obj, L, policies); }
39 template<class R, class T, class Policies>
40 int get(R(*f)(const T&), T* obj, lua_State* L, Policies* policies) { return returns<R>::call(f, obj, L, policies); }
43 template<class R
, class T
, class Policies
>
44 int get(R(T::*f
)() const, T
* obj
, lua_State
* L
, Policies
* policies
) { return returns
<R
>::call(f
, obj
, L
, policies
); }
46 template<class R
, class T
, class U
, class Policies
>
47 int get(R(*f
)(T
), U
* obj
, lua_State
* L
, Policies
* policies
) { return returns
<R
>::call(f
, obj
, L
, policies
); }
49 template<class T
, class F
, class Policies
>
50 struct get_caller
: Policies
53 get_caller(const Policies
& p
): Policies(p
) {}
55 int operator()(lua_State
* L
, int pointer_offset
, F f
)
57 // parameters on the lua stack:
59 // 2. key (property name)
61 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, 1));
62 return get(f
, reinterpret_cast<T
*>(static_cast<char*>(obj
->ptr()) + pointer_offset
), L
, static_cast<Policies
*>(this));
66 template<class T
, class A1
, class Policies
>
67 int set(void(T::*f
)(A1
), T
* obj
, lua_State
* L
, Policies
* policies
) { return returns
<void>::call(f
, obj
, L
, policies
); }
69 template<class R, class T, class A1, class Policies>
70 int set(void(*f)(T*, A1), T* obj, lua_State* L, Policies* policies) { return returns<void>::call(f, obj, L, policies); }
72 template<class T, class A1, class Policies>
73 int set(void(*f)(T&, A1), T* obj, lua_State* L, Policies* policies) { return returns<void>::call(f, obj, L, policies); }
76 template<class T
, class U
, class A1
, class Policies
>
77 int set(void(*f
)(T
, A1
), U
* obj
, lua_State
* L
, Policies
* policies
) { return returns
<void>::call(f
, obj
, L
, policies
); }
79 template<class T
, class F
, class Policies
>
80 struct set_caller
: Policies
82 int operator()(lua_State
* L
, int pointer_offset
, F f
)
84 // parameters on the lua stack:
86 // 2. key (property name)
89 // and since call() expects it's first
90 // parameter on index 2 we need to
91 // remove the key-parameter (parameter 2).
92 object_rep
* obj
= reinterpret_cast<object_rep
*>(lua_touserdata(L
, 1));
94 return set(f
, reinterpret_cast<T
*>(static_cast<char*>(obj
->ptr()) + pointer_offset
), L
, static_cast<Policies
*>(this));
98 // TODO: add support for policies
99 template<class T
, class D
, class Policies
>
100 struct auto_set
: Policies
103 auto_set(const Policies
& p
): Policies(p
) {}
105 int operator()(lua_State
* L
, int pointer_offset
, D
T::*member
)
107 int nargs
= lua_gettop(L
);
109 // parameters on the lua stack:
111 // 2. key (property name)
113 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, 1));
114 T
* ptr
= reinterpret_cast<T
*>(static_cast<char*>(obj
->ptr()) + pointer_offset
);
116 typedef typename find_conversion_policy
<1,Policies
>::type converter_policy
;
117 typename
converter_policy::template generate_converter
<D
,lua_to_cpp
>::type converter
;
118 ptr
->*member
= converter
.apply(L
, LUABIND_DECORATE_TYPE(D
), 3);
120 int nret
= lua_gettop(L
) - nargs
;
122 const int indices
[] = { 1, nargs
+ nret
, 3 };
124 policy_list_postcall
<Policies
>::apply(L
, indices
);
130 // TODO: add support for policies
131 template<class T
, class D
, class Policies
>
132 struct auto_get
: Policies
135 auto_get(const Policies
& p
): Policies(p
) {}
137 int operator()(lua_State
* L
, int pointer_offset
, D
T::*member
)
139 int nargs
= lua_gettop(L
);
141 // parameters on the lua stack:
143 // 2. key (property name)
144 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, 1));
145 T
* ptr
= reinterpret_cast<T
*>(static_cast<char*>(obj
->ptr()) + pointer_offset
);
147 typedef typename find_conversion_policy
<0,Policies
>::type converter_policy
;
148 typename
converter_policy::template generate_converter
<D
,cpp_to_lua
>::type converter
;
149 converter
.apply(L
, ptr
->*member
);
151 int nret
= lua_gettop(L
) - nargs
;
153 const int indices
[] = { 1, nargs
+ nret
};
155 policy_list_postcall
<Policies
>::apply(L
, indices
);
163 #endif // LUABIND_PROPERTY_HPP_INCLUDED