Aria
2.0.0
C++23 MVVM framework (C++20 minimum) — reactive, coroutine-first, ABI-layered
Toggle main menu visibility
Loading...
Searching...
No Matches
container.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include "
aria/abi/export.hpp
"
4
#include <any>
5
#include <functional>
6
#include <memory>
7
#include <stdexcept>
8
#include <string>
9
#include <typeindex>
10
11
namespace
aria::runtime
{
12
37
class
ARIA_RUNTIME_API
Container
{
38
public
:
39
Container
();
40
~Container
();
41
42
Container
(
const
Container
&) =
delete
;
43
Container
&
operator=
(
const
Container
&) =
delete
;
44
Container
(
Container
&&) =
delete
;
45
Container
&
operator=
(
Container
&&) =
delete
;
46
47
template
<
typename
Interface,
typename
Impl>
48
void
register_singleton
() {
49
std::shared_ptr<Interface> ptr = std::make_shared<Impl>();
50
register_instance_<Interface>(std::move(ptr));
51
}
52
53
template
<
typename
Interface>
54
void
register_instance
(std::shared_ptr<Interface> ptr) {
55
register_instance_<Interface>(std::move(ptr));
56
}
57
58
template
<
typename
Interface,
typename
Impl>
59
void
register_transient
() {
60
std::function<std::shared_ptr<Interface>()> typed =
61
[]() -> std::shared_ptr<Interface> {
return
std::make_shared<Impl>(); };
62
do_register_(
typeid
(Interface), std::any(std::move(typed)),
true
);
63
}
64
65
template
<
typename
Interface>
66
void
register_factory
(std::function<std::shared_ptr<Interface>()> fn) {
67
do_register_(
typeid
(Interface), std::any(std::move(fn)),
true
);
68
}
69
70
template
<
typename
Interface>
71
[[nodiscard]] std::shared_ptr<Interface>
resolve
() {
72
auto
found = do_find_(
typeid
(Interface));
73
if
(found) {
74
if
(found->factory) {
75
const
auto
& fn = std::any_cast<
76
const
std::function<std::shared_ptr<Interface>()>&>(found->payload);
77
return
fn();
78
}
79
return
std::any_cast<const std::shared_ptr<Interface>&>(found->payload);
80
}
81
throw
std::runtime_error(std::string(
"Container: not registered: "
)
82
+
typeid
(Interface).name());
83
}
84
85
template
<
typename
Interface>
86
[[nodiscard]]
bool
has
()
const
{
87
return
do_has_(
typeid
(Interface));
88
}
89
92
void
clear
();
93
94
private
:
95
template
<
typename
Interface>
96
void
register_instance_(std::shared_ptr<Interface> ptr) {
97
do_register_(
typeid
(Interface), std::any(std::move(ptr)),
false
);
98
}
99
100
struct
Registration {
101
std::any payload;
102
bool
factory;
103
};
104
105
void
do_register_(std::type_index ti, std::any payload,
bool
factory);
106
std::shared_ptr<const Registration> do_find_(std::type_index ti)
const
;
107
bool
do_has_(std::type_index ti)
const
;
108
109
struct
Impl;
110
// RAII pImpl; C4251 on the shared_ptr member is a false positive for an
111
// incomplete opaque pointee consumed only via non-template API.
112
#ifdef _MSC_VER
113
# pragma warning(push)
114
# pragma warning(disable: 4251)
115
#endif
116
std::shared_ptr<Impl> impl_;
117
#ifdef _MSC_VER
118
# pragma warning(pop)
119
#endif
120
};
121
122
}
// namespace aria::runtime
aria::runtime::Container::register_instance
void register_instance(std::shared_ptr< Interface > ptr)
Definition
container.hpp:54
aria::runtime::Container::~Container
~Container()
aria::runtime::Container::operator=
Container & operator=(const Container &)=delete
aria::runtime::Container::register_singleton
void register_singleton()
Definition
container.hpp:48
aria::runtime::Container::Container
Container(const Container &)=delete
aria::runtime::Container::operator=
Container & operator=(Container &&)=delete
aria::runtime::Container::has
bool has() const
Definition
container.hpp:86
aria::runtime::Container::resolve
std::shared_ptr< Interface > resolve()
Definition
container.hpp:71
aria::runtime::Container::Container
Container()
aria::runtime::Container::Container
Container(Container &&)=delete
aria::runtime::Container::clear
void clear()
Release every registration in reverse registration order (L-40).
aria::runtime::Container::register_factory
void register_factory(std::function< std::shared_ptr< Interface >()> fn)
Definition
container.hpp:66
aria::runtime::Container::register_transient
void register_transient()
Definition
container.hpp:59
export.hpp
ARIA_RUNTIME_API
#define ARIA_RUNTIME_API
Definition
export.hpp:30
aria::runtime
Definition
container.hpp:11
modules
runtime
include
aria
runtime
container.hpp
Generated by
1.18.0