VAPOR3 3.9.4
ParamsBase.h
Go to the documentation of this file.
1//************************************************************************
2// *
3// Copyright (C) 2008 *
4// University Corporation for Atmospheric Research *
5// All Rights Reserved *
6// *
7//************************************************************************/
8//
9// File: ParamsBase.h
10//
11// Author: John Clyne, modified by Alan Norton
12// National Center for Atmospheric Research
13// PO 3000, Boulder, Colorado
14//
15// Date: March 2008
16//
17// Description:
18// Defines the ParamsBase class
19// This is an abstract class for classes that rely on
20// accessing an XML node for get/set
21//
22
23#ifndef ParamsBase_H
24#define ParamsBase_H
25
26#include "vapor/VAssert.h"
27#include <functional>
28#include <vapor/MyBase.h>
29#include <vapor/XmlNode.h>
30
31#define PARAMS_IMPL_TAG(class, tag) const std::string class::tag = #tag;
32
33namespace VAPoR {
34
35//
51public:
61 //
62 class StateSave {
63 public:
69 virtual void Reinit(const XmlNode *rootNode) {}
70 virtual void Save(const XmlNode *node, string description) {}
71 virtual void BeginGroup(string description) {}
72 virtual void EndGroup() {}
73 virtual void IntermediateChange() {}
74 virtual void SetEnabled(bool onOff) {}
75 virtual bool GetEnabled() const { return (false); }
76 };
77
78 // NO DEFAULT CONSTRUCTOR
79 //
80 // ParamsBase();
81
91 ParamsBase(StateSave *ssave, const string &classname);
92
98 //
100
103
105
108 ParamsBase(ParamsBase &&) = delete;
110
112 //
113 bool operator==(const ParamsBase &rhs) const { return (_ssave == rhs._ssave && *_node == *(rhs._node)); }
114
115 bool operator!=(const ParamsBase &rhs) const { return (!(*this == rhs)); };
116
124 virtual ~ParamsBase();
125
133 //
134 void SetParent(ParamsBase *parent);
135
136 XmlNode *GetNode() const { return _node; }
137
138 void BeginGroup(const string &description) { _ssave->BeginGroup(description); }
139 void EndGroup() { _ssave->EndGroup(); }
140 void IntermediateChange() { _ssave->IntermediateChange(); }
141
142 virtual vector<long> GetValueLongVec(const string tag) const;
143
144 virtual vector<long> GetValueLongVec(const string tag, const vector<long> &defaultVal) const;
145
146 virtual long GetValueLong(const string tag, long defaultVal) const;
147
148 virtual vector<double> GetValueDoubleVec(const string tag) const;
149
150 virtual vector<double> GetValueDoubleVec(const string tag, const vector<double> &defaultVal) const;
151
152 virtual double GetValueDouble(const string tag, double defaultVal) const;
153
154 virtual vector<string> GetValueStringVec(const string tag) const;
155
156 virtual vector<string> GetValueStringVec(const string tag, const vector<string> &defaultVal) const;
157
158 virtual string GetValueString(const string tag, string defaultVal) const;
159
160 virtual void SetValueLongVec(const string &tag, string description, const vector<long> &values);
161
162 virtual void SetValueLong(const string &tag, string description, long value);
163
164 virtual void SetValueDoubleVec(const string &tag, string description, const vector<double> &values);
165
166 virtual void SetValueDouble(const string &tag, string description, double value);
167
168 virtual void SetValueStringVec(const string &tag, string description, const vector<string> &values);
169
170 virtual void SetValueString(const string &tag, string description, const string &value);
171
175 string GetName() const
176 {
177 VAssert(_node);
178 return (_node->Tag());
179 }
180
181protected:
183
192 //
193 void Remove(const string &name);
194
198 //
199 const map<string, string> &GetAttributes();
200
205 //
206 void Clear();
207
208protected:
211};
212
214//
215// ParamsSeparator Class
216//
218
220public:
221 ParamsSeparator(StateSave *ssave, const string &name);
222
224
225 ParamsSeparator(ParamsSeparator *parent, const string &name);
226
227 virtual ~ParamsSeparator() {}
228
229 bool HasChild(const string &name) { return (GetNode()->HasChild(name)); }
230};
231
233//
234// ParamsFactory Class
235//
237
239public:
241 {
242 static ParamsFactory instance;
243 return &instance;
244 }
245
246 void RegisterFactoryFunction(string name, function<ParamsBase *(ParamsBase::StateSave *, XmlNode *)> classFactoryFunction)
247 {
248 // register the class factory function
249 m_factoryFunctionRegistry[name] = classFactoryFunction;
250 }
251
253
254 vector<string> GetFactoryNames() const;
255
256private:
257 map<string, function<ParamsBase *(ParamsBase::StateSave *, XmlNode *)>> m_factoryFunctionRegistry;
258
259 ParamsFactory() {}
260 ParamsFactory(const ParamsFactory &) {}
261 ParamsFactory &operator=(const ParamsFactory &) { return *this; }
262};
263
265//
266// ParamsRegistrar Class
267//
268// Register ParamsBase derived class with:
269//
270// static ParamsRegistrar<ParamsClass> registrar("myclassname");
271//
272// where 'ParamsClass' is a class derived from 'ParamsBase', and
273// "myclassname" is the name of the class
274//
276
277template<class T> class ParamsRegistrar {
278public:
279 ParamsRegistrar(string classType)
280 {
281 // register the class factory function
282 //
284 if (node)
285 return new T(ssave, node);
286 else
287 return new T(ssave);
288 });
289 }
290};
291
293//
294// ParamsContainer Class
295//
297
298//
299// The ParamsContainer class constructs an XML tree as depicted below,
300// where 'Container Name' is the root of XML tree, and is the name
301// passed into the constructor as 'myname'; 'Class Name' is the name of
302// the derived ParamsBase class used to construct new instances of
303// the derived class; and 'ele name x' is the unique name of the element
304// contained in the container.
305//
306/*
307 |----------------|
308 | Container Name |
309 |----------------|
310 |
311 \|/
312 |----------------|
313 | Class Name |
314 |----------------|
315 | \
316 \|/ \
317 |----------------| \ |----------------|
318 | ele name 1 |....| ele name n |
319 |----------------| |----------------|
320*/
322public:
323 ParamsContainer(ParamsBase::StateSave *ssave, const string &myname);
324
326
329
331
339
347 //
348 void SetParent(ParamsBase *parent) { GetNode()->SetParent(parent->GetNode()); }
349
350 ParamsBase *Insert(ParamsBase *pb, string name);
351
352 ParamsBase *Create(string classType, string name);
353
354 void Remove(string name);
355
356 void Remove(const ParamsBase *pb) { Remove(GetParamsName(pb)); }
357
358 ParamsBase *GetParams(string name) const;
359
360 string GetParamsName(const ParamsBase *pb) const;
361
362 vector<string> GetNames() const;
363
364 size_t Size() const { return (_elements.size()); }
365
366 XmlNode *GetNode() const { return _separator->GetNode(); }
367
368private:
369 ParamsBase::StateSave *_ssave;
370 // XmlNode *_node;
371 ParamsSeparator * _separator;
372 map<string, ParamsBase *> _elements;
373};
374
375}; // End namespace VAPoR
376
377#endif // ParamsBase_H
#define VAssert(expr)
Definition: VAssert.h:9
State capture class.
Definition: ParamsBase.h:62
virtual void IntermediateChange()
Definition: ParamsBase.h:73
virtual bool GetEnabled() const
Definition: ParamsBase.h:75
virtual void Reinit(const XmlNode *rootNode)
Definition: ParamsBase.h:69
virtual void BeginGroup(string description)
Definition: ParamsBase.h:71
virtual void SetEnabled(bool onOff)
Definition: ParamsBase.h:74
virtual void Save(const XmlNode *node, string description)
Definition: ParamsBase.h:70
Nodes with state in Xml tree representation.
Definition: ParamsBase.h:50
virtual vector< double > GetValueDoubleVec(const string tag, const vector< double > &defaultVal) const
XmlNode * GetNode() const
Definition: ParamsBase.h:136
virtual vector< string > GetValueStringVec(const string tag, const vector< string > &defaultVal) const
void IntermediateChange()
Definition: ParamsBase.h:140
ParamsBase & operator=(const ParamsBase &rhs)
void SetParent(ParamsBase *parent)
virtual void SetValueString(const string &tag, string description, const string &value)
virtual vector< long > GetValueLongVec(const string tag, const vector< long > &defaultVal) const
ParamsBase(ParamsBase &&)=delete
ParamsBase(const ParamsBase &rhs)
Copy constructor.
virtual void SetValueDoubleVec(const string &tag, string description, const vector< double > &values)
virtual void SetValueLongVec(const string &tag, string description, const vector< long > &values)
StateSave * _ssave
Definition: ParamsBase.h:209
ParamsBase(StateSave *ssave)
ParamsBase(StateSave *ssave, XmlNode *node)
const map< string, string > & GetAttributes()
ParamsBase(StateSave *ssave, const string &classname)
virtual ~ParamsBase()
virtual double GetValueDouble(const string tag, double defaultVal) const
virtual string GetValueString(const string tag, string defaultVal) const
virtual vector< string > GetValueStringVec(const string tag) const
void BeginGroup(const string &description)
Definition: ParamsBase.h:138
ParamsBase & operator=(ParamsBase &)=delete
string GetName() const
Definition: ParamsBase.h:175
virtual void SetValueLong(const string &tag, string description, long value)
void Remove(const string &name)
virtual vector< double > GetValueDoubleVec(const string tag) const
virtual void SetValueDouble(const string &tag, string description, double value)
virtual vector< long > GetValueLongVec(const string tag) const
virtual long GetValueLong(const string tag, long defaultVal) const
bool operator!=(const ParamsBase &rhs) const
Definition: ParamsBase.h:115
virtual void SetValueStringVec(const string &tag, string description, const vector< string > &values)
XmlNode * _node
Definition: ParamsBase.h:210
bool operator==(const ParamsBase &rhs) const
Equivalence operator.
Definition: ParamsBase.h:113
ParamsContainer & operator=(const ParamsContainer &rhs)
ParamsContainer(ParamsBase::StateSave *ssave, XmlNode *node)
ParamsContainer(ParamsBase::StateSave *ssave, const string &myname)
ParamsBase * GetParams(string name) const
ParamsBase * Insert(ParamsBase *pb, string name)
ParamsContainer(const ParamsContainer &rhs)
Copy constructor.
XmlNode * GetNode() const
Definition: ParamsBase.h:366
void Remove(string name)
size_t Size() const
Definition: ParamsBase.h:364
vector< string > GetNames() const
ParamsBase * Create(string classType, string name)
string GetParamsName(const ParamsBase *pb) const
void SetParent(ParamsBase *parent)
Definition: ParamsBase.h:348
void Remove(const ParamsBase *pb)
Definition: ParamsBase.h:356
vector< string > GetFactoryNames() const
static ParamsFactory * Instance()
Definition: ParamsBase.h:240
ParamsBase * CreateInstance(string classType, ParamsBase::StateSave *, XmlNode *))
void RegisterFactoryFunction(string name, function< ParamsBase *(ParamsBase::StateSave *, XmlNode *)> classFactoryFunction)
Definition: ParamsBase.h:246
ParamsRegistrar(string classType)
Definition: ParamsBase.h:279
ParamsSeparator(StateSave *ssave, XmlNode *node)
ParamsSeparator(ParamsSeparator *parent, const string &name)
bool HasChild(const string &name)
Definition: ParamsBase.h:229
virtual ~ParamsSeparator()
Definition: ParamsBase.h:227
ParamsSeparator(StateSave *ssave, const string &name)
An Xml tree.
Definition: XmlNode.h:49
virtual XmlNode * GetNode(std::vector< string > path, bool absolute) const
Wasp base class.
Definition: MyBase.h:67
#define PARAMS_API
Definition: common.h:77