| 1 | /* |
| 2 | * %injeqt copyright begin% |
| 3 | * Copyright 2014 Rafał Malinowski ([email protected]) |
| 4 | * %injeqt copyright end% |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <injeqt/injector.h> |
| 22 | #include <injeqt/module.h> |
| 23 | |
| 24 | #include <QtCore/QObject> |
| 25 | #include <iostream> |
| 26 | #include <memory> |
| 27 | #include <string> |
| 28 | |
| 29 | class hello_service : public QObject |
| 30 | { |
| 31 | Q_OBJECT |
| 32 | |
| 33 | public: |
| 34 | hello_service() {} |
| 35 | virtual ~hello_service() {} |
| 36 | |
| 37 | std::string say_hello() const |
| 38 | { |
| 39 | return {"Hello"}; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | class world_service : public QObject |
| 44 | { |
| 45 | Q_OBJECT |
| 46 | |
| 47 | public: |
| 48 | world_service() {} |
| 49 | virtual ~world_service() {} |
| 50 | |
| 51 | std::string say_world() const |
| 52 | { |
| 53 | return {"World"}; |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | class hello_factory : public QObject |
| 58 | { |
| 59 | Q_OBJECT |
| 60 | |
| 61 | public: |
| 62 | Q_INVOKABLE hello_factory() {} |
| 63 | virtual ~hello_factory() {} |
| 64 | |
| 65 | Q_INVOKABLE hello_service * create_service() |
| 66 | { |
| 67 | return new hello_service{}; |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | class hello_client : public QObject |
| 72 | { |
| 73 | Q_OBJECT |
| 74 | |
| 75 | public: |
| 76 | Q_INVOKABLE hello_client() : _s{nullptr}, _w{nullptr} {} |
| 77 | virtual ~hello_client() {} |
| 78 | |
| 79 | std::string say() const |
| 80 | { |
| 81 | return _s->say_hello() + " " + _w->say_world() + "!"; |
| 82 | } |
| 83 | |
| 84 | private slots: |
| 85 | INJEQT_INIT void init() |
| 86 | { |
| 87 | std::cerr << "all services set" << std::endl; |
| 88 | } |
| 89 | |
| 90 | INJEQT_DONE void done() |
| 91 | { |
| 92 | std::cerr << "ready for destruction" << std::endl; |
| 93 | } |
| 94 | |
| 95 | INJEQT_SET void set_hello_service(hello_service *s) |
| 96 | { |
| 97 | _s = s; |
| 98 | } |
| 99 | |
| 100 | INJEQT_SET void set_world_service(world_service *w) |
| 101 | { |
| 102 | _w = w; |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | hello_service *_s; |
| 107 | world_service *_w; |
| 108 | |
| 109 | }; |
| 110 | |
| 111 | class module : public injeqt::module |
| 112 | { |
| 113 | public: |
| 114 | explicit module() |
| 115 | { |
| 116 | _w = std::unique_ptr<world_service>{new world_service{}}; |
| 117 | |
| 118 | add_type<hello_client>(); |
| 119 | add_type<hello_factory>(); |
| 120 | add_factory<hello_service, hello_factory>(); |
| 121 | add_ready_object<world_service>(_w.get()); |
| 122 | } |
| 123 | |
| 124 | virtual ~module() {} |
| 125 | |
| 126 | private: |
| 127 | std::unique_ptr<world_service> _w; |
| 128 | |
| 129 | }; |
| 130 | |
| 131 | int main() |
| 132 | { |
| 133 | auto modules = std::vector<std::unique_ptr<injeqt::module>>{}; |
| 134 | modules.emplace_back(std::unique_ptr<injeqt::module>{new module{}}); |
| 135 | |
| 136 | auto injector = injeqt::injector{std::move(modules)}; |
| 137 | auto client = injector.get<hello_client>(); |
| 138 | auto hello = client->say(); |
| 139 | |
| 140 | std::cout << hello << std::endl; |
| 141 | } |
| 142 | |
| 143 | #include "hello-world.moc" |