libkdepim

pluginloader.h

00001 /*  -*- c++ -*-
00002     This file is part of libkdepim.
00003 
00004     Copyright (c) 2002,2004 Marc Mutz <mutz@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #ifndef __KPIM_SHARED_PLUGINLOADER_H__
00023 #define __KPIM_SHARED_PLUGINLOADER_H__
00024 
00025 #include <pluginloaderbase.h>
00026 
00027 namespace KPIM {
00028 
00078   template< typename T, typename T_config >
00079   class KDE_EXPORT PluginLoader : public PluginLoaderBase {
00080   protected:
00081     PluginLoader() : PluginLoaderBase() {}
00082 
00083   private:
00084     static PluginLoader<T,T_config> * mSelf;
00085 
00086   public:
00087     virtual ~PluginLoader() { mSelf = 0; }
00088 
00090     static PluginLoader<T,T_config> * instance() {
00091       if ( !mSelf ) {
00092     mSelf = new PluginLoader<T,T_config>();
00093     mSelf->scan();
00094       }
00095       return mSelf;
00096     }
00097 
00101     virtual void scan() {
00102       doScan( T_config::path );
00103     }
00104 
00108     virtual T * createForName( const QString & type ) const {
00109       void * main_func = mainFunc( type, T_config::mainfunc );
00110       if ( !main_func ) return 0;
00111 
00112       // cast to a pointer to a function returning T*, call it and
00113       // return the result; don't you love C? ;-)
00114       return ((T* (*)())( main_func ))();
00115     }
00116   };
00117 
00118   template< typename T, typename T_config >
00119   PluginLoader<T,T_config> * PluginLoader<T,T_config>::mSelf = 0;
00120 
00121 }
00122 
00123 #define KPIM_DEFINE_PLUGIN_LOADER( pl, t, mf, p ) \
00124   namespace { /* don't pollute namespaces */ \
00125     struct KDE_EXPORT pl##Config { \
00126       static const char * const mainfunc; \
00127       static const char * const path; \
00128     }; \
00129     const char * const pl##Config::mainfunc = mf; \
00130     const char * const pl##Config::path = p; \
00131   } \
00132   typedef KPIM::PluginLoader< t, pl##Config > pl; \
00133 
00134 
00135 #endif // __KPIM_SHARED_PLUGINLOADER_H__
KDE Home | KDE Accessibility Home | Description of Access Keys