capydi
Loading...
Searching...
No Matches
Singleton.hpp
Go to the documentation of this file.
1#ifndef SINGLETON_HPP_
2#define SINGLETON_HPP_
3
6
11#include <tuple>
12#include <expected>
13
14namespace capy::di
15{
16
17template<typename Type>
19 : public DecoratableConfig<
20 Singleton<Type>
21 >
22{
23public:
24 using CentralType = Type;
25 using /* meta::Pack<meta::Pack<?>> */ ResolutionKeysPack = meta::Pack<
28 >;
29
30 using DependenciesPack = meta::args_pack_t<decltype(CentralType::create)>;
31
32public:
34
35public:
36 template<typename... Dependencies>
37 std::expected<meta::RuntimeRef<Type>, Error> do_resolve(
38 meta::Pack<Type> keys,
39 std::tuple<Dependencies...>& dependencies,
41 const auto& input
42 ) const
43 {
44 if (!this->singleton_value_.has_value())
45 {
46 this->singleton_value_.emplace(
47 std::apply(Type::create, dependencies)
48 );
49
50 context.flags.just_created = true;
51 }
52
53 return meta::RuntimeRef<Type> { this->singleton_value_.value() };
54 }
55
56 template<typename... Dependencies>
57 std::expected<meta::RuntimeRef<const Type>, Error> do_resolve(
59 std::tuple<Dependencies...>& dependencies,
61 const auto& input
62 ) const
63 {
65 this->do_resolve(meta::Pack<Type>{}, dependencies, context, input).value()
66 };
67 }
68
69 template<std::size_t DependencyIndex>
70 std::optional<std::tuple<>> get_dependencies_input() const
71 {
72 return std::nullopt;
73 }
74
75private:
76 mutable std::optional<Type> singleton_value_ = std::nullopt;
77};
78
79}
80
81#ifndef GENERATE_UNIQUE_NAME
82# define GENERATE_UNIQUE_NAME SOME_UNIQUE_NAME
83#endif
84
85#endif // !SINGLETON_HPP
Configuration type enumeration for the dependency injection system.
Compile-time type pack utilities and metaprogramming foundations.
Definition Singleton.hpp:22
Type CentralType
Definition Singleton.hpp:24
std::expected< meta::RuntimeRef< Type >, Error > do_resolve(meta::Pack< Type > keys, std::tuple< Dependencies... > &dependencies, meta::wrapped_with< ResolutionContext > auto &context, const auto &input) const
Definition Singleton.hpp:37
static constexpr ConfigType CONFIG_TYPE
Definition Singleton.hpp:33
std::optional< std::tuple<> > get_dependencies_input() const
Definition Singleton.hpp:70
std::expected< meta::RuntimeRef< const Type >, Error > do_resolve(meta::Pack< const Type > keys, std::tuple< Dependencies... > &dependencies, meta::wrapped_with< ResolutionContext > auto &context, const auto &input) const
Definition Singleton.hpp:57
meta::args_pack_t< decltype(CentralType::create)> DependenciesPack
Definition Singleton.hpp:30
meta::Pack< meta::Pack< Type >, meta::Pack< const Type > > ResolutionKeysPack
Definition Singleton.hpp:25
Definition RuntimeRef.hpp:13
Definition WrappedWIth.hpp:30
Definition Decorator.hpp:19
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
Error
Enumeration of possible errors during dependency injection resolution.
Definition Error.hpp:26
typename function_traits< Function >::Params args_pack_t
Definition FunctionTraits.hpp:34
Definition DecoratableConfig.hpp:9
A compile-time heterogeneous type list.
Definition Pack.hpp:70