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:
parent
b37695f1a4
commit
fa619fc1bf
6 changed files with 426 additions and 131 deletions
|
@ -12,5 +12,7 @@
|
|||
#include <sprout/stateful/counter.hpp>
|
||||
#include <sprout/stateful/rand.hpp>
|
||||
#include <sprout/stateful/slot.hpp>
|
||||
#include <sprout/stateful/typed_id.hpp>
|
||||
#include <sprout/stateful/typed_slot.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_STATEFUL_HPP
|
||||
|
|
|
@ -9,15 +9,11 @@
|
|||
#define SPROUT_STATEFUL_COUNTER_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/workaround/std/cstddef.hpp>
|
||||
#include <sprout/type_traits/integral_constant.hpp>
|
||||
|
||||
namespace sprout {
|
||||
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
|
||||
|
||||
namespace detail {
|
||||
SPROUT_STATIC_CONSTEXPR std::size_t counter_default_call_limit = 128;
|
||||
} // namespace detail
|
||||
|
||||
namespace counter_detail {
|
||||
#if defined(__GNUC__) && !defined(__clang__)
|
||||
# pragma GCC diagnostic push
|
||||
|
@ -43,63 +39,54 @@ namespace sprout {
|
|||
friend SPROUT_CONSTEXPR int adl_counter(sprout::counter_detail::tag<N>) {
|
||||
return N;
|
||||
}
|
||||
SPROUT_STATIC_CONSTEXPR int value = N - 1;
|
||||
SPROUT_STATIC_CONSTEXPR int value = N;
|
||||
};
|
||||
template<int N>
|
||||
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>())>
|
||||
SPROUT_CONSTEXPR int counter(
|
||||
int, sprout::counter_detail::tag<N>
|
||||
template<int N, int = adl_counter(sprout::counter_detail::tag<N>())>
|
||||
SPROUT_CONSTEXPR bool check_impl(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;
|
||||
}
|
||||
SPROUT_CONSTEXPR int counter(
|
||||
long, sprout::counter_detail::tag<0>
|
||||
)
|
||||
{
|
||||
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>())
|
||||
)
|
||||
{
|
||||
template<int N = 0>
|
||||
SPROUT_CONSTEXPR int counter(int R = sprout::counter_detail::counter_impl(sprout::true_type(), sprout::counter_detail::tag<N>())) {
|
||||
return R;
|
||||
}
|
||||
} // namespace counter_detail
|
||||
//
|
||||
// counter
|
||||
// counter_before
|
||||
//
|
||||
template<
|
||||
std::size_t Limit = sprout::detail::counter_default_call_limit,
|
||||
int N = 1,
|
||||
int R = sprout::counter_detail::state<
|
||||
sprout::counter_detail::counter(0, sprout::counter_detail::tag<Limit>()) + 1
|
||||
sprout::counter_detail::counter() + N - 1
|
||||
>::value
|
||||
>
|
||||
SPROUT_CONSTEXPR int counter() {
|
||||
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
|
||||
} // namespace sprout
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include <sprout/random/default_random_engine.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#include <sprout/random/unique_seed.hpp>
|
||||
#include <sprout/type_traits/integral_constant.hpp>
|
||||
#include <sprout/workaround/detail/uniform_int_distribution.hpp>
|
||||
|
||||
namespace sprout {
|
||||
|
@ -29,7 +30,6 @@ namespace sprout {
|
|||
> rand_result_type;
|
||||
|
||||
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 rand_detail {
|
||||
|
@ -68,7 +68,8 @@ namespace sprout {
|
|||
// generate a next random number
|
||||
SPROUT_STATIC_CONSTEXPR sprout::detail::rand_result_type result
|
||||
SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_INNER((
|
||||
!IsSrand ? sprout::rand_detail::state<
|
||||
!IsSrand
|
||||
? sprout::rand_detail::state<
|
||||
N - 1,
|
||||
adl_is_srand(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>
|
||||
SPROUT_CONSTEXPR_OR_CONST sprout::detail::rand_result_type sprout::rand_detail::state<N, IsSrand, Seed>::result
|
||||
SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_OUTER((
|
||||
!IsSrand ? sprout::rand_detail::state<
|
||||
!IsSrand
|
||||
? sprout::rand_detail::state<
|
||||
N - 1,
|
||||
adl_is_srand(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>
|
||||
SPROUT_CONSTEXPR_OR_CONST int sprout::rand_detail::state<N, IsSrand, Seed>::value;
|
||||
template<bool IsSrand, unsigned Seed>
|
||||
struct state<1, IsSrand, Seed> {
|
||||
friend SPROUT_CONSTEXPR int adl_counter(sprout::rand_detail::tag<1>) {
|
||||
return 1;
|
||||
struct state<0, IsSrand, Seed> {
|
||||
friend SPROUT_CONSTEXPR int adl_counter(sprout::rand_detail::tag<0>) {
|
||||
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;
|
||||
}
|
||||
friend SPROUT_CONSTEXPR unsigned adl_seed(sprout::rand_detail::tag<1>) {
|
||||
friend SPROUT_CONSTEXPR unsigned adl_seed(sprout::rand_detail::tag<0>) {
|
||||
return Seed;
|
||||
}
|
||||
// generate a first random number between [0, RAND_MAX]
|
||||
SPROUT_STATIC_CONSTEXPR sprout::detail::rand_result_type result
|
||||
SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_INNER(
|
||||
sprout::detail::rand_distribution_type(0, RAND_MAX)
|
||||
SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_INNER((
|
||||
!IsSrand
|
||||
? 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;
|
||||
};
|
||||
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::detail::rand_distribution_type(0, RAND_MAX)
|
||||
!IsSrand
|
||||
? 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>
|
||||
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>())>
|
||||
SPROUT_CONSTEXPR int counter(
|
||||
int, sprout::rand_detail::tag<N>
|
||||
template<int N, int = adl_counter(sprout::rand_detail::tag<N>())>
|
||||
SPROUT_CONSTEXPR bool check_impl(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;
|
||||
}
|
||||
SPROUT_CONSTEXPR int counter(
|
||||
long, sprout::rand_detail::tag<0>
|
||||
)
|
||||
{
|
||||
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>())
|
||||
)
|
||||
{
|
||||
template<int N = 0>
|
||||
SPROUT_CONSTEXPR int counter(int R = sprout::rand_detail::counter_impl(sprout::true_type(), sprout::rand_detail::tag<N>())) {
|
||||
return R;
|
||||
}
|
||||
} // namespace rand_detail
|
||||
//
|
||||
// rand
|
||||
// rand_before
|
||||
//
|
||||
template<
|
||||
std::size_t Limit = sprout::detail::rand_default_call_limit,
|
||||
int N = 1,
|
||||
int R = sprout::rand_detail::state<
|
||||
sprout::rand_detail::counter(0, sprout::rand_detail::tag<Limit>()) + 1
|
||||
sprout::rand_detail::counter() + N - 1
|
||||
>::value
|
||||
>
|
||||
SPROUT_CONSTEXPR int rand() {
|
||||
|
@ -171,18 +193,18 @@ namespace sprout {
|
|||
//
|
||||
template<
|
||||
unsigned Seed,
|
||||
std::size_t Limit = sprout::detail::rand_default_call_limit,
|
||||
int N = 1,
|
||||
int = sprout::rand_detail::state<
|
||||
sprout::rand_detail::counter(0, sprout::rand_detail::tag<Limit>()) + 1,
|
||||
sprout::rand_detail::counter() + N - 1,
|
||||
true, Seed
|
||||
>::value
|
||||
>
|
||||
SPROUT_CXX14_CONSTEXPR void srand() {}
|
||||
template<
|
||||
unsigned Seed,
|
||||
std::size_t Limit = sprout::detail::rand_default_call_limit,
|
||||
int N = 1,
|
||||
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
|
||||
>::value
|
||||
>
|
||||
|
|
|
@ -10,17 +10,12 @@
|
|||
|
||||
#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
|
||||
|
@ -46,7 +41,9 @@ namespace sprout {
|
|||
#endif
|
||||
|
||||
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>) {
|
||||
return N;
|
||||
}
|
||||
|
@ -72,18 +69,20 @@ namespace sprout {
|
|||
}
|
||||
};
|
||||
template<int Key, std::intmax_t Value>
|
||||
struct state<1, Key, Value> {
|
||||
friend SPROUT_CONSTEXPR int adl_counter(sprout::slot_detail::tag<1>) {
|
||||
return 1;
|
||||
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;
|
||||
}
|
||||
friend SPROUT_CONSTEXPR int adl_key(sprout::slot_detail::tag<1>) {
|
||||
friend SPROUT_CONSTEXPR int adl_key(sprout::slot_detail::tag<0>) {
|
||||
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;
|
||||
}
|
||||
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>();
|
||||
}
|
||||
template<int K>
|
||||
|
@ -92,58 +91,61 @@ namespace sprout {
|
|||
}
|
||||
};
|
||||
|
||||
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, int = adl_counter(sprout::slot_detail::tag<N>())>
|
||||
SPROUT_CONSTEXPR bool check_impl(int, sprout::slot_detail::tag<N>) {
|
||||
return true;
|
||||
}
|
||||
template<int N>
|
||||
SPROUT_CONSTEXPR int counter(
|
||||
long, sprout::slot_detail::tag<N>,
|
||||
int R = counter(0, sprout::slot_detail::tag<N - 1>())
|
||||
)
|
||||
{
|
||||
SPROUT_CONSTEXPR bool check_impl(long, sprout::slot_detail::tag<N>) {
|
||||
return false;
|
||||
}
|
||||
template<int N>
|
||||
SPROUT_CONSTEXPR bool check(bool R = sprout::slot_detail::check_impl(0, sprout::slot_detail::tag<N>())) {
|
||||
return R;
|
||||
}
|
||||
|
||||
template<int K, int N, std::intmax_t R = adl_get(sprout::slot_detail::tag<N>(), sprout::integral_constant<int, K>())>
|
||||
SPROUT_CONSTEXPR std::intmax_t get(
|
||||
int, sprout::slot_detail::tag<N>
|
||||
template<int N>
|
||||
SPROUT_CONSTEXPR int counter_impl(sprout::false_type, 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;
|
||||
}
|
||||
template<int K>
|
||||
SPROUT_CONSTEXPR std::intmax_t get(
|
||||
long, sprout::slot_detail::tag<0>
|
||||
)
|
||||
{
|
||||
template<int N = 0>
|
||||
SPROUT_CONSTEXPR int counter(int R = sprout::slot_detail::counter_impl(sprout::true_type(), sprout::slot_detail::tag<N>())) {
|
||||
return R;
|
||||
}
|
||||
|
||||
template<int K, int N>
|
||||
SPROUT_CONSTEXPR std::intmax_t get_impl(sprout::false_type, sprout::slot_detail::tag<N>) {
|
||||
return 0;
|
||||
}
|
||||
template<int K, int N>
|
||||
SPROUT_CONSTEXPR std::intmax_t get(
|
||||
long, sprout::slot_detail::tag<N>,
|
||||
std::intmax_t R = get<K>(0, sprout::slot_detail::tag<N - 1>())
|
||||
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>())
|
||||
)
|
||||
{
|
||||
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
|
||||
//
|
||||
// slot
|
||||
//
|
||||
template<
|
||||
int K,
|
||||
std::size_t Limit = sprout::detail::slot_default_call_limit,
|
||||
std::intmax_t R = sprout::slot_detail::get<K>(0, sprout::slot_detail::tag<Limit>())
|
||||
std::intmax_t R = sprout::slot_detail::get<K>()
|
||||
>
|
||||
SPROUT_CONSTEXPR std::intmax_t slot() {
|
||||
return R;
|
||||
|
@ -153,23 +155,19 @@ namespace sprout {
|
|||
// assign_slot_return
|
||||
//
|
||||
template<
|
||||
int K,
|
||||
std::intmax_t Value,
|
||||
std::size_t Limit = sprout::detail::slot_default_call_limit,
|
||||
int K, std::intmax_t Value,
|
||||
std::intmax_t = sprout::slot_detail::state<
|
||||
sprout::slot_detail::counter(0, sprout::slot_detail::tag<Limit>()) + 1,
|
||||
sprout::slot_detail::counter(),
|
||||
K, Value
|
||||
>::template get<K>()
|
||||
>::value
|
||||
>
|
||||
SPROUT_CXX14_CONSTEXPR void assign_slot() {}
|
||||
template<
|
||||
int K,
|
||||
std::intmax_t Value,
|
||||
std::size_t Limit = sprout::detail::slot_default_call_limit,
|
||||
int K, std::intmax_t Value,
|
||||
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
|
||||
>::template get<K>()
|
||||
>::value
|
||||
>
|
||||
SPROUT_CONSTEXPR std::intmax_t assign_slot_return() {
|
||||
return R;
|
||||
|
|
104
sprout/stateful/typed_id.hpp
Normal file
104
sprout/stateful/typed_id.hpp
Normal 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
|
182
sprout/stateful/typed_slot.hpp
Normal file
182
sprout/stateful/typed_slot.hpp
Normal 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
|
Loading…
Reference in a new issue