capydi
Loading...
Searching...
No Matches
DependencyTags.hpp
Go to the documentation of this file.
1#ifndef CAPYDI_DEPENDENCY_TAGS_HPP_
2#define CAPYDI_DEPENDENCY_TAGS_HPP_
3
6
9#include <initializer_list>
10#include <array>
11
12namespace capy::di
13{
14
15using DependencyTagPair = std::pair<std::size_t, tag_t>;
16
18{
19
20template<CreationalConfig Decoratee, std::size_t SIZE>
22{
23public:
24 constexpr explicit DependencyTags(
25 std::array<DependencyTagPair, SIZE> dependency_tags,
26 Decoratee&& decoratee
27 )
28 : dependency_tags_ { dependency_tags }
29 , decoratee_ { std::move(decoratee) }
30 {}
31
32public:
34 using /* meta::Pack<meta::Pack<?>> */ ResolutionKeysPack = resolution_keys_pack_t<Decoratee>;
36
37public:
39
40public:
41 template<typename... Args>
42 auto do_resolve(Args&&... args) const
43 {
44 return decoratee_.do_resolve(std::forward<Args>(args)...);
45 }
46
47 template<std::size_t DependencyIndex>
49 {
50 using DecorateeDependenciesTuple
51 = typename decltype(this->decoratee_.template get_dependencies_input<DependencyIndex>())::value_type;
52 using ResultType = decltype(std::tuple_cat(
53 std::declval<DecorateeDependenciesTuple>(),
54 std::declval<std::tuple<TagInput>>()
55 ));
56
57 const auto found_tag_pair_itor = std::find_if(
58 this->dependency_tags_.cbegin(),
59 this->dependency_tags_.cend(),
60 [](const DependencyTagPair& tag_pair) {
61 return tag_pair.first == DependencyIndex;
62 }
63 );
64
65 if (found_tag_pair_itor == this->dependency_tags_.cend()) [[unlikely]]
66 {
67 return std::optional<ResultType> {
68 std::nullopt
69 };
70 }
71
72 const auto& [_, found_tag] = *found_tag_pair_itor;
73
74 auto decoratee_dependencies_input
75 = this->decoratee_.template get_dependencies_input<DependencyIndex>();
76
77 if (decoratee_dependencies_input.has_value()) [[likely]]
78 {
79 return std::optional { std::tuple_cat(
80 std::move(decoratee_dependencies_input).value(),
81 std::tuple { TagInput {
82 found_tag
83 }}
84 )};
85 }
86 else
87 {
88 return std::optional { std::tuple_cat(
89 std::tuple{},
90 std::tuple { TagInput {
91 found_tag
92 }}
93 )};
94 }
95 }
96
97private:
98 std::array<DependencyTagPair, SIZE> dependency_tags_;
99 Decoratee decoratee_;
100};
101
102}
103
105{
106 template<CreationalConfig Decoratee, std::size_t SIZE>
108 Decoratee&& decoratee,
109 std::array<DependencyTagPair, SIZE> dependency_tags
110 ) {
112 dependency_tags,
113 std::forward<Decoratee>(decoratee)
114 };
115 }
116};
117
118}
119
120#endif // !CAPYDI_DEPENDENCY_TAGS_HPP_
Concept and utilities for creational (factory/constructor) configurations.
Compile-time type pack utilities and metaprogramming foundations.
meta::wrapped_with< std::optional > auto get_dependencies_input() const
Definition DependencyTags.hpp:48
static constexpr ConfigType CONFIG_TYPE
Definition DependencyTags.hpp:38
central_type_t< Decoratee > CentralType
Definition DependencyTags.hpp:33
dependencies_pack_t< Decoratee > DependenciesPack
Definition DependencyTags.hpp:35
resolution_keys_pack_t< Decoratee > ResolutionKeysPack
Definition DependencyTags.hpp:34
constexpr DependencyTags(std::array< DependencyTagPair, SIZE > dependency_tags, Decoratee &&decoratee)
Definition DependencyTags.hpp:24
auto do_resolve(Args &&... args) const
Definition DependencyTags.hpp:42
Definition WrappedWIth.hpp:30
Definition DependencyTags.hpp:18
Definition Decorator.hpp:19
typename Config::DependenciesPack dependencies_pack_t
Definition CreationalConfig.hpp:82
typename Config::ResolutionKeysPack resolution_keys_pack_t
Helper alias to extract the resolution keys from a CreationalConfig.
Definition CreationalConfig.hpp:79
ConfigType
Categorization of configuration strategies in the DI container.
Definition ConfigType.hpp:24
@ CREATIONAL
Creational configs handle object instantiation and factory patterns. These are responsible for creati...
Definition ConfigType.hpp:27
std::pair< std::size_t, tag_t > DependencyTagPair
Definition DependencyTags.hpp:15
typename Config::CentralType central_type_t
Helper alias to extract the central type from a CreationalConfig.
Definition CreationalConfig.hpp:75
Definition DependencyTags.hpp:105
static constexpr implementation_details_::DependencyTags< Decoratee, SIZE > decorate(Decoratee &&decoratee, std::array< DependencyTagPair, SIZE > dependency_tags)
Definition DependencyTags.hpp:107
Definition TagInput.hpp:12