NuriaProject Framework
0.1
The NuriaProject Framework
|
Dependency injection manager. More...
#include <dependencymanager.hpp>
Public Types | |
enum | ThreadingPolicy { DefaultPolicy, ApplicationGlobal, SingleThread, ThreadLocal } |
Public Member Functions | |
~DependencyManager () | |
ThreadingPolicy | defaultThreadingPolicy () const |
bool | hasObject (const QByteArray &name, ThreadingPolicy policy=DefaultPolicy) const |
void * | objectByName (const QByteArray &name, int type=-1, ThreadingPolicy policy=DefaultPolicy) |
int | objectType (const QByteArray &name, ThreadingPolicy policy=DefaultPolicy) const |
void | setCreator (const QByteArray &name, const std::function< QObject *() > &creator) |
void | setDefaultThreadingPolicy (ThreadingPolicy policy) |
void | storeObject (const QByteArray &name, void *object, int type, ThreadingPolicy policy=DefaultPolicy) |
template<typename T > | |
void | storeObject (const QByteArray &name, T *object) |
Static Public Member Functions | |
template<typename T > | |
static T * | get (const QByteArray &name, ThreadingPolicy policy=DefaultPolicy) |
static DependencyManager * | instance () |
Dependency injection manager.
Dependency injection is interesting whenever a class has dependencies to other utility classes. Those classes usually only have a single application-wide instance, thus those are often implemented as singletons. While using singletons is mostly easy to do, it has also its flaws:
First, you'll need to register all classes which you want to use as dependencies to the Qt meta system:
This line should come right after the class definition. Please note that the macro itself must be invoked on the global scope.
After this, you can start using it right away with the NURIA_DEPENDENCY macro:
If the name of the object doesn't match the typename, or if you need to do further initialisation work alongside the constructor, you can set a 'creator' using setCreator().
For more fine-grained control, please see the methods this class offers. If you're writing unit tests, then you're probably interested in storeObject(). Example:
Behaviours for multi-threaded applications.
Nuria::DependencyManager::~DependencyManager | ( | ) |
Destructor.
ThreadingPolicy Nuria::DependencyManager::defaultThreadingPolicy | ( | ) | const |
Returns the current default threading policy.
|
inlinestatic |
Tries to find object name of type T. On failure, nullptr
is returned.
|
inline |
Returns true
if there is object name.
|
static |
Returns the global instance of the manager.
void* Nuria::DependencyManager::objectByName | ( | const QByteArray & | name, |
int | type = -1 , |
||
ThreadingPolicy | policy = DefaultPolicy |
||
) |
Returns object name. If type is not -1
and name wasn't created yet, it will be created then, stored and returned. Else, nullptr
is returned.
-1
, it will be used as type check. If type and the type of object name don't match, nullptr is returned.int Nuria::DependencyManager::objectType | ( | const QByteArray & | name, |
ThreadingPolicy | policy = DefaultPolicy |
||
) | const |
Returns the meta type of object name or -1
if not found.
void Nuria::DependencyManager::setCreator | ( | const QByteArray & | name, |
const std::function< QObject *() > & | creator | ||
) |
Sets the creator of object name to creator. When the object name is read and has not been created yet, creator will be called.
Please note that this function is not thread-safe, as you'll use this function at the start of your application and then start threads.
void Nuria::DependencyManager::setDefaultThreadingPolicy | ( | ThreadingPolicy | policy | ) |
Sets the default threading policy. Passing DefaultPolicy has no effect. Changing the policy is not thread-safe.
void Nuria::DependencyManager::storeObject | ( | const QByteArray & | name, |
void * | object, | ||
int | type, | ||
ThreadingPolicy | policy = DefaultPolicy |
||
) |
Stores object of type as name. If there is already a object of the same name, it will be overwritten. object must be a registered type.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.