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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/*
* %injeqt copyright begin%
* Copyright 2014 RafaĆ Malinowski (rafal.przemyslaw.malinowski@gmail.com)
* %injeqt copyright end%
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <injeqt/injector.h>
#include <injeqt/module.h>
#include <QtCore/QObject>
#include <iostream>
#include <memory>
#include <string>
class hello_service : public QObject
{
Q_OBJECT
public:
hello_service() {}
virtual ~hello_service() {}
std::string say_hello() const
{
return {"Hello"};
}
};
class world_service : public QObject
{
Q_OBJECT
public:
world_service() {}
virtual ~world_service() {}
std::string say_world() const
{
return {"World"};
}
};
class hello_factory : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE hello_factory() {}
virtual ~hello_factory() {}
Q_INVOKABLE hello_service * create_service()
{
return new hello_service{};
}
};
class hello_client : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE hello_client() : _s{nullptr}, _w{nullptr} {}
virtual ~hello_client() {}
std::string say() const
{
return _s->say_hello() + " " + _w->say_world() + "!";
}
private slots:
INJEQT_INIT void init()
{
std::cerr << "all services set" << std::endl;
}
INJEQT_DONE void done()
{
std::cerr << "ready for destruction" << std::endl;
}
INJEQT_SET void set_hello_service(hello_service *s)
{
_s = s;
}
INJEQT_SET void set_world_service(world_service *w)
{
_w = w;
}
private:
hello_service *_s;
world_service *_w;
};
class module : public injeqt::module
{
public:
explicit module()
{
_w = std::unique_ptr<world_service>{new world_service{}};
add_type<hello_client>();
add_type<hello_factory>();
add_factory<hello_service, hello_factory>();
add_ready_object<world_service>(_w.get());
}
virtual ~module() {}
private:
std::unique_ptr<world_service> _w;
};
int main()
{
auto modules = std::vector<std::unique_ptr<injeqt::module>>{};
modules.emplace_back(std::unique_ptr<injeqt::module>{new module{}});
auto injector = injeqt::injector{std::move(modules)};
auto client = injector.get<hello_client>();
auto hello = client->say();
std::cout << hello << std::endl;
}
#include "hello-world.moc"
|