| 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 |
#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 |
|
| 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 |
|
| 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 |
|
| 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 |