Sprout/sprout/utility/pack.hpp

85 lines
2.4 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2015-01-10 10:13:57 +00:00
Copyright (c) 2011-2015 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
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)
=============================================================================*/
2012-01-09 12:32:51 +00:00
#ifndef SPROUT_UTILITY_PACK_HPP
#define SPROUT_UTILITY_PACK_HPP
#include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
2012-01-09 12:32:51 +00:00
#include <sprout/utility/forward.hpp>
#include <sprout/type_traits/integral_constant.hpp>
2014-03-29 00:16:43 +00:00
#include <sprout/type_traits/identity.hpp>
2012-05-26 15:43:38 +00:00
#include <sprout/type_traits/enabler_if.hpp>
#include <sprout/type/type_tuple.hpp>
#include <sprout/detail/nil_base.hpp>
2012-01-09 12:32:51 +00:00
namespace sprout {
//
2013-11-29 13:21:07 +00:00
// pack_element
2012-01-09 12:32:51 +00:00
//
2013-04-06 04:06:51 +00:00
template<std::size_t I, typename... Args>
2013-11-29 13:21:07 +00:00
struct pack_element
: public std::tuple_element<I, sprout::types::type_tuple<Args...> >
2013-04-06 04:06:51 +00:00
{};
//
2013-11-29 13:21:07 +00:00
// pack_element_c
2013-04-06 04:06:51 +00:00
//
template<std::size_t I, typename T, T... Args>
2013-11-29 13:21:07 +00:00
struct pack_element_c
: public sprout::pack_element<I, sprout::integral_constant<T, Args>...>
2012-01-09 12:32:51 +00:00
{};
//
2013-11-29 13:21:07 +00:00
// pack_get
2012-01-09 12:32:51 +00:00
//
namespace detail {
2013-11-30 04:16:30 +00:00
template<typename Tup>
struct pack_get_helper;
template<typename... Args>
struct pack_get_helper<sprout::types::type_tuple<Args...> > {
template<typename Head, typename... Tail>
static SPROUT_CONSTEXPR Head&&
eval(Args&&..., Head&& head, Tail&&...) {
return SPROUT_FORWARD(Head, head);
2013-11-30 04:16:30 +00:00
}
};
2012-01-09 12:32:51 +00:00
} // namespace detail
2013-04-06 04:06:51 +00:00
template<std::size_t I, typename... Args>
2013-11-29 13:21:07 +00:00
inline SPROUT_CONSTEXPR typename sprout::pack_element<I, Args&&...>::type
pack_get(Args&&... args) {
2013-11-30 04:16:30 +00:00
return sprout::detail::pack_get_helper<
typename sprout::types::detail::tuple_take<I, sprout::types::type_tuple<Args...> >::type
>::eval(SPROUT_FORWARD(Args, args)...);
2012-01-09 12:32:51 +00:00
}
2014-03-29 00:16:43 +00:00
//
// head_element
//
template<typename... Args>
struct head_element;
2014-03-29 00:16:43 +00:00
template<typename Head, typename... Tail>
struct head_element<Head, Tail...>
2014-03-29 00:16:43 +00:00
: public sprout::identity<Head>
{};
template<>
struct head_element<>
: public sprout::detail::nil_base
{};
2014-03-29 00:16:43 +00:00
//
// head_get
//
template<typename Head, typename... Tail>
inline SPROUT_CONSTEXPR Head&&
2014-03-31 08:18:22 +00:00
head_get(Head&& head, Tail&&...) {
2014-03-29 00:16:43 +00:00
return SPROUT_FORWARD(Head, head);
}
2012-01-09 12:32:51 +00:00
} // namespace sprout
#endif // #ifndef SPROUT_UTILITY_PACK_HPP