libtdepim

pluginloader.h
1 /*
2  This file is part of libtdepim.
3 
4  Copyright (c) 2002,2004 Marc Mutz <mutz@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #ifndef __KPIM_SHARED_PLUGINLOADER_H__
23 #define __KPIM_SHARED_PLUGINLOADER_H__
24 
25 #include <pluginloaderbase.h>
26 
27 namespace KPIM {
28 
78  template< typename T, typename T_config >
79  class TDE_EXPORT PluginLoader : public PluginLoaderBase {
80  protected:
81  PluginLoader() : PluginLoaderBase() {}
82 
83  private:
84  static PluginLoader<T,T_config> * mSelf;
85 
86  public:
87  virtual ~PluginLoader() { mSelf = 0; }
88 
91  if ( !mSelf ) {
92  mSelf = new PluginLoader<T,T_config>();
93  mSelf->scan();
94  }
95  return mSelf;
96  }
97 
101  virtual void scan() {
102  doScan( T_config::path );
103  }
104 
108  virtual T * createForName( const TQString & type ) const {
109  void * main_func = mainFunc( type, T_config::mainfunc );
110  if ( !main_func ) return 0;
111 
112  // cast to a pointer to a function returning T*, call it and
113  // return the result; don't you love C? ;-)
114  return ((T* (*)())( main_func ))();
115  }
116  };
117 
118  template< typename T, typename T_config >
119  PluginLoader<T,T_config> * PluginLoader<T,T_config>::mSelf = 0;
120 
121 }
122 
123 #define KPIM_DEFINE_PLUGIN_LOADER( pl, t, mf, p ) \
124  namespace { /* don't pollute namespaces */ \
125  struct TDE_EXPORT pl##Config { \
126  static const char * const mainfunc; \
127  static const char * const path; \
128  }; \
129  const char * const pl##Config::mainfunc = mf; \
130  const char * const pl##Config::path = p; \
131  } \
132  typedef KPIM::PluginLoader< t, pl##Config > pl; \
133 
134 
135 #endif // __KPIM_SHARED_PLUGINLOADER_H__
A generic plugin loader for when KPart::Plugin is overkill.
Definition: pluginloader.h:79
static PluginLoader< T, T_config > * instance()
Returns the single instance of this loader.
Definition: pluginloader.h:90
virtual T * createForName(const TQString &type) const
Returns a pointer to a plugin object (of type T) or a null pointer if the type wasn't found.
Definition: pluginloader.h:108
virtual void scan()
Rescans the plugin directory to find any newly installed plugins.
Definition: pluginloader.h:101
TDEPIM classes for drag and drop of mails.