properties.h

00001 /***************************************************************************
00002  *   Copyright (C) 2003 by Gav Wood                                        *
00003  *   gav@cs.york.ac.uk                                                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Library General Public License as       *
00007  *   published by the Free Software Foundation; either version 2 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  ***************************************************************************/
00010 #ifndef _GEDDEI_PROPERTIES_H
00011 #define _GEDDEI_PROPERTIES_H
00012 
00013 #include <qbuffer.h>
00014 #include <qcstring.h>
00015 #include <qdatastream.h>
00016 #include <qvariant.h>
00017 #include <qmap.h>
00018 
00019 class NodeServerSession;
00020 
00021 namespace rGeddei { class RemoteSession; }
00022 
00023 namespace Geddei
00024 {
00025 
00026 class PropertiesInfo;
00027 
00038 class Properties
00039 {
00040  friend class RSCoupling;
00041  friend class DRCoupling;
00042  friend class rGeddei::RemoteSession;
00043  friend class ::NodeServerSession;
00044 
00045  QMap<QString, QVariant> theData;
00046 
00052  void toBuffer(QBuffer &data) const;
00053 
00059  void fromBuffer(QBuffer &data);
00060 
00066  QByteArray serialise() const;
00067 
00074  void deserialise(QByteArray &data);
00075 
00084  Properties(QByteArray &a) { deserialise(a); }
00085 
00086 public:
00101  const QVariant &get(const QString &key) const { return theData[key]; }
00102 
00117  void set(const QString &key, const QVariant value) { theData[key] = value; }
00118 
00127  void set(const Properties &pairs);
00128 
00134  const uint size() const { return theData.size(); }
00135 
00141  const QStringList keys() const { return theData.keys(); }
00142 
00153  const QVariant &operator[](const QString &key) const { if(!theData.contains(key)) qWarning("*** WARNING: Reading undefined property (%s)", key.latin1()); return theData[key]; }
00154 
00171  QVariant &operator[](const QString &key) { return theData[key]; }
00172 
00193  Properties &operator()(const QString &key, const QVariant &value) { set(key, value); return *this; }
00194  
00202  Properties(const QString &key, const QVariant &value) { set(key, value); }
00203  
00207  Properties() {}
00208 
00216  Properties(const PropertiesInfo &info);
00217 };
00218 
00223 struct PropertiesDatum
00224 {
00225  QString description;
00226 };
00227 
00240 class PropertiesInfo: private Properties
00241 {
00242  friend class Properties;
00243 
00244  QMap<QString, PropertiesDatum> theInfo;
00245  
00246 public:
00252  const uint size() const { return Properties::size(); }
00253 
00259  const QStringList keys() const { return Properties::keys(); }
00260  
00280  void set(const PropertiesInfo &merge) { for(QMap<QString, PropertiesDatum>::const_iterator i = merge.theInfo.begin(); i != merge.theInfo.end(); i++) theInfo[i.key()] = i.data(); Properties::set(merge); }
00281 
00291  void set(const QString &key, const QVariant defaultValue, const QString &description) { Properties::set(key, defaultValue); theInfo[key].description = description; }
00292 
00299  const QVariant &defaultValue(const QString &key) const { return Properties::get(key); }
00300  
00307  const QString description(const QString &key) const;
00308  
00337  PropertiesInfo &operator()(const QString &key, const QVariant defaultValue, const QString &description) { set(key, defaultValue, description); return *this; }
00338  
00359  PropertiesInfo &operator()(const PropertiesInfo &merge) { set(merge); return *this; }
00360  
00372  PropertiesInfo(const QString &key, const QVariant defaultValue, const QString &description) { set(key, defaultValue, description); }
00373  
00377  PropertiesInfo() {}
00378 };
00379 
00380 }
00381 
00382 #endif

Generated on Thu Jul 13 06:56:33 2006 for Exscalibar by  doxygen 1.4.7