Simpatico  v1.10
Parameter.h
1 #ifndef UTIL_PARAMETER_H
2 #define UTIL_PARAMETER_H
3 
4 /*
5 * Util Package - C++ Utilities for Scientific Computation
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include <util/param/ParamComponent.h>
12 #include <util/archives/Serializable_includes.h>
13 #include <util/param/Label.h>
14 #include <util/global.h>
15 
16 namespace Util
17 {
18 
45  class Parameter : public ParamComponent
46  {
47 
48  public:
49 
50  // Static members
51 
53  static const int Width = 20;
54 
56  static const int Precision = 12;
57 
65  template <class Type>
66  static void
67  saveOptional(Serializable::OArchive& ar, Type& value, bool isActive);
68 
77  template <class Type>
78  static void
79  saveOptionalCArray(Serializable::OArchive& ar, Type* ptr, int n,
80  bool isActive);
81 
92  template <class Type>
93  static void
95  int m, int n, int np, bool isActive);
96 
97  // Non-static member functions
98 
105  Parameter(const char *label, bool isRequired = true);
106 
110  virtual ~Parameter();
111 
134  virtual void readParam(std::istream &in);
135 
150  virtual void load(Serializable::IArchive& ar);
151 
165  virtual void save(Serializable::OArchive& ar);
166 
170  std::string label() const;
171 
175  bool isRequired() const;
176 
180  bool isActive() const;
181 
182  protected:
183 
186 
188  bool isActive_;
189 
195  virtual void readValue(std::istream& in){}
196 
202  virtual void loadValue(Serializable::IArchive& ar){}
203 
209  virtual void saveValue(Serializable::OArchive& ar){}
210 
211  #ifdef UTIL_MPI
212 
215  virtual void bcastValue(){}
216  #endif
217 
218  };
219 
220  /*
221  * Save a parameter value to an output archive
222  */
223  template <class Type>
225  Type& value, bool isActive)
226  {
227  ar << isActive;
228  if (isActive) {
229  ar & value;
230  }
231  }
232 
233  /*
234  * Save C-array of n ptrs to an output archive
235  */
236  template <class Type>
238  Type* ptr, int n, bool isActive)
239  {
240  ar << isActive;
241  if (isActive) {
242  ar.pack(ptr, n);
243  }
244  }
245 
246  /*
247  * Save a two-dimensional C array to an output archive.
248  */
249  template <class Type>
251  Type* ptr, int m, int n, int np,
252  bool isActive)
253  {
254  ar << isActive;
255  if (isActive) {
256  ar.pack(ptr, m, n, np);
257  }
258  }
259 
260 }
261 #endif
std::string label() const
Return label string.
Definition: Parameter.cpp:158
Label label_
Label object that contains parameter label string.
Definition: Parameter.h:185
A single variable in a parameter file.
Definition: Parameter.h:45
virtual void saveValue(Serializable::OArchive &ar)
Save parameter value to an archive.
Definition: Parameter.h:209
File containing preprocessor macros for error handling.
Parameter(const char *label, bool isRequired=true)
Constructor.
Definition: Parameter.cpp:22
Saving / output archive for binary ostream.
virtual ~Parameter()
Destructor.
Definition: Parameter.cpp:30
void pack(const T &data)
Pack one object of type T.
static void saveOptionalCArray2D(Serializable::OArchive &ar, Type *ptr, int m, int n, int np, bool isActive)
Save an optional two-dimensional C array to an output archive.
Definition: Parameter.h:250
static void saveOptionalCArray(Serializable::OArchive &ar, Type *ptr, int n, bool isActive)
Save an optional C-array of n values to an output archive.
Definition: Parameter.h:237
virtual void bcastValue()
Broadcast parameter value within the ioCommunicator.
Definition: Parameter.h:215
Utility classes for scientific computation.
Definition: accumulators.mod:1
virtual void readValue(std::istream &in)
Read parameter value from an input stream.
Definition: Parameter.h:195
virtual void save(Serializable::OArchive &ar)
Save to an archive.
Definition: Parameter.cpp:145
static const int Precision
Precision for io of floating point data field.
Definition: Parameter.h:56
bool isActive() const
Is this parameter active?
Definition: Parameter.cpp:170
Abstract base class for classes that input and ouput parameters to file.
virtual void loadValue(Serializable::IArchive &ar)
Load bare parameter value from an archive.
Definition: Parameter.h:202
A label string in a file format.
Definition: Label.h:36
Saving archive for binary istream.
static const int Width
Width of output field for a scalar variable.
Definition: Parameter.h:53
virtual void load(Serializable::IArchive &ar)
Load from an archive.
Definition: Parameter.cpp:98
bool isActive_
Is this parameter active (always true if isRequired).
Definition: Parameter.h:188
bool isRequired() const
Is this an optional parameter?
Definition: Parameter.cpp:164
virtual void readParam(std::istream &in)
Read a label and (if the label matches) a parameter value.
Definition: Parameter.cpp:36
static void saveOptional(Serializable::OArchive &ar, Type &value, bool isActive)
Save an optional parameter value to an output archive.
Definition: Parameter.h:224