Sauce-0.10.1: sauce/provider.h Source File

Sauce-0.10.1 A C++ Dependency Injection Framework

- Main Page
- Classes
- Files

- File List

provider.h
1 #ifndef SAUCE_PROVIDER_H_
2 #define SAUCE_PROVIDER_H_
3
4 #include <cassert>
5
6 #include <sauce/memory.h>
7 #include <sauce/internal/disposal_deleter.h>
8 #include <sauce/internal/key.h>
9
10 namespace sauce {
11
12 class Injector;
13
14 template<typename Dependency>
15 class Provider;
16
20 template<typename Dependency>
21 class Provider {
22 public:
23
27 typedef typename i::Key<Dependency>::Iface Provides;
28
29 virtual ~Provider() {}
30
34 virtual typename i::Key<Dependency>::Ptr get() = 0;
35
36 };
37
41 template<typename Dependency>
42 class AbstractProvider: public Provider<Dependency> {
43
44 typedef typename i::Key<Dependency>::Iface Iface;
45 typedef AbstractProvider<Dependency> Abstract;
46 typedef i::DisposalDeleter<Iface, Abstract> Deleter;
47
48 friend class i::DisposalDeleter<Iface, Abstract>;
49
50 sauce::weak_ptr<Abstract> weak;
51
55 virtual Iface * provide() = 0;
56
60 virtual void dispose(Iface *) = 0;
61
62 public:
63
64 typedef Abstract RequestsSelfInjection;
65
66 virtual ~AbstractProvider() {}
67
68 void setSelf(sauce::weak_ptr<Abstract> weak) {
69 assert(weak.lock().get() == this);
70 this->weak = weak;
71 }
72
79 sauce::shared_ptr<Iface> get() {
80 sauce::shared_ptr<Abstract> self = weak.lock();
81 assert(self.get() == this);
82 Deleter deleter(sauce::static_pointer_cast<Abstract>(self));
83 return sauce::shared_ptr<Iface>(provide(), deleter);
84 }
85
86 };
87
88 }
89
90 #endif // SAUCE_PROVIDER_H_