Home · Modules · All Classes · All Namespaces
optional-interface-factory.h
1 
23 #ifndef _TelepathyQt_optional_interface_factory_h_HEADER_GUARD_
24 #define _TelepathyQt_optional_interface_factory_h_HEADER_GUARD_
25 
26 #ifndef IN_TP_QT_HEADER
27 #error IN_TP_QT_HEADER
28 #endif
29 
30 #include <TelepathyQt/Global>
31 
32 #include <QObject>
33 #include <QStringList>
34 #include <QtGlobal>
35 
36 namespace Tp
37 {
38 
39 class AbstractInterface;
40 
41 #ifndef DOXYGEN_SHOULD_SKIP_THIS
42 
43 class TP_QT_EXPORT OptionalInterfaceCache
44 {
45  Q_DISABLE_COPY(OptionalInterfaceCache)
46 
47 public:
48  explicit OptionalInterfaceCache(QObject *proxy);
49 
50  ~OptionalInterfaceCache();
51 
52 protected:
53  AbstractInterface *getCached(const QString &name) const;
54  void cache(AbstractInterface *interface) const;
55  QObject *proxy() const;
56 
57 private:
58  struct Private;
59  friend struct Private;
60  Private *mPriv;
61 };
62 
63 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
64 
65 template <typename DBusProxySubclass> class OptionalInterfaceFactory
66 #ifndef DOXYGEN_SHOULD_SKIP_THIS
67  : private OptionalInterfaceCache
68 #endif
69 {
70  Q_DISABLE_COPY(OptionalInterfaceFactory)
71 
72 public:
74  {
77  };
78 
79  inline OptionalInterfaceFactory(DBusProxySubclass *this_)
80  : OptionalInterfaceCache(this_)
81  {
82  }
83 
85  {
86  }
87 
88  inline QStringList interfaces() const { return mInterfaces; }
89 
90  inline bool hasInterface(const QString &name) const
91  {
92  return mInterfaces.contains(name);
93  }
94 
95  template <class Interface>
96  inline Interface *optionalInterface(
98  {
99  // Check for the remote object supporting the interface
100  // Note that extra whitespace on "name" declaration is significant to avoid
101  // vexing-parse
102  QString name( (QLatin1String(Interface::staticInterfaceName())) );
103  if (check == CheckInterfaceSupported && !mInterfaces.contains(name)) {
104  return 0;
105  }
106 
107  // If present or forced, delegate to OptionalInterfaceFactory
108  return interface<Interface>();
109  }
110 
111  template <typename Interface>
112  inline Interface *interface() const
113  {
114  AbstractInterface* interfaceMustBeASubclassOfAbstractInterface = static_cast<Interface *>(NULL);
115  Q_UNUSED(interfaceMustBeASubclassOfAbstractInterface);
116 
117  // If there is a interface cached already, return it
118  // Note that extra whitespace on "name" declaration is significant to avoid
119  // vexing-parse
120  QString name( (QLatin1String(Interface::staticInterfaceName())) );
121  AbstractInterface *cached = getCached(name);
122  if (cached)
123  return static_cast<Interface *>(cached);
124 
125  // Otherwise, cache and return a newly constructed proxy
126  Interface *interface = new Interface(
127  static_cast<DBusProxySubclass *>(proxy()));
128  cache(interface);
129  return interface;
130  }
131 
132 protected:
133  inline void setInterfaces(const QStringList &interfaces)
134  {
135  mInterfaces = interfaces;
136  }
137 
138 private:
139  QStringList mInterfaces;
140 };
141 
142 } // Tp
143 
144 #endif
Interface * optionalInterface(InterfaceSupportedChecking check=CheckInterfaceSupported) const
Definition: optional-interface-factory.h:96
bool hasInterface(const QString &name) const
Definition: optional-interface-factory.h:90
OptionalInterfaceFactory(DBusProxySubclass *this_)
Definition: optional-interface-factory.h:79
The AbstractInterface class is the base class for all client side D-Bus interfaces,...
Definition: abstract-interface.h:42
Definition: optional-interface-factory.h:76
Definition: optional-interface-factory.h:75
QStringList interfaces() const
Definition: optional-interface-factory.h:88
Interface * interface() const
Definition: optional-interface-factory.h:112
The OptionalInterfaceFactory class is a helper class for high-level D-Bus proxy classes willing to of...
Definition: optional-interface-factory.h:65
void setInterfaces(const QStringList &interfaces)
Definition: optional-interface-factory.h:133
InterfaceSupportedChecking
Definition: optional-interface-factory.h:73
Definition: abstract-adaptor.cpp:31
~OptionalInterfaceFactory()
Definition: optional-interface-factory.h:84