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
);
211 detail::proxy_object
operator[](const T
& key
) const;
213 proxy_object
& operator=(const object
& p
);
214 proxy_object
& operator=(const proxy_object
& p
);
215 proxy_object
& operator=(const proxy_raw_object
& p
);
216 proxy_object
& operator=(const proxy_array_object
& p
);
218 void swap(const proxy_object
& rhs
);
219 proxy_object
* operator->()
222 operator luabind::object();
227 detail::stack_pop
p(lua_state(), 1);
228 return lua_type(lua_state(), -1);
231 #define LUABIND_PROXY_RAW_AT_BODY \
233 lua_State* L = lua_state();\
235 detail::convert_to_lua(L, key);\
237 detail::lua_reference ref;\
240 return object(L, ref, true);\
243 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
245 inline object
raw_at(const T
& key
)
246 LUABIND_PROXY_RAW_AT_BODY
249 inline object
raw_at(const T
& key
);
252 #define LUABIND_PROXY_AT_BODY \
254 lua_State* L = lua_state();\
256 detail::convert_to_lua(L, key);\
257 lua_gettable(L, -2);\
258 detail::lua_reference ref;\
261 return object(L, ref, true);\
264 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
266 inline object
at(const T
& key
)
267 LUABIND_PROXY_AT_BODY
270 inline object
at(const T
& key
);
273 inline bool is_valid() const { return true; }
274 lua_State
* lua_state() const;
275 void pushvalue() const;
278 // this is a safe substitute for an implicit converter to bool
279 typedef void (proxy_object::*member_ptr
)() const;
280 operator member_ptr() const
282 if (is_valid()) return &proxy_object::dummy
;
288 void dummy() const {}
290 proxy_object(luabind::object
* o
, const lua_reference
& key
)
296 luabind::object
* m_obj
;
297 detail::lua_reference m_key
;
302 class LUABIND_API proxy_raw_object
304 friend class luabind::object
;
305 friend class luabind::detail::proxy_array_object
;
306 friend class luabind::detail::proxy_object
;
307 // template<class T> friend T luabind::object_cast(const proxy_object& obj);
311 proxy_raw_object
& operator=(const T
& val
)
313 //std::cout << "proxy assigment\n";
314 lua_State
* L
= m_obj
->m_state
;
316 detail::getref(L
, m_key_ref
);
317 detail::convert_to_lua(L
, val
);
324 template<class T
, class Policies
>
325 void assign(const T
& val
, const Policies
& p
)
327 //std::cout << "proxy assigment\n";
328 lua_State
* L
= m_obj
->m_state
;
330 detail::getref(L
, m_key_ref
);
331 detail::convert_to_lua_p(L
, val
, p
);
339 detail::proxy_object
operator[](const T
& key
) const;
341 proxy_raw_object
& operator=(const object
& p
);
342 proxy_raw_object
& operator=(const proxy_object
& p
);
343 proxy_raw_object
& operator=(const proxy_raw_object
& p
);
344 proxy_raw_object
& operator=(const proxy_array_object
& p
);
345 void swap(const proxy_raw_object
& rhs
);
346 proxy_raw_object
* operator->()
349 operator luabind::object();
354 detail::stack_pop
p(lua_state(), 1);
355 return lua_type(lua_state(), -1);
358 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
360 inline object
raw_at(const T
& key
)
361 LUABIND_PROXY_RAW_AT_BODY
364 inline object
raw_at(const T
& key
);
367 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
369 inline object
at(const T
& key
)
370 LUABIND_PROXY_AT_BODY
373 inline object
at(const T
& key
);
376 inline bool is_valid() const { return true; }
377 lua_State
* lua_state() const;
378 void pushvalue() const;
381 // this is a safe substitute for an implicit converter to bool
382 typedef void (proxy_raw_object::*member_ptr
)() const;
383 operator member_ptr() const
385 if (is_valid()) return &proxy_raw_object::dummy
;
392 void dummy() const {}
394 proxy_raw_object(luabind::object
* o
, const lua_reference
& key
)
400 luabind::object
* m_obj
;
401 detail::lua_reference m_key
;
406 class LUABIND_API proxy_array_object
408 friend class luabind::object
;
409 friend class luabind::detail::proxy_object
;
410 friend class luabind::detail::proxy_raw_object
;
411 // template<class T> friend T object_cast(const proxy_array_object& obj);
415 proxy_array_object
& operator=(const T
& val
)
417 //std::cout << "array proxy assigment\n";
418 lua_State
* L
= m_obj
->m_state
;
420 detail::convert_to_lua(L
, val
);
421 lua_rawseti(L
, -2, m_key
);
428 template<class T
, class Policies
>
429 void assign(const T
& val
, const Policies
& p
)
431 //std::cout << "proxy assigment\n";
432 lua_State
* L
= m_obj
->m_state
;
434 detail::convert_to_lua_p(L
, val
, p
);
435 lua_rawseti(L
, -2, m_key
);
442 detail::proxy_object
operator[](const T
& key
) const;
444 proxy_array_object
& operator=(const object
& p
);
445 proxy_array_object
& operator=(const proxy_object
& p
);
446 proxy_array_object
& operator=(const proxy_raw_object
& p
);
447 proxy_array_object
& operator=(const proxy_array_object
& p
);
448 void swap(const proxy_array_object
& rhs
);
450 proxy_array_object
* operator->()
452 operator luabind::object();
457 detail::stack_pop
p(lua_state(), 1);
458 return lua_type(lua_state(), -1);
461 #define LUABIND_PROXY_ARRAY_RAW_AT_BODY\
464 detail::convert_to_lua(m_state, key);\
465 lua_rawget(m_state, -2);\
468 lua_pop(m_state, 1);\
469 return object(m_state, ref, true);\
472 #define LUABIND_PROXY_ARRAY_AT_BODY\
475 detail::convert_to_lua(m_state, key);\
476 lua_gettable(m_state, -2);\
479 lua_pop(m_state, 1);\
480 return object(m_state, ref, true);\
483 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
485 inline object
at(const T
& key
)
486 LUABIND_PROXY_ARRAY_AT_BODY
489 inline object
at(const T
& key
);
493 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
495 inline object
raw_at(const T
& key
)
496 LUABIND_PROXY_ARRAY_RAW_AT_BODY
499 inline object
raw_at(const T
& key
);
503 inline detail::proxy_object operator[](const T& key) const
505 detail::convert_to_lua(m_state, key);
508 return detail::proxy_object(const_cast<object*>(this), ref);
511 inline bool is_valid() const { return true; }
512 lua_State
* lua_state() const;
513 void pushvalue() const;
516 // this is a safe substitute for an implicit converter to bool
517 typedef void (proxy_array_object::*member_ptr
)() const;
518 operator member_ptr() const
520 if (is_valid()) return &proxy_array_object::dummy
;
526 void dummy() const {}
528 proxy_array_object(luabind::object
* o
, int key
)
532 luabind::object
* m_obj
;
537 struct primitive_converter
;
539 struct tuple_object_ref
;
543 class LUABIND_API object
546 #if !(defined (BOOST_MSVC) && (BOOST_MSVC <= 1200))
549 friend T
object_cast(const object
& obj
);
551 friend struct detail::primitive_converter
;
555 friend object
get_globals(lua_State
*);
556 friend object
get_registry(lua_State
*);
557 friend object
newtable(lua_State
*);
558 friend class detail::proxy_object
;
559 friend class detail::proxy_array_object
;
560 friend class detail::proxy_raw_object
;
569 typedef std::forward_iterator_tag iterator_category
;
570 typedef luabind::object value_type
;
571 typedef value_type
& reference
;
572 typedef value_type
* pointer
;
573 typedef void difference_type
;
581 array_iterator(const array_iterator
& iter
)
589 array_iterator
& operator=(const array_iterator
& rhs
)
596 detail::proxy_array_object
operator*()
598 return m_obj
->make_array_proxy(m_key
);
601 detail::proxy_array_object
operator->()
603 return m_obj
->make_array_proxy(m_key
);
606 inline array_iterator
& operator++()
608 LUABIND_CHECK_STACK(m_obj
->lua_state());
612 // invalidate the iterator if we hit a nil element
613 lua_State
* L
= m_obj
->lua_state();
615 lua_rawgeti(L
, -1, m_key
);
616 if (lua_isnil(L
, -1)) m_key
= -1;
622 inline array_iterator
operator++(int)
624 LUABIND_CHECK_STACK(m_obj
->lua_state());
629 // invalidate the iterator if we hit a nil element
630 lua_State
* L
= m_obj
->lua_state();
632 lua_rawgeti(L
, -1, m_key
);
633 if (lua_isnil(L
, -1)) m_key
= -1;
636 return array_iterator(m_obj
, old_key
);
639 bool operator!=(const array_iterator
& rhs
) const
641 return m_obj
!= rhs
.m_obj
|| m_key
!= rhs
.m_key
;
646 array_iterator(object
* obj
, int key
)
666 typedef std::forward_iterator_tag iterator_category
;
667 typedef luabind::object value_type
;
668 typedef value_type
& reference
;
669 typedef value_type
* pointer
;
670 typedef void difference_type
;
677 iterator(const iterator
& iter
)
686 iterator
& operator=(const iterator
& rhs
)
700 detail::proxy_object
operator*()
702 return m_obj
->make_proxy(m_key
);
705 detail::proxy_object
operator->()
707 return m_obj
->make_proxy(m_key
);
710 iterator
& operator++()
712 lua_State
* L
= m_obj
->lua_state();
714 int n
= lua_gettop(L
);
719 if (lua_next(L
, -2) != 0)
732 assert(n
== lua_gettop(L
));
736 bool operator!=(const iterator
& rhs
) const
738 if (m_obj
!= rhs
.m_obj
) return true;
739 if (m_obj
== 0) return false;
740 if (m_obj
->lua_state() != rhs
.m_obj
->lua_state()) return true;
741 if (m_key
.is_valid() != rhs
.m_key
.is_valid()) return true;
743 // TODO: fix this. add a real equality test of the keys
751 iterator(luabind::object
* obj
, detail::lua_reference
const& key
)
758 detail::lua_reference m_key
;
769 typedef std::forward_iterator_tag iterator_category
;
770 typedef luabind::object value_type
;
771 typedef value_type
& reference
;
772 typedef value_type
* pointer
;
773 typedef void difference_type
;
780 raw_iterator(const raw_iterator
& iter
)
790 raw_iterator
& operator=(const raw_iterator
& rhs
)
792 //std::cout << "===\n";
805 detail::proxy_raw_object
operator*()
807 return m_obj
->make_raw_proxy(m_key
);
810 detail::proxy_raw_object
operator->()
812 return m_obj
->make_raw_proxy(m_key
);
815 raw_iterator
& operator++()
817 lua_State
* L
= m_obj
->lua_state();
821 if (lua_next(L
, -2) != 0)
839 bool operator!=(const raw_iterator
& rhs
) const
841 if (m_obj
!= rhs
.m_obj
) return true;
842 if (m_obj
== 0) return false;
843 if (m_obj
->lua_state() != rhs
.m_obj
->lua_state()) return true;
844 if (m_key
.is_valid() != rhs
.m_key
.is_valid()) return true;
846 // TODO: fix this. add a real equality test of the keys
852 raw_iterator(object
* obj
, detail::lua_reference
const& key
)
858 detail::lua_reference m_key
;
871 explicit object(lua_State
* L
)
877 object(lua_State
* L
, const T
& val
)
883 object(const object
& o
)
886 o
.m_ref
.get(m_state
);
893 inline bool is_valid() const { return m_ref
.is_valid(); }
895 // this is a safe substitute for an implicit converter to bool
896 typedef void (object::*member_ptr
)() const;
897 operator member_ptr() const
899 if (is_valid()) return &object::dummy
;
906 detail::stack_pop
p(lua_state(), 1);
907 return lua_type(lua_state(), -1);
910 inline iterator
begin() const
913 lua_pushnil(m_state
);
914 lua_next(m_state
, -2);
916 detail::lua_reference r
;
918 iterator
i(const_cast<object
*>(this), r
);
923 inline iterator
end() const
925 return iterator(0, detail::lua_reference());
928 inline array_iterator
abegin() const
930 return array_iterator(const_cast<object
*>(this), 1);
933 inline array_iterator
aend() const
935 return array_iterator(const_cast<object
*>(this), -1);
938 raw_iterator
raw_begin() const
941 lua_pushnil(m_state
);
942 lua_next(m_state
, -2);
944 detail::lua_reference r
;
946 raw_iterator
i(const_cast<object
*>(this), r
);
951 raw_iterator
raw_end() const
953 return raw_iterator(0, detail::lua_reference());
956 inline void set() const
958 // you are trying to access an invalid object
959 assert((m_state
!= 0) && "you are trying to access an invalid (uninitialized) object");
962 m_ref
.replace(m_state
);
964 inline lua_State
* lua_state() const { return m_state
; }
965 inline void pushvalue() const
967 // you are trying to dereference an invalid object
968 assert((m_ref
.is_valid()) && "you are trying to access an invalid (uninitialized) object");
969 assert((m_state
!= 0) && "internal error, please report");
974 void swap(object
& rhs
);
977 inline object
raw_at(const T
& key
)
979 lua_State
* L
= lua_state();
981 detail::convert_to_lua(L
, key
);
983 detail::lua_reference ref
;
986 return object(L
, ref
, true);
990 inline object
at(const T
& key
)
992 LUABIND_CHECK_STACK(m_state
);
994 lua_State
* L
= lua_state();
996 detail::convert_to_lua(L
, key
);
998 detail::lua_reference ref
;
1001 return object(L
, ref
, true);
1005 inline detail::proxy_object
operator[](const T
& key
) const
1007 LUABIND_CHECK_STACK(m_state
);
1009 detail::convert_to_lua(m_state
, key
);
1010 detail::lua_reference ref
;
1012 return detail::proxy_object(const_cast<object
*>(this), ref
);
1017 // *****************************
1021 object
& operator=(const T
& val
) const
1023 assert((m_state
!= 0) && "you cannot assign a non-lua value to an uninitialized object");
1024 // you cannot assign a non-lua value to an uninitialized object
1026 detail::convert_to_lua(m_state
, val
);
1028 return const_cast<luabind::object
&>(*this);
1031 object
& operator=(const object
& o
) const;
1032 object
& operator=(const detail::proxy_object
& o
) const;
1033 object
& operator=(const detail::proxy_raw_object
& o
) const;
1034 object
& operator=(const detail::proxy_array_object
& o
) const;
1036 template<class T
, class Policies
>
1037 void assign(const T
& val
, const Policies
& p
) const
1039 assert((m_state
!= 0) && "you cannot assign a non-lua value to an uninitialized object");
1040 // you cannot assign a non-lua value to an uninitialized object
1042 detail::convert_to_lua_p(m_state
, val
, p
);
1046 // const overload should return a tuple_object..?
1047 inline detail::tuple_object_ref
operator,(const object
& rhs
) const;
1049 // *****************************
1052 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/object.hpp>, 1))
1053 #include BOOST_PP_ITERATE()
1057 inline detail::proxy_object
make_proxy(detail::lua_reference
const& key
)
1059 return detail::proxy_object(this, key
);
1062 inline detail::proxy_raw_object
make_raw_proxy(detail::lua_reference
const& key
)
1064 return detail::proxy_raw_object(this, key
);
1067 inline detail::proxy_array_object
make_array_proxy(int key
)
1069 return detail::proxy_array_object(this, key
);
1072 // TODO: it's not possible to make object friend with wrapped_constructor_helper::apply (since
1073 // it's an inner class), that's why this interface is public
1076 object(lua_State
* L
, detail::lua_reference
const& ref
, bool/*, reference*/)
1084 void dummy() const {}
1086 void allocate_slot() const
1088 if (!m_ref
.is_valid())
1090 lua_pushboolean(m_state
, 0);
1095 mutable lua_State
* m_state
;
1096 mutable detail::lua_reference m_ref
;
1100 // *************************************
1103 inline void object::swap(object
& rhs
)
1105 // you cannot swap objects from different lua states
1106 assert((lua_state() == rhs
.lua_state()) && "you cannot swap objects from different lua states");
1107 m_ref
.swap(rhs
.m_ref
);
1110 inline object
object::iterator::key() const
1112 lua_State
* L
= m_obj
->lua_state();
1113 return object(L
, m_key
, true);
1116 inline object
object::raw_iterator::key() const
1118 lua_State
* L
= m_obj
->lua_state();
1119 return object(L
, m_key
, true);
1124 // tuple object ----------------------------------------------
1126 struct tuple_object
;
1128 struct tuple_object_ref
1130 tuple_object_ref(object
* a
, object
* b
)
1132 { refs
[0] = a
; refs
[1] = b
; }
1134 tuple_object_ref
& operator,(const object
& x
)
1135 { refs
[n
++] = const_cast<object
*>(&x
); return *this; }
1142 assign_into(tuple_object_ref
& to
, const T
& val
)
1146 if (n
>= target
->n
) return;
1147 *target
->refs
[n
++] = val
;
1151 assign_into
& operator,(const T
& val
)
1153 if (n
>= target
->n
) return *this;
1154 *target
->refs
[n
++] = val
;
1158 tuple_object_ref
* target
;
1163 assign_into
operator=(const T
& val
)
1164 { return assign_into(*this, val
); }
1166 tuple_object_ref(const tuple_object_ref
&);
1167 assign_into
operator=(const tuple_object_ref
& x
)
1169 for (std::size_t i
= 0; i
< n
&& i
< x
.n
; ++i
)
1170 *refs
[i
] = *x
.refs
[i
];
1171 return assign_into();
1174 inline assign_into
operator=(const tuple_object
&);
1182 tuple_object(const object
& x
)
1186 tuple_object(const tuple_object_ref
& x
)
1188 for (std::size_t i
= 0; i
< x
.n
; ++i
)
1189 objs
[i
] = *x
.refs
[i
];
1196 inline tuple_object_ref::assign_into
tuple_object_ref::operator=(const tuple_object
& x
)
1198 for (std::size_t i
= 0; i
< n
&& i
< x
.n
; ++i
)
1199 *refs
[i
] = x
.objs
[i
];
1200 return assign_into();
1203 // *************************************
1206 #if !defined(BOOST_MSVC) || (defined(BOOST_MSVC) && (BOOST_MSVC > 1300))
1207 template<class Tuple
>
1208 template<class Policies
>
1209 luabind::object proxy_caller
<Tuple
>::operator[](const Policies
& p
)
1212 lua_State
* L
= m_obj
->lua_state();
1214 detail::push_args_from_tuple
<1>::apply(L
, m_args
, p
);
1215 if (pcall(L
, boost::tuples::length
<Tuple
>::value
, 1))
1217 #ifndef LUABIND_NO_EXCEPTIONS
1220 error_callback_fun e
= get_error_callback();
1223 assert(0 && "the lua function threw an error and exceptions are disabled."
1224 "if you want to handle this error use luabind::set_error_callback()");
1228 detail::lua_reference ref
;
1230 return luabind::object(m_obj
->lua_state(), ref
, true/*luabind::object::reference()*/);
1233 // *************************************
1236 #if !defined(BOOST_MSVC) || (defined(BOOST_MSVC) && (BOOST_MSVC > 1300))
1238 inline object
proxy_object::raw_at(const T
& key
)
1239 LUABIND_PROXY_RAW_AT_BODY
1242 inline object
proxy_object::at(const T
& key
)
1243 LUABIND_PROXY_AT_BODY
1246 inline lua_State
* proxy_object::lua_state() const
1248 return m_obj
->lua_state();
1251 inline proxy_object::operator luabind::object()
1253 lua_State
* L
= m_obj
->lua_state();
1255 detail::lua_reference ref
;
1257 return luabind::object(L
, ref
, true/*luabind::object::reference()*/);
1261 // *************************************
1262 // PROXY ARRAY OBJECT
1264 #if !defined(BOOST_MSVC) || (defined(BOOST_MSVC) && (BOOST_MSVC > 1300))
1266 inline object
proxy_array_object::raw_at(const T
& key
)
1267 LUABIND_PROXY_ARRAY_RAW_AT_BODY
1270 inline object
proxy_array_object::at(const T
& key
)
1271 LUABIND_PROXY_ARRAY_AT_BODY
1274 #undef LUABIND_PROXY_ARRAY_AT_BODY
1275 #undef LUABIND_PROXY_ARRAY_RAW_AT_BODY
1277 inline lua_State
* proxy_array_object::lua_state() const
1279 return m_obj
->lua_state();
1282 inline proxy_array_object::operator luabind::object()
1284 lua_State
* L
= m_obj
->lua_state();
1286 detail::lua_reference ref
;
1288 return luabind::object(L
, ref
, true/*luabind::object::reference()*/);
1292 // *************************************
1295 #if !defined(BOOST_MSVC) || (defined(BOOST_MSVC) && (BOOST_MSVC > 1300))
1297 inline object
proxy_raw_object::raw_at(const T
& key
)
1298 LUABIND_PROXY_RAW_AT_BODY
1301 inline object
proxy_raw_object::at(const T
& key
)
1302 LUABIND_PROXY_AT_BODY
1305 #undef LUABIND_PROXY_RAW_AT_BODY
1306 #undef LUABIND_PROXY_AT_BODY
1308 inline lua_State
* proxy_raw_object::lua_state() const
1310 return m_obj
->lua_state();
1313 inline proxy_raw_object::operator luabind::object()
1315 lua_State
* L
= lua_state();
1317 detail::lua_reference ref
;
1319 return luabind::object(L
, ref
, true/*luabind::object::reference()*/);
1323 // *************************************
1327 template<class Tuple
>
1328 proxy_caller
<Tuple
>::~proxy_caller()
1330 if (m_called
) return;
1333 lua_State
* L
= m_obj
->lua_state();
1336 push_args_from_tuple
<1>::apply(L
, m_args
);
1337 if (pcall(L
, boost::tuples::length
<Tuple
>::value
, 0))
1339 #ifndef LUABIND_NO_EXCEPTIONS
1340 throw luabind::error(L
);
1342 error_callback_fun e
= get_error_callback();
1345 assert(0 && "the lua function threw an error and exceptions are disabled."
1346 "if you want to handle this error use luabind::set_error_callback()");
1352 template<class Tuple
>
1353 proxy_caller
<Tuple
>::operator luabind::object()
1356 lua_State
* L
= m_obj
->lua_state();
1359 push_args_from_tuple
<1>::apply(L
, m_args
);
1360 if (pcall(L
, boost::tuples::length
<Tuple
>::value
, 1))
1362 #ifndef LUABIND_NO_EXCEPTIONS
1363 throw luabind::error(L
);
1365 error_callback_fun e
= get_error_callback();
1368 assert(0 && "the lua function threw an error and exceptions are disabled."
1369 "if you want to handle this error use luabind::set_error_callback()");
1373 detail::lua_reference ref
;
1375 return luabind::object(m_obj
->lua_state(), ref
, true/*luabind::object::reference()*/);
1380 inline detail::tuple_object_ref
object::operator,(const object
& rhs
) const
1382 return detail::tuple_object_ref(
1383 const_cast<object
*>(this), const_cast<object
*>(&rhs
));
1386 typedef detail::tuple_object function_
;
1388 #define LUABIND_DECLARE_OPERATOR(MACRO)\
1389 MACRO(object, object) \
1390 MACRO(object, detail::proxy_object) \
1391 MACRO(object, detail::proxy_array_object) \
1392 MACRO(object, detail::proxy_raw_object) \
1393 MACRO(detail::proxy_object, object) \
1394 MACRO(detail::proxy_object, detail::proxy_object) \
1395 MACRO(detail::proxy_object, detail::proxy_array_object) \
1396 MACRO(detail::proxy_object, detail::proxy_raw_object) \
1397 MACRO(detail::proxy_array_object, object) \
1398 MACRO(detail::proxy_array_object, detail::proxy_object) \
1399 MACRO(detail::proxy_array_object, detail::proxy_array_object) \
1400 MACRO(detail::proxy_array_object, detail::proxy_raw_object) \
1401 MACRO(detail::proxy_raw_object, object) \
1402 MACRO(detail::proxy_raw_object, detail::proxy_object) \
1403 MACRO(detail::proxy_raw_object, detail::proxy_array_object) \
1404 MACRO(detail::proxy_raw_object, detail::proxy_raw_object)
1407 #define LUABIND_EQUALITY_OPERATOR(lhs, rhs) LUABIND_API bool operator==(const lhs&, const rhs&);
1408 LUABIND_DECLARE_OPERATOR(LUABIND_EQUALITY_OPERATOR
)
1409 #undef LUABIND_EQUALITY_OPERATOR
1411 #define LUABIND_LESSTHAN_OPERATOR(lhs, rhs) LUABIND_API bool operator<(const lhs&, const rhs&);
1412 LUABIND_DECLARE_OPERATOR(LUABIND_LESSTHAN_OPERATOR
)
1413 #undef LUABIND_LESSTHAN_OPERATOR
1415 #define LUABIND_LESSOREQUAL_OPERATOR(lhs_t, rhs_t) LUABIND_API bool operator<=(const lhs_t&, const rhs_t&);
1416 LUABIND_DECLARE_OPERATOR(LUABIND_LESSOREQUAL_OPERATOR
)
1417 #undef LUABIND_LESSOREQUAL_OPERATOR
1419 #define LUABIND_INEQUALITY_OPERATOR(lhs_t, rhs_t)\
1420 inline bool operator!=(const rhs_t& rhs, const lhs_t& lhs) \
1422 return !(rhs == lhs); \
1425 LUABIND_DECLARE_OPERATOR(LUABIND_INEQUALITY_OPERATOR
)
1427 #undef LUABIND_INEQUALITY_OPERATOR
1429 #define LUABIND_GREATEROREQUAL_OPERATOR(lhs_t, rhs_t)\
1430 inline bool operator>=(const rhs_t& rhs, const lhs_t& lhs) \
1432 return !(rhs < lhs); \
1435 LUABIND_DECLARE_OPERATOR(LUABIND_GREATEROREQUAL_OPERATOR
)
1437 #undef LUABIND_GREATEROREQUAL_OPERATOR
1439 #define LUABIND_GREATERTHAN_OPERATOR(lhs_t, rhs_t)\
1440 inline bool operator>(const lhs_t& lhs, const rhs_t& rhs) \
1442 return !(lhs <= rhs); \
1445 LUABIND_DECLARE_OPERATOR(LUABIND_GREATERTHAN_OPERATOR
)
1446 #undef LUABIND_GREATERTHAN_OPERATOR
1448 #undef LUABIND_DECLARE_OPERATOR
1455 #define LUABIND_DEFINE_SWAP(t1,t2)\
1456 inline void swap(t1 lhs, t2 rhs)\
1458 assert((lhs.lua_state() == rhs.lua_state()) && "you cannot swap objects from different lua states");\
1465 inline void swap(luabind::object
& lhs
, luabind::object
& rhs
)
1470 // object against all other
1471 LUABIND_DEFINE_SWAP(luabind::object
&, const luabind::detail::proxy_object
&)
1472 LUABIND_DEFINE_SWAP(luabind::object
&, const luabind::detail::proxy_raw_object
&)
1473 LUABIND_DEFINE_SWAP(luabind::object
&, const luabind::detail::proxy_array_object
&)
1474 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_object
&, luabind::object
&)
1475 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_raw_object
&, luabind::object
&)
1476 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_array_object
&, luabind::object
&)
1478 // proxy_object against all other
1479 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_object
&, const luabind::detail::proxy_object
&)
1480 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_object
&, const luabind::detail::proxy_raw_object
&)
1481 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_object
&, const luabind::detail::proxy_array_object
&)
1482 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_raw_object
&, const luabind::detail::proxy_object
&)
1483 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_array_object
&, const luabind::detail::proxy_object
&)
1485 // proxy_raw_object against all other
1486 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_raw_object
&, const luabind::detail::proxy_raw_object
&)
1487 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_raw_object
&, const luabind::detail::proxy_array_object
&)
1488 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_array_object
&, const luabind::detail::proxy_raw_object
&)
1490 // proxy_array_object against all other
1491 LUABIND_DEFINE_SWAP(const luabind::detail::proxy_array_object
&, const luabind::detail::proxy_array_object
&)
1493 #undef LUABIND_DEFINE_SWAP
1497 #endif // LUABIND_OBJECT_HPP_INCLUDED
1499 #elif BOOST_PP_ITERATION_FLAGS() == 1
1501 #define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
1502 #define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n
1504 #if BOOST_PP_ITERATION() > 0
1505 template<BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
1507 detail::proxy_caller
<boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> >
1508 operator()(BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_OPERATOR_PARAMS
, _
)) const
1510 typedef boost::tuples::tuple
<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS
, _
)> tuple_t
;
1511 #if BOOST_PP_ITERATION() == 0
1514 tuple_t
args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a
));
1516 return detail::proxy_caller
<tuple_t
>(const_cast<luabind::object
*>(this), args
);
1519 #undef LUABIND_OPERATOR_PARAMS
1520 #undef LUABIND_TUPLE_PARAMS