Debian Codegraph @

src/dependencyInjector/DependencyInjector.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2021 Damir Porobic <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /*
21 * Implementation based on the Miniature IOC Container from phillipvoyle on codeproject.com, kudos to him.
22 * Link: https://www.codeproject.com/Articles/1029836/A-Miniature-IOC-Container-in-Cplusplus
23 */
24
25 #ifndef KSNIP_DEPENDENCYINJECTOR_H
26 #define KSNIP_DEPENDENCYINJECTOR_H
27
28 #include <QSharedPointer>
29 #include <QMap>
30
31 class DependencyInjector
32 {
33 private:
34 static int NextTypeId;
35
36 public:
37
38 //one typeid per type
39 template<typename T>
40 static int getTypeID()
41 {
42 static int typeId = NextTypeId++;
43 return typeId;
44 }
45
46 class FactoryRoot
47 {
48 public:
49 virtual ~FactoryRoot() = default;
50 };
51
52 QMap<int, QSharedPointer<FactoryRoot>> mFactories;
53
54 template<typename T>
55 class CFactory: public FactoryRoot
56 {
57 private:
58 std::function<QSharedPointer<T> ()> mFunctor;
59
60 public:
61 ~CFactory() override = default;
62
63 explicit CFactory(std::function<QSharedPointer<T> ()> functor) : mFunctor(functor)
64 {
65 }
66
67 QSharedPointer<T> getObject()
68 {
69 return mFunctor();
70 }
71 };
72
73 template<typename T>
74 QSharedPointer<T> get()
75 {
76 auto typeId = getTypeID<T>();
77 auto factoryBase = mFactories[typeId];
78 auto factory = qSharedPointerCast<CFactory<T>>(factoryBase);
79 return factory->getObject();
80 }
81
82 template<typename TInterface, typename ...TS>
83 void registerFunctor(std::function<QSharedPointer<TInterface>
84 (QSharedPointer<TS> ...ts)> functor)
85 {
86 mFactories[getTypeID<TInterface>()] = QSharedPointer<CFactory<TInterface>>::create([=]{
87 return functor(get<TS>()...);
88 });
89 }
90
91 // Register a single instance, always returns same instance, effectively single instance
92 template<typename TInterface>
93 void registerInstance(QSharedPointer<TInterface> t)
94 {
95 mFactories[getTypeID<TInterface>()] =
96 QSharedPointer<CFactory<TInterface>>::create([=]{
97 return t;
98 });
99 }
100
101 template<typename TInterface, typename ...TS>
102 void registerFunctor(QSharedPointer<TInterface> (*functor)(QSharedPointer<TS> ...ts))
103 {
104 registerFunctor(std::function<QSharedPointer<TInterface>
105 (QSharedPointer<TS> ...ts)>(functor));
106 }
107
108 // Returns for every request a new instance
109 template<typename TInterface, typename TConcrete, typename ...TArguments>
110 void registerFactory()
111 {
112 registerFunctor(
113 std::function<QSharedPointer<TInterface>(QSharedPointer<TArguments> ...ts)>(
114 [](QSharedPointer<TArguments>...arguments) -> QSharedPointer<TInterface> {
115 return QSharedPointer<TConcrete>::create(std::forward<QSharedPointer<TArguments>>(arguments)...);
116 }));
117 }
118
119 template<typename TInterface, typename TConcrete, typename ...TArguments>
120 void registerInstance()
121 {
122 registerInstance<TInterface>(QSharedPointer<TConcrete>::create(get<TArguments>()...));
123 }
124 };
125
126 #endif //KSNIP_DEPENDENCYINJECTOR_H