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
); }
42 template<class T
, class F
, class Policies
>
43 struct get_caller
: Policies
46 get_caller(const Policies
& p
): Policies(p
) {}
48 int operator()(lua_State
* L
, int pointer_offset
, F f
)
50 // parameters on the lua stack:
52 // 2. key (property name)
54 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, 1));
55 return get(f
, reinterpret_cast<T
*>(static_cast<char*>(obj
->ptr()) + pointer_offset
), L
, static_cast<Policies
*>(this));
59 template<class T
, class A1
, class Policies
>
60 int set(void(T::*f
)(A1
), T
* obj
, lua_State
* L
, Policies
* policies
) { return returns
<void>::call(f
, obj
, L
, policies
); }
62 template<class R
, class T
, class A1
, class Policies
>
63 int set(void(*f
)(T
*, A1
), T
* obj
, lua_State
* L
, Policies
* policies
) { return returns
<void>::call(f
, obj
, L
, policies
); }
65 template<class T
, class A1
, class Policies
>
66 int set(void(*f
)(T
&, A1
), T
* obj
, lua_State
* L
, Policies
* policies
) { return returns
<void>::call(f
, obj
, L
, policies
); }
68 template<class T
, class F
, class Policies
>
69 struct set_caller
: Policies
71 int operator()(lua_State
* L
, int pointer_offset
, F f
)
73 // parameters on the lua stack:
75 // 2. key (property name)
78 // and since call() expects it's first
79 // parameter on index 2 we need to
80 // remove the key-parameter (parameter 2).
81 object_rep
* obj
= reinterpret_cast<object_rep
*>(lua_touserdata(L
, 1));
83 return set(f
, reinterpret_cast<T
*>(static_cast<char*>(obj
->ptr()) + pointer_offset
), L
, static_cast<Policies
*>(this));
87 // TODO: add support for policies
88 template<class T
, class D
, class Policies
>
89 struct auto_set
: Policies
92 auto_set(const Policies
& p
): Policies(p
) {}
94 int operator()(lua_State
* L
, int pointer_offset
, D
T::*member
)
96 int nargs
= lua_gettop(L
);
98 // parameters on the lua stack:
100 // 2. key (property name)
102 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, 1));
103 T
* ptr
= reinterpret_cast<T
*>(static_cast<char*>(obj
->ptr()) + pointer_offset
);
105 typedef typename find_conversion_policy
<1,Policies
>::type converter_policy
;
106 typename
converter_policy::template generate_converter
<D
,lua_to_cpp
>::type converter
;
107 ptr
->*member
= converter
.apply(L
, LUABIND_DECORATE_TYPE(D
), 3);
109 int nret
= lua_gettop(L
) - nargs
;
111 const int indices
[] = { 1, nargs
+ nret
, 3 };
113 policy_list_postcall
<Policies
>::apply(L
, indices
);
119 // TODO: add support for policies
120 template<class T
, class D
, class Policies
>
121 struct auto_get
: Policies
124 auto_get(const Policies
& p
): Policies(p
) {}
126 int operator()(lua_State
* L
, int pointer_offset
, D
T::*member
)
128 int nargs
= lua_gettop(L
);
130 // parameters on the lua stack:
132 // 2. key (property name)
133 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, 1));
134 T
* ptr
= reinterpret_cast<T
*>(static_cast<char*>(obj
->ptr()) + pointer_offset
);
136 typedef typename find_conversion_policy
<0,Policies
>::type converter_policy
;
137 typename
converter_policy::template generate_converter
<D
,cpp_to_lua
>::type converter
;
138 converter
.apply(L
, ptr
->*member
);
140 int nret
= lua_gettop(L
) - nargs
;
142 const int indices
[] = { 1, nargs
+ nret
};
144 policy_list_postcall
<Policies
>::apply(L
, indices
);
152 #endif // LUABIND_PROPERTY_HPP_INCLUDED