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

add component for stateful lib

This commit is contained in:
bolero-MURAKAMI 2015-05-17 08:52:34 +09:00
parent b37695f1a4
commit fa619fc1bf
6 changed files with 426 additions and 131 deletions

View file

@ -12,5 +12,7 @@
#include <sprout/stateful/counter.hpp> #include <sprout/stateful/counter.hpp>
#include <sprout/stateful/rand.hpp> #include <sprout/stateful/rand.hpp>
#include <sprout/stateful/slot.hpp> #include <sprout/stateful/slot.hpp>
#include <sprout/stateful/typed_id.hpp>
#include <sprout/stateful/typed_slot.hpp>
#endif // #ifndef SPROUT_STATEFUL_HPP #endif // #ifndef SPROUT_STATEFUL_HPP

View file

@ -9,15 +9,11 @@
#define SPROUT_STATEFUL_COUNTER_HPP #define SPROUT_STATEFUL_COUNTER_HPP
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp> #include <sprout/type_traits/integral_constant.hpp>
namespace sprout { namespace sprout {
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR #ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
namespace detail {
SPROUT_STATIC_CONSTEXPR std::size_t counter_default_call_limit = 128;
} // namespace detail
namespace counter_detail { namespace counter_detail {
#if defined(__GNUC__) && !defined(__clang__) #if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic push # pragma GCC diagnostic push
@ -43,63 +39,54 @@ namespace sprout {
friend SPROUT_CONSTEXPR int adl_counter(sprout::counter_detail::tag<N>) { friend SPROUT_CONSTEXPR int adl_counter(sprout::counter_detail::tag<N>) {
return N; return N;
} }
SPROUT_STATIC_CONSTEXPR int value = N - 1; SPROUT_STATIC_CONSTEXPR int value = N;
}; };
template<int N> template<int N>
SPROUT_CONSTEXPR_OR_CONST int sprout::counter_detail::state<N>::value; SPROUT_CONSTEXPR_OR_CONST int sprout::counter_detail::state<N>::value;
template<>
struct state<1> {
friend SPROUT_CONSTEXPR int adl_counter(sprout::counter_detail::tag<1>) {
return 1;
}
static SPROUT_CONSTEXPR int value = 0;
};
SPROUT_CONSTEXPR_OR_CONST int sprout::counter_detail::state<1>::value;
template<int N, int R = adl_counter(sprout::counter_detail::tag<N>())> template<int N, int = adl_counter(sprout::counter_detail::tag<N>())>
SPROUT_CONSTEXPR int counter( SPROUT_CONSTEXPR bool check_impl(int, sprout::counter_detail::tag<N>) {
int, sprout::counter_detail::tag<N> return true;
}
template<int N>
SPROUT_CONSTEXPR bool check_impl(long, sprout::counter_detail::tag<N>) {
return false;
}
template<int N>
SPROUT_CONSTEXPR bool check(bool R = sprout::counter_detail::check_impl(0, sprout::counter_detail::tag<N>())) {
return R;
}
template<int N>
SPROUT_CONSTEXPR int counter_impl(sprout::false_type, sprout::counter_detail::tag<N>) {
return 0;
}
template<int N>
SPROUT_CONSTEXPR int counter_impl(
sprout::true_type, sprout::counter_detail::tag<N>,
int R = !sprout::counter_detail::check<N>() ? N
: counter_impl(sprout::bool_constant<sprout::counter_detail::check<N>()>(), sprout::counter_detail::tag<N + 1>())
) )
{ {
return R; return R;
} }
SPROUT_CONSTEXPR int counter( template<int N = 0>
long, sprout::counter_detail::tag<0> SPROUT_CONSTEXPR int counter(int R = sprout::counter_detail::counter_impl(sprout::true_type(), sprout::counter_detail::tag<N>())) {
)
{
return 0;
}
template<int N>
SPROUT_CONSTEXPR int counter(
long, sprout::counter_detail::tag<N>,
int R = counter(0, sprout::counter_detail::tag<N - 1>())
)
{
return R; return R;
} }
} // namespace counter_detail } // namespace counter_detail
// //
// counter // counter
// counter_before
// //
template< template<
std::size_t Limit = sprout::detail::counter_default_call_limit, int N = 1,
int R = sprout::counter_detail::state< int R = sprout::counter_detail::state<
sprout::counter_detail::counter(0, sprout::counter_detail::tag<Limit>()) + 1 sprout::counter_detail::counter() + N - 1
>::value >::value
> >
SPROUT_CONSTEXPR int counter() { SPROUT_CONSTEXPR int counter() {
return R; return R;
} }
template<
std::size_t Limit = sprout::detail::counter_default_call_limit,
int R = sprout::counter_detail::state<
sprout::counter_detail::counter(0, sprout::counter_detail::tag<Limit>())
>::value
>
SPROUT_CONSTEXPR int counter_before() {
return R;
}
#endif #endif
} // namespace sprout } // namespace sprout

View file

@ -15,6 +15,7 @@
#include <sprout/random/default_random_engine.hpp> #include <sprout/random/default_random_engine.hpp>
#include <sprout/random/random_result.hpp> #include <sprout/random/random_result.hpp>
#include <sprout/random/unique_seed.hpp> #include <sprout/random/unique_seed.hpp>
#include <sprout/type_traits/integral_constant.hpp>
#include <sprout/workaround/detail/uniform_int_distribution.hpp> #include <sprout/workaround/detail/uniform_int_distribution.hpp>
namespace sprout { namespace sprout {
@ -29,7 +30,6 @@ namespace sprout {
> rand_result_type; > rand_result_type;
SPROUT_STATIC_CONSTEXPR std::size_t rand_default_seed = SPROUT_UNIQUE_SEED; SPROUT_STATIC_CONSTEXPR std::size_t rand_default_seed = SPROUT_UNIQUE_SEED;
SPROUT_STATIC_CONSTEXPR std::size_t rand_default_call_limit = 128;
} // namespace detail } // namespace detail
namespace rand_detail { namespace rand_detail {
@ -68,7 +68,8 @@ namespace sprout {
// generate a next random number // generate a next random number
SPROUT_STATIC_CONSTEXPR sprout::detail::rand_result_type result SPROUT_STATIC_CONSTEXPR sprout::detail::rand_result_type result
SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_INNER(( SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_INNER((
!IsSrand ? sprout::rand_detail::state< !IsSrand
? sprout::rand_detail::state<
N - 1, N - 1,
adl_is_srand(sprout::rand_detail::tag<N - 1>()), adl_is_srand(sprout::rand_detail::tag<N - 1>()),
adl_seed(sprout::rand_detail::tag<N - 1>()) adl_seed(sprout::rand_detail::tag<N - 1>())
@ -85,7 +86,8 @@ namespace sprout {
template<int N, bool IsSrand, unsigned Seed> template<int N, bool IsSrand, unsigned Seed>
SPROUT_CONSTEXPR_OR_CONST sprout::detail::rand_result_type sprout::rand_detail::state<N, IsSrand, Seed>::result SPROUT_CONSTEXPR_OR_CONST sprout::detail::rand_result_type sprout::rand_detail::state<N, IsSrand, Seed>::result
SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_OUTER(( SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_OUTER((
!IsSrand ? sprout::rand_detail::state< !IsSrand
? sprout::rand_detail::state<
N - 1, N - 1,
adl_is_srand(sprout::rand_detail::tag<N - 1>()), adl_is_srand(sprout::rand_detail::tag<N - 1>()),
adl_seed(sprout::rand_detail::tag<N - 1>()) adl_seed(sprout::rand_detail::tag<N - 1>())
@ -100,65 +102,85 @@ namespace sprout {
template<int N, bool IsSrand, unsigned Seed> template<int N, bool IsSrand, unsigned Seed>
SPROUT_CONSTEXPR_OR_CONST int sprout::rand_detail::state<N, IsSrand, Seed>::value; SPROUT_CONSTEXPR_OR_CONST int sprout::rand_detail::state<N, IsSrand, Seed>::value;
template<bool IsSrand, unsigned Seed> template<bool IsSrand, unsigned Seed>
struct state<1, IsSrand, Seed> { struct state<0, IsSrand, Seed> {
friend SPROUT_CONSTEXPR int adl_counter(sprout::rand_detail::tag<1>) { friend SPROUT_CONSTEXPR int adl_counter(sprout::rand_detail::tag<0>) {
return 1; return 0;
} }
friend SPROUT_CONSTEXPR bool adl_is_srand(sprout::rand_detail::tag<1>) { friend SPROUT_CONSTEXPR bool adl_is_srand(sprout::rand_detail::tag<0>) {
return IsSrand; return IsSrand;
} }
friend SPROUT_CONSTEXPR unsigned adl_seed(sprout::rand_detail::tag<1>) { friend SPROUT_CONSTEXPR unsigned adl_seed(sprout::rand_detail::tag<0>) {
return Seed; return Seed;
} }
// generate a first random number between [0, RAND_MAX] // generate a first random number between [0, RAND_MAX]
SPROUT_STATIC_CONSTEXPR sprout::detail::rand_result_type result SPROUT_STATIC_CONSTEXPR sprout::detail::rand_result_type result
SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_INNER( SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_INNER((
sprout::detail::rand_distribution_type(0, RAND_MAX) !IsSrand
(sprout::as_const(sprout::detail::rand_generator_type(!IsSrand ? sprout::detail::rand_default_seed : Seed))) ? sprout::detail::rand_distribution_type(0, RAND_MAX)
) (sprout::as_const(sprout::detail::rand_generator_type(!IsSrand ? sprout::detail::rand_default_seed : Seed)))
: sprout::detail::rand_result_type(
Seed,
sprout::detail::rand_generator_type(Seed),
sprout::detail::rand_distribution_type(0, RAND_MAX)
)
))
; ;
static SPROUT_CONSTEXPR int value = result; static SPROUT_CONSTEXPR int value = result;
}; };
template<bool IsSrand, unsigned Seed> template<bool IsSrand, unsigned Seed>
SPROUT_CONSTEXPR_OR_CONST sprout::detail::rand_result_type sprout::rand_detail::state<1, IsSrand, Seed>::result SPROUT_CONSTEXPR_OR_CONST sprout::detail::rand_result_type sprout::rand_detail::state<0, IsSrand, Seed>::result
SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_OUTER( SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_OUTER(
sprout::detail::rand_distribution_type(0, RAND_MAX) !IsSrand
(sprout::as_const(sprout::detail::rand_generator_type(!IsSrand ? sprout::detail::rand_default_seed : Seed))) ? sprout::detail::rand_distribution_type(0, RAND_MAX)
(sprout::as_const(sprout::detail::rand_generator_type(!IsSrand ? sprout::detail::rand_default_seed : Seed)))
: sprout::detail::rand_result_type(
Seed,
sprout::detail::rand_generator_type(Seed),
sprout::detail::rand_distribution_type(0, RAND_MAX)
)
) )
; ;
template<bool IsSrand, unsigned Seed> template<bool IsSrand, unsigned Seed>
SPROUT_CONSTEXPR_OR_CONST int sprout::rand_detail::state<1, IsSrand, Seed>::value; SPROUT_CONSTEXPR_OR_CONST int sprout::rand_detail::state<0, IsSrand, Seed>::value;
template<int N, int R = adl_counter(sprout::rand_detail::tag<N>())> template<int N, int = adl_counter(sprout::rand_detail::tag<N>())>
SPROUT_CONSTEXPR int counter( SPROUT_CONSTEXPR bool check_impl(int, sprout::rand_detail::tag<N>) {
int, sprout::rand_detail::tag<N> return true;
}
template<int N>
SPROUT_CONSTEXPR bool check_impl(long, sprout::rand_detail::tag<N>) {
return false;
}
template<int N>
SPROUT_CONSTEXPR bool check(bool R = sprout::rand_detail::check_impl(0, sprout::rand_detail::tag<N>())) {
return R;
}
template<int N>
SPROUT_CONSTEXPR int counter_impl(sprout::false_type, sprout::rand_detail::tag<N>) {
return 0;
}
template<int N>
SPROUT_CONSTEXPR int counter_impl(
sprout::true_type, sprout::rand_detail::tag<N>,
int R = !sprout::rand_detail::check<N>() ? N
: counter_impl(sprout::bool_constant<sprout::rand_detail::check<N>()>(), sprout::rand_detail::tag<N + 1>())
) )
{ {
return R; return R;
} }
SPROUT_CONSTEXPR int counter( template<int N = 0>
long, sprout::rand_detail::tag<0> SPROUT_CONSTEXPR int counter(int R = sprout::rand_detail::counter_impl(sprout::true_type(), sprout::rand_detail::tag<N>())) {
)
{
return 0;
}
template<int N>
SPROUT_CONSTEXPR int counter(
long, sprout::rand_detail::tag<N>,
int R = counter(0, sprout::rand_detail::tag<N - 1>())
)
{
return R; return R;
} }
} // namespace rand_detail } // namespace rand_detail
// //
// rand // rand
// rand_before
// //
template< template<
std::size_t Limit = sprout::detail::rand_default_call_limit, int N = 1,
int R = sprout::rand_detail::state< int R = sprout::rand_detail::state<
sprout::rand_detail::counter(0, sprout::rand_detail::tag<Limit>()) + 1 sprout::rand_detail::counter() + N - 1
>::value >::value
> >
SPROUT_CONSTEXPR int rand() { SPROUT_CONSTEXPR int rand() {
@ -171,18 +193,18 @@ namespace sprout {
// //
template< template<
unsigned Seed, unsigned Seed,
std::size_t Limit = sprout::detail::rand_default_call_limit, int N = 1,
int = sprout::rand_detail::state< int = sprout::rand_detail::state<
sprout::rand_detail::counter(0, sprout::rand_detail::tag<Limit>()) + 1, sprout::rand_detail::counter() + N - 1,
true, Seed true, Seed
>::value >::value
> >
SPROUT_CXX14_CONSTEXPR void srand() {} SPROUT_CXX14_CONSTEXPR void srand() {}
template< template<
unsigned Seed, unsigned Seed,
std::size_t Limit = sprout::detail::rand_default_call_limit, int N = 1,
int R = sprout::rand_detail::state< int R = sprout::rand_detail::state<
sprout::rand_detail::counter(0, sprout::rand_detail::tag<Limit>()) + 1, sprout::rand_detail::counter() + N - 1,
true, Seed true, Seed
>::value >::value
> >

View file

@ -10,17 +10,12 @@
#include <cstdint> #include <cstdint>
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
#include <sprout/type_traits/integral_constant.hpp> #include <sprout/type_traits/integral_constant.hpp>
#include <sprout/assert.hpp> #include <sprout/assert.hpp>
namespace sprout { namespace sprout {
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR #ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
namespace detail {
SPROUT_STATIC_CONSTEXPR std::size_t slot_default_call_limit = 128;
} // namespace detail
namespace slot_detail { namespace slot_detail {
#if defined(__GNUC__) && !defined(__clang__) #if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic push # pragma GCC diagnostic push
@ -46,7 +41,9 @@ namespace sprout {
#endif #endif
template<int N, int Key, std::intmax_t Value> template<int N, int Key, std::intmax_t Value>
struct state { struct state
: public sprout::integral_constant<std::intmax_t, Value>
{
friend SPROUT_CONSTEXPR int adl_counter(sprout::slot_detail::tag<N>) { friend SPROUT_CONSTEXPR int adl_counter(sprout::slot_detail::tag<N>) {
return N; return N;
} }
@ -72,18 +69,20 @@ namespace sprout {
} }
}; };
template<int Key, std::intmax_t Value> template<int Key, std::intmax_t Value>
struct state<1, Key, Value> { struct state<0, Key, Value>
friend SPROUT_CONSTEXPR int adl_counter(sprout::slot_detail::tag<1>) { : public sprout::integral_constant<std::intmax_t, Value>
return 1; {
friend SPROUT_CONSTEXPR int adl_counter(sprout::slot_detail::tag<0>) {
return 0;
} }
friend SPROUT_CONSTEXPR int adl_key(sprout::slot_detail::tag<1>) { friend SPROUT_CONSTEXPR int adl_key(sprout::slot_detail::tag<0>) {
return Key; return Key;
} }
friend SPROUT_CONSTEXPR std::intmax_t adl_value(sprout::slot_detail::tag<1>) { friend SPROUT_CONSTEXPR std::intmax_t adl_value(sprout::slot_detail::tag<0>) {
return Value; return Value;
} }
template<int K> template<int K>
friend SPROUT_CONSTEXPR std::intmax_t adl_get(sprout::slot_detail::tag<1>, sprout::integral_constant<int, K>) { friend SPROUT_CONSTEXPR std::intmax_t adl_get(sprout::slot_detail::tag<0>, sprout::integral_constant<int, K>) {
return get<K>(); return get<K>();
} }
template<int K> template<int K>
@ -92,58 +91,61 @@ namespace sprout {
} }
}; };
template<int N, int R = adl_counter(sprout::slot_detail::tag<N>())> template<int N, int = adl_counter(sprout::slot_detail::tag<N>())>
SPROUT_CONSTEXPR int counter( SPROUT_CONSTEXPR bool check_impl(int, sprout::slot_detail::tag<N>) {
int, sprout::slot_detail::tag<N> return true;
)
{
return R;
}
SPROUT_CONSTEXPR int counter(
long, sprout::slot_detail::tag<0>
)
{
return 0;
} }
template<int N> template<int N>
SPROUT_CONSTEXPR int counter( SPROUT_CONSTEXPR bool check_impl(long, sprout::slot_detail::tag<N>) {
long, sprout::slot_detail::tag<N>, return false;
int R = counter(0, sprout::slot_detail::tag<N - 1>()) }
) template<int N>
{ SPROUT_CONSTEXPR bool check(bool R = sprout::slot_detail::check_impl(0, sprout::slot_detail::tag<N>())) {
return R; return R;
} }
template<int K, int N, std::intmax_t R = adl_get(sprout::slot_detail::tag<N>(), sprout::integral_constant<int, K>())> template<int N>
SPROUT_CONSTEXPR std::intmax_t get( SPROUT_CONSTEXPR int counter_impl(sprout::false_type, sprout::slot_detail::tag<N>) {
int, sprout::slot_detail::tag<N> return 0;
}
template<int N>
SPROUT_CONSTEXPR int counter_impl(
sprout::true_type, sprout::slot_detail::tag<N>,
int R = !sprout::slot_detail::check_impl(0, sprout::slot_detail::tag<N>()) ? N
: counter_impl(sprout::bool_constant<sprout::slot_detail::check_impl(0, sprout::slot_detail::tag<N>())>(), sprout::slot_detail::tag<N + 1>())
) )
{ {
return R; return R;
} }
template<int K> template<int N = 0>
SPROUT_CONSTEXPR std::intmax_t get( SPROUT_CONSTEXPR int counter(int R = sprout::slot_detail::counter_impl(sprout::true_type(), sprout::slot_detail::tag<N>())) {
long, sprout::slot_detail::tag<0> return R;
) }
{
template<int K, int N>
SPROUT_CONSTEXPR std::intmax_t get_impl(sprout::false_type, sprout::slot_detail::tag<N>) {
return 0; return 0;
} }
template<int K, int N> template<int K, int N>
SPROUT_CONSTEXPR std::intmax_t get( SPROUT_CONSTEXPR std::intmax_t get_impl(
long, sprout::slot_detail::tag<N>, sprout::true_type, sprout::slot_detail::tag<N>,
std::intmax_t R = get<K>(0, sprout::slot_detail::tag<N - 1>()) 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>())
) )
{ {
return R; return R;
} }
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;
}
} // namespace slot_detail } // namespace slot_detail
// //
// slot // slot
// //
template< template<
int K, int K,
std::size_t Limit = sprout::detail::slot_default_call_limit, std::intmax_t R = sprout::slot_detail::get<K>()
std::intmax_t R = sprout::slot_detail::get<K>(0, sprout::slot_detail::tag<Limit>())
> >
SPROUT_CONSTEXPR std::intmax_t slot() { SPROUT_CONSTEXPR std::intmax_t slot() {
return R; return R;
@ -153,23 +155,19 @@ namespace sprout {
// assign_slot_return // assign_slot_return
// //
template< template<
int K, int K, std::intmax_t Value,
std::intmax_t Value,
std::size_t Limit = sprout::detail::slot_default_call_limit,
std::intmax_t = sprout::slot_detail::state< std::intmax_t = sprout::slot_detail::state<
sprout::slot_detail::counter(0, sprout::slot_detail::tag<Limit>()) + 1, sprout::slot_detail::counter(),
K, Value K, Value
>::template get<K>() >::value
> >
SPROUT_CXX14_CONSTEXPR void assign_slot() {} SPROUT_CXX14_CONSTEXPR void assign_slot() {}
template< template<
int K, int K, std::intmax_t Value,
std::intmax_t Value,
std::size_t Limit = sprout::detail::slot_default_call_limit,
std::intmax_t R = sprout::slot_detail::state< std::intmax_t R = sprout::slot_detail::state<
sprout::slot_detail::counter(0, sprout::slot_detail::tag<Limit>()) + 1, sprout::slot_detail::counter(),
K, Value K, Value
>::template get<K>() >::value
> >
SPROUT_CONSTEXPR std::intmax_t assign_slot_return() { SPROUT_CONSTEXPR std::intmax_t assign_slot_return() {
return R; return R;

View file

@ -0,0 +1,104 @@
/*=============================================================================
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_TYPED_ID_HPP
#define SPROUT_STATEFUL_TYPED_ID_HPP
#include <sprout/config.hpp>
#include <sprout/type_traits/integral_constant.hpp>
#include <sprout/type_traits/identity.hpp>
namespace sprout {
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
namespace typed_id_detail {
template<typename T>
struct typed {
#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(tag<N>);
};
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic pop
#endif
template<int N>
struct state
: public sprout::integral_constant<int, N>
{
friend SPROUT_CONSTEXPR int adl_counter(tag<N>) {
return N;
}
};
template<int N, int = adl_counter(tag<N>())>
static SPROUT_CONSTEXPR bool check_impl(int, tag<N>) {
return true;
}
template<int N>
static SPROUT_CONSTEXPR bool check_impl(long, tag<N>) {
return false;
}
template<int N>
static SPROUT_CONSTEXPR bool check(bool R = check_impl(0, tag<N>())) {
return R;
}
template<int N>
static SPROUT_CONSTEXPR int counter_impl(sprout::false_type, tag<N>) {
return 0;
}
template<int N>
static SPROUT_CONSTEXPR int counter_impl(
sprout::true_type, tag<N>,
int R = !check<N>() ? N
: counter_impl(sprout::bool_constant<check<N>()>(), tag<N + 1>())
)
{
return R;
}
template<int N = 0>
static SPROUT_CONSTEXPR int counter(int R = counter_impl(sprout::true_type(), tag<N>())) {
return R;
}
template<int N = 1>
static SPROUT_CONSTEXPR int id(int R = state<counter() + N - 1>::value) {
return R;
}
};
template<typename T>
struct depends_on
: public sprout::identity<void>
{};
} // namespace typed_id_detail
//
// typed_id
//
template<typename T>
struct typed_id
: public sprout::integral_constant<
int, sprout::typed_id_detail::typed<typename sprout::typed_id_detail::depends_on<T>::type>::id()
>
{};
#endif
} // namespace sprout
#endif // #ifndef SPROUT_STATEFUL_TYPED_ID_HPP

View file

@ -0,0 +1,182 @@
/*=============================================================================
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_TYPED_SLOT_HPP
#define SPROUT_STATEFUL_TYPED_SLOT_HPP
#include <cstdint>
#include <sprout/config.hpp>
#include <sprout/type_traits/integral_constant.hpp>
#include <sprout/assert.hpp>
#include <sprout/stateful/typed_id.hpp>
namespace sprout {
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
namespace typed_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::typed_slot_detail::tag<N>);
friend SPROUT_CONSTEXPR int adl_key(sprout::typed_slot_detail::tag<N>);
friend SPROUT_CONSTEXPR std::intmax_t adl_value(sprout::typed_slot_detail::tag<N>);
template<int K>
friend SPROUT_CONSTEXPR std::intmax_t adl_get(sprout::typed_slot_detail::tag<N>, sprout::integral_constant<int, K>);
};
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic pop
#endif
template<int N, int Key, std::intmax_t Value>
struct state
: public sprout::integral_constant<std::intmax_t, Value>
{
friend SPROUT_CONSTEXPR int adl_counter(sprout::typed_slot_detail::tag<N>) {
return N;
}
friend SPROUT_CONSTEXPR int adl_key(sprout::typed_slot_detail::tag<N>) {
return Key;
}
friend SPROUT_CONSTEXPR std::intmax_t adl_value(sprout::typed_slot_detail::tag<N>) {
return Value;
}
template<int K>
friend SPROUT_CONSTEXPR std::intmax_t adl_get(sprout::typed_slot_detail::tag<N>, sprout::integral_constant<int, K>) {
return get<K>();
}
template<int K>
static SPROUT_CONSTEXPR std::intmax_t get() {
return K == Key ? Value
: state<
N - 1,
adl_key(sprout::typed_slot_detail::tag<N - 1>()),
adl_value(sprout::typed_slot_detail::tag<N - 1>())
>::template get<K>()
;
}
};
template<int Key, std::intmax_t Value>
struct state<0, Key, Value>
: public sprout::integral_constant<std::intmax_t, Value>
{
friend SPROUT_CONSTEXPR int adl_counter(sprout::typed_slot_detail::tag<0>) {
return 0;
}
friend SPROUT_CONSTEXPR int adl_key(sprout::typed_slot_detail::tag<0>) {
return Key;
}
friend SPROUT_CONSTEXPR std::intmax_t adl_value(sprout::typed_slot_detail::tag<0>) {
return Value;
}
template<int K>
friend SPROUT_CONSTEXPR std::intmax_t adl_get(sprout::typed_slot_detail::tag<0>, sprout::integral_constant<int, K>) {
return get<K>();
}
template<int K>
static SPROUT_CONSTEXPR std::intmax_t get() {
return SPROUT_ASSERT(K == Key), Value;
}
};
template<int N, int = adl_counter(sprout::typed_slot_detail::tag<N>())>
SPROUT_CONSTEXPR bool check_impl(int, sprout::typed_slot_detail::tag<N>) {
return true;
}
template<int N>
SPROUT_CONSTEXPR bool check_impl(long, sprout::typed_slot_detail::tag<N>) {
return false;
}
template<int N>
SPROUT_CONSTEXPR bool check(bool R = sprout::typed_slot_detail::check_impl(0, sprout::typed_slot_detail::tag<N>())) {
return R;
}
template<int N>
SPROUT_CONSTEXPR int counter_impl(sprout::false_type, sprout::typed_slot_detail::tag<N>) {
return 0;
}
template<int N>
SPROUT_CONSTEXPR int counter_impl(
sprout::true_type, sprout::typed_slot_detail::tag<N>,
int R = !sprout::typed_slot_detail::check<N>() ? N
: counter_impl(sprout::bool_constant<sprout::typed_slot_detail::check<N>()>(), sprout::typed_slot_detail::tag<N + 1>())
)
{
return R;
}
template<int N = 0>
SPROUT_CONSTEXPR int counter(int R = sprout::typed_slot_detail::counter_impl(sprout::true_type(), sprout::typed_slot_detail::tag<N>())) {
return R;
}
template<int K, int N>
SPROUT_CONSTEXPR std::intmax_t get_impl(sprout::false_type, sprout::typed_slot_detail::tag<N>) {
return 0;
}
template<int K, int N>
SPROUT_CONSTEXPR std::intmax_t get_impl(
sprout::true_type, sprout::typed_slot_detail::tag<N>,
std::intmax_t R = !sprout::typed_slot_detail::check<N>() ? adl_get(sprout::typed_slot_detail::tag<N - 1>(), sprout::integral_constant<int, K>())
: get_impl<K>(sprout::bool_constant<sprout::typed_slot_detail::check<N>()>(), sprout::typed_slot_detail::tag<N + 1>())
)
{
return R;
}
template<int K, int N = 0>
SPROUT_CONSTEXPR std::intmax_t get(int R = sprout::typed_slot_detail::get_impl<K>(sprout::true_type(), sprout::typed_slot_detail::tag<N>())) {
return R;
}
} // namespace typed_slot_detail
//
// slot
//
template<
typename T,
std::intmax_t R = sprout::typed_slot_detail::get<sprout::typed_id<T>::value>()
>
SPROUT_CONSTEXPR std::intmax_t slot() {
return R;
}
//
// assign_slot
// assign_slot_return
//
template<
typename T,
std::intmax_t Value,
std::intmax_t = sprout::typed_slot_detail::state<
sprout::typed_slot_detail::counter(),
sprout::typed_id<T>::value, Value
>::value
>
SPROUT_CXX14_CONSTEXPR void assign_slot() {}
template<
typename T,
std::intmax_t Value,
std::intmax_t R = sprout::typed_slot_detail::state<
sprout::typed_slot_detail::counter(),
sprout::typed_id<T>::value, Value
>::value
>
SPROUT_CONSTEXPR std::intmax_t assign_slot_return() {
return R;
}
#endif
} // namespace sprout
#endif // #ifndef SPROUT_STATEFUL_TYPED_SLOT_HPP