Simpatico  v1.10
ScalarParam.h
1 #ifndef UTIL_SCALAR_PARAM_H
2 #define UTIL_SCALAR_PARAM_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/Parameter.h>
12 #include <util/archives/Serializable_includes.h>
13 #include <util/global.h>
14 
15 #ifdef UTIL_MPI
16 #include <util/mpi/MpiSendRecv.h>
17 #endif
18 
19 #include <iomanip>
20 
21 namespace Util
22 {
23 
33  template <class Type>
34  class ScalarParam : public Parameter
35  {
36 
37  public:
38 
46  ScalarParam(const char *label, Type& value, bool isRequired = true);
47 
53  void writeParam(std::ostream& out);
54 
60  void setValue(Type& value);
61 
62  protected:
63 
69  virtual void readValue(std::istream& in);
70 
76  virtual void loadValue(Serializable::IArchive& ar);
77 
83  virtual void saveValue(Serializable::OArchive& ar);
84 
85  #ifdef UTIL_MPI
86 
89  virtual void bcastValue();
90  #endif
91 
92  private:
93 
95  Type* valuePtr_;
96 
98  ScalarParam(const ScalarParam<Type>& other);
99 
101  ScalarParam<Type> operator = (const ScalarParam<Type>& other);
102 
103  };
104 
105  // Member Function Definitions
106 
107  /*
108  * ScalarParam<Type> constructor.
109  */
110  template <class Type>
111  ScalarParam<Type>::ScalarParam(const char *label, Type &value,
112  bool isRequired)
113  : Parameter(label, isRequired),
114  valuePtr_(&value)
115  {}
116 
117  template <class Type>
118  void ScalarParam<Type>::readValue(std::istream &in)
119  { in >> *valuePtr_; }
120 
121  template <class Type>
123  { ar & *valuePtr_; }
124 
125  template <class Type>
127  { ar & *valuePtr_; }
128 
129  #ifdef UTIL_MPI
130  template <class Type>
132  { bcast<Type>(ioCommunicator(), *valuePtr_, 0); }
133  #endif
134 
135  /*
136  * Write a parameter.
137  */
138  template <class Type>
139  void ScalarParam<Type>::writeParam(std::ostream& out)
140  {
141  if (isActive()) {
142  out << indent();
143  out << label_;
144  out << std::right << std::scientific
145  << std::setprecision(Parameter::Precision)
146  << std::setw(Parameter::Width) << *valuePtr_
147  << std::endl;
148  }
149  }
150 
151  /*
152  * Set the pointer to the parameter value.
153  */
154  template <class Type>
155  void ScalarParam<Type>::setValue(Type& value)
156  { valuePtr_ = &value; }
157 
158 }
159 #endif
void setValue(Type &value)
Set the pointer to point a specific variable.
Definition: ScalarParam.h:155
std::string label() const
Return label string.
Definition: Parameter.cpp:158
virtual void saveValue(Serializable::OArchive &ar)
Save parameter value to an archive.
Definition: ScalarParam.h:126
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 readValue(std::istream &in)
Read parameter value from an input stream.
Definition: ScalarParam.h:118
File containing preprocessor macros for error handling.
Saving / output archive for binary ostream.
MPI::Intracomm & ioCommunicator() const
Get the MPI communicator by reference.
Definition: MpiFileIo.h:105
Template for a Parameter object associated with a scalar variable.
Definition: ScalarParam.h:34
Utility classes for scientific computation.
Definition: accumulators.mod:1
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
void writeParam(std::ostream &out)
Write parameter to stream.
Definition: ScalarParam.h:139
Saving archive for binary istream.
static const int Width
Width of output field for a scalar variable.
Definition: Parameter.h:53
This file contains templates for global functions send<T>, recv<T> and bcast<T>.
ScalarParam(const char *label, Type &value, bool isRequired=true)
Constructor.
Definition: ScalarParam.h:111
virtual void loadValue(Serializable::IArchive &ar)
Load bare parameter value from an archive.
Definition: ScalarParam.h:122
bool isRequired() const
Is this an optional parameter?
Definition: Parameter.cpp:164
virtual void bcastValue()
Broadcast parameter value within the ioCommunicator.
Definition: ScalarParam.h:131
std::string indent() const
Return indent string for this object (string of spaces).