Nuria Framework - Core
Core module of the NuriaProject Framework
 All Classes Functions Typedefs Enumerations Enumerator Groups
Public Types | Public Member Functions | Static Public Member Functions | List of all members
Nuria::DependencyManager Class Reference

DepedencyManager enables easy to use dependency injection. More...

#include <dependencymanager.hpp>

Inheritance diagram for Nuria::DependencyManager:

Public Types

enum  ThreadingPolicy { DefaultPolicy, ApplicationGlobal, SingleThread, ThreadLocal }
 

Public Member Functions

 ~DependencyManager ()
 
ThreadingPolicy defaultThreadingPolicy () const
 
void setDefaultThreadingPolicy (ThreadingPolicy policy)
 
void * objectByName (const QString &name, int type=-1, ThreadingPolicy policy=DefaultPolicy)
 
int objectType (const QString &name, ThreadingPolicy policy=DefaultPolicy) const
 
bool hasObject (const QString &name, ThreadingPolicy policy=DefaultPolicy) const
 
void storeObject (const QString &name, void *object, int type, ThreadingPolicy policy=DefaultPolicy)
 
template<typename T >
void storeObject (const QString &name, T *object)
 

Static Public Member Functions

static DependencyManagerinstance ()
 
template<typename T >
static T * get (const QString &name, ThreadingPolicy policy=DefaultPolicy)
 

Detailed Description

DepedencyManager enables easy to use dependency injection.

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.

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.
Advanced usage
For more fine-grained control, please see the methods this class offers. If you're writing unit tests, then you're probably most interested in storeObject(). Example: Nuria::DependencyManager::instance ()->storeObject ("MyType", myType);
Note
You have to supply a name here as the code won't be able to figure out the name itself. You may want to consider writing a simple macro for convenience: #define STORE_DEP(T, Inst) Nuria::DependencyManager::instance ()->storeObject (#T, Inst);

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 behavious

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 QString &  name,
ThreadingPolicy  policy = DefaultPolicy 
)
inlinestatic

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

bool Nuria::DependencyManager::hasObject ( const QString &  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 QString &  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 QString &  name,
ThreadingPolicy  policy = DefaultPolicy 
) const

Returns the meta type of object name or -1 if not found.

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 QString &  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 QString &  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: