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 class_rep
* crep
= obj
->crep();
66 if (crep
->has_holder())
67 ptr
= crep
->extractor()(obj
->ptr());
71 return get(f
, reinterpret_cast<T
*>(static_cast<char*>(ptr
) + pointer_offset
), L
, static_cast<Policies
*>(this));
75 template<class T
, class A1
, class Policies
>
76 int set(void(T::*f
)(A1
), T
* obj
, lua_State
* L
, Policies
* policies
) { return returns
<void>::call(f
, obj
, L
, policies
); }
78 template<class R, class T, class A1, class Policies>
79 int set(void(*f)(T*, A1), T* obj, lua_State* L, Policies* policies) { return returns<void>::call(f, obj, L, policies); }
81 template<class T, class A1, class Policies>
82 int set(void(*f)(T&, A1), T* obj, lua_State* L, Policies* policies) { return returns<void>::call(f, obj, L, policies); }
85 template<class T
, class U
, class A1
, class Policies
>
86 int set(void(*f
)(T
, A1
), U
* obj
, lua_State
* L
, Policies
* policies
) { return returns
<void>::call(f
, obj
, L
, policies
); }
88 template<class T
, class F
, class Policies
>
89 struct set_caller
: Policies
91 int operator()(lua_State
* L
, int pointer_offset
, F f
)
93 // parameters on the lua stack:
95 // 2. key (property name)
98 // and since call() expects it's first
99 // parameter on index 2 we need to
100 // remove the key-parameter (parameter 2).
101 object_rep
* obj
= reinterpret_cast<object_rep
*>(lua_touserdata(L
, 1));
102 class_rep
* crep
= obj
->crep();
106 if (crep
->has_holder())
107 ptr
= crep
->extractor()(obj
->ptr());
112 return set(f
, reinterpret_cast<T
*>(static_cast<char*>(ptr
) + pointer_offset
), L
, static_cast<Policies
*>(this));
116 // TODO: add support for policies
117 template<class T
, class D
, class Policies
>
118 struct auto_set
: Policies
121 auto_set(const Policies
& p
): Policies(p
) {}
123 int operator()(lua_State
* L
, int pointer_offset
, D
T::*member
)
125 int nargs
= lua_gettop(L
);
127 // parameters on the lua stack:
129 // 2. key (property name)
131 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, 1));
132 T
* ptr
= reinterpret_cast<T
*>(static_cast<char*>(obj
->ptr()) + pointer_offset
);
134 typedef typename find_conversion_policy
<1,Policies
>::type converter_policy
;
135 typename
converter_policy::template generate_converter
<D
,lua_to_cpp
>::type converter
;
136 ptr
->*member
= converter
.apply(L
, LUABIND_DECORATE_TYPE(D
), 3);
138 int nret
= lua_gettop(L
) - nargs
;
140 const int indices
[] = { 1, nargs
+ nret
, 3 };
142 policy_list_postcall
<Policies
>::apply(L
, indices
);
148 // TODO: add support for policies
149 template<class T
, class D
, class Policies
>
150 struct auto_get
: Policies
153 auto_get(const Policies
& p
): Policies(p
) {}
155 int operator()(lua_State
* L
, int pointer_offset
, D
T::*member
)
157 int nargs
= lua_gettop(L
);
159 // parameters on the lua stack:
161 // 2. key (property name)
162 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, 1));
163 T
* ptr
= reinterpret_cast<T
*>(static_cast<char*>(obj
->ptr()) + pointer_offset
);
165 typedef typename find_conversion_policy
<0,Policies
>::type converter_policy
;
166 typename
converter_policy::template generate_converter
<D
,cpp_to_lua
>::type converter
;
167 converter
.apply(L
, ptr
->*member
);
169 int nret
= lua_gettop(L
) - nargs
;
171 const int indices
[] = { 1, nargs
+ nret
};
173 policy_list_postcall
<Policies
>::apply(L
, indices
);
181 #endif // LUABIND_PROPERTY_HPP_INCLUDED