-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquick.cpp
57 lines (48 loc) · 1.78 KB
/
quick.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// This file is part of dingo project <https://github.com/romanpauk/dingo>
//
// See LICENSE for license and copyright information
// SPDX-License-Identifier: MIT
//
// Include required dingo headers
#include <dingo/container.h>
#include <dingo/storage/shared.h>
#include <dingo/storage/unique.h>
int main() {
using namespace dingo;
////
// Class types to be managed by the container. Note that there is no special
// code required for a type to be used by the container and that conversions
// are applied automatically based on an registered type scope.
struct A {
A() {}
};
struct B {
B(A&, std::shared_ptr<A>) {}
};
struct C {
C(B*, std::unique_ptr<B>&, A&) {}
};
container<> container;
// Register struct A with a shared scope, stored as std::shared_ptr<A>
container.register_type<scope<shared>, storage<std::shared_ptr<A>>>();
// Register struct B with a shared scope, stored as std::unique_ptr<B>
container.register_type<scope<shared>, storage<std::unique_ptr<B>>>();
// Register struct C with an unique scope, stored as plain C
container.register_type<scope<unique>, storage<C>>();
// Resolving the struct C will recursively instantiate required dependencies
// (structs A and B) and inject the instances based on their scopes into C.
// As C is in unique scope, each resolve<C> will return new C instance.
// As A and B are in shared scope, each C will get the same instances
// injected.
C c = container.resolve<C>();
struct D {
A& a;
B* b;
};
// Construct an un-managed struct using dependencies from the container
D d = container.construct<D>();
// Invoke callable
D e = container.invoke([&](A& a, B* b) { return D{a, b}; });
////
}