NuriaProject Framework  0.1
The NuriaProject Framework
Public Types | Public Member Functions | Static Public Member Functions | List of all members
Nuria::DependencyManager Class Reference

Dependency injection manager. More...

#include <dependencymanager.hpp>

Inheritance diagram for Nuria::DependencyManager:

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 DependencyManagerinstance ()
 

Detailed Description

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:

  1. It requires additional methods to be implemented. Granted this is easy, but it's also really repetitive.
  2. It's really hard to test classes which rely on singletons.
Usage

First, you'll need to register all classes which you want to use as dependencies to the Qt meta system:

Q_DECLARE_METATYPE(MyType*)

This line should come right after the class definition. Please note that the macro itself must be invoked on the global scope.

Note
'MyType' must inherit QObject.

After this, you can start using it right away with the NURIA_DEPENDENCY macro:

MyType *myType = NURIA_DEPENDENCY(MyType)
Requirements for dependency classes
If DependencyManager should create instances on-demand, a constructor which takes zero (Or only optional ones) arguments must be annotated using Q_INVOKABLE.
Using creators

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().

Note
This is especially helpful in multi-threaded applications!
Usage in unit-tests

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:

Note
You have to supply a name here as the code won't be able to figure out the name itself.

Member Enumeration Documentation

Behaviours for multi-threaded applications.

See also
defaultBehaviour
Enumerator
DefaultPolicy 

Maps to the current default policy.

ApplicationGlobal 

One pool for all objects, but with a mutex guarding the internal structures.

SingleThread 

One pool for all objects, with no mutex guards. Use this setting for single-threaded applications.

ThreadLocal 

One pool per thread. Objects are freed when the thread gets destroyed, though some structures can only be freed upon application exit.

Note
This is the default behaviour

Constructor & Destructor Documentation

Nuria::DependencyManager::~DependencyManager ( )

Destructor.

Member Function Documentation

ThreadingPolicy Nuria::DependencyManager::defaultThreadingPolicy ( ) const

Returns the current default threading policy.

See also
setDefaultThreadingPolicy ThreadingPolicy
template<typename T >
static T* Nuria::DependencyManager::get ( const QByteArray &  name,
ThreadingPolicy  policy = DefaultPolicy 
)
inlinestatic

Tries to find object name of type T. On failure, nullptr is returned.

bool Nuria::DependencyManager::hasObject ( const QByteArray &  name,
ThreadingPolicy  policy = DefaultPolicy 
) const
inline

Returns true if there is object name.

static DependencyManager* Nuria::DependencyManager::instance ( )
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.

Note
If type is not -1, it will be used as type check. If type and the type of object name don't match, nullptr is returned.
See also
getDependency NURIA_DEPENDENCY objectType
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.

Note
creator must be thread-safe!
void Nuria::DependencyManager::setDefaultThreadingPolicy ( ThreadingPolicy  policy)

Sets the default threading policy. Passing DefaultPolicy has no effect. Changing the policy is not thread-safe.

See also
ThreadingPolicy
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.

See also
Q_DECLARE_METATYPE qRegisterMetaType
template<typename T >
void Nuria::DependencyManager::storeObject ( const QByteArray &  name,
T *  object 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.


The documentation for this class was generated from the following file: