PSCF v1.3
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
21namespace 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) const;
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
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)
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) const
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>
156 { valuePtr_ = &value; }
157
158}
159#endif
std::string indent() const
Return indent string for this object (string of spaces).
std::string label() const
Return label string.
Parameter(const char *label, bool isRequired=true)
Constructor.
Definition Parameter.cpp:22
static const int Precision
Precision for io of floating point data field.
Definition Parameter.h:56
bool isRequired() const
Is this an optional parameter?
Label label_
Label object that contains parameter label string.
Definition Parameter.h:185
bool isActive() const
Is this parameter active?
static const int Width
Width of output field for a scalar variable.
Definition Parameter.h:53
Template for a Parameter object associated with a scalar variable.
Definition ScalarParam.h:35
virtual void readValue(std::istream &in)
Read parameter value from an input stream.
void setValue(Type &value)
Set the pointer to point a specific variable.
virtual void saveValue(Serializable::OArchive &ar)
Save parameter value to an archive.
void writeParam(std::ostream &out) const
Write parameter to stream.
virtual void loadValue(Serializable::IArchive &ar)
Load bare parameter value from an archive.
ScalarParam(const char *label, Type &value, bool isRequired=true)
Constructor.
BinaryFileIArchive IArchive
Type of input archive used by load method.
BinaryFileOArchive OArchive
Type of output archive used by save method.
File containing preprocessor macros for error handling.
Utility classes for scientific computation.