//////////////////////////////////////////////////////////////////////////////// /// \file tuple.hpp /// /// \brief This header provides definitions from the C++ header //////////////////////////////////////////////////////////////////////////////// /* The MIT License (MIT) Copyright (c) 2020 Matthew Rodusek All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef BPSTD_TUPLE_HPP #define BPSTD_TUPLE_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "type_traits.hpp" // invoke_result #include "utility.hpp" // index_sequence, forward #include "functional.hpp" // invoke #include // std::tuple_element, and to proxy API #include // std::size_t BPSTD_COMPILER_DIAGNOSTIC_PREAMBLE namespace bpstd { //============================================================================ // class : tuple //============================================================================ template using tuple = std::tuple; //============================================================================ // utilities : tuple //============================================================================ template using tuple_element = std::tuple_element; template using tuple_element_t = typename tuple_element::type; //---------------------------------------------------------------------------- template using tuple_size = std::tuple_size; #if BPSTD_HAS_TEMPLATE_VARIABLES template BPSTD_CPP17_INLINE constexpr auto tuple_size_v = tuple_size::value; #endif //============================================================================ // non-member functions : class : tuple //============================================================================ //---------------------------------------------------------------------------- // Utilities //---------------------------------------------------------------------------- namespace detail { template struct is_tuple : false_type{}; template struct is_tuple> : true_type{}; } // namespace detail template >::value && is_lvalue_reference::value>> inline BPSTD_INLINE_VISIBILITY BPSTD_CPP14_CONSTEXPR tuple_element_t>& get(Tuple&& t) noexcept { return std::get(t); } template >::value && !is_lvalue_reference::value>> inline BPSTD_INLINE_VISIBILITY BPSTD_CPP14_CONSTEXPR tuple_element_t>&& get(Tuple&& t) noexcept { return bpstd::move(std::get(t)); } template BPSTD_CPP14_CONSTEXPR T& get(tuple& t) noexcept; template BPSTD_CPP14_CONSTEXPR T&& get(tuple&& t) noexcept; template BPSTD_CPP14_CONSTEXPR const T& get(const tuple& t) noexcept; template BPSTD_CPP14_CONSTEXPR const T&& get(const tuple&& t) noexcept; //---------------------------------------------------------------------------- namespace detail { // primary template left undefined template struct apply_result_impl; template struct apply_result_impl, Tuple> : invoke_result...>{}; template struct apply_result : apply_result_impl< Fn, make_index_sequence>::value>, Tuple >{}; template using apply_result_t = typename apply_result::type; } // namespace detail /// \brief Invokes the function \p fn using the arguments in \p tuple /// /// This invokes \p fn as if it were a call to bpstd::invoke using the /// arguments in \p tuple /// /// \param fn the function to invoke /// \param tuple the tuple of arguments to pass to fn /// \return the result from \p fn template constexpr detail::apply_result_t apply(Fn&& fn, Tuple&& tuple); /// \brief Constructs a type \p T using the arguments in \p tuple /// /// \tparam T the type to construct /// \param tuple the tuple of arguments to pass to T's constructor template constexpr T make_from_tuple(Tuple&& tuple); } // namespace bpstd //============================================================================== // definitions : non-member functions : class : tuple //============================================================================== //------------------------------------------------------------------------------ // Utilities //------------------------------------------------------------------------------ namespace bpstd { namespace detail { template struct index_of_impl; template struct index_of_impl : index_of_impl{}; template struct index_of_impl : integral_constant{}; template struct index_of : index_of_impl{}; }} // namespace bpstd::detail template inline BPSTD_INLINE_VISIBILITY BPSTD_CPP14_CONSTEXPR T& bpstd::get(tuple& t) noexcept { return std::get::value>(t); } template inline BPSTD_INLINE_VISIBILITY BPSTD_CPP14_CONSTEXPR T&& bpstd::get(tuple&& t) noexcept { return move(std::get::value>(t)); } template inline BPSTD_INLINE_VISIBILITY BPSTD_CPP14_CONSTEXPR const T& bpstd::get(const tuple& t) noexcept { return std::get::value>(t); } template inline BPSTD_INLINE_VISIBILITY BPSTD_CPP14_CONSTEXPR const T&& bpstd::get(const tuple&& t) noexcept { return move(std::get::value>(t)); } //============================================================================== // definition : apply //============================================================================== #if defined(_MSC_VER) # pragma warning(push) # pragma warning(disable:4100) // MSVC warns that 'tuple' is not used below #endif namespace bpstd { namespace detail { template inline BPSTD_INLINE_VISIBILITY constexpr apply_result_t apply_impl(Fn&& fn, Tuple&& tuple, index_sequence) { return ::bpstd::invoke( bpstd::forward(fn), std::get(bpstd::forward(tuple))... ); } } // namespace detail } // namespace bpstd #if defined(_MSC_VER) # pragma warning(pop) #endif template inline BPSTD_INLINE_VISIBILITY constexpr bpstd::detail::apply_result_t bpstd::apply(Fn&& fn, Tuple&& tuple) { return detail::apply_impl( bpstd::forward(fn), bpstd::forward(tuple), make_index_sequence>::value>{} ); } //============================================================================== // definition : make_from_tuple //============================================================================== #if defined(_MSC_VER) # pragma warning(push) # pragma warning(disable:4100) // MSVC warns that 'tuple' is not used below #endif namespace bpstd { namespace detail { template inline BPSTD_INLINE_VISIBILITY constexpr T make_from_tuple_impl(Tuple&& tuple, index_sequence) { return T(std::get(bpstd::forward(tuple))...); } } // namespace detail } // namespace bpstd #if defined(_MSC_VER) # pragma warning(pop) #endif template inline BPSTD_INLINE_VISIBILITY constexpr T bpstd::make_from_tuple(Tuple&& tuple) { return detail::make_from_tuple_impl( bpstd::forward(tuple), make_index_sequence>::value>{} ); } BPSTD_COMPILER_DIAGNOSTIC_POSTAMBLE #endif /* BPSTD_TUPLE_HPP */