Nuria Framework - Core
Core module of the NuriaProject Framework
 All Classes Functions Typedefs Enumerations Enumerator Groups
dependencymanager.hpp
1 /* Copyright (c) 2014, The Nuria Project
2  * This software is provided 'as-is', without any express or implied
3  * warranty. In no event will the authors be held liable for any damages
4  * arising from the use of this software.
5  * Permission is granted to anyone to use this software for any purpose,
6  * including commercial applications, and to alter it and redistribute it
7  * freely, subject to the following restrictions:
8  * 1. The origin of this software must not be misrepresented; you must not
9  * claim that you wrote the original software. If you use this software
10  * in a product, an acknowledgment in the product documentation would be
11  * appreciated but is not required.
12  * 2. Altered source versions must be plainly marked as such, and must not be
13  * misrepresented as being the original software.
14  * 3. This notice may not be removed or altered from any source
15  * distribution.
16  */
17 
18 #ifndef NURIA_DEPEDENCYMANAGER_HPP
19 #define NURIA_DEPEDENCYMANAGER_HPP
20 
21 #include "essentials.hpp"
22 #include <QObject>
23 
24 namespace Nuria {
25 class DependencyManagerPrivate;
26 
67 class NURIA_CORE_EXPORT DependencyManager : public QObject {
68  Q_OBJECT
69 public:
70 
80 
86 
92 
99  ThreadLocal
100  };
101 
103  ~DependencyManager ();
104 
108  static DependencyManager *instance ();
109 
114  ThreadingPolicy defaultThreadingPolicy () const;
115 
122  void setDefaultThreadingPolicy (ThreadingPolicy policy);
123 
136  void *objectByName (const QString &name, int type = -1, ThreadingPolicy policy = DefaultPolicy);
137 
141  int objectType (const QString &name, ThreadingPolicy policy = DefaultPolicy) const;
142 
146  inline bool hasObject (const QString &name, ThreadingPolicy policy = DefaultPolicy) const
147  { return objectType (name, policy) != -1; }
148 
155  void storeObject (const QString &name, void *object, int type,
156  ThreadingPolicy policy = DefaultPolicy);
157 
161  template< typename T >
162  void storeObject (const QString &name, T *object)
163  { storeObject (name, object, qMetaTypeId< T * > ()); }
164 
169  template< typename T >
170  inline static T *get (const QString &name, ThreadingPolicy policy = DefaultPolicy) {
171  return static_cast< T * > (instance ()->objectByName (name, qMetaTypeId< T * > (), policy));
172  }
173 
174 private slots:
175 
176  void freeAllObjects ();
177 
178 private:
179  explicit DependencyManager (QObject *parent = 0);
180 
181  DependencyManagerPrivate *d_ptr;
182 };
183 
184 }
185 
186 #define NURIA_DEPENDENCY(T) Nuria::DependencyManager::get< T > (#T)
187 
188 #endif // NURIA_DEPEDENCYMANAGER_HPP
DepedencyManager enables easy to use dependency injection.
Definition: dependencymanager.hpp:67
ThreadingPolicy
Definition: dependencymanager.hpp:75
bool hasObject(const QString &name, ThreadingPolicy policy=DefaultPolicy) const
Definition: dependencymanager.hpp:146
Definition: dependencymanager.hpp:85
void storeObject(const QString &name, T *object)
Definition: dependencymanager.hpp:162
Definition: dependencymanager.hpp:79
Definition: dependencymanager.hpp:91