From b97169f26b66315771d54cd0263bf58b6f743889 Mon Sep 17 00:00:00 2001 From: Damian Jarek Date: Wed, 21 Nov 2018 01:38:09 +0100 Subject: [PATCH] Add detail::lean_tuple Provides a subset of std::tuple functionality, for internal use to reduce the resources required during compilation. Does not support reference-like semantics on assignment. Signed-off-by: Damian Jarek --- .../boost/beast/core/detail/lean_tuple.hpp | 87 +++++++++++++++++++ test/beast/core/CMakeLists.txt | 1 + test/beast/core/Jamfile | 1 + test/beast/core/detail/lean_tuple.cpp | 54 ++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 include/boost/beast/core/detail/lean_tuple.hpp create mode 100644 test/beast/core/detail/lean_tuple.cpp diff --git a/include/boost/beast/core/detail/lean_tuple.hpp b/include/boost/beast/core/detail/lean_tuple.hpp new file mode 100644 index 00000000..64cc91a6 --- /dev/null +++ b/include/boost/beast/core/detail/lean_tuple.hpp @@ -0,0 +1,87 @@ +// +// Copyright (c) 2018 Damian Jarek (damian dot jarek93 at gmail dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// Official repository: https://github.com/boostorg/beast +// + +#ifndef BOOST_BEAST_DETAIL_LEAN_TUPLE_HPP +#define BOOST_BEAST_DETAIL_LEAN_TUPLE_HPP + +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace beast { +namespace detail { + +template +struct lean_tuple_element +{ + T t; +}; + +template +struct lean_tuple_impl; + +template +struct lean_tuple_impl< + boost::mp11::index_sequence, Ts...> + : lean_tuple_element... +{ + template + explicit lean_tuple_impl(Us&&... us) + : lean_tuple_element{std::forward(us)}... + { + } +}; + +template +struct lean_tuple : lean_tuple_impl< + boost::mp11::index_sequence_for, Ts...> +{ + template + explicit lean_tuple(Us&&... us) + : lean_tuple_impl< + boost::mp11::index_sequence_for, Ts...>{ + std::forward(us)...} + { + } +}; + +template +T& +get(lean_tuple_element& te) +{ + return te.t; +} + +template +T const& +get(lean_tuple_element const& te) +{ + return te.t; +} + +template +T&& +get(lean_tuple_element&& te) +{ + return std::move(te.t); +} + +template +using tuple_element_t = typename boost::copy_cv< + mp11::mp_at_c::type, I>, T>::type; + +} // detail +} // beast +} // boost + +#endif diff --git a/test/beast/core/CMakeLists.txt b/test/beast/core/CMakeLists.txt index 95f0171c..74435db1 100644 --- a/test/beast/core/CMakeLists.txt +++ b/test/beast/core/CMakeLists.txt @@ -46,6 +46,7 @@ add_executable (tests-beast-core type_traits.cpp detail/base64.cpp detail/clamp.cpp + detail/lean_tuple.cpp detail/sha1.cpp detail/variant.cpp detail/varint.cpp diff --git a/test/beast/core/Jamfile b/test/beast/core/Jamfile index 16d88133..704355fb 100644 --- a/test/beast/core/Jamfile +++ b/test/beast/core/Jamfile @@ -36,6 +36,7 @@ local SOURCES = type_traits.cpp detail/base64.cpp detail/clamp.cpp + detail/lean_tuple.cpp detail/sha1.cpp detail/variant.cpp detail/varint.cpp diff --git a/test/beast/core/detail/lean_tuple.cpp b/test/beast/core/detail/lean_tuple.cpp new file mode 100644 index 00000000..95c394e3 --- /dev/null +++ b/test/beast/core/detail/lean_tuple.cpp @@ -0,0 +1,54 @@ +// +// Copyright (c) 2018 Damian Jarek (damian dot jarek93 at gmail dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// Official repository: https://github.com/boostorg/beast +// + +// Test that header file is self-contained. +#include + +#include + +namespace boost { +namespace beast { +namespace detail { + +class lean_tuple_test : public beast::unit_test::suite +{ +public: + void + run() + { + struct explicit_constructible + { + explicit_constructible(std::nullptr_t) + : i_(0) + { + } + + explicit explicit_constructible(int i) + : i_(i) + { + } + + int i_; + }; + + lean_tuple t{nullptr, 42}; + BEAST_EXPECT(detail::get<1>(t) == 42); + BEAST_EXPECT(detail::get<0>(t).i_ == 0); + + t = lean_tuple{explicit_constructible(42), 43}; + BEAST_EXPECT(detail::get<1>(t) == 43); + BEAST_EXPECT(detail::get<0>(t).i_ == 42); + } +}; + +BEAST_DEFINE_TESTSUITE(beast,core,lean_tuple); + +} // detail +} // beast +} // boost