QGpCoreTools/XMLClassFactory.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  See http://www.geopsy.org for more information.
00020 **
00021 **  Created : 2004-11-27
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (ULg, Liège, Belgium)
00025 **    Marc Wathelet (LGIT, Grenoble, France)
00026 **
00027 ***************************************************************************/
00028 
00029 #ifndef XMLCLASSFACTORY_H
00030 #define XMLCLASSFACTORY_H
00031 
00032 #include <QtCore>
00033 
00034 #include "CoreApplication.h"
00035 #include "QGpCoreToolsDLLExport.h"
00036 
00037 namespace QGpCoreTools {
00038 
00039   class XMLClass;
00040   class XMLClassCreator;
00041 
00042   class QGPCORETOOLS_EXPORT XMLClassFactory
00043   {
00044   public:
00045     XMLClassFactory() {_nextId=0;}
00046     ~XMLClassFactory();
00047 
00048     int id(const QString& tag) const;
00049     XMLClass * create(const QString& tag) const;
00050     XMLClass * create(int id) const;
00051     XMLClassCreator * creator(const QString& tag) const;
00052     XMLClassCreator * creator(int id) const;
00053 
00054     QStringList registeredTags() const;
00055     QList<int> registeredIds() const;
00056 
00057     int registerClass(const QString& tag, XMLClassCreator * creator);
00058     void registerTag(const QString& tag, int id);
00059     void registerTag(const QString& tag, const QString& destTag);
00060 
00061     static void registerFactory(XMLClassFactory * f);
00062     static void clearFactories();
00063   protected:
00064     int newId() {return _nextId++;}
00065   private:
00066     QMap<int, XMLClassCreator *> _registeredClasses;
00067     QMap<QString, int> _registeredTags;
00068     int _nextId;
00069 
00070     static QList<XMLClassFactory *> _factories;
00071   };
00072 
00073   class QGPCORETOOLS_EXPORT XMLClassCreator
00074   {
00075   public:
00076     virtual ~XMLClassCreator() {}
00077 
00078     virtual XMLClass * create() const=0;
00079   };
00080 
00081   #define XMLREGISTER_HEAD(_factory_, _creator_, _className_, _tagName_) \
00082     class _className_##XMLClassCreator : public _creator_ \
00083     { \
00084     public: \
00085       _className_##XMLClassCreator() \
00086       { \
00087         _factory_::init(); \
00088         _factory_::instance()->registerClass(_tagName_, this); \
00089       }
00090 
00091 
00092   #define XMLREGISTER_TAIL(_className_) \
00093     private: \
00094       virtual XMLClass * create() const {return new _className_;} \
00095     }; \
00096     _className_##XMLClassCreator register##_className_;
00097 
00098   #define XMLSYNONYM(_factory_, _synName_, _tagName_) \
00099     class _synName_##XMLClassSynonym \
00100     { \
00101     public: \
00102       _synName_##XMLClassSynonym() { \
00103         _factory_::instance()->registerTag(# _synName_, _tagName_); \
00104       } \
00105     }; \
00106     _synName_##XMLClassSynonym register##_synName_;
00107 
00108 } // namespace QGpCoreTools
00109 
00110 #endif // XMLCLASSFACTORY_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines