Home · All Classes · All Namespaces · Modules · Functions · Files

optional-interface-factory.h

00001 /*
00002  * This file is part of TelepathyQt4
00003  *
00004  * Copyright (C) 2008 Collabora Ltd. <http://www.collabora.co.uk/>
00005  * Copyright (C) 2008 Nokia Corporation
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #ifndef _TelepathyQt4_cli_optional_interface_factory_h_HEADER_GUARD_
00023 #define _TelepathyQt4_cli_optional_interface_factory_h_HEADER_GUARD_
00024 
00025 #ifndef IN_TELEPATHY_QT4_HEADER
00026 #error IN_TELEPATHY_QT4_HEADER
00027 #endif
00028 
00040 #include <QtGlobal>
00041 #include <QObject>
00042 
00043 namespace Tp
00044 {
00045 
00046 class AbstractInterface;
00047 
00048 class OptionalInterfaceCache
00049 {
00050     Q_DISABLE_COPY(OptionalInterfaceCache)
00051 
00052     public:
00056         explicit OptionalInterfaceCache(QObject *proxy);
00057 
00063         ~OptionalInterfaceCache();
00064 
00065     protected:
00066         AbstractInterface *getCached(const QString &name) const;
00067         void cache(AbstractInterface *interface) const;
00068         QObject *proxy() const;
00069 
00070     private:
00071         struct Private;
00072         Private *mPriv;
00073 };
00074 
00093 template <typename DBusProxySubclass> class OptionalInterfaceFactory
00094     : private OptionalInterfaceCache
00095 {
00096     public:
00103         inline OptionalInterfaceFactory(DBusProxySubclass *this_)
00104             : OptionalInterfaceCache(this_)
00105         {
00106         }
00107 
00113         inline ~OptionalInterfaceFactory()
00114         {
00115         }
00116 
00135         template <typename Interface>
00136         inline Interface *interface() const
00137         {
00138             AbstractInterface* interfaceMustBeASubclassOfAbstractInterface = static_cast<Interface *>(NULL);
00139             Q_UNUSED(interfaceMustBeASubclassOfAbstractInterface);
00140 
00141             // If there is a interface cached already, return it
00142             QString name(Interface::staticInterfaceName());
00143             AbstractInterface *cached = getCached(name);
00144             if (cached)
00145                 return static_cast<Interface *>(cached);
00146 
00147             // Otherwise, cache and return a newly constructed proxy
00148             Interface *interface = new Interface(
00149                     static_cast<DBusProxySubclass *>(proxy()));
00150             cache(interface);
00151             return interface;
00152         }
00153 };
00154 
00155 } // Tp
00156 
00157 #endif


Copyright © 2009 Collabora Ltd. and Nokia Corporation
Telepathy-Qt4 0.1.8