Sprout/sprout/stateful/slot.hpp

182 lines
5.6 KiB
C++
Raw Normal View History

2015-05-14 19:52:27 +09:00
/*=============================================================================
Copyright (c) 2011-2015 Bolero MURAKAMI
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)
=============================================================================*/
#ifndef SPROUT_STATEFUL_SLOT_HPP
#define SPROUT_STATEFUL_SLOT_HPP
#include <cstdint>
#include <sprout/config.hpp>
#include <sprout/type_traits/integral_constant.hpp>
#include <sprout/assert.hpp>
namespace sprout {
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
namespace slot_detail {
#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wnon-template-friend"
#endif
#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wundefined-inline"
#endif
template<int N>
struct tag {
friend SPROUT_CONSTEXPR int adl_counter(sprout::slot_detail::tag<N>);
2015-05-15 11:36:21 +09:00
friend SPROUT_CONSTEXPR int adl_key(sprout::slot_detail::tag<N>);
2015-05-14 19:52:27 +09:00
friend SPROUT_CONSTEXPR std::intmax_t adl_value(sprout::slot_detail::tag<N>);
2015-05-15 11:36:21 +09:00
template<int K>
friend SPROUT_CONSTEXPR std::intmax_t adl_get(sprout::slot_detail::tag<N>, sprout::integral_constant<int, K>);
2015-05-14 19:52:27 +09:00
};
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic pop
#endif
2015-05-15 11:36:21 +09:00
template<int N, int Key, std::intmax_t Value>
2015-05-17 08:52:34 +09:00
struct state
: public sprout::integral_constant<std::intmax_t, Value>
{
2015-05-14 19:52:27 +09:00
friend SPROUT_CONSTEXPR int adl_counter(sprout::slot_detail::tag<N>) {
return N;
}
2015-05-15 11:36:21 +09:00
friend SPROUT_CONSTEXPR int adl_key(sprout::slot_detail::tag<N>) {
return Key;
2015-05-14 19:52:27 +09:00
}
friend SPROUT_CONSTEXPR std::intmax_t adl_value(sprout::slot_detail::tag<N>) {
return Value;
}
2015-05-15 11:36:21 +09:00
template<int K>
friend SPROUT_CONSTEXPR std::intmax_t adl_get(sprout::slot_detail::tag<N>, sprout::integral_constant<int, K>) {
return get<K>();
2015-05-14 19:52:27 +09:00
}
2015-05-15 11:36:21 +09:00
template<int K>
2015-05-14 19:52:27 +09:00
static SPROUT_CONSTEXPR std::intmax_t get() {
2015-05-15 11:36:21 +09:00
return K == Key ? Value
2015-05-14 19:52:27 +09:00
: state<
N - 1,
2015-05-15 11:36:21 +09:00
adl_key(sprout::slot_detail::tag<N - 1>()),
2015-05-14 19:52:27 +09:00
adl_value(sprout::slot_detail::tag<N - 1>())
2015-05-15 11:36:21 +09:00
>::template get<K>()
2015-05-14 19:52:27 +09:00
;
}
};
2015-05-15 11:36:21 +09:00
template<int Key, std::intmax_t Value>
2015-05-17 08:52:34 +09:00
struct state<0, Key, Value>
: public sprout::integral_constant<std::intmax_t, Value>
{
friend SPROUT_CONSTEXPR int adl_counter(sprout::slot_detail::tag<0>) {
return 0;
2015-05-14 19:52:27 +09:00
}
2015-05-17 08:52:34 +09:00
friend SPROUT_CONSTEXPR int adl_key(sprout::slot_detail::tag<0>) {
2015-05-15 11:36:21 +09:00
return Key;
2015-05-14 19:52:27 +09:00
}
2015-05-17 08:52:34 +09:00
friend SPROUT_CONSTEXPR std::intmax_t adl_value(sprout::slot_detail::tag<0>) {
2015-05-14 19:52:27 +09:00
return Value;
}
2015-05-15 11:36:21 +09:00
template<int K>
2015-05-17 08:52:34 +09:00
friend SPROUT_CONSTEXPR std::intmax_t adl_get(sprout::slot_detail::tag<0>, sprout::integral_constant<int, K>) {
2015-05-15 11:36:21 +09:00
return get<K>();
2015-05-14 19:52:27 +09:00
}
2015-05-15 11:36:21 +09:00
template<int K>
2015-05-14 19:52:27 +09:00
static SPROUT_CONSTEXPR std::intmax_t get() {
2015-05-15 11:36:21 +09:00
return SPROUT_ASSERT(K == Key), Value;
2015-05-14 19:52:27 +09:00
}
};
2015-05-17 08:52:34 +09:00
template<int N, int = adl_counter(sprout::slot_detail::tag<N>())>
2015-05-17 12:17:30 +09:00
SPROUT_CONSTEXPR bool check(int, sprout::slot_detail::tag<N>) {
2015-05-17 08:52:34 +09:00
return true;
}
template<int N>
2015-05-17 12:17:30 +09:00
SPROUT_CONSTEXPR bool check(long, sprout::slot_detail::tag<N>) {
2015-05-17 08:52:34 +09:00
return false;
}
template<int N>
2015-05-17 12:17:30 +09:00
SPROUT_CONSTEXPR bool check(bool R = sprout::slot_detail::check(0, sprout::slot_detail::tag<N>())) {
2015-05-14 19:52:27 +09:00
return R;
}
2015-05-17 08:52:34 +09:00
template<int N>
2015-05-17 12:17:30 +09:00
SPROUT_CONSTEXPR int counter(sprout::false_type, sprout::slot_detail::tag<N>) {
2015-05-14 19:52:27 +09:00
return 0;
}
template<int N>
2015-05-17 12:17:30 +09:00
SPROUT_CONSTEXPR int counter(
2015-05-17 08:52:34 +09:00
sprout::true_type, sprout::slot_detail::tag<N>,
2015-05-17 12:17:30 +09:00
int R = !sprout::slot_detail::check(0, sprout::slot_detail::tag<N>()) ? N
: counter(sprout::bool_constant<sprout::slot_detail::check(0, sprout::slot_detail::tag<N>())>(), sprout::slot_detail::tag<N + 1>())
2015-05-14 19:52:27 +09:00
)
{
return R;
}
2015-05-17 08:52:34 +09:00
template<int N = 0>
2015-05-17 12:17:30 +09:00
SPROUT_CONSTEXPR int counter(int R = sprout::slot_detail::counter(sprout::true_type(), sprout::slot_detail::tag<N>())) {
2015-05-14 19:52:27 +09:00
return R;
}
2015-05-17 08:52:34 +09:00
template<int K, int N>
SPROUT_CONSTEXPR std::intmax_t get_impl(sprout::false_type, sprout::slot_detail::tag<N>) {
2015-05-14 19:52:27 +09:00
return 0;
}
2015-05-15 11:36:21 +09:00
template<int K, int N>
2015-05-17 08:52:34 +09:00
SPROUT_CONSTEXPR std::intmax_t get_impl(
sprout::true_type, sprout::slot_detail::tag<N>,
std::intmax_t R = !sprout::slot_detail::check<N>() ? adl_get(sprout::slot_detail::tag<N - 1>(), sprout::integral_constant<int, K>())
: get_impl<K>(sprout::bool_constant<sprout::slot_detail::check<N>()>(), sprout::slot_detail::tag<N + 1>())
2015-05-14 19:52:27 +09:00
)
{
return R;
}
2015-05-17 08:52:34 +09:00
template<int K, int N = 0>
SPROUT_CONSTEXPR std::intmax_t get(int R = sprout::slot_detail::get_impl<K>(sprout::true_type(), sprout::slot_detail::tag<N>())) {
return R;
}
2015-05-14 19:52:27 +09:00
} // namespace slot_detail
//
// slot
//
template<
2015-05-15 11:36:21 +09:00
int K,
2015-05-17 08:52:34 +09:00
std::intmax_t R = sprout::slot_detail::get<K>()
2015-05-14 19:52:27 +09:00
>
SPROUT_CONSTEXPR std::intmax_t slot() {
return R;
}
//
// assign_slot
// assign_slot_return
//
template<
2015-05-17 08:52:34 +09:00
int K, std::intmax_t Value,
2015-05-17 12:17:30 +09:00
int N = 0,
2015-05-14 19:52:27 +09:00
std::intmax_t = sprout::slot_detail::state<
2015-05-17 12:17:30 +09:00
sprout::slot_detail::counter(sprout::true_type(), sprout::slot_detail::tag<N>()),
2015-05-15 11:36:21 +09:00
K, Value
2015-05-17 08:52:34 +09:00
>::value
2015-05-14 19:52:27 +09:00
>
SPROUT_CXX14_CONSTEXPR void assign_slot() {}
template<
2015-05-17 08:52:34 +09:00
int K, std::intmax_t Value,
2015-05-17 12:17:30 +09:00
int N = 0,
2015-05-14 19:52:27 +09:00
std::intmax_t R = sprout::slot_detail::state<
2015-05-17 12:17:30 +09:00
sprout::slot_detail::counter(sprout::true_type(), sprout::slot_detail::tag<N>()),
2015-05-15 11:36:21 +09:00
K, Value
2015-05-17 08:52:34 +09:00
>::value
2015-05-14 19:52:27 +09:00
>
SPROUT_CONSTEXPR std::intmax_t assign_slot_return() {
return R;
}
#endif
} // namespace sprout
#endif // #ifndef SPROUT_STATEFUL_SLOT_HPP