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.
23 #if !BOOST_PP_IS_ITERATING
25 #ifndef LUABIND_OBJECT_HPP_INCLUDED
26 #define LUABIND_OBJECT_HPP_INCLUDED
30 #include <luabind/prefix.hpp>
31 #include <luabind/config.hpp>
32 #include <luabind/error.hpp>
33 #include <luabind/detail/pcall.hpp>
35 #include <boost/preprocessor/repeat.hpp>
36 #include <boost/preprocessor/iteration/iterate.hpp>
37 #include <boost/preprocessor/repetition/enum.hpp>
38 #include <boost/preprocessor/repetition/enum_params.hpp>
39 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
40 #include <boost/tuple/tuple.hpp>
49 class proxy_raw_object
;
50 class proxy_array_object
;
53 void convert_to_lua(lua_State
*, const T
&);
55 template<int Index
, class T
, class Policies
>
56 void convert_to_lua_p(lua_State
*, const T
&, const Policies
&);
59 struct push_args_from_tuple
61 template<class H
, class T
, class Policies
>
62 inline static void apply(lua_State
* L
, const boost::tuples::cons
<H
, T
>& x
, const Policies
& p
)
64 convert_to_lua_p
<Index
>(L
, *x
.get_head(), p
);
65 push_args_from_tuple
<Index
+1>::apply(L
, x
.get_tail(), p
);
68 template<class H
, class T
>
69 inline static void apply(lua_State
* L
, const boost::tuples::cons
<H
, T
>& x
)
71 convert_to_lua(L
, *x
.get_head());
72 push_args_from_tuple
<Index
+1>::apply(L
, x
.get_tail());
75 template<class Policies
>
76 inline static void apply(lua_State
*, const boost::tuples::null_type
&, const Policies
&) {};
78 inline static void apply(lua_State
*, const boost::tuples::null_type
&) {};
85 friend class luabind::object
;
88 proxy_caller(luabind::object
* o
, const Tuple args
)
95 proxy_caller(const detail::proxy_caller
<Tuple
>& rhs
)
98 , m_called(rhs
.m_called
)
104 operator luabind::object();
106 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
107 #define LUABIND_SEMICOLON
109 #define LUABIND_SEMICOLON ;
112 template<class Policies
>
113 luabind::object
operator[](const Policies
& p
) LUABIND_SEMICOLON
114 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
117 lua_State
* L
= m_obj
->lua_state();
119 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
120 if (pcall(L
, boost::tuples::length
<Tuple
>::value
, 1))
122 #ifndef LUABIND_NO_EXCEPTIONS
125 error_callback_fun e
= get_error_callback();
128 assert(0 && "the lua function threw an error and exceptions are disabled."
129 "if you want to handle this error use luabind::set_error_callback()");
133 detail::lua_reference ref
;
135 return luabind::object(m_obj
->lua_state(), ref
, true/*luabind::object::reference()*/);
140 #undef LUABIND_SEMICOLON
143 luabind::object
* m_obj
;
145 mutable bool m_called
;
153 stack_pop(lua_State
* L
, int n
)
161 lua_pop(m_state
, m_n
);
174 class LUABIND_API proxy_object
176 friend class luabind::object
;
177 friend class luabind::detail::proxy_array_object
;
178 friend class luabind::detail::proxy_raw_object
;
179 // template<class T> friend T object_cast(const proxy_object& obj);
183 proxy_object
& operator=(const T
& val
)
185 //std::cout << "proxy assigment\n";
186 lua_State
* L
= m_obj
->m_state
;
189 detail::convert_to_lua(L
, val
);
196 template<class T
, class Policies
>
197 void assign(const T
& val
, const Policies
& p
)
199 //std::cout << "proxy assigment\n";
200 lua_State
* L
= m_obj
->m_state
;
202 detail::getref(L
, m_key_ref
);
203 detail::convert_to_lua_p(L
, val
, p
);
210 proxy_object
& operator=(const object
& p
);
211 proxy_object
& operator=(const proxy_object
& p
);
212 proxy_object
& operator=(const proxy_raw_object
& p
);
213 proxy_object
& operator=(const proxy_array_object
& p
);
215 void swap(const proxy_object
& rhs
);
217 operator luabind::object();
222 detail::stack_pop
p(lua_state(), 1);
223 return lua_type(lua_state(), -1);
226 #define LUABIND_PROXY_RAW_AT_BODY \
228 lua_State* L = lua_state();\
230 detail::convert_to_lua(L, key);\
232 detail::lua_reference ref;\
235 return object(L, ref, true);\
238 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
240 inline object
raw_at(const T
& key
)
241 LUABIND_PROXY_RAW_AT_BODY
244 inline object
raw_at(const T
& key
);
247 #define LUABIND_PROXY_AT_BODY \
249 lua_State* L = lua_state();\
251 detail::convert_to_lua(L, key);\
252 lua_gettable(L, -2);\
253 detail::lua_reference ref;\
256 return object(L, ref, true);\
259 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
261 inline object
at(const T
& key
)
262 LUABIND_PROXY_AT_BODY
265 inline object
at(const T
& key
);
268 inline bool is_valid() const { return true; }
269 lua_State
* lua_state() const;
270 void pushvalue() const;
273 // this is a safe substitute for an implicit converter to bool
274 typedef void (proxy_object::*member_ptr
)() const;
275 operator member_ptr() const
277 if (is_valid()) return &proxy_object::dummy
;
283 void dummy() const {}
285 proxy_object(luabind::object
* o
, const lua_reference
& key
)
291 luabind::object
* m_obj
;
292 detail::lua_reference m_key
;
297 class LUABIND_API proxy_raw_object
299 friend class luabind::object
;
300 friend class luabind::detail::proxy_array_object
;
301 friend class luabind::detail::proxy_object
;
302 // template<class T> friend T luabind::object_cast(const proxy_object& obj);
306 proxy_raw_object
& operator=(const T
& val
)
308 //std::cout << "proxy assigment\n";
309 lua_State
* L
= m_obj
->m_state
;
311 detail::getref(L
, m_key_ref
);
312 detail::convert_to_lua(L
, val
);
319 template<class T
, class Policies
>
320 void assign(const T
& val
, const Policies
& p
)
322 //std::cout << "proxy assigment\n";
323 lua_State
* L
= m_obj
->m_state
;
325 detail::getref(L
, m_key_ref
);
326 detail::convert_to_lua_p(L
, val
, p
);
333 proxy_raw_object
& operator=(const object
& p
);
334 proxy_raw_object
& operator=(const proxy_object
& p
);
335 proxy_raw_object
& operator=(const proxy_raw_object
& p
);
336 proxy_raw_object
& operator=(const proxy_array_object
& p
);
337 void swap(const proxy_raw_object
& rhs
);
339 operator luabind::object();
344 detail::stack_pop
p(lua_state(), 1);
345 return lua_type(lua_state(), -1);
348 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
350 inline object
raw_at(const T
& key
)
351 LUABIND_PROXY_RAW_AT_BODY
354 inline object
raw_at(const T
& key
);
357 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
359 inline object
at(const T
& key
)
360 LUABIND_PROXY_AT_BODY
363 inline object
at(const T
& key
);
366 inline bool is_valid() const { return true; }
367 lua_State
* lua_state() const;
368 void pushvalue() const;
371 // this is a safe substitute for an implicit converter to bool
372 typedef void (proxy_raw_object::*member_ptr
)() const;
373 operator member_ptr() const
375 if (is_valid()) return &proxy_raw_object::dummy
;
382 void dummy() const {}
384 proxy_raw_object(luabind::object
* o
, const lua_reference
& key
)
390 luabind::object
* m_obj
;
391 detail::lua_reference m_key
;
396 class LUABIND_API proxy_array_object
398 friend class luabind::object
;
399 friend class luabind::detail::proxy_object
;
400 friend class luabind::detail::proxy_raw_object
;
401 // template<class T> friend T object_cast(const proxy_array_object& obj);
405 proxy_array_object
& operator=(const T
& val
)
407 //std::cout << "array proxy assigment\n";
408 lua_State
* L
= m_obj
->m_state
;
410 detail::convert_to_lua(L
, val
);
411 lua_rawseti(L
, -2, m_key
);
418 template<class T
, class Policies
>
419 void assign(const T
& val
, const Policies
& p
)
421 //std::cout << "proxy assigment\n";
422 lua_State
* L
= m_obj
->m_state
;
424 detail::convert_to_lua_p(L
, val
, p
);
425 lua_rawseti(L
, -2, m_key
);
431 proxy_array_object
& operator=(const object
& p
);
432 proxy_array_object
& operator=(const proxy_object
& p
);
433 proxy_array_object
& operator=(const proxy_raw_object
& p
);
434 proxy_array_object
& operator=(const proxy_array_object
& p
);
435 void swap(const proxy_array_object
& rhs
);
437 operator luabind::object();
442 detail::stack_pop
p(lua_state(), 1);
443 return lua_type(lua_state(), -1);
446 #define LUABIND_PROXY_ARRAY_RAW_AT_BODY\
449 detail::convert_to_lua(m_state, key);\
450 lua_rawget(m_state, -2);\
453 lua_pop(m_state, 1);\
454 return object(m_state, ref, true);\
457 #define LUABIND_PROXY_ARRAY_AT_BODY\
460 detail::convert_to_lua(m_state, key);\
461 lua_gettable(m_state, -2);\
464 lua_pop(m_state, 1);\
465 return object(m_state, ref, true);\
468 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
470 inline object
at(const T
& key
)
471 LUABIND_PROXY_ARRAY_AT_BODY
474 inline object
at(const T
& key
);
478 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
480 inline object
raw_at(const T
& key
)
481 LUABIND_PROXY_ARRAY_RAW_AT_BODY
484 inline object
raw_at(const T
& key
);
488 inline detail::proxy_object
operator[](const T
& key
) const
490 detail::convert_to_lua(m_state
, key
);
493 return detail::proxy_object(const_cast<object
*>(this), ref
);
496 inline bool is_valid() const { return true; }
497 lua_State
* lua_state() const;
498 void pushvalue() const;
501 // this is a safe substitute for an implicit converter to bool
502 typedef void (proxy_array_object::*member_ptr
)() const;
503 operator member_ptr() const
505 if (is_valid()) return &proxy_array_object::dummy
;
511 void dummy() const {}
513 proxy_array_object(luabind::object
* o
, int key
)
517 luabind::object
* m_obj
;
522 struct primitive_converter
;
524 struct tuple_object_ref
;
528 class LUABIND_API object
531 #if !(defined (BOOST_MSVC) && (BOOST_MSVC <= 1200))
534 friend T
object_cast(const object
& obj
);
536 friend struct detail::primitive_converter
;
540 friend object
get_globals(lua_State
*);
541 friend object
get_registry(lua_State
*);
542 friend object
newtable(lua_State
*);
543 friend class detail::proxy_object
;
544 friend class detail::proxy_array_object
;
545 friend class detail::proxy_raw_object
;
554 typedef std::forward_iterator_tag iterator_category
;
555 typedef luabind::object value_type
;
556 typedef value_type
& reference
;
557 typedef value_type
* pointer
;
558 typedef void difference_type
;
566 array_iterator(const array_iterator
& iter
)
574 array_iterator
& operator=(const array_iterator
& rhs
)
581 detail::proxy_array_object
operator*()
583 return m_obj
->make_array_proxy(m_key
);
586 inline array_iterator
& operator++()
588 LUABIND_CHECK_STACK(m_obj
->lua_state());
592 // invalidate the iterator if we hit a nil element
593 lua_State
* L
= m_obj
->lua_state();
595 lua_rawgeti(L
, -1, m_key
);
596 if (lua_isnil(L
, -1)) m_key
= -1;
602 inline array_iterator
operator++(int)
604 LUABIND_CHECK_STACK(m_obj
->lua_state());
609 // invalidate the iterator if we hit a nil element
610 lua_State
* L
= m_obj
->lua_state();
612 lua_rawgeti(L
, -1, m_key
);
613 if (lua_isnil(L
, -1)) m_key
= -1;
616 return array_iterator(m_obj
, old_key
);
619 bool operator!=(const array_iterator
& rhs
) const
621 return m_obj
!= rhs
.m_obj
|| m_key
!= rhs
.m_key
;
626 array_iterator(object
* obj
, int key
)
646 typedef std::forward_iterator_tag iterator_category
;
647 typedef luabind::object value_type
;
648 typedef value_type
& reference
;
649 typedef value_type
* pointer
;
650 typedef void difference_type
;
657 iterator(const iterator
& iter
)
666 iterator
& operator=(const iterator
& rhs
)
680 detail::proxy_object
operator*()
682 return m_obj
->make_proxy(m_key
);
685 iterator
& operator++()
687 lua_State
* L
= m_obj
->lua_state();
689 int n
= lua_gettop(L
);
694 if (lua_next(L
, -2) != 0)
707 assert(n
== lua_gettop(L
));
711 bool operator!=(const iterator
& rhs
) const
713 if (m_obj
!= rhs
.m_obj
) return true;
714 if (m_obj
== 0) return false;
715 if (m_obj
->lua_state() != rhs
.m_obj
->lua_state()) return true;
716 if (m_key
.is_valid() != rhs
.m_key
.is_valid()) return true;
718 // TODO: fix this. add a real equality test of the keys
726 iterator(luabind::object
* obj
, detail::lua_reference
const& key
)
733 detail::lua_reference m_key
;
744 typedef std::forward_iterator_tag iterator_category
;
745 typedef luabind::object value_type
;
746 typedef value_type
& reference
;
747 typedef value_type
* pointer
;
748 typedef void difference_type
;
755 raw_iterator(const raw_iterator
& iter
)
765 raw_iterator
& operator=(const raw_iterator
& rhs
)
767 //std::cout << "===\n";
780 detail::proxy_raw_object
operator*()
782 return m_obj
->make_raw_proxy(m_key
);
785 raw_iterator
& operator++()
787 lua_State
* L
= m_obj
->lua_state();
791 if (lua_next(L
, -2) != 0)
809 bool operator!=(const raw_iterator
& rhs
) const
811 if (m_obj
!= rhs
.m_obj
) return true;
812 if (m_obj
== 0) return false;
813 if (m_obj
->lua_state() != rhs
.m_obj
->lua_state()) return true;
814 if (m_key
.is_valid() != rhs
.m_key
.is_valid()) return true;
816 // TODO: fix this. add a real equality test of the keys
822 raw_iterator(object
* obj
, detail::lua_reference
const& key
)
828 detail::lua_reference m_key
;
841 explicit object(lua_State
* L
)
847 object(lua_State
* L
, const T
& val
)
853 object(const object
& o
)
856 o
.m_ref
.get(m_state
);
863 inline bool is_valid() const { return m_ref
.is_valid(); }
865 // this is a safe substitute for an implicit converter to bool
866 typedef void (object::*member_ptr
)() const;
867 operator member_ptr() const
869 if (is_valid()) return &object::dummy
;
876 detail::stack_pop
p(lua_state(), 1);
877 return lua_type(lua_state(), -1);
880 inline iterator
begin() const
883 lua_pushnil(m_state
);
884 lua_next(m_state
, -2);
886 detail::lua_reference r
;
888 iterator
i(const_cast<object
*>(this), r
);
893 inline iterator
end() const
895 return iterator(0, detail::lua_reference());
898 inline array_iterator
abegin() const
900 return array_iterator(const_cast<object
*>(this), 1);
903 inline array_iterator
aend() const
905 return array_iterator(const_cast<object
*>(this), -1);
908 raw_iterator
raw_begin() const
911 lua_pushnil(m_state
);
912 lua_next(m_state
, -2);
914 detail::lua_reference r
;
916 raw_iterator
i(const_cast<object
*>(this), r
);
921 raw_iterator
raw_end() const
923 return raw_iterator(0, detail::lua_reference());
926 inline void set() const
928 // you are trying to access an invalid object
929 assert((m_state
!= 0) && "you are trying to access an invalid (uninitialized) object");
932 m_ref
.replace(m_state
);
934 inline lua_State
* lua_state() const { return m_state
; }
935 inline void pushvalue() const
937 // you are trying to dereference an invalid object
938 assert((m_ref
.is_valid()) && "you are trying to access an invalid (uninitialized) object");
939 assert((m_state
!= 0) && "internal error, please report");
944 void swap(object
& rhs
);
947 inline object
raw_at(const T
& key
)
949 lua_State
* L
= lua_state();
951 detail::convert_to_lua(L
, key
);
953 detail::lua_reference ref
;
956 return object(L
, ref
, true);
960 inline object
at(const T
& key
)
962 LUABIND_CHECK_STACK(m_state
);
964 lua_State
* L
= lua_state();
966 detail::convert_to_lua(L
, key
);
968 detail::lua_reference ref
;
971 return object(L
, ref
, true);
975 inline detail::proxy_object
operator[](const T
& key
) const
977 LUABIND_CHECK_STACK(m_state
);
979 detail::convert_to_lua(m_state
, key
);
980 detail::lua_reference ref
;
982 return detail::proxy_object(const_cast<object
*>(this), ref
);
987 // *****************************
991 object
& operator=(const T
& val
) const
993 assert((m_state
!= 0) && "you cannot assign a non-lua value to an uninitialized object");
994 // you cannot assign a non-lua value to an uninitialized object
996 detail::convert_to_lua(m_state
, val
);
998 return const_cast<luabind::object
&>(*this);
1001 object
& operator=(const object
& o
) const;
1002 object
& operator=(const detail::proxy_object
& o
) const;
1003 object
& operator=(const detail::proxy_raw_object
& o
) const;
1004 object
& operator=(const detail::proxy_array_object
& o
) const;
1006 template<class T
, class Policies
>
1007 void assign(const T
& val
, const Policies
& p
) const
1009 assert((m_state
!= 0) && "you cannot assign a non-lua value to an uninitialized object");
1010 // you cannot assign a non-lua value to an uninitialized object
1012 detail::convert_to_lua_p(m_state
, val
, p
);
1016 // const overload should return a tuple_object..?
1017 inline detail::tuple_object_ref
operator,(const object
& rhs
) const;
1019 // *****************************
1022 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/object.hpp>, 1))
1023 #include BOOST_PP_ITERATE()
1027 inline detail::proxy_object
make_proxy(detail::lua_reference
const& key
)
1029 return detail::proxy_object(this, key
);
1032 inline detail::proxy_raw_object
make_raw_proxy(detail::lua_reference
const& key
)
1034 return detail::proxy_raw_object(this, key
);
1037 inline detail::proxy_array_object
make_array_proxy(int key
)
1039 return detail::proxy_array_object(this, key
);
1042 // TODO: it's not possible to make object friend with wrapped_constructor_helper::apply (since
1043 // it's an inner class), that's why this interface is public
1046 object(lua_State
* L
, detail::lua_reference
const& ref
, bool/*, reference*/)
1054 void dummy() const {}
1056 void allocate_slot() const
1058 if (!m_ref
.is_valid())
1060 lua_pushboolean(m_state
, 0);
1065 mutable lua_State
* m_state
;
1066 mutable detail::lua_reference m_ref
;
1070 // *************************************
1073 inline void object::swap(object
& rhs
)
1075 // you cannot swap objects from different lua states
1076 assert((lua_state() == rhs
.lua_state()) && "you cannot swap objects from different lua states");
1077 m_ref
.swap(rhs
.m_ref
);
1080 inline object
object::iterator::key() const
1082 lua_State
* L
= m_obj
->lua_state();
1083 return object(L
, m_key
, true);
1086 inline object
object::raw_iterator::key() const
1088 lua_State
* L
= m_obj
->lua_state();
1089 return object(L
, m_key
, true);
1094 // tuple object ----------------------------------------------
1096 struct tuple_object
;
1098 struct tuple_object_ref
1100 tuple_object_ref(object
* a
, object
* b
)
1102 { refs
[0] = a
; refs
[1] = b
; }
1104 tuple_object_ref
& operator,(const object
& x
)
1105 { refs
[n
++] = const_cast<object
*>(&x
); return *this; }
1112 assign_into(tuple_object_ref
& to
, const T
& val
)
1116 if (n
>= target
->n
) return;
1117 *target
->refs
[n
++] = val
;
1121 assign_into
& operator,(const T
& val
)
1123 if (n
>= target
->n
) return *this;
1124 *target
->refs
[n
++] = val
;
1128 tuple_object_ref
* target
;
1133 assign_into
operator=(const T
& val
)
1134 { return assign_into(*this, val
); }
1136 tuple_object_ref(const tuple_object_ref
&);
1137 assign_into
operator=(const tuple_object_ref
& x
)
1139 for (std::size_t i
= 0; i
< n
&& i
< x
.n
; ++i
)
1140 *refs
[i
] = *x
.refs
[i
];
1141 return assign_into();
1144 inline assign_into
operator=(const tuple_object
&);
1152 tuple_object(const object
& x
)
1156 tuple_object(const tuple_object_ref
& x
)
1158 for (std::size_t i
= 0; i
< x
.n
; ++i
)
1159 objs
[i
] = *x
.refs
[i
];
1166 inline tuple_object_ref::assign_into
tuple_object_ref::operator=(const tuple_object
& x
)
1168 for (std::size_t i
= 0; i
< n
&& i
< x
.n
; ++i
)
1169 *refs
[i
] = x
.objs
[i
];
1170 return assign_into();
1173 // *************************************
1176 #if !defined(BOOST_MSVC) || (defined(BOOST_MSVC) && (BOOST_MSVC > 1300))
1177 template<class Tuple
>
1178 template<class Policies
>
1179 luabind::object proxy_caller
<Tuple
>::operator[](const Policies
& p
)
1182 lua_State
* L
= m_obj
->lua_state();
1184 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
1185 if (pcall(L
, boost::tuples::length
<Tuple
>::value
, 1))
1187 #ifndef LUABIND_NO_EXCEPTIONS
1190 error_callback_fun e
= get_error_callback();
1193 assert(0 && "the lua function threw an error and exceptions are disabled."
1194 "if you want to handle this error use luabind::set_error_callback()");
1198 detail::lua_reference ref
;
1200 return luabind::object(m_obj
->lua_state(), ref
, true/*luabind::object::reference()*/);
1203 // *************************************
1206 #if !defined(BOOST_MSVC) || (defined(BOOST_MSVC) && (BOOST_MSVC > 1300))
1208 inline object
proxy_object::raw_at(const T
& key
)
1209 LUABIND_PROXY_RAW_AT_BODY
1212 inline object
proxy_object::at(const T
& key
)
1213 LUABIND_PROXY_AT_BODY
1216 inline lua_State
* proxy_object::lua_state() const
1218 return m_obj
->lua_state();
1221 inline proxy_object::operator luabind::object()
1223 lua_State
* L
= m_obj
->lua_state();
1225 detail::lua_reference ref
;
1227 return luabind::object(L
, ref
, true/*luabind::object::reference()*/);
1231 // *************************************
1232 // PROXY ARRAY OBJECT
1234 #if !defined(BOOST_MSVC) || (defined(BOOST_MSVC) && (BOOST_MSVC > 1300))
1236 inline object
proxy_array_object::raw_at(const T
& key
)
1237 LUABIND_PROXY_ARRAY_RAW_AT_BODY
1240 inline object
proxy_array_object::at(const T
& key
)
1241 LUABIND_PROXY_ARRAY_AT_BODY
1244 #undef LUABIND_PROXY_ARRAY_AT_BODY
1245 #undef LUABIND_PROXY_ARRAY_RAW_AT_BODY
1247 inline lua_State
* proxy_array_object::lua_state() const
1249 return m_obj
->lua_state();
1252 inline proxy_array_object::operator luabind::object()
1254 lua_State
* L
= m_obj
->lua_state();
1256 detail::lua_reference ref
;
1258 return luabind::object(L
, ref
, true/*luabind::object::reference()*/);
1262 // *************************************
1265 #if !defined(BOOST_MSVC) || (defined(BOOST_MSVC) && (BOOST_MSVC > 1300))
1267 inline object
proxy_raw_object::raw_at(const T
& key
)
1268 LUABIND_PROXY_RAW_AT_BODY
1271 inline object
proxy_raw_object::at(const T
& key
)
1272 LUABIND_PROXY_AT_BODY
1275 #undef LUABIND_PROXY_RAW_AT_BODY
1276 #undef LUABIND_PROXY_AT_BODY
1278 inline lua_State
* proxy_raw_object::lua_state() const
1280 return m_obj
->lua_state();
1283 inline proxy_raw_object::operator luabind::object()
1285 lua_State
* L
= lua_state();
1287 detail::lua_reference ref
;
1289 return luabind::object(L
, ref
, true/*luabind::object::reference()*/);
1293 // *************************************
1297 template<class Tuple
>
1298 proxy_caller
<Tuple
>::~proxy_caller()
1300 if (m_called
) return;
1303 lua_State
* L
= m_obj
->lua_state();
1306 push_args_from_tuple
<1>::apply(L
, m_args
);
1307 if (pcall(L
, boost::tuples::length
<Tuple
>::value
, 0))
1309 #ifndef LUABIND_NO_EXCEPTIONS
1310 throw luabind::error(L
);
1312 error_callback_fun e
= get_error_callback();
1315 assert(0 && "the lua function threw an error and exceptions are disabled."
1316 "if you want to handle this error use luabind::set_error_callback()");
1322 template<class Tuple
>
1323 proxy_caller
<Tuple
>::operator luabind::object()
1326 lua_State
* L
= m_obj
->lua_state();
1329 push_args_from_tuple
<1>::apply(L
, m_args
);
1330 if (pcall(L
, boost::tuples::length
<Tuple
>::value
, 1))
1332 #ifndef LUABIND_NO_EXCEPTIONS
1333 throw luabind::error(L
);
1335 error_callback_fun e
= get_error_callback();
1338 assert(0 && "the lua function threw an error and exceptions are disabled."
1339 "if you want to handle this error use luabind::set_error_callback()");
1343 detail::lua_reference ref
;
1345 return luabind::object(m_obj
->lua_state(), ref
, true/*luabind::object::reference()*/);
1350 inline detail::tuple_object_ref
object::operator,(const object
& rhs
) const
1352 return detail::tuple_object_ref(
1353 const_cast<object
*>(this), const_cast<object
*>(&rhs
));
1356 typedef detail::tuple_object function_
;
1358 #define LUABIND_DECLARE_OPERATOR(MACRO)\
1359 MACRO(object, object) \
1360 MACRO(object, detail::proxy_object) \
1361 MACRO(object, detail::proxy_array_object) \
1362 MACRO(object, detail::proxy_raw_object) \
1363 MACRO(detail::proxy_object, object) \
1364 MACRO(detail::proxy_object, detail::proxy_object) \
1365 MACRO(detail::proxy_object, detail::proxy_array_object) \
1366 MACRO(detail::proxy_object, detail::proxy_raw_object) \
1367 MACRO(detail::proxy_array_object, object) \
1368 MACRO(detail::proxy_array_object, detail::proxy_object) \
1369 MACRO(detail::proxy_array_object, detail::proxy_array_object) \
1370 MACRO(detail::proxy_array_object, detail::proxy_raw_object) \
1371 MACRO(detail::proxy_raw_object, object) \
1372 MACRO(detail::proxy_raw_object, detail::proxy_object) \
1373 MACRO(detail::proxy_raw_object, detail::proxy_array_object) \
1374 MACRO(detail::proxy_raw_object, detail::proxy_raw_object)
1377 #define LUABIND_EQUALITY_OPERATOR(lhs, rhs) LUABIND_API bool operator==(const lhs&, const rhs&);
1378 LUABIND_DECLARE_OPERATOR(LUABIND_EQUALITY_OPERATOR
)
1379 #undef LUABIND_EQUALITY_OPERATOR
1381 #define LUABIND_LESSTHAN_OPERATOR(lhs, rhs) LUABIND_API bool operator<(const lhs&, const rhs&);
1382 LUABIND_DECLARE_OPERATOR(LUABIND_LESSTHAN_OPERATOR
)
1383 #undef LUABIND_LESSTHAN_OPERATOR
1385 #define LUABIND_LESSOREQUAL_OPERATOR(lhs_t, rhs_t) LUABIND_API bool operator<=(const lhs_t&, const rhs_t&);
1386 LUABIND_DECLARE_OPERATOR(LUABIND_LESSOREQUAL_OPERATOR
)
1387 #undef LUABIND_LESSOREQUAL_OPERATOR
1389 #define LUABIND_INEQUALITY_OPERATOR(lhs_t, rhs_t)\
1390 inline bool operator!=(const rhs_t& rhs, const lhs_t& lhs) \
1392 return !(rhs == lhs); \
1395 LUABIND_DECLARE_OPERATOR(LUABIND_INEQUALITY_OPERATOR
)
1397 #undef LUABIND_INEQUALITY_OPERATOR
1399 #define LUABIND_GREATEROREQUAL_OPERATOR(lhs_t, rhs_t)\
1400 inline bool operator>=(const rhs_t& rhs, const lhs_t& lhs) \
1402 return !(rhs < lhs); \
1405 LUABIND_DECLARE_OPERATOR(LUABIND_GREATEROREQUAL_OPERATOR
)
1407 #undef LUABIND_GREATEROREQUAL_OPERATOR
1409 #define LUABIND_GREATERTHAN_OPERATOR(lhs_t, rhs_t)\
1410 inline bool operator>(const lhs_t& lhs, const rhs_t& rhs) \
1412 return !(lhs <= rhs); \
1415 LUABIND_DECLARE_OPERATOR(LUABIND_GREATERTHAN_OPERATOR
)
1416 #undef LUABIND_GREATERTHAN_OPERATOR
1418 #undef LUABIND_DECLARE_OPERATOR
1425 #define LUABIND_DEFINE_SWAP(t1,t2)\
1426 inline void swap(t1 lhs, t2 rhs)\
1428 assert((lhs.lua_state() == rhs.lua_state()) && "you cannot swap objects from different lua states");\
1435 inline void swap(luabind::object
& lhs
, luabind::object
& rhs
)
1440 // object against all other
1441 LUABIND_DEFINE_SWAP(luabind::object
&, const luabind::detail::proxy_object
&)
1442 LUABIND_DEFINE_SWAP(luabind::object
&, const luabind::detail::proxy_raw_object
&)
1443 LUABIND_DEFINE_SWAP(luabind::object
&, const luabind::detail::proxy_array_object
&)
1444 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_object
&, luabind::object
&)
1445 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_raw_object
&, luabind::object
&)
1446 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_array_object
&, luabind::object
&)
1448 // proxy_object against all other
1449 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_object
&, const luabind::detail::proxy_object
&)
1450 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_object
&, const luabind::detail::proxy_raw_object
&)
1451 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_object
&, const luabind::detail::proxy_array_object
&)
1452 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_raw_object
&, const luabind::detail::proxy_object
&)
1453 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_array_object
&, const luabind::detail::proxy_object
&)
1455 // proxy_raw_object against all other
1456 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_raw_object
&, const luabind::detail::proxy_raw_object
&)
1457 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_raw_object
&, const luabind::detail::proxy_array_object
&)
1458 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_array_object
&, const luabind::detail::proxy_raw_object
&)
1460 // proxy_array_object against all other
1461 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_array_object
&, const luabind::detail::proxy_array_object
&)
1463 #undef LUABIND_DEFINE_SWAP
1467 #endif // LUABIND_OBJECT_HPP_INCLUDED
1469 #elif BOOST_PP_ITERATION_FLAGS() == 1
1471 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
1472 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n
1474 #if BOOST_PP_ITERATION() > 0
1475 template<BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
1477 detail::proxy_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
1478 operator()(BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_OPERATOR_PARAMS
, _
)) const
1480 typedef boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> tuple_t
;
1481 #if BOOST_PP_ITERATION() == 0
1484 tuple_t
args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a
));
1486 return detail::proxy_caller
<tuple_t
>(const_cast<luabind::object
*>(this), args
);
1489 #undef LUABIND_OPERATOR_PARAMS
1490 #undef LUABIND_TUPLE_PARAMS