1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/stateful/counter.hpp

94 lines
2.7 KiB
C++
Raw Normal View History

2015-05-14 10:52:27 +00: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_COUNTER_HPP
#define SPROUT_STATEFUL_COUNTER_HPP
#include <sprout/config.hpp>
2015-05-16 23:52:34 +00:00
#include <sprout/type_traits/integral_constant.hpp>
2015-05-14 10:52:27 +00:00
namespace sprout {
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
namespace counter_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::counter_detail::tag<N>);
};
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic pop
#endif
template<int N>
2015-05-17 03:17:30 +00:00
struct state
: public sprout::integral_constant<int, N>
{
2015-05-14 10:52:27 +00:00
friend SPROUT_CONSTEXPR int adl_counter(sprout::counter_detail::tag<N>) {
return N;
}
};
2015-05-16 23:52:34 +00:00
template<int N, int = adl_counter(sprout::counter_detail::tag<N>())>
2015-05-17 03:17:30 +00:00
SPROUT_CONSTEXPR bool check(int, sprout::counter_detail::tag<N>) {
2015-05-16 23:52:34 +00:00
return true;
}
template<int N>
2015-05-17 03:17:30 +00:00
SPROUT_CONSTEXPR bool check(long, sprout::counter_detail::tag<N>) {
2015-05-16 23:52:34 +00:00
return false;
}
template<int N>
2015-05-17 03:17:30 +00:00
SPROUT_CONSTEXPR bool check(bool R = sprout::counter_detail::check(0, sprout::counter_detail::tag<N>())) {
2015-05-14 10:52:27 +00:00
return R;
}
2015-05-16 23:52:34 +00:00
template<int N>
2015-05-17 03:17:30 +00:00
SPROUT_CONSTEXPR int counter(sprout::false_type, sprout::counter_detail::tag<N>) {
2015-05-14 10:52:27 +00:00
return 0;
}
template<int N>
2015-05-17 03:17:30 +00:00
SPROUT_CONSTEXPR int counter(
2015-05-16 23:52:34 +00:00
sprout::true_type, sprout::counter_detail::tag<N>,
int R = !sprout::counter_detail::check<N>() ? N
2015-05-17 03:17:30 +00:00
: counter(sprout::bool_constant<sprout::counter_detail::check<N>()>(), sprout::counter_detail::tag<N + 1>())
2015-05-14 10:52:27 +00:00
)
{
return R;
}
2015-05-16 23:52:34 +00:00
template<int N = 0>
2015-05-17 03:17:30 +00:00
SPROUT_CONSTEXPR int counter(int R = sprout::counter_detail::counter(sprout::true_type(), sprout::counter_detail::tag<N>())) {
2015-05-16 23:52:34 +00:00
return R;
}
2015-05-14 10:52:27 +00:00
} // namespace counter_detail
//
// counter
//
template<
2015-05-16 23:52:34 +00:00
int N = 1,
2015-05-14 10:52:27 +00:00
int R = sprout::counter_detail::state<
2015-05-16 23:52:34 +00:00
sprout::counter_detail::counter() + N - 1
2015-05-14 10:52:27 +00:00
>::value
>
SPROUT_CONSTEXPR int counter() {
return R;
}
#endif
} // namespace sprout
#endif // #ifndef SPROUT_STATEFUL_COUNTER_HPP