| 1 | injeqt |
| 2 | ====== |
| 3 | |
| 4 | Simple dependency injection framework for Qt |
| 5 | |
| 6 | Documentation |
| 7 | ------------- |
| 8 | |
| 9 | Documentation available at [github](https://github.com/vogel/injeqt/wiki). |
| 10 | |
| 11 | Example |
| 12 | ------- |
| 13 | |
| 14 | Here is example of what can be done using injeqt: |
| 15 | |
| 16 | #include <injeqt/injector.h> |
| 17 | #include <injeqt/module.h> |
| 18 | |
| 19 | #include <QtCore/QObject> |
| 20 | #include <iostream> |
| 21 | #include <memory> |
| 22 | #include <string> |
| 23 | |
| 24 | class hello_service : public QObject |
| 25 | { |
| 26 | Q_OBJECT |
| 27 | |
| 28 | public: |
| 29 | hello_service() {} |
| 30 | virtual ~hello_service() {} |
| 31 | |
| 32 | std::string say_hello() const |
| 33 | { |
| 34 | return {"Hello"}; |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | class world_service : public QObject |
| 39 | { |
| 40 | Q_OBJECT |
| 41 | |
| 42 | public: |
| 43 | world_service() {} |
| 44 | virtual ~world_service() {} |
| 45 | |
| 46 | std::string say_world() const |
| 47 | { |
| 48 | return {"World"}; |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | class hello_factory : public QObject |
| 53 | { |
| 54 | Q_OBJECT |
| 55 | |
| 56 | public: |
| 57 | Q_INVOKABLE hello_factory() {} |
| 58 | virtual ~hello_factory() {} |
| 59 | |
| 60 | Q_INVOKABLE hello_service * create_service() |
| 61 | { |
| 62 | return new hello_service{}; |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | class hello_client : public QObject |
| 67 | { |
| 68 | Q_OBJECT |
| 69 | |
| 70 | public: |
| 71 | Q_INVOKABLE hello_client() : _s{nullptr}, _w{nullptr} {} |
| 72 | virtual ~hello_client() {} |
| 73 | |
| 74 | std::string say() const |
| 75 | { |
| 76 | return _s->say_hello() + " " + _w->say_world() + "!"; |
| 77 | } |
| 78 | |
| 79 | private slots: |
| 80 | INJEQT_INIT void init() |
| 81 | { |
| 82 | std::cerr << "all services set" << std::endl; |
| 83 | } |
| 84 | |
| 85 | INJEQT_DONE void done() |
| 86 | { |
| 87 | std::cerr << "ready for destruction" << std::endl; |
| 88 | } |
| 89 | |
| 90 | INJEQT_SET void set_hello_service(hello_service *s) |
| 91 | { |
| 92 | _s = s; |
| 93 | } |
| 94 | |
| 95 | INJEQT_SET void set_world_service(world_service *w) |
| 96 | { |
| 97 | _w = w; |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | hello_service *_s; |
| 102 | world_service *_w; |
| 103 | |
| 104 | }; |
| 105 | |
| 106 | class module : public injeqt::module |
| 107 | { |
| 108 | public: |
| 109 | explicit module() |
| 110 | { |
| 111 | _w = std::unique_ptr<world_service>{new world_service{}}; |
| 112 | |
| 113 | add_type<hello_client>(); |
| 114 | add_type<hello_factory>(); |
| 115 | add_factory<hello_service, hello_factory>(); |
| 116 | add_ready_object<world_service>(_w.get()); |
| 117 | } |
| 118 | |
| 119 | virtual ~module() {} |
| 120 | |
| 121 | private: |
| 122 | std::unique_ptr<world_service> _w; |
| 123 | |
| 124 | }; |
| 125 | |
| 126 | int main() |
| 127 | { |
| 128 | auto modules = std::vector<std::unique_ptr<injeqt::module>>{}; |
| 129 | modules.emplace_back(std::unique_ptr<injeqt::module>{new module{}}); |
| 130 | |
| 131 | auto injector = injeqt::injector{std::move(modules)}; |
| 132 | auto client = injector.get<hello_client>(); |
| 133 | auto hello = client->say(); |
| 134 | |
| 135 | std::cout << hello << std::endl; |
| 136 | } |
| 137 | |
| 138 | #include "hello-world.moc" |
| 139 | |
| 140 | In that example we can see two main services names `hello_service` and `world_service`. There |
| 141 | is also client of these names `hello_client`. In `module` class we configure how we create |
| 142 | and access these instances. |
| 143 | |
| 144 | `hello_client` is added using `add_type` function. It means that injeqt will try to create it |
| 145 | using default constructor. We provide that by declaration of `Q_INVOKABLE hello_client()` |
| 146 | (`Q_INVOKABLE` is requires by Qt's meta object system). |
| 147 | |
| 148 | `hello_service` is added using `add_factory` function. It means that injeqt will first try to |
| 149 | create a `hello_factory` object, then it will look for a method of that objet that returns |
| 150 | `hello_service` object. It will find `Q_INVOKABLE hello_service * create_service()` and use it. |
| 151 | |
| 152 | To be able to create `hello_factory` injeqt must know about it, so we also add it using `add_type` |
| 153 | method. |
| 154 | |
| 155 | Last, `world_service`, is added as a ready object - provided from outside of injeqt scope. |
| 156 | |
| 157 | In `main` method list of conifguration modules are passed to newly created `injector` instance. |
| 158 | From that moment, we can use `injector` to create and manage our services. Just one line below |
| 159 | an `hello_client` instance is required. This is what happens next: |
| 160 | |
| 161 | * injeqt looks for dependencies of `hello_client` and found that it first needs to create `hello_factory` |
| 162 | * injeqt creates `hello_factory` without problems, as it does not have dependencies of its own |
| 163 | * injeqt adds new instance to object pool |
| 164 | * injeqt calls `hello_factory::create_service()` methods and ads its result to object pool |
| 165 | * now all dependencies of `hello_client` are available, so new instance of it is created with |
| 166 | default constructor and its added to objec tpool |
| 167 | * all methods of `hello_client` marked with `INJEQT_SET` are called with proper objects from pool |
| 168 | * all methods of `hello_client` marked with `INJEQT_INIT` are called |
| 169 | * this instance is returned to caller |
| 170 | * before injector is destructed, all methods of `hello_client` marked with `INJEQT_DONE` are called |