PSCF v1.1
ArrayParam.tpp
1/*
2* Util Package - C++ Utilities for Scientific Computation
3*
4* Copyright 2010 - 2017, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "ArrayParam.h"
9#include "BracketPolicy.h"
10
11#ifdef UTIL_MPI
13#endif
14
15#include <iomanip>
16
17namespace Util
18{
19
20 /*
21 * Constructor.
22 */
23 template <typename Type>
24 ArrayParam<Type>::ArrayParam(const char *label, int n, bool isRequired)
25 : Parameter(label, isRequired),
26 name_(label),
27 n_(n),
28 isRequired_(isRequired),
29 hasBrackets_(false)
30 {
31 if (BracketPolicy::get() != BracketPolicy::Forbidden) {
32 std::string name = name_;
33 name += "[";
34 label_.setString(name);
35 hasBrackets_ = true;
36 }
37 }
38
39 /*
40 * Destructor.
41 */
42 template <typename Type>
44 {}
45
46 /*
47 * Read the array label.
48 */
49 template <typename Type>
50 void ArrayParam<Type>::readLabel(std::istream &in)
51 {
52 isActive_ = false;
53
54 if (BracketPolicy::get() == BracketPolicy::Optional) {
55
56 // First try to match array name with an appended [ bracket
57 std::string string = name_;
58 string += "[";
59 label_.setString(string); // Tentatively reset label string
60 label_.setIsRequired(false); // Temporarily set isRequired false
61 hasBrackets_ = true;
62 in >> label_;
63 // This will not throw an exception, because isRequired is false
64
65 // Restore isRequired value of label
66 label_.setIsRequired(isRequired_);
67
68 if (Label::isMatched()) {
69 //isActive_ = true;
70 hasBrackets_ = true;
71 } else {
72 // Try to match array name without appended [ bracket
73 label_.setString(name_);
74 hasBrackets_ = false;
75 in >> label_;
76 }
77
78 } else
79 if (BracketPolicy::get() == BracketPolicy::Required) {
80
81 std::string string = name_;
82 string += "[";
83 label_.setString(string);
84 hasBrackets_ = true;
85 in >> label_;
86
87 } else
88 if (BracketPolicy::get() == BracketPolicy::Forbidden) {
89
90 std::string string = name_;
91 label_.setString(string);
92 hasBrackets_ = false;
93 in >> label_;
94
95 }
96
97 }
98
99 /*
100 * Read array of values from isteam.
101 */
102 template <class Type>
103 void ArrayParam<Type>::readValue(std::istream &in)
104 {
105 for (int i = 0; i < n_; ++i) {
106 in >> element(i);
107 }
108 readEndBracket(in);
109 }
110
111 /*
112 * Read closing bracket delimiter, if any.
113 */
114 template <class Type>
115 void ArrayParam<Type>::readEndBracket(std::istream& in)
116 {
117 // If necessary, attempt to read closing bracket
118 if (hasBrackets_) {
119 UTIL_CHECK(BracketPolicy::get() != BracketPolicy::Forbidden);
120 in >> Label("]");
121 // Operator >> throws an Exception if "]" is not matched
122 }
123
124 // Reset label_ and hasBrackets_ to default values for output
125 std::string string = name_;
126 if (BracketPolicy::get() == BracketPolicy::Forbidden) {
127 hasBrackets_ = false;
128 } else {
129 string += "[";
130 hasBrackets_ = true;
131 }
132 label_.setString(string);
133 }
134
135 /*
136 * Write an array-valued parameter.
137 */
138 template <class Type>
139 void ArrayParam<Type>::writeParam(std::ostream &out) const
140 {
141 if (isActive()) {
142
143 Label space("");
144 int i;
145 if (hasBrackets_) {
146 out << indent() << Parameter::label_ << std::endl;
147 }
148 for (i = 0; i < n_; ++i) {
149 if (i == 0 && !hasBrackets_) {
150 out << indent() << Parameter::label_;
151 } else {
152 out << indent() << space;
153 }
154 out << std::right << std::scientific
155 << std::setprecision(Parameter::Precision)
156 << std::setw(Parameter::Width)
157 << element(i)
158 << std::endl;
159 }
160 if (hasBrackets_) {
161 out << indent() << "]" << std::endl;
162 }
163
164 } // if isActive
165 }
166
167}
This file contains templates for global functions send<T>, recv<T> and bcast<T>.
ArrayParam(const char *label, int n, bool isRequired=true)
Constructor.
Definition: ArrayParam.tpp:24
void writeParam(std::ostream &out) const
Write an array-valued parameter to stream.
Definition: ArrayParam.tpp:139
virtual void readValue(std::istream &in)
Read array of element values from an input stream.
Definition: ArrayParam.tpp:103
virtual ~ArrayParam()
Destructor.
Definition: ArrayParam.tpp:43
void readEndBracket(std::istream &in)
Read a closing bracket, if necessary.
Definition: ArrayParam.tpp:115
Label label_
Label object that contains parameter label string.
Definition: Parameter.h:185
virtual void readLabel(std::istream &in)
Read parameter label from an input stream.
Definition: ArrayParam.tpp:50
A label string in a file format.
Definition: Label.h:73
static bool isMatched()
Did the most recent attempt to match a Label succeed?
Definition: Label.h:274
void setString(std::string string)
Set the label string.
Definition: Label.cpp:99
A single variable in a parameter file.
Definition: Parameter.h:46
static const int Precision
Precision for io of floating point data field.
Definition: Parameter.h:56
Label label_
Label object that contains parameter label string.
Definition: Parameter.h:185
static const int Width
Width of output field for a scalar variable.
Definition: Parameter.h:53
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
BracketPolicy::Type get()
Get value of bracket policy.
Utility classes for scientific computation.
Definition: accumulators.mod:1