libqutim
0.3.2.0
|
00001 /**************************************************************************** 00002 ** 00003 ** qutIM - instant messenger 00004 ** 00005 ** Copyright © 2011 Ruslan Nigmatullin <euroelessar@yandex.ru> 00006 ** Copyright © 2011 Aleksey Sidorov <gorthauer87@yandex.ru> 00007 ** 00008 ***************************************************************************** 00009 ** 00010 ** $QUTIM_BEGIN_LICENSE$ 00011 ** This program is free software: you can redistribute it and/or modify 00012 ** it under the terms of the GNU General Public License as published by 00013 ** the Free Software Foundation, either version 3 of the License, or 00014 ** (at your option) any later version. 00015 ** 00016 ** This program is distributed in the hope that it will be useful, 00017 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00019 ** See the GNU General Public License for more details. 00020 ** 00021 ** You should have received a copy of the GNU General Public License 00022 ** along with this program. If not, see http://www.gnu.org/licenses/. 00023 ** $QUTIM_END_LICENSE$ 00024 ** 00025 ****************************************************************************/ 00026 00027 #ifndef SERVICEMANAGER_H 00028 #define SERVICEMANAGER_H 00029 00030 #include "libqutim_global.h" 00031 #include "extensioninfo.h" 00032 00033 namespace qutim_sdk_0_3 00034 { 00035 00036 class ServiceManagerPrivate; 00037 00038 class LIBQUTIM_EXPORT ServicePointerData : public QSharedData 00039 { 00040 Q_DISABLE_COPY(ServicePointerData) 00041 public: 00042 typedef QWeakPointer<ServicePointerData> Ptr; 00043 ServicePointerData(); 00044 00045 QByteArray name; 00046 QObject *object; 00047 }; 00048 00049 class LIBQUTIM_EXPORT ServiceManager : public QObject 00050 { 00051 Q_OBJECT 00052 Q_DECLARE_PRIVATE(ServiceManager) 00053 00054 Q_PROPERTY(bool inited READ isInited NOTIFY initedChanged) 00055 public: 00056 static bool isInited(); 00057 static QObject *getByName(const QByteArray &name); 00058 template<typename T> 00059 static inline T getByName(const QByteArray &name) 00060 { return qobject_cast<T>(ServiceManager::getByName(name)); } 00061 static QList<QByteArray> names(); 00062 static ServiceManager *instance(); 00065 static ExtensionInfoList listImplementations(const QByteArray &name); 00071 static bool setImplementation(const QByteArray &name, const ExtensionInfo &info); 00072 00073 signals: 00076 void serviceChanged(const QByteArray &name, QObject *newObject, QObject *oldObject); 00077 void serviceChanged(QObject *newObject, QObject *oldObject); 00078 void initedChanged(bool inited = true); 00079 private: 00080 ServiceManager(); 00081 ~ServiceManager(); 00082 template <typename T> 00083 static ServicePointerData::Ptr getData(); 00084 static ServicePointerData::Ptr getData(const QMetaObject *meta); 00085 static ServicePointerData::Ptr getData(const QByteArray &name); 00086 template <typename T> 00087 static ServicePointerData::Ptr getDataHelper(QObject *); 00088 template <typename T> 00089 static ServicePointerData::Ptr getDataHelper(void *); 00090 00091 template<typename T> friend class ServicePointer; 00092 QScopedPointer<ServiceManagerPrivate> d_ptr; 00093 }; 00094 00095 template<typename T> 00096 class ServicePointer 00097 { 00098 public: 00099 inline ServicePointer() : d(ServiceManager::getData<T>()) {} 00100 inline ServicePointer(Qt::Initialization) {} 00101 inline ServicePointer(const QByteArray &name) : d(ServiceManager::getData(name)) {} 00102 inline ServicePointer(const ServicePointer &o) : d(o.d) {} 00103 inline ServicePointer &operator =(const ServicePointer &o) { d = o.d; return *this; } 00104 inline ~ServicePointer() {} 00105 00106 inline QByteArray name() const { return d ? d.data()->name : QByteArray(); } 00107 inline T *data() const { return qobject_cast<T*>(d ? d.data()->object : 0); } 00108 inline T *operator ->() const { Q_ASSERT(d); return data(); } 00109 inline T &operator *() const { Q_ASSERT(d); return *data(); } 00110 inline operator bool() const { return d && d.data()->object; } 00111 inline operator T*() const { return data(); } 00112 inline bool operator !() const { return !operator bool(); } 00113 inline bool isNull() const { return !data(); } 00114 00115 private: 00116 ServicePointerData::Ptr d; 00117 }; 00118 00119 template <typename T> 00120 Q_INLINE_TEMPLATE ServicePointerData::Ptr ServiceManager::getData() 00121 { 00122 return getDataHelper<T>(reinterpret_cast<T*>(0)); 00123 } 00124 00125 template <typename T> 00126 Q_INLINE_TEMPLATE ServicePointerData::Ptr ServiceManager::getDataHelper(QObject *) 00127 { 00128 return getData(&T::staticMetaObject); 00129 } 00130 00131 template <typename T> 00132 Q_INLINE_TEMPLATE ServicePointerData::Ptr ServiceManager::getDataHelper(void *) 00133 { 00134 return getData(qobject_interface_iid<T*>()); 00135 } 00136 00137 } // namespace qutim_sdk_0_3 00138 00139 #endif // SERVICEMANAGER_H 00140