in_place will be the death of me

This commit is contained in:
ThePhD 2017-07-09 13:44:28 -04:00
parent 930f82cd5b
commit afaecbad2c
5 changed files with 18 additions and 13 deletions

View File

@ -12,7 +12,7 @@ turn an iterable argument into a multiple-return type
as_returns_t<T> as_returns( T&& );
This allows you to wrap up a source that has ``begin`` and ``end`` iterator-returning functions on it and return it as multiple results into Lua. To have more control over the returns, use :doc:`sol::variadic_results<variadic_results>`
This allows you to wrap up a source that has ``begin`` and ``end`` iterator-returning functions on it and return it as multiple results into Lua. To have more control over the returns, use :doc:`sol::variadic_results<variadic_results>`.
.. code-block:: cpp

View File

@ -7,5 +7,6 @@ push multiple disparate arguments into lua
struct variadic_results : std::vector<object> { ... };
template <typename T>
as_args_t<T> as_args( T&& );
This type allows someone to prepare multiple returns before returning them into Lua. It derives from ``std::vector``, so it can be used exactly like that, and objects can be added using the various constructors and functions relating to :doc:`sol::object<object>`. You can see it and other return-type helpers in action `here`_.
.. _here: https://github.com/ThePhD/sol2/blob/develop/examples/multi_results.cpp

View File

@ -30,6 +30,7 @@ namespace sol {
#ifdef SOL_CXX17_FEATURES
using in_place_t = std::in_place_t;
constexpr std::in_place_t in_place{};
constexpr std::in_place_t in_place_of{};
template <typename T> using in_place_type_t = std::in_place_type_t<T>;
template <typename T>
@ -40,7 +41,7 @@ namespace sol {
constexpr in_place_index_t<I> in_place_index{};
#else
namespace detail {
struct in_place_of {};
struct in_place_of_tag {};
template <std::size_t I>
struct in_place_of_i {};
template <typename T>
@ -49,18 +50,19 @@ namespace sol {
struct in_place_tag { constexpr in_place_tag() = default; };
constexpr inline in_place_tag in_place (detail::in_place_of) { return in_place_tag(); }
constexpr inline in_place_tag in_place (detail::in_place_of_tag) { return in_place_tag(); }
template <typename T>
constexpr inline in_place_tag in_place (detail::in_place_of_t<T>) { return in_place_tag(); }
template <std::size_t I>
constexpr inline in_place_tag in_place (detail::in_place_of_i<I>) { return in_place_tag(); }
constexpr inline in_place_tag in_place_of(detail::in_place_of_tag) { return in_place_tag(); }
template <typename T>
constexpr inline in_place_tag in_place_type (detail::in_place_of_t<T>) { return in_place_tag(); }
template <std::size_t I>
constexpr inline in_place_tag in_place_index (detail::in_place_of_i<I>) { return in_place_tag(); }
using in_place_t = in_place_tag(&)(detail::in_place_of);
using in_place_t = in_place_tag(&)(detail::in_place_of_tag);
template <typename T>
using in_place_type_t = in_place_tag(&)(detail::in_place_of_t<T>);
template <std::size_t I>

View File

@ -80,9 +80,11 @@ namespace sol {
basic_object(lua_State* L, int index = -1) noexcept : base_t(L, index) {}
basic_object(lua_State* L, ref_index index) noexcept : base_t(L, index) {}
template <typename T, typename... Args>
basic_object(lua_State* L, in_place_type_t<T>, Args&&... args) noexcept : basic_object(std::integral_constant<bool, !std::is_base_of<stack_reference, base_t>::value>(), L, -stack::push<T>(L, std::forward<Args>(args)...)) {}
basic_object(lua_State* L, in_place_type_t<T>, Args&&... args) noexcept
: basic_object(std::integral_constant<bool, !std::is_base_of<stack_reference, base_t>::value>(), L, -stack::push<T>(L, std::forward<Args>(args)...)) {}
template <typename T, typename... Args>
basic_object(lua_State* L, in_place_t, T&& arg, Args&&... args) noexcept : basic_object(L, in_place_type<T>, std::forward<T>(arg), std::forward<Args>(args)...) {}
basic_object(lua_State* L, in_place_t, T&& arg, Args&&... args) noexcept
: basic_object(L, in_place_type<T>, std::forward<T>(arg), std::forward<Args>(args)...) {}
basic_object& operator=(const basic_object&) = default;
basic_object& operator=(basic_object&&) = default;
basic_object& operator=(const base_type& b) { base_t::operator=(b); return *this; }

View File

@ -141,17 +141,17 @@ TEST_CASE("variadics/variadic_results", "returning a variable amount of argument
lua.set_function("f", [](sol::this_state ts, bool maybe) {
if (maybe) {
sol::variadic_results vr;
vr.emplace_back(ts, sol::in_place<int>, 1);
vr.push_back({ ts, sol::in_place, 1 });
vr.push_back({ ts, sol::in_place, 2 });
vr.insert(vr.cend(), { ts, sol::in_place, 3 });
return vr;
}
else {
sol::variadic_results vr;
vr.emplace_back(ts, sol::in_place<const char*>, "bark");
vr.push_back({ ts, sol::in_place<const char*>, "woof" });
vr.insert(vr.cend(), { ts, sol::in_place<const char*>, "arf" });
vr.emplace_back(ts, sol::in_place<const char*>, "borf");
vr.push_back({ ts, sol::in_place, "bark" });
vr.push_back({ ts, sol::in_place, "woof" });
vr.insert(vr.cend(), { ts, sol::in_place, "arf" });
vr.push_back({ ts, sol::in_place, "borf" });
return vr;
}
});