Skip to content

Commit

Permalink
Add support for conversion between Lua and rvalue references.
Browse files Browse the repository at this point in the history
With C++11, boost::make_shared and std::make_shared use rvalue
references to forward constructor arguments without copying.

If the macro BOOST_HAS_RVALUE_REFS is defined, let the type trait
is_nonconst_reference evaluate to true only for lvalue references.

Use value_converter for rvalue reference types. Add specialisations
for rvalue references of default_converter of number types, bool,
std::string and boost::shared_ptr.

Note that support of rvalue references requires Boost ≥ 1.44.0.

Conflicts:
	luabind/detail/decorate_type.hpp
	luabind/detail/typetraits.hpp
  • Loading branch information
Peter Colberg authored and Oberon00 committed Jun 30, 2013
1 parent ca8ce7b commit 950e1e3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
12 changes: 12 additions & 0 deletions luabind/detail/decorate_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ namespace luabind { namespace detail
template<class T>
by_const_reference<T> decorated_type<const T&>::t;

#ifdef BOOST_HAS_RVALUE_REFS
template<class T>
struct decorated_type<T&&>
{
static by_value<T> t;
static inline by_value<T>& get() { return /*by_value<T>()*/t; }
};

template<class T>
by_value<T> decorated_type<T&&>::t;
#endif

#define LUABIND_DECORATE_TYPE(t) luabind::detail::decorated_type<t>::get()

#else
Expand Down
56 changes: 56 additions & 0 deletions luabind/detail/policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,46 @@ lua_Number as_lua_number(T v)
return static_cast<lua_Number>(v);
}

#ifdef BOOST_HAS_RVALUE_REFS

# define LUABIND_NUMBER_CONVERTER(type, kind) \
template <> \
struct default_converter<type> \
: native_converter_base<type> \
{ \
int compute_score(lua_State* L, int index) \
{ \
return lua_type(L, index) == LUA_TNUMBER ? 0 : -1; \
}; \
\
type from(lua_State* L, int index) \
{ \
return static_cast<type>(BOOST_PP_CAT(lua_to, kind)(L, index)); \
} \
\
void to(lua_State* L, type const& value) \
{ \
BOOST_PP_CAT(lua_push, kind)(L, BOOST_PP_CAT(as_lua_, kind)(value)); \
} \
}; \
\
template <> \
struct default_converter<type const> \
: default_converter<type> \
{}; \
\
template <> \
struct default_converter<type const&> \
: default_converter<type> \
{}; \
\
template <> \
struct default_converter<type&&> \
: default_converter<type> \
{};

#else

# define LUABIND_NUMBER_CONVERTER(type, kind) \
template <> \
struct default_converter<type> \
Expand Down Expand Up @@ -705,6 +745,8 @@ struct default_converter<type const&> \
: default_converter<type> \
{};

#endif

LUABIND_NUMBER_CONVERTER(char, integer)
LUABIND_NUMBER_CONVERTER(signed char, integer)
LUABIND_NUMBER_CONVERTER(unsigned char, integer)
Expand Down Expand Up @@ -752,6 +794,13 @@ struct default_converter<bool const&>
: default_converter<bool>
{};

#ifdef BOOST_HAS_RVALUE_REFS
template <>
struct default_converter<bool&&>
: default_converter<bool>
{};
#endif

template <>
struct default_converter<std::string>
: native_converter_base<std::string>
Expand Down Expand Up @@ -782,6 +831,13 @@ struct default_converter<std::string const&>
: default_converter<std::string>
{};

#ifdef BOOST_HAS_RVALUE_REFS
template <>
struct default_converter<std::string&&>
: default_converter<std::string>
{};
#endif

template <>
struct default_converter<char const*>
{
Expand Down
4 changes: 4 additions & 0 deletions luabind/detail/typetraits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ namespace luabind { namespace detail
{
enum
{
#ifdef BOOST_HAS_RVALUE_REFS
value = boost::is_lvalue_reference<T>::value && !is_const_reference<T>::value
#else
value = boost::is_reference<T>::value && !is_const_reference<T>::value
#endif
};
typedef boost::mpl::bool_<value> type;
};
Expand Down
7 changes: 7 additions & 0 deletions luabind/shared_ptr_converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ struct default_converter<boost::shared_ptr<T> const&>
: default_converter<boost::shared_ptr<T> >
{};

#ifdef BOOST_HAS_RVALUE_REFS
template <class T>
struct default_converter<boost::shared_ptr<T>&&>
: default_converter<boost::shared_ptr<T> >
{};
#endif

} // namespace luabind

#endif // LUABIND_SHARED_PTR_CONVERTER_090211_HPP

0 comments on commit 950e1e3

Please sign in to comment.