qfactoryexporter.h

00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Gav Wood                                        *
00003  *   gav@kde.org                                                           *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 /*
00022  * Usage:
00023  *
00024  * For all classes to be exported, you must provide *exactly one*:
00025  *   EXPORT_CLASS(...) (or any of the derivitive functions)
00026  * This should be found in single-instance file (i.e. a .cpp, *not* a .h)
00027  * This exports the given class for provision in the plugin library. It
00028  * also specifies the version, so that the most recent class can automatically
00029  * be provided by the factory.
00030  *
00031  * At the start of each .cpp file that uses the above command, you must
00032  * use FACTORY_TYPE(...) to declare the base type that all exported classes
00033  * are derived from. This *must be the same* throughout the plugin. If not,
00034  * you'll get no error, but a lot strange stuff when the classes are used.
00035  *
00036  * You must also use the FACTORY_EXPORT declaration _once_ in the entire
00037  * plugin. Put this in the library's equivalent of "main.cpp".
00038  *
00039  * If only one .cpp file is compiled you can use the compound command instead
00040  * of the last two commands:
00041  *   FACTORY_EXPORT_TYPE(...);
00042  * Whose argument must be the common base type that all exported classes
00043  * are derived from. This, like FACTORY_TYPE, should go before any
00044  * EXPORT_CLASS()s.
00045  *
00046  * Example 1: one .cpp file, one .h file:
00047  *
00048  * FILE "b.h":
00049  * #include <a.h>
00050  * #include <qtextra/qfactoryexporter.h>
00051  * class B: public A { void foo(); };
00052  *
00053  * FILE "b.cpp":
00054  * #include "b.h"
00055  * void B::foo() { return; }
00056  * FACTORY_EXPORT;
00057  * EXPORT_CLASS(B, 1,0,0, A);
00058  *
00059  * Example 2: two class .cpp files, one "main" .cpp file:
00060  *
00061  * FILE "common.cpp":
00062  * FACTORY_EXPORT;
00063  *
00064  * FILE "b.cpp":
00065  * #include <a.h>
00066  * class B: public A { void foo(); };
00067  * void B::foo() { return; }
00068  * EXPORT_CLASS(B, 1,0,0, A);
00069  *
00070  * FILE "c.cpp":
00071  * #include <a.h>
00072  * class C: public A { void foo(); };
00073  * void C::foo() { return; }
00074  * EXPORT_CLASS(C, 1,0,0, A);
00075  */
00076 
00077 #ifndef _QT_FACTORYEXPORTER_H
00078 #define _QT_FACTORYEXPORTER_H
00079 
00080 #include <typeinfo>
00081 #include <qstringlist.h>
00082 #include <qmap.h>
00083 
00086 class Register
00087 {
00088  static QMap<QString, QStringList> *theLists;
00089 public:
00090  static QMap<QString, QStringList> &getLists() { return theLists ? *theLists : *(theLists = new QMap<QString, QStringList>); }
00091  Register(const char *name, const char *base) { getLists()[base] += name; }
00092 };
00093 
00094 #define FACTORY_EXPORT \
00095 QMap<QString, QStringList> *Register::theLists = 0; \
00096 extern "C" \
00097 { \
00098  const QStringList &getAvailable(const QString &base) { return Register::getLists()[base]; } \
00099 }
00100 
00101 #define EXPORT_CLASS(Class, MajorVersion, MinorVersion, TinyVersion, Base) \
00102 extern "C" \
00103 { \
00104  Register reg ## Class(#Class, typeid(Base).name()); \
00105  Base *create ## Class() { return new Class; } \
00106  int version ## Class() { return (((MajorVersion << 8) + MinorVersion) << 8) + TinyVersion; }; \
00107 }
00108 
00109 #define EXPORT_CLASS_I1(Name, Class, I1, MajorVersion, MinorVersion, TinyVersion, Base) \
00110 extern "C" \
00111 { \
00112  Register reg ## Name(#Name, typeid(Base).name()); \
00113  Base *create ## Name() { return new Class(I1); } \
00114  int version ## Name() { return (((MajorVersion << 8) + MinorVersion) << 8) + TinyVersion; }; \
00115 }
00116 
00117 #define EXPORT_CLASS_I2(Name, Class, I1, I2, MajorVersion, MinorVersion, TinyVersion, Base) \
00118 extern "C" \
00119 { \
00120  Register reg ## Name(#Name, typeid(Base).name()); \
00121  Base *create ## Name() { return new Class(I1, I2); } \
00122  int version ## Name() { return (((MajorVersion << 8) + MinorVersion) << 8) + TinyVersion; }; \
00123 }
00124 
00125 #define EXPORT_CLASS_I3(Name, Class, I1, I2, I3, MajorVersion, MinorVersion, TinyVersion, Base) \
00126 extern "C" \
00127 { \
00128  Register reg ## Name(#Name, typeid(Base).name()); \
00129  Base *create ## Name() { return new Class(I1, I2, I3); } \
00130  int version ## Name() { return (((MajorVersion << 8) + MinorVersion) << 8) + TinyVersion; }; \
00131 }
00132 
00133 #define EXPORT_CLASS_I4(Name, Class, I1, I2, I3, I4, MajorVersion, MinorVersion, TinyVersion, Base) \
00134 extern "C" \
00135 { \
00136  Register reg ## Name(#Name, typeid(Base).name()); \
00137  Base *create ## Name() { return new Class(I1, I2, I3, I4); } \
00138  int version ## Name() { return (((MajorVersion << 8) + MinorVersion) << 8) + TinyVersion; }; \
00139 }
00140 
00141 #define EXPORT_CLASS_I5(Name, Class, I1, I2, I3, I4, I5, MajorVersion, MinorVersion, TinyVersion, Base) \
00142 extern "C" \
00143 { \
00144  Register reg ## Name(#Name, typeid(Base).name()); \
00145  Base *create ## Name() { return new Class(I1, I2, I3, I4, I5); } \
00146  int version ## Name() { return (((MajorVersion << 8) + MinorVersion) << 8) + TinyVersion; }; \
00147 }
00148 
00149 #endif

Generated on Fri Nov 10 21:58:26 2006 for Exscalibar by  doxygen 1.5.1