libqutim  0.3.1.0
objectgenerator.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** qutIM - instant messenger
4 **
5 ** Copyright © 2011 Ruslan Nigmatullin <euroelessar@yandex.ru>
6 **
7 *****************************************************************************
8 **
9 ** $QUTIM_BEGIN_LICENSE$
10 ** This program is free software: you can redistribute it and/or modify
11 ** it under the terms of the GNU General Public License as published by
12 ** the Free Software Foundation, either version 3 of the License, or
13 ** (at your option) any later version.
14 **
15 ** This program is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 ** See the GNU General Public License for more details.
19 **
20 ** You should have received a copy of the GNU General Public License
21 ** along with this program. If not, see http://www.gnu.org/licenses/.
22 ** $QUTIM_END_LICENSE$
23 **
24 ****************************************************************************/
25 
26 #ifndef OBJECTGENERATOR_H
27 #define OBJECTGENERATOR_H
28 
29 #include "libqutim_global.h"
30 #include <QWeakPointer>
31 
32 namespace qutim_sdk_0_3
33 {
34 class ObjectGeneratorPrivate;
35 class ExtensionInfo;
36 
37 class ObjectGenerator;
39 typedef QList<const ObjectGenerator *> GeneratorList;
40 
41 class LIBQUTIM_EXPORT ObjectGeneratorHolderData : public QSharedData
42 {
43  Q_DISABLE_COPY(ObjectGeneratorHolderData)
44 public:
45  ObjectGenerator *generator() const;
46  ActionGenerator *actionGenerator() const;
47 
48 private:
49  friend class ObjectGenerator;
51  ObjectGenerator *m_generator;
52 };
53 
70 {
71  Q_DECLARE_PRIVATE(ObjectGenerator)
72  Q_DISABLE_COPY(ObjectGenerator)
73  Q_GADGET
74 protected:
79 #ifndef Q_QDOC
80  ObjectGenerator(ObjectGeneratorPrivate &priv);
81 #endif
82 public:
83  typedef QExplicitlySharedDataPointer<ObjectGeneratorHolderData> Ptr;
84 
88  virtual ~ObjectGenerator();
101  ObjectGenerator *addProperty(const QByteArray &name, const QVariant &value);
105  inline QObject *generate() const
106  { return generateHelper2(); }
116  template<typename T>
117  inline T *generate() const
118  {
119  QObject *object = generateHelper2();
120  T *t = qobject_cast<T *>(object);
121  if (!t) delete object;
122  return t ? t : 0;
123  }
124 // /**
125 // * @brief Generate object
126 // *
127 // * @param super Meta info of superiour class
128 // * @return Generated object or null if object doesn't extends class
129 // * represented by superiour meta info error
130 // */
131 // inline QObject *generate(const QMetaObject *super) const
132 // { return extends(super) ? generateHelper2() : 0; }
133 // /**
134 // * @brief Generate object
135 // *
136 // * @param id Identification of needed interface
137 // * @return Generated object or null if class doesn't implement interface
138 // */
139 // inline QObject *generate(const char *id) const
140 // { return extends(id) ? generateHelper2() : 0; }
147  virtual const QMetaObject *metaObject() const = 0;
154  bool hasInterface(const char *id) const;
155 
156  virtual QList<QByteArray> interfaces() const;
157 
158  // TODO: There should be a way for getting interfaces list
159 // virtual QList<const char *> interfaces() const = 0;
166  bool extends(const QMetaObject *super) const;
173  inline bool extends(const char *id) const
174  { return id && hasInterface(id); }
180  template<typename T>
181  inline bool extends() const
182  { return extends_helper<T>(reinterpret_cast<T *>(0)); }
183 
187  static bool isInited();
191  static GeneratorList module(const QMetaObject *module);
192 
193  Ptr pointerHolder();
197  static GeneratorList module(const char *iid);
198  template<typename T> static inline GeneratorList module()
199  { return module_helper<T>(reinterpret_cast<T *>(0)); }
200 #ifndef Q_QDOC
201  ExtensionInfo info() const;
202 protected:
203  template<typename T> static inline GeneratorList module_helper(const QObject *)
204  { return ObjectGenerator::module(&T::staticMetaObject); }
205  template<typename T> static inline GeneratorList module_helper(const void *)
206  { return ObjectGenerator::module(qobject_interface_iid<T *>()); }
207  template<typename T>
208  inline bool extends_helper(const QObject *) const
209  { return extends(&T::staticMetaObject); }
210  template<typename T>
211  inline bool extends_helper(const void *) const
212  { return extends(qobject_interface_iid<T *>()); }
213  QObject *generateHelper2() const;
214 #endif
215 protected:
223  virtual QObject *generateHelper() const = 0;
224  virtual void virtual_hook(int id, void *data);
225  QScopedPointer<ObjectGeneratorPrivate> d_ptr;
226 #ifndef Q_QDOC
227 public:
228  typedef ObjectGeneratorPrivate Data;
229  inline Data *data() { return d_ptr.data(); }
230 #endif
231 };
232 
233 template<typename T, typename I0 = void,
234 typename I1 = void, typename I2 = void, typename I3 = void,
235 typename I4 = void, typename I5 = void, typename I6 = void,
236 typename I7 = void, typename I8 = void, typename I9 = void>
238 {
239  Q_DISABLE_COPY(GeneralGenerator)
240 public:
244  inline GeneralGenerator() {}
245 protected:
250  virtual QObject *generateHelper() const
251  {
252  return new T();
253  }
259  virtual const QMetaObject *metaObject() const
260  {
261  return &T::staticMetaObject;
262  }
269  virtual QList<QByteArray> interfaces() const
270  {
271  QList<QByteArray> result;
272  addInterface<I0>(result);
273  addInterface<I1>(result);
274  addInterface<I2>(result);
275  addInterface<I3>(result);
276  addInterface<I4>(result);
277  addInterface<I5>(result);
278  addInterface<I6>(result);
279  addInterface<I7>(result);
280  addInterface<I8>(result);
281  addInterface<I9>(result);
282  return result;
283  }
284 private:
285  template<typename Interface>
286  Q_INLINE_TEMPLATE void addInterface(QList<QByteArray> &result, T *pointer = 0) const
287  {
288  register Interface *i = pointer;
289  Q_UNUSED(i);
290  if (qobject_interface_iid<Interface*>())
291  result << qobject_interface_iid<Interface*>();
292  }
293 };
294 
295 
296 template<typename T, typename I0 = void,
297 typename I1 = void, typename I2 = void, typename I3 = void,
298 typename I4 = void, typename I5 = void, typename I6 = void,
299 typename I7 = void, typename I8 = void, typename I9 = void>
300 class SingletonGenerator : public GeneralGenerator<T, I0, I1, I2, I3, I4, I5, I6, I7, I8, I9>
301 {
302  Q_DISABLE_COPY(SingletonGenerator)
303 public:
307  inline SingletonGenerator() {}
308 protected:
317  virtual QObject *generateHelper() const
318  {
319  if(m_object.isNull())
320  m_object = new T;
321  return m_object.data();
322  }
326  mutable QWeakPointer<QObject> m_object;
327 };
328 }
329 
330 #endif // OBJECTGENERATOR_H
331 

Generated by Doxygen