Langmuir
Simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
keyvalueparser.h
Go to the documentation of this file.
1 #ifndef KEYVALUEPARSER_H
2 #define KEYVALUEPARSER_H
3 
4 #include <QObject>
5 
6 #include "variable.h"
7 #include "parameters.h"
8 #include "world.h"
9 
10 namespace LangmuirCore
11 {
12 
27 class KeyValueParser : public QObject
28 {
29  Q_OBJECT
30 public:
38  KeyValueParser(World& world, QObject *parent = 0);
39 
44 
49 
53  void parse(const QString& line);
54 
58  void save(const QString& fileName = "%stub.parm");
59 
64  Variable& getVariable(const QString& key);
65 
69  const QMap<QString,Variable*>& getVariableMap() const;
70 
74  const QStringList& getOrderedNames() const;
75 
79  friend std::ostream& operator<<(std::ostream &stream, const KeyValueParser &keyValueParser);
80 
84  friend QDebug operator<<(QDebug dbg, KeyValueParser &keyValueParser);
85 
89  QString toQString();
90 
91 private:
95  QMap<QString,Variable*> m_variableMap;
96 
100  QStringList m_orderedNames;
101 
106 
111 
115  template <typename T> void registerVariable(
116  const QString &key,
117  T &value,
118  Variable::VariableMode mode = 0);
119 };
120 
121 template <typename T> void KeyValueParser::registerVariable(
122  const QString &key,
123  T &value,
124  Variable::VariableMode mode)
125 {
126  if (m_variableMap.find(key) != m_variableMap.end())
127  {
128  qFatal("langmuir: variable already registered; key %s",
129  qPrintable(key));
130  }
131  m_variableMap.insert(key, new TypedVariable<T>(key,value,mode,this));
132  m_orderedNames.push_back(key);
133 }
134 
135 }
136 
137 #endif // KEYVALUEPARSER_H
KeyValueParser(World &world, QObject *parent=0)
Create a KeyValueParser.
QStringList m_orderedNames
A list of variable names in a specific order.
Definition: keyvalueparser.h:100
void parse(const QString &line)
Parse a string and assign a value to the correct parameter.
Variable & getVariable(const QString &key)
Get a reference to a variable by name.
World & m_world
Reference to World object.
Definition: keyvalueparser.h:110
A template class to map between variable names (keys) and locations (references)
Definition: variable.h:91
A class to map between variable names (keys) and locations (references)
Definition: variable.h:26
void registerVariable(const QString &key, T &value, Variable::VariableMode mode=0)
Register an allowed variable with the parser.
Definition: keyvalueparser.h:121
const QStringList & getOrderedNames() const
get list of ordered keys
A class to hold all objects in a simulation.
Definition: world.h:51
SimulationParameters & parameters()
Get the SimulationParameters.
const QMap< QString, Variable * > & getVariableMap() const
get the variable map
A struct to store all simulation options.
Definition: parameters.h:46
Definition: agent.h:12
SimulationParameters m_parameters
The simulation parameters.
Definition: keyvalueparser.h:105
friend std::ostream & operator<<(std::ostream &stream, const KeyValueParser &keyValueParser)
Write the parameters to a std::ostream in a "key=value" fashion.
~KeyValueParser()
Destroy the KeyValueParser.
QString toQString()
convert parameters to a QString
QMap< QString, Variable * > m_variableMap
A map between variable names and variables.
Definition: keyvalueparser.h:95
A class to read the parameters and store them in the correct place.
Definition: keyvalueparser.h:27
void save(const QString &fileName="%stub.parm")
Write the parameters to a file in a "key=value" fashion.