QGpCoreTools/XMLMap.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 : 2008-07-12
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (LGIT, Grenoble, France)
00025 **
00026 ***************************************************************************/
00027 
00028 #ifndef XMLMAP_H
00029 #define XMLMAP_H
00030 
00031 #include "XMLClass.h"
00032 #include "QGpCoreToolsDLLExport.h"
00033 
00034 namespace QGpCoreTools {
00035 
00036 template <class Key, class T>
00037 class QGPCORETOOLS_EXPORT XMLMap : public XMLClass
00038 {
00039 public:
00040   XMLMap(QString tag, QMap<Key,T> * map);
00041   XMLMap(QString tag, const QMap<Key,T> * map);
00042 
00043   virtual const QString& xml_tagName() const {return _tag;}
00044   QMap<Key,T> * map() {return _map;}
00045 protected:
00046   virtual void xml_writeProperties(XML_WRITEPROPERTIES_ARGS) const;
00047   virtual bool xml_setProperty(XML_SETPROPERTY_ARGS);
00048   virtual XMLMember xml_member(XML_MEMBER_ARGS);
00049 private:
00050   QString _tag;
00051   QMap<Key,T> * _map;
00052 };
00053 
00054 template <class Key, class T>
00055 XMLMap<Key,T>::XMLMap(QString tag, QMap<Key,T> * map)
00056 {
00057   _tag=tag;
00058   _map=map;
00059 }
00060 
00061 template <class Key, class T>
00062 XMLMap<Key,T>::XMLMap(QString tag, const QMap<Key,T> * map)
00063 {
00064   _tag=tag;
00065   _map=const_cast<QMap<Key,T> *>(map);
00066 }
00067 
00068 template <class Key, class T>
00069 void XMLMap<Key,T>::xml_writeProperties(XML_WRITEPROPERTIES_ARGS) const
00070 {
00071   TRACE;
00072   Q_UNUSED(context);
00073   static const QString tag="value";
00074   static const QString key("key");
00075   XMLSaveAttributes att;
00076   QString& value=att.add(key);
00077   QMapIterator<Key,T> it(*_map);
00078   while(it.hasNext()) {
00079     it.next();
00080     value=it.key();
00081     writeProperty(s, tag, att, it.value());
00082   }
00083 }
00084 
00085 template <class Key, class T>
00086 XMLMember XMLMap<Key,T>::xml_member(XML_MEMBER_ARGS)
00087 {
00088   TRACE;
00089   Q_UNUSED(attributes);
00090   Q_UNUSED(context);
00091   if(tag=="value" ) return XMLMember(0);
00092   return XMLMember(XMLMember::Unknown);
00093 }
00094 
00095 template <class Key, class T>
00096 bool XMLMap<Key,T>::xml_setProperty(XML_SETPROPERTY_ARGS)
00097 {
00098   TRACE;
00099   Q_UNUSED(context);
00100   static const QString att="key";
00101   if(memberID==0) {
00102     XMLRestoreAttributeIterator it=attributes.find(att);
00103     if(it!=attributes.end()) {
00104       Key tmpKey;
00105       T tmpT;
00106       _map->insert(it.value().to(tmpKey), content.to(tmpT));
00107       return true;
00108     }
00109   }
00110   return false;
00111 }
00112 
00113 } // namespace QGpCoreTools
00114 
00115 #endif // XMLMAP_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines