capydi
Loading...
Searching...
No Matches
Container.hpp
Go to the documentation of this file.
1
44
45#ifndef CONTAINER_TRY_HPP_
46#define CONTAINER_TRY_HPP_
47
54#include "ResolutionContext.hpp"
55
60#include <tuple>
61#include <utility>
62
63
64namespace capy::di
65{
66
84
85template<typename... Configs>
86class DI
87{
88private:
89 using ConfigsPack = meta::Pack<Configs...>;
90 using CreationalConfigsPack = meta::pack_filter_t<
91 ConfigsPack,
93 >;
94 using ChainableConfigsPack = meta::pack_filter_t<
95 ConfigsPack,
97 >;
98
99 using CreationalDispatcherType = meta::rebind_pack_t<
100 CreationalConfigsPack,
102 >;
103
104 using ChainableDispatcherType = meta::rebind_pack_t<
105 ChainableConfigsPack,
107 >;
108
109public:
110 explicit constexpr DI(Configs&&... configs)
111 : creational_dispatcher_ { make_dispatcher(
112 meta::Unit<CreationalDispatcherType>{},
115 configs...
116 )
117 )}
118 , chainable_dispatcher_ { make_dispatcher(
119 meta::Unit<ChainableDispatcherType>{},
121 meta::UnaryMetaFunction<IsChainableConfig>{},
122 configs...
123 )
124 )}
125 {}
126
127public:
128 template<
129 typename Type,
130 typename KeyPack = meta::Pack<Type>,
131 typename InputType = std::tuple<>
132 >
133 [[nodiscard]] constexpr Resolution<Type, Error> auto
134 resolve(InputType&& input = std::tuple{}) const
135 {
136 /* TODO: implement dispatcher for retrieving key */
137 // using /* meta::Pack<?> */ KeyPack = meta::Pack<Type>;
138
139 ResolutionContext context {
140 .input = std::move(input),
141 .container = *this,
142 .flags = ResolutionFlags{},
143 };
144
145 return this->creational_dispatcher_
146 .template resolve<Type, KeyPack>(context)
147 .and_then([this, &context](meta::Reference<Type> auto entity) {
148 return this->chainable_dispatcher_
149 .template apply_configs_chain<KeyPack, Type>(
150 entity,
151 context
152 );
153 });
154 }
155
156private:
157 template<typename Dispatcher, typename... Configs_>
158 [[nodiscard]] static constexpr Dispatcher
159 make_dispatcher(
160 meta::Unit<Dispatcher>&&,
161 std::tuple<Configs_...>&& configs_tuple
162 )
163 {
164 return std::apply(
165 [](auto&&... configs) {
166 return Dispatcher(
167 std::forward<decltype(configs)>(configs)...
168 );
169 },
170 std::move(configs_tuple)
171 );
172 }
173
174private:
175 CreationalDispatcherType creational_dispatcher_;
176 ChainableDispatcherType chainable_dispatcher_;
177};
178
179}
180
181#endif // !CONTAINER_TRY_HPP_
Concept and utilities for chainable (decorator/pipeline) configurations.
Concept and utilities for creational (factory/constructor) configurations.
Compile-time type pack utilities and metaprogramming foundations.
Definition ChainableConfigDispatcher.hpp:24
Definition CreationalConfigDispatcher.hpp:31
constexpr DI(Configs &&... configs)
Definition Container.hpp:110
constexpr Resolution< Type, Error > auto resolve(InputType &&input=std::tuple{}) const
Definition Container.hpp:134
Definition Resolution.hpp:30
Definition Decorator.hpp:19
constexpr auto filter_configs(meta::UnaryMetaFunction< Predicate > &&predicates, Configs &... configs) noexcept
Definition ConfigClassifier.hpp:112
Definition Rebind.hpp:7
typename RebindPack< Pack_, Template >::type rebind_pack_t
Convenience alias for RebindPack.
Definition Pack.hpp:164
legacy::filter_t< Pack_, as_unary_fv< Predicate >::template Functor > pack_filter_t
Definition Filter.hpp:18
Definition ResolutionContext.hpp:17
Definition ResolutionContext.hpp:8
A compile-time heterogeneous type list.
Definition Pack.hpp:70
A tag for template template parameters representing unary predicates.
Definition Pack.hpp:131
Definition Template.hpp:43