Sprout/sprout/stateful/slot.hpp

182 lines
5.1 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/workaround/std/cstddef.hpp>
#include <sprout/type_traits/integral_constant.hpp>
#include <sprout/assert.hpp>
namespace sprout {
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
namespace detail {
SPROUT_STATIC_CONSTEXPR std::size_t slot_default_call_limit = 128;
} // namespace detail
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-14 19:52:27 +09:00
struct state {
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>
struct state<1, Key, Value> {
2015-05-14 19:52:27 +09:00
friend SPROUT_CONSTEXPR int adl_counter(sprout::slot_detail::tag<1>) {
return 1;
}
2015-05-15 11:36:21 +09:00
friend SPROUT_CONSTEXPR int adl_key(sprout::slot_detail::tag<1>) {
return Key;
2015-05-14 19:52:27 +09:00
}
friend SPROUT_CONSTEXPR std::intmax_t adl_value(sprout::slot_detail::tag<1>) {
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<1>, 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 SPROUT_ASSERT(K == Key), Value;
2015-05-14 19:52:27 +09:00
}
};
template<int N, int R = adl_counter(sprout::slot_detail::tag<N>())>
SPROUT_CONSTEXPR int counter(
int, sprout::slot_detail::tag<N>
)
{
return R;
}
SPROUT_CONSTEXPR int counter(
long, sprout::slot_detail::tag<0>
)
{
return 0;
}
template<int N>
SPROUT_CONSTEXPR int counter(
long, sprout::slot_detail::tag<N>,
int R = counter(0, sprout::slot_detail::tag<N - 1>())
)
{
return R;
}
2015-05-15 11:36:21 +09:00
template<int K, int N, std::intmax_t R = adl_get(sprout::slot_detail::tag<N>(), sprout::integral_constant<int, K>())>
2015-05-14 19:52:27 +09:00
SPROUT_CONSTEXPR std::intmax_t get(
int, sprout::slot_detail::tag<N>
)
{
return R;
}
2015-05-15 11:36:21 +09:00
template<int K>
2015-05-14 19:52:27 +09:00
SPROUT_CONSTEXPR std::intmax_t get(
long, sprout::slot_detail::tag<0>
)
{
return 0;
}
2015-05-15 11:36:21 +09:00
template<int K, int N>
2015-05-14 19:52:27 +09:00
SPROUT_CONSTEXPR std::intmax_t get(
long, sprout::slot_detail::tag<N>,
2015-05-15 11:36:21 +09:00
std::intmax_t R = get<K>(0, sprout::slot_detail::tag<N - 1>())
2015-05-14 19:52:27 +09:00
)
{
return R;
}
} // namespace slot_detail
//
// slot
//
template<
2015-05-15 11:36:21 +09:00
int K,
2015-05-14 19:52:27 +09:00
std::size_t Limit = sprout::detail::slot_default_call_limit,
2015-05-15 11:36:21 +09:00
std::intmax_t R = sprout::slot_detail::get<K>(0, sprout::slot_detail::tag<Limit>())
2015-05-14 19:52:27 +09:00
>
SPROUT_CONSTEXPR std::intmax_t slot() {
return R;
}
//
// assign_slot
// assign_slot_return
//
template<
2015-05-15 11:36:21 +09:00
int K,
2015-05-14 19:52:27 +09:00
std::intmax_t Value,
std::size_t Limit = sprout::detail::slot_default_call_limit,
std::intmax_t = sprout::slot_detail::state<
sprout::slot_detail::counter(0, sprout::slot_detail::tag<Limit>()) + 1,
2015-05-15 11:36:21 +09:00
K, Value
>::template get<K>()
2015-05-14 19:52:27 +09:00
>
SPROUT_CXX14_CONSTEXPR void assign_slot() {}
template<
2015-05-15 11:36:21 +09:00
int K,
2015-05-14 19:52:27 +09:00
std::intmax_t Value,
std::size_t Limit = sprout::detail::slot_default_call_limit,
std::intmax_t R = sprout::slot_detail::state<
sprout::slot_detail::counter(0, sprout::slot_detail::tag<Limit>()) + 1,
2015-05-15 11:36:21 +09:00
K, Value
>::template get<K>()
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