Initial revision
[luabind.git] / luabind / detail / property.hpp
blob98455804d03793065140071046d568e5c81e1e9a
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
31 class object_rep;
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
45 get_caller() {}
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:
51 // 1. object_rep
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:
74 // 1. object_rep
75 // 2. key (property name)
76 // 3. value
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));
82 lua_remove(L, 2);
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>
89 struct auto_set
91 int operator()(lua_State* L, int pointer_offset, D T::*member)
93 // parameters on the lua stack:
94 // 1. object_rep
95 // 2. key (property name)
96 // 3. value
97 object_rep* obj = static_cast<object_rep*>(lua_touserdata(L, 1));
98 T* ptr = reinterpret_cast<T*>(static_cast<char*>(obj->ptr()) + pointer_offset);
99 typename default_policy::template generate_converter<D, lua_to_cpp>::type converter;
100 ptr->*member = converter.apply(L, LUABIND_DECORATE_TYPE(D), 3);
101 return 0;
105 // TODO: add support for policies
106 template<class T, class D>
107 struct auto_get
109 int operator()(lua_State* L, int pointer_offset, D T::*member)
111 // parameters on the lua stack:
112 // 1. object_rep
113 // 2. key (property name)
114 object_rep* obj = static_cast<object_rep*>(lua_touserdata(L, 1));
115 T* ptr = reinterpret_cast<T*>(static_cast<char*>(obj->ptr()) + pointer_offset);
116 typename default_policy::template generate_converter<D, cpp_to_lua>::type converter;
117 converter.apply(L, ptr->*member);
118 return 1;
124 #endif // LUABIND_PROPERTY_HPP_INCLUDED