Use utempl counter and injector instead custom realisation
This commit is contained in:
parent
888ec0f342
commit
f98f8484d0
1 changed files with 9 additions and 52 deletions
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <utempl/utils.hpp>
|
#include <utempl/utils.hpp>
|
||||||
|
#include <utempl/loopholes/counter.hpp>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
namespace cserver {
|
namespace cserver {
|
||||||
|
@ -99,49 +100,6 @@ struct ServiceContext {
|
||||||
|
|
||||||
namespace impl {
|
namespace impl {
|
||||||
|
|
||||||
namespace loopholes {
|
|
||||||
|
|
||||||
template <auto I>
|
|
||||||
struct Getter {
|
|
||||||
friend constexpr auto Magic(Getter<I>);
|
|
||||||
};
|
|
||||||
template <auto I, auto Value = 0>
|
|
||||||
struct Injector {
|
|
||||||
friend constexpr auto Magic(Getter<I>) {return Value;};
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
template <auto I, typename...>
|
|
||||||
concept Injected = requires{Magic(Getter<I>{});};
|
|
||||||
|
|
||||||
template <typename Tag, auto Value>
|
|
||||||
struct TagWithTalue {};
|
|
||||||
|
|
||||||
|
|
||||||
template <auto I = 0, typename Tag, typename... Ts, auto = Injector<TagWithTalue<Tag, I>{}>{}>
|
|
||||||
constexpr auto CounterImpl(...) {
|
|
||||||
return I;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <auto I = 0, typename Tag, typename... Ts>
|
|
||||||
consteval auto CounterImpl(std::size_t arg) requires Injected<TagWithTalue<Tag, I>{}, Ts...> {
|
|
||||||
return CounterImpl<I + 1, Tag, Ts...>(arg);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
template <
|
|
||||||
typename Tag,
|
|
||||||
typename... Ts,
|
|
||||||
auto R = CounterImpl<0, Tag, Ts...>(std::size_t{})
|
|
||||||
>
|
|
||||||
consteval auto Counter(auto...) {
|
|
||||||
return R;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace loopholes
|
|
||||||
|
|
||||||
|
|
||||||
template <typename Current, std::size_t I>
|
template <typename Current, std::size_t I>
|
||||||
struct DependencyInfoKey {};
|
struct DependencyInfoKey {};
|
||||||
|
|
||||||
|
@ -176,8 +134,8 @@ public:
|
||||||
template <
|
template <
|
||||||
utempl::ConstexprString name,
|
utempl::ConstexprString name,
|
||||||
typename...,
|
typename...,
|
||||||
std::size_t I = loopholes::Counter<Current, utempl::Wrapper<name>>(),
|
std::size_t I = utempl::loopholes::Counter<Current, utempl::Wrapper<name>>(),
|
||||||
auto = loopholes::Injector<
|
auto = utempl::loopholes::Injector<
|
||||||
DependencyInfoKey<
|
DependencyInfoKey<
|
||||||
Current,
|
Current,
|
||||||
I
|
I
|
||||||
|
@ -186,12 +144,11 @@ public:
|
||||||
>{}
|
>{}
|
||||||
>
|
>
|
||||||
static constexpr auto FindComponent() -> FindComponentType<name>&;
|
static constexpr auto FindComponent() -> FindComponentType<name>&;
|
||||||
|
|
||||||
template <
|
template <
|
||||||
typename T,
|
typename T,
|
||||||
typename...,
|
typename...,
|
||||||
std::size_t I = loopholes::Counter<Current, utempl::Wrapper<FindComponentName<T>>>(),
|
std::size_t I = utempl::loopholes::Counter<Current, utempl::Wrapper<FindComponentName<T>>>(),
|
||||||
auto = loopholes::Injector<
|
auto = utempl::loopholes::Injector<
|
||||||
DependencyInfoKey<
|
DependencyInfoKey<
|
||||||
Current,
|
Current,
|
||||||
I
|
I
|
||||||
|
@ -203,14 +160,14 @@ public:
|
||||||
|
|
||||||
template <std::size_t I = 0, utempl::ConstexprString... names>
|
template <std::size_t I = 0, utempl::ConstexprString... names>
|
||||||
static consteval auto GetDependencies() requires (I == 0 ||
|
static consteval auto GetDependencies() requires (I == 0 ||
|
||||||
requires {Magic(loopholes::Getter<DependencyInfoKey<Current, I>{}>{});}) {
|
requires {Magic(utempl::loopholes::Getter<DependencyInfoKey<Current, I>{}>{});}) {
|
||||||
if constexpr(I == 0 && !requires {Magic(loopholes::Getter<DependencyInfoKey<Current, I>{}>{});}) {
|
if constexpr(I == 0 && !requires {Magic(utempl::loopholes::Getter<DependencyInfoKey<Current, I>{}>{});}) {
|
||||||
return utempl::Tuple{};
|
return utempl::Tuple{};
|
||||||
} else {
|
} else {
|
||||||
if constexpr(requires{GetDependencies<I + 1, names...>();}) {
|
if constexpr(requires{GetDependencies<I + 1, names...>();}) {
|
||||||
return GetDependencies<I + 1, names..., Magic(loopholes::Getter<DependencyInfoKey<Current, I>{}>{})>();
|
return GetDependencies<I + 1, names..., Magic(utempl::loopholes::Getter<DependencyInfoKey<Current, I>{}>{})>();
|
||||||
} else {
|
} else {
|
||||||
return utempl::Tuple{names..., Magic(loopholes::Getter<DependencyInfoKey<Current, I>{}>{})};
|
return utempl::Tuple{names..., Magic(utempl::loopholes::Getter<DependencyInfoKey<Current, I>{}>{})};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue