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 <QPointer>
31 #include <QSharedDataPointer>
32 
33 namespace qutim_sdk_0_3
34 {
35 class ObjectGeneratorPrivate;
36 class ExtensionInfo;
37 
38 class ObjectGenerator;
40 typedef QList<const ObjectGenerator *> GeneratorList;
41 
42 class LIBQUTIM_EXPORT ObjectGeneratorHolderData : public QSharedData
43 {
44  Q_DISABLE_COPY(ObjectGeneratorHolderData)
45 public:
46  ObjectGenerator *generator() const;
47  ActionGenerator *actionGenerator() const;
48 
49 private:
50  friend class ObjectGenerator;
52  ObjectGenerator *m_generator;
53 };
54 
71 {
72  Q_DECLARE_PRIVATE(ObjectGenerator)
73  Q_DISABLE_COPY(ObjectGenerator)
74  Q_GADGET
75 protected:
80 #ifndef Q_QDOC
81  ObjectGenerator(ObjectGeneratorPrivate &priv);
82 #endif
83 public:
84  typedef QExplicitlySharedDataPointer<ObjectGeneratorHolderData> Ptr;
85 
89  virtual ~ObjectGenerator();
102  ObjectGenerator *addProperty(const QByteArray &name, const QVariant &value);
106  inline QObject *generate() const
107  { return generateHelper2(); }
117  template<typename T>
118  inline T *generate() const
119  {
120  QObject *object = generateHelper2();
121  T *t = qobject_cast<T *>(object);
122  if (!t) delete object;
123  return t ? t : 0;
124  }
125 // /**
126 // * @brief Generate object
127 // *
128 // * @param super Meta info of superiour class
129 // * @return Generated object or null if object doesn't extends class
130 // * represented by superiour meta info error
131 // */
132 // inline QObject *generate(const QMetaObject *super) const
133 // { return extends(super) ? generateHelper2() : 0; }
134 // /**
135 // * @brief Generate object
136 // *
137 // * @param id Identification of needed interface
138 // * @return Generated object or null if class doesn't implement interface
139 // */
140 // inline QObject *generate(const char *id) const
141 // { return extends(id) ? generateHelper2() : 0; }
148  virtual const QMetaObject *metaObject() const = 0;
155  bool hasInterface(const char *id) const;
156 
157  virtual QList<QByteArray> interfaces() const;
158 
159  // TODO: There should be a way for getting interfaces list
160 // virtual QList<const char *> interfaces() const = 0;
167  bool extends(const QMetaObject *super) const;
174  inline bool extends(const char *id) const
175  { return id && hasInterface(id); }
181  template<typename T>
182  inline bool extends() const
183  { return extends_helper<T>(reinterpret_cast<T *>(0)); }
184 
188  static bool isInited();
192  static GeneratorList module(const QMetaObject *module);
193 
194  Ptr pointerHolder();
198  static GeneratorList module(const char *iid);
199  template<typename T> static inline GeneratorList module()
200  { return module_helper<T>(reinterpret_cast<T *>(0)); }
201 #ifndef Q_QDOC
202  ExtensionInfo info() const;
203 protected:
204  template<typename T> static inline GeneratorList module_helper(const QObject *)
205  { return ObjectGenerator::module(&T::staticMetaObject); }
206  template<typename T> static inline GeneratorList module_helper(const void *)
207  { return ObjectGenerator::module(qobject_interface_iid<T *>()); }
208  template<typename T>
209  inline bool extends_helper(const QObject *) const
210  { return extends(&T::staticMetaObject); }
211  template<typename T>
212  inline bool extends_helper(const void *) const
213  { return extends(qobject_interface_iid<T *>()); }
214  QObject *generateHelper2() const;
215 #endif
216 protected:
224  virtual QObject *generateHelper() const = 0;
225  virtual void virtual_hook(int id, void *data);
226  QScopedPointer<ObjectGeneratorPrivate> d_ptr;
227 #ifndef Q_QDOC
228 public:
229  typedef ObjectGeneratorPrivate Data;
230  inline Data *data() { return d_ptr.data(); }
231 #endif
232 };
233 
234 template<typename T, typename I0 = void,
235 typename I1 = void, typename I2 = void, typename I3 = void,
236 typename I4 = void, typename I5 = void, typename I6 = void,
237 typename I7 = void, typename I8 = void, typename I9 = void>
239 {
240  Q_DISABLE_COPY(GeneralGenerator)
241 public:
245  inline GeneralGenerator() {}
246 protected:
251  virtual QObject *generateHelper() const
252  {
253  return new T();
254  }
260  virtual const QMetaObject *metaObject() const
261  {
262  return &T::staticMetaObject;
263  }
270  virtual QList<QByteArray> interfaces() const
271  {
272  QList<QByteArray> result;
273  addInterface<I0>(result);
274  addInterface<I1>(result);
275  addInterface<I2>(result);
276  addInterface<I3>(result);
277  addInterface<I4>(result);
278  addInterface<I5>(result);
279  addInterface<I6>(result);
280  addInterface<I7>(result);
281  addInterface<I8>(result);
282  addInterface<I9>(result);
283  return result;
284  }
285 private:
286  template<typename Interface>
287  Q_INLINE_TEMPLATE void addInterface(QList<QByteArray> &result, T *pointer = 0) const
288  {
289  register Interface *i = pointer;
290  Q_UNUSED(i);
291  if (qobject_interface_iid<Interface*>())
292  result << qobject_interface_iid<Interface*>();
293  }
294 };
295 
296 
297 template<typename T, typename I0 = void,
298 typename I1 = void, typename I2 = void, typename I3 = void,
299 typename I4 = void, typename I5 = void, typename I6 = void,
300 typename I7 = void, typename I8 = void, typename I9 = void>
301 class SingletonGenerator : public GeneralGenerator<T, I0, I1, I2, I3, I4, I5, I6, I7, I8, I9>
302 {
303  Q_DISABLE_COPY(SingletonGenerator)
304 public:
308  inline SingletonGenerator() {}
309 protected:
318  virtual QObject *generateHelper() const
319  {
320  if(m_object.isNull())
321  m_object = new T;
322  return m_object.data();
323  }
327  mutable QPointer<QObject> m_object;
328 };
329 }
330 
331 #endif // OBJECTGENERATOR_H
332 

Generated by Doxygen