libqutim  0.3.1.0
servicemanager.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** qutIM - instant messenger
4 **
5 ** Copyright © 2011 Ruslan Nigmatullin <euroelessar@yandex.ru>
6 ** Copyright © 2011 Aleksey Sidorov <gorthauer87@yandex.ru>
7 **
8 *****************************************************************************
9 **
10 ** $QUTIM_BEGIN_LICENSE$
11 ** This program is free software: you can redistribute it and/or modify
12 ** it under the terms of the GNU General Public License as published by
13 ** the Free Software Foundation, either version 3 of the License, or
14 ** (at your option) any later version.
15 **
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 ** See the GNU General Public License for more details.
20 **
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program. If not, see http://www.gnu.org/licenses/.
23 ** $QUTIM_END_LICENSE$
24 **
25 ****************************************************************************/
26 
27 #ifndef SERVICEMANAGER_H
28 #define SERVICEMANAGER_H
29 
30 #include "libqutim_global.h"
31 #include "extensioninfo.h"
32 
33 namespace qutim_sdk_0_3
34 {
35 
36 class ServiceManagerPrivate;
37 
38 class LIBQUTIM_EXPORT ServicePointerData : public QSharedData
39 {
40  Q_DISABLE_COPY(ServicePointerData)
41 public:
42  typedef QWeakPointer<ServicePointerData> Ptr;
44 
45  QByteArray name;
46  QObject *object;
47 };
48 
49 class LIBQUTIM_EXPORT ServiceManager : public QObject
50 {
51  Q_OBJECT
52  Q_DECLARE_PRIVATE(ServiceManager)
53 
54  Q_PROPERTY(bool inited READ isInited NOTIFY initedChanged)
55 public:
56  static bool isInited();
57  static QObject *getByName(const QByteArray &name);
58  template<typename T>
59  static inline T getByName(const QByteArray &name)
60  { return qobject_cast<T>(ServiceManager::getByName(name)); }
61  static QList<QByteArray> names();
62  static ServiceManager *instance();
65  static ExtensionInfoList listImplementations(const QByteArray &name);
71  static bool setImplementation(const QByteArray &name, const ExtensionInfo &info);
72 
73 signals:
76  void serviceChanged(const QByteArray &name, QObject *newObject, QObject *oldObject);
77  void serviceChanged(QObject *newObject, QObject *oldObject);
78  void initedChanged(bool inited = true);
79 private:
81  ~ServiceManager();
82  template <typename T>
83  static ServicePointerData::Ptr getData();
84  static ServicePointerData::Ptr getData(const QMetaObject *meta);
85  static ServicePointerData::Ptr getData(const QByteArray &name);
86  template <typename T>
87  static ServicePointerData::Ptr getDataHelper(QObject *);
88  template <typename T>
89  static ServicePointerData::Ptr getDataHelper(void *);
90 
91  template<typename T> friend class ServicePointer;
92  QScopedPointer<ServiceManagerPrivate> d_ptr;
93 };
94 
95 template<typename T>
97 {
98 public:
99  inline ServicePointer() : d(ServiceManager::getData<T>()) {}
100  inline ServicePointer(Qt::Initialization) {}
101  inline ServicePointer(const QByteArray &name) : d(ServiceManager::getData(name)) {}
102  inline ServicePointer(const ServicePointer &o) : d(o.d) {}
103  inline ServicePointer &operator =(const ServicePointer &o) { d = o.d; return *this; }
104  inline ~ServicePointer() {}
105 
106  inline QByteArray name() const { return d ? d.data()->name : QByteArray(); }
107  inline T *data() const { return qobject_cast<T*>(d ? d.data()->object : 0); }
108  inline T *operator ->() const { Q_ASSERT(d); return data(); }
109  inline T &operator *() const { Q_ASSERT(d); return *data(); }
110  inline operator bool() const { return d && d.data()->object; }
111  inline operator T*() const { return data(); }
112  inline bool operator !() const { return !operator bool(); }
113  inline bool isNull() const { return !data(); }
114 
115 private:
117 };
118 
119 template <typename T>
120 Q_INLINE_TEMPLATE ServicePointerData::Ptr ServiceManager::getData()
121 {
122  return getDataHelper<T>(reinterpret_cast<T*>(0));
123 }
124 
125 template <typename T>
126 Q_INLINE_TEMPLATE ServicePointerData::Ptr ServiceManager::getDataHelper(QObject *)
127 {
128  return getData(&T::staticMetaObject);
129 }
130 
131 template <typename T>
132 Q_INLINE_TEMPLATE ServicePointerData::Ptr ServiceManager::getDataHelper(void *)
133 {
134  return getData(qobject_interface_iid<T*>());
135 }
136 
137 } // namespace qutim_sdk_0_3
138 
139 #endif // SERVICEMANAGER_H
140 

Generated by Doxygen